-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathDDUtils.pas
280 lines (242 loc) · 9.25 KB
/
DDUtils.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
unit DDUtils;
interface
uses Windows, SysUtils, Classes, DDraw;
{ DDCopyBitmap
- Draw a bitmap into a DirectDrawSurface }
procedure DDCopyBitmap(Surface : IDirectDrawSurface;
Bitmap : HBITMAP;
x, y, Width, Height : integer);
{ DDLoadBitmap
- Create a DirectDrawSurface from a bitmap resource or file. }
function DDLoadBitmap(DirectDraw : IDirectDraw;
const BitmapName : string;
Width : integer;
Height : integer) : IDirectDrawSurface;
{ DDReLoadBitmap
- Load a bitmap from a file or resource into a directdraw surface.
- Normally used to re-load a surface after a restore. }
function DDReLoadBitmap(Surface: IDirectDrawSurface;
const BitmapName: string): HResult;
{ DDLoadPalette
- Create a DirectDraw palette object from a bitmap resource or file.
- If the resource doesn't exist or '' is passed create a default 332 palette. }
function DDLoadPalette(DirectDraw: IDirectDraw;
const BitmapName : string) : IDirectDrawPalette;
{ DDColorMatch
- Convert an RGB color to a pysical color.
- Works by letting GDI SetPixel() do the color matching
then we lock the memory and see what it got mapped to. }
function DDColorMatch(Surface: IDirectDrawSurface;
RGB: TColorRef) : integer;
{ DDSetColorKey
- Set a color key for a surface, given an RGB.
- If you pass CLR_INVALID as the color key, the pixel
in the upper-left corner will be used. }
function DDSetColorKey(Surface : IDirectDrawSurface;
RGB : TColorRef) : HResult;
implementation
procedure DDCopyBitmap(Surface : IDirectDrawSurface;
Bitmap : HBITMAP;
x, y, Width, Height : integer);
var
ImageDC : HDC;
DC : HDC;
BM : Windows.TBitmap;
SurfaceDesc : TDDSurfaceDesc;
begin
if (Surface = nil) or (Bitmap = 0) then
raise Exception.Create('Invalid parameters for DDCopyBitmap');
// make sure this surface is restored.
Surface.Restore;
// select bitmap into a memoryDC so we can use it.
ImageDC:= CreateCompatibleDC(0);
try
SelectObject(ImageDC, Bitmap);
// get size of the bitmap
GetObject(Bitmap, SizeOf(BM), @BM);
if Width = 0 then Width:= BM.bmWidth;
if Height = 0 then Height:= BM.bmHeight;
// get size of surface.
SurfaceDesc.dwSize:= SizeOf(SurfaceDesc);
SurfaceDesc.dwFlags:= DDSD_HEIGHT or DDSD_WIDTH;
Surface.GetSurfaceDesc(SurfaceDesc);
if Surface.GetDC(DC) <> DD_OK then
Raise Exception.Create('GetDC failed for DirectDraw surface');
try
StretchBlt(DC, 0, 0, SurfaceDesc.dwWidth, SurfaceDesc.dwHeight,
ImageDC, x, y, Width, Height, SRCCOPY);
finally Surface.ReleaseDC(DC);
end;
finally DeleteDC(ImageDC);
end;
end;
function DDLoadBitmap(DirectDraw : IDirectDraw;
const BitmapName : string;
Width : integer;
Height : integer) : IDirectDrawSurface;
var
Bitmap : HBitmap;
BM : Windows.TBitmap;
SurfaceDesc : TDDSurfaceDesc;
begin
// try to load the bitmap as a resource, if that fails, try it as a file
Bitmap:= LoadImage(GetModuleHandle(nil), PChar(BitmapName),
IMAGE_BITMAP, Width, Height, LR_CREATEDIBSECTION);
try
if Bitmap = 0 then
Bitmap:= LoadImage(0, PChar(BitmapName), IMAGE_BITMAP, Width, Height,
LR_LOADFROMFILE or LR_CREATEDIBSECTION);
if Bitmap = 0 then
Raise Exception.CreateFmt('Unable to load bitmap %s', [ BitmapName ]);
// get size of the bitmap
GetObject(Bitmap, SizeOf(BM), @BM);
// create a DirectDrawSurface for this bitmap
FillChar(SurfaceDesc, SizeOf(SurfaceDesc), 0);
with SurfaceDesc do begin
dwSize:= SizeOf(SurfaceDesc);
dwFlags:= DDSD_CAPS or DDSD_HEIGHT or DDSD_WIDTH;
ddsCaps.dwCaps:= DDSCAPS_OFFSCREENPLAIN or DDSCAPS_VIDEOMEMORY;
dwWidth:= BM.bmWidth;
dwHeight:= BM.bmHeight;
end;
if DirectDraw.CreateSurface(SurfaceDesc, Result, nil) = HResult(DDERR_OUTOFVIDEOMEMORY)
then with SurfaceDesc do begin
ddsCaps.dwCaps:= DDSCAPS_OFFSCREENPLAIN;
if DirectDraw.CreateSurface(SurfaceDesc, Result, nil) <> DD_OK then
raise Exception.Create('CreateSurface failed');
end; {if with}
DDCopyBitmap(Result, Bitmap, 0, 0, 0, 0);
finally if Bitmap <> 0 then DeleteObject(Bitmap);
end; {try}
end;
function DDReLoadBitmap(Surface : IDirectDrawSurface;
const BitmapName : string): HResult;
var
Bitmap : HBitmap;
begin
Result:= DD_OK;
// try to load the bitmap as a resource, if that fails, try it as a file
Bitmap:= LoadImage(GetModuleHandle(nil), PChar(BitmapName),
IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
try
if Bitmap = 0 then
Bitmap:= LoadImage(0, PChar(BitmapName), IMAGE_BITMAP,
0, 0, LR_LOADFROMFILE or LR_CREATEDIBSECTION);
if Bitmap = 0 then Result:= HResult(DDERR_EXCEPTION);
DDCopyBitmap(Surface, Bitmap, 0, 0, 0, 0);
finally DeleteObject(Bitmap);
end; {try}
end;
function DDLoadPalette(DirectDraw : IDirectDraw;
const BitmapName : string) : IDirectDrawPalette;
type
TRGB = array[ 0..255 ] of TRGBQuad;
PRGB = ^TRGB;
var
i, n : integer;
h : HRsrc;
BitmapInfo : PBitmapInfo;
APE : array[0..255] of TPaletteEntry;
RGB : PRGB;
bfHeader : TBitmapFileHeader;
biHeader : TBitmapInfoHeader;
Temp : byte;
begin
// build a 332 palette as the default.
for i:= 0 to 255 do with APE[i] do begin
peRed:= (((i shr 5) and $07) * 255 div 7);
peGreen:= (((i shr 2) and $07) * 255 div 7);
peBlue:= ((i and $03) * 255 div 3);
peFlags:= 0;
end; {for i}
// get a pointer to the bitmap resource.
if BitmapName <> '' then begin
h:= FindResource(0, PChar(BitmapName), RT_BITMAP);
if h <> 0 then begin
BitmapInfo:= PBitmapInfo(LockResource(LoadResource(0, h)));
RGB:= PRGB(@BitmapInfo^.bmiColors);
if (BitmapInfo = nil) or
(BitmapInfo^.bmiHeader.biSize < sizeof(TBITMAPINFOHEADER)) then n:= 0
else if (BitmapInfo^.bmiHeader.biBitCount > 8) then n:= 0
else if (BitmapInfo^.bmiHeader.biClrUsed = 0) then
n:= 1 shl BitmapInfo^.bmiHeader.biBitCount
else n:= BitmapInfo^.bmiHeader.biClrUsed;
// a DIB color table has its colors stored BGR not RGB
// so flip them around.
for i:= 0 to n - 1 do with APE[i], RGB^[i] do begin
peRed:= rgbRed;
peGreen:= rgbGreen;
peBlue:= rgbBlue;
peFlags:= 0;
end; {for i}
end {if}
else begin
with TFileStream.Create(BitmapName, fmOpenRead) do try
Read(bfHeader, SizeOf(bfHeader));
Read(biHeader, SizeOf(biHeader));
Read(APE, SizeOf(APE));
finally Free;
end; {with try}
// get the number of colors in the color table
if biHeader.biSize <> SizeOf(TBitmapInfoHeader) then n:= 0
else if biHeader.biBitCount > 8 then n:= 0
else if biHeader.biClrUsed = 0 then n:= 1 shl biHeader.biBitCount
else n:= biHeader.biClrUsed;
// a DIB color table has its colors stored BGR not RGB
// so flip them around.
for i:= 0 to n - 1 do with APE[i] do begin
Temp:= peRed;
peRed:= peBlue;
peBlue:= Temp;
end; {for i}
end; {else}
end; {if}
// create the DD palette
if DirectDraw.CreatePalette(DDPCAPS_8BIT, @APE[0], Result, nil) <> DD_OK then
raise Exception.Create('DirectDraw.CreatePalette failed');
end;
function DDColorMatch(Surface : IDirectDrawSurface;
RGB : TColorRef) : integer;
var
TempValue : TColorRef;
DC : HDC;
SurfaceDesc : TDDSurfaceDesc;
DDResult : HResult;
begin
TempValue:= 0;
Result:= Integer(CLR_INVALID);
// use GDI SetPixel to color match for us
if (RGB <> CLR_INVALID) and (Surface.GetDC(DC) = DD_OK) then try
TempValue:= GetPixel(DC, 0, 0); // save current pixel value
SetPixel(DC, 0, 0, RGB); // set our value
finally Surface.ReleaseDC(DC);
end; {try}
// now lock the surface so we can read back the converted color
SurfaceDesc.dwSize:= sizeof(SurfaceDesc);
try
repeat
DDResult:= Surface.Lock(nil, @SurfaceDesc, 0, 0);
until DDResult <> HResult(DDERR_WASSTILLDRAWING);
if DDResult = DD_OK then begin
// get value and mask it to bpp
Result:= PInteger(SurfaceDesc.lpSurface)^ and
(1 shl SurfaceDesc.ddpfPixelFormat.dwRGBBitCount) - 1;
end; {if}
finally Surface.Unlock(nil);
end;
// now put the color that was there back.
if (RGB <> CLR_INVALID) and (Surface.GetDC(DC) = DD_OK) then try
SetPixel(DC, 0, 0, TempValue);
finally Surface.ReleaseDC(DC);
end; {if try}
end;
function DDSetColorKey(Surface : IDirectDrawSurface;
RGB : TColorRef) : HResult;
var
ColorKey : TDDColorKey;
begin
ColorKey.dwColorSpaceLowValue:= DDColorMatch(Surface, RGB);
ColorKey.dwColorSpaceHighValue:= ColorKey.dwColorSpaceLowValue;
Result:= Surface.SetColorKey(DDCKEY_SRCBLT, ColorKey);
end;
end.