PDA

View Full Version : Solution?



xGTx
30-07-2004, 08:35 AM
Well, ive read and heard that DelphiX's FillRectAlpha method was slow as heck. And now that I actually try and use it, IT IS! In my RPG, I was trying to FillRectAlpha to darken the whole scene to simulate night time... And man it dropped to ~8 FPS... FROM 210 FPS! So im here hoping someone knows a better way to do the alpha recting, or possibly a suggestion on another way of darkening the picture to simulate night time. Thanks for all your help by the way, you've all be wonderfull here.

Harry Hunt
30-07-2004, 09:18 AM
Well, if you're using 8 Bit color mode, you can darken your image by darken the 256 colors in your palette. If you're using hi- or true color mode, you could try to do it manually by darkening each pixel of your image. Alpha blending requires more complicated calculations than darkening so you might be able to speed everything up if you do it yourself.
Another alternative would be to use different wrappers. DelphiX is based on DirectDraw which doesn't use hardware acceleration for alpha blending. You could try DelphiX' Direct3D component, but I've never worked with that so I can't help you there.

xGTx
01-08-2004, 08:44 PM
Well i've been trying to darken every pixel, but.. imagine this.. can't figure out how to darken a TColor. Any idea?

Harry Hunt
01-08-2004, 09:14 PM
function ClipByte(X: Integer): Byte;
begin
if X > 255 then
Result := 255
else if X < 0 then
Result := 0
else
Result := X;
end;

var
Color, DarkenedColor: TColor;
ColorInt: Integer;
R, G, B: Byte;
NewR, NewG, NewB: Byte;
DarkenBy: Byte;
begin
DarkenBy := 10;
ColorInt := ColorToRGB(Color);
R := GetRValue(ColorInt);
G := GetGValue(ColorInt);
B := GetBValue(ColorInt);
NewR := ClipByte(R - DarkenBy);
NewG := ClipByte(G - DarkenBy);
NewB := ClipByte(B - DarkenBy);
DarkenedColor := RGB(NewR, NewG, NewB);
end;

Paulius
01-08-2004, 09:31 PM
r:= color and $ff;
g:= color shr 8 and $ff;
b:= color shr 16;
dec(r, DarkenBy);
dec(g, DarkenBy);
dec(b, DarkenBy);
if r<0 then r:= 0;
if g<0 then g:= 0;
if b<0 then b:= 0;
color:= r or (g shl 8) or (b shl 16);
What Harray said only without function calls; r, g, b are integers
edit: wrote and instead of or

xGTx
02-08-2004, 12:11 AM
I really appreciate the help, but that ended up being slower then DelphiX's FillRectAlpha.... so, im still looking for a solution :(

Harry Hunt
02-08-2004, 07:05 AM
Then I guess you'll have to say "goodbye" to DelphiX.

Paulius
02-08-2004, 08:11 AM
Or post you're surface darkening procedure and we'll try to optimize it.

Harry Hunt
02-08-2004, 09:05 AM
or that :lol:

xGTx
02-08-2004, 08:52 PM
Well, it's basicly what you guys offered me above:

Procedure TConsole.FillRectDarken(Const DestRect: TRect; Const DarkenBy: Integer);
Var
Color: TColor;
R, G, B, I, N: Integer;
Begin

For I := DestRect.Top to DestRect.Bottom Do
For N := DestRect.Left to DestRect.Right Do
Begin
color := gmScreen.Surface.Pixels[N,I];
r:= color and $ff;
g:= color shr 8 and $ff;
b:= color shr 16;
dec(r, DarkenBy);
dec(g, DarkenBy);
dec(b, DarkenBy);
if r<0 then r:= 0;
if g<0 then g:= 0;
if b<0 then b:= 0;
gmScreen.Surface.Pixels[N,I]:= r and (g shl 8) and (b shl 16);
End;

End;

Paulius
02-08-2004, 10:19 PM
Please don't let me catch any of you using Pixels again EVER :evil: . I'm making the assumption that you're using 24 bit color and your DestRect is clipped to screen size.
procedure TConsole.FillRectDarken(Const DestRect: TRect; Const DarkenBy: Integer);
Var
desc: TDDSurfaceDesc;
Color: PByte;
val, i, j, NextLine: Integer;
Begin
FillChar(desc, SizeOf(desc), 0);
gmScreen.Surface.Lock(DestRect, desc);
Color:= PByte(desc.lpSurface);
NextLine:= desc.lPitch - 3 * (DestRect.Right - DestRect.Left + 1);
for j := DestRect.Top to DestRect.Bottom do
begin
for i := DestRect.Left to DestRect.Right do
begin
Val:= Color^;
dec(Val, DarkenBy);
if Val<0 then Val:= 0;
Color^:= Val;
inc(Color);

Val:= Color^;
dec(Val, DarkenBy);
if Val<0 then Val:= 0;
Color^:= Val;
inc(Color);

Val:= Color^;
dec(Val, DarkenBy);
if Val<0 then Val:= 0;
Color^:= Val;
inc(Color);
end;
inc(Color, NextLine);
end;
gmScreen.Surface.UnLock;
end;

Harry Hunt
03-08-2004, 06:56 AM
AHH! You were using Pixels. Well then it's no surprise that it was slow as hell.
Nice job Paulius.