ddutil.cpp 8.99 KB
Newer Older
Peter Achten's avatar
Peter Achten committed
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
/*==========================================================================
 *
 *  Copyright (C) 1995-1997 Microsoft Corporation. All Rights Reserved.
 *
 *  File:       ddutil.cpp
 *  Content:    Routines for loading bitmap and palettes from resources
 *
 ***************************************************************************/
#include <windows.h>
#include <windowsx.h>
#include <ddraw.h>
#include "ddutil.h"

/*
 *  DDLoadBitmap
 *
 *  create a DirectDrawSurface from a bitmap resource.
 *
 */
extern "C" IDirectDrawSurface * DDLoadBitmap(IDirectDraw *pdd, LPCSTR szBitmap, int dx, int dy)
{
    HBITMAP             hbm;
    BITMAP              bm;
    DDSURFACEDESC       ddsd;
    IDirectDrawSurface *pdds;

    //
    //  try to load the bitmap as a resource, if that fails, try it as a file
    //
    hbm = (HBITMAP)LoadImage(GetModuleHandle(NULL), szBitmap, IMAGE_BITMAP, dx, dy, LR_CREATEDIBSECTION);

    if (hbm == NULL)
	hbm = (HBITMAP)LoadImage(NULL, szBitmap, IMAGE_BITMAP, dx, dy, LR_LOADFROMFILE|LR_CREATEDIBSECTION);

    if (hbm == NULL)
	return NULL;

    //
    // get size of the bitmap
    //
    GetObject(hbm, sizeof(bm), &bm);      // get size of bitmap

    //
    // create a DirectDrawSurface for this bitmap
    //
    ZeroMemory(&ddsd, sizeof(ddsd));
    ddsd.dwSize = sizeof(ddsd);
Mike Wiering's avatar
Mike Wiering committed
48
    ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
Peter Achten's avatar
Peter Achten committed
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
    ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
    ddsd.dwWidth = bm.bmWidth;
    ddsd.dwHeight = bm.bmHeight;

    if (pdd->CreateSurface(&ddsd, &pdds, NULL) != DD_OK)
	return NULL;

    DDCopyBitmap(pdds, hbm, 0, 0, 0, 0);

    DeleteObject(hbm);

    return pdds;
}

/*
 *  DDReLoadBitmap
 *
 *  load a bitmap from a file or resource into a directdraw surface.
 *  normaly used to re-load a surface after a restore.
 *
 */
HRESULT DDReLoadBitmap(IDirectDrawSurface *pdds, LPCSTR szBitmap)
{
    HBITMAP             hbm;
    HRESULT             hr;

    //
    //  try to load the bitmap as a resource, if that fails, try it as a file
    //
    hbm = (HBITMAP)LoadImage(GetModuleHandle(NULL), szBitmap, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);

    if (hbm == NULL)
	hbm = (HBITMAP)LoadImage(NULL, szBitmap, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE|LR_CREATEDIBSECTION);

    if (hbm == NULL)
    {
	OutputDebugString("handle is null\n");
	return E_FAIL;
    }

    hr = DDCopyBitmap(pdds, hbm, 0, 0, 0, 0);
    if (hr != DD_OK)
    {
	OutputDebugString("ddcopybitmap failed\n");
    }


    DeleteObject(hbm);
    return hr;
}

/*
 *  DDCopyBitmap
 *
 *  draw a bitmap into a DirectDrawSurface
 *
 */
extern "C" HRESULT DDCopyBitmap(IDirectDrawSurface *pdds, HBITMAP hbm, int x, int y, int dx, int dy)
{
    HDC                 hdcImage;
    HDC                 hdc;
    BITMAP              bm;
    DDSURFACEDESC       ddsd;
    HRESULT             hr;

    if (hbm == NULL || pdds == NULL)
	return E_FAIL;

    //
    // make sure this surface is restored.
    //
    pdds->Restore();

    //
    //  select bitmap into a memoryDC so we can use it.
    //
    hdcImage = CreateCompatibleDC(NULL);
    if (!hdcImage)
	OutputDebugString("createcompatible dc failed\n");
    SelectObject(hdcImage, hbm);

    //
    // get size of the bitmap
    //
    GetObject(hbm, sizeof(bm), &bm);    // get size of bitmap
    dx = dx == 0 ? bm.bmWidth  : dx;    // use the passed size, unless zero
    dy = dy == 0 ? bm.bmHeight : dy;

    //
    // get size of surface.
    //
    ddsd.dwSize = sizeof(ddsd);
    ddsd.dwFlags = DDSD_HEIGHT | DDSD_WIDTH;
    pdds->GetSurfaceDesc(&ddsd);

    if ((hr = pdds->GetDC(&hdc)) == DD_OK)
    {
	StretchBlt(hdc, 0, 0, ddsd.dwWidth, ddsd.dwHeight, hdcImage, x, y, dx, dy, SRCCOPY);
	pdds->ReleaseDC(hdc);
    }

    DeleteDC(hdcImage);

    return hr;
}

//
//  DDLoadPalette
//
//  Create a DirectDraw palette object from a bitmap resoure
//
//  if the resource does not exist or NULL is passed create a
//  default 332 palette.
//
Mike Wiering's avatar
Mike Wiering committed
163
extern "C" IDirectDrawPalette * DDLoadPalette (IDirectDraw *pdd, LPCSTR szBitmap)
Peter Achten's avatar
Peter Achten committed
164
165
{
    IDirectDrawPalette* ddpal;
Mike Wiering's avatar
Mike Wiering committed
166
167
168
169
170
171
172
    int i, j, r, g, b;
    int n;
//    int                 fh;
// Mike ... //
    HANDLE              fh;
    SECURITY_ATTRIBUTES sa;
// ... Mike //
Peter Achten's avatar
Peter Achten committed
173
174
175
176
177
    HRSRC               h;
    LPBITMAPINFOHEADER  lpbi;
    PALETTEENTRY        ape[256];
    RGBQUAD *           prgb;

Mike Wiering's avatar
Mike Wiering committed
178
179
180
181
182
183
// Mike ... //
	sa.nLength = sizeof (SECURITY_ATTRIBUTES);
    sa.lpSecurityDescriptor = NULL;
    sa.bInheritHandle = FALSE;
// ... Mike //

Peter Achten's avatar
Peter Achten committed
184
185
186
    //
    // build a 332 palette as the default.
    //
Mike Wiering's avatar
Mike Wiering committed
187
/*
Peter Achten's avatar
Peter Achten committed
188
189
190
191
192
193
194
    for (i=0; i<256; i++)
    {
	ape[i].peRed   = (BYTE)(((i >> 5) & 0x07) * 255 / 7);
	ape[i].peGreen = (BYTE)(((i >> 2) & 0x07) * 255 / 7);
	ape[i].peBlue  = (BYTE)(((i >> 0) & 0x03) * 255 / 3);
	ape[i].peFlags = (BYTE)0;
    }
Mike Wiering's avatar
Mike Wiering committed
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
*/
    for (i = 0; i < 256; i++)
    {
        if (i < 6 * 7 * 6)
        {
            r = 224 * ( i       % 6) / 5 + 16;
            g = 224 * ((i / 6)  % 7) / 6 + 16;
            b = 224 * ((i / 42) % 6) / 5 + 16;
        }
        else
        {
            switch (i)
            {
                case 252: { r = 0; g = 0; b = 0; break; }
                case 253: { r = 8; g = 8; b = 8; break; }
                case 254: { r = 247; g = 247; b = 247; break; }
                case 255: { r = 255; g = 255; b = 255; break; }
            }
        }
	ape[i].peRed   = (BYTE) r;
	ape[i].peGreen = (BYTE) g;
	ape[i].peBlue  = (BYTE) b;
        
	ape[i].peFlags = (BYTE) 0;
    }
Peter Achten's avatar
Peter Achten committed
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

    //
    // get a pointer to the bitmap resource.
    //
    if (szBitmap && (h = FindResource(NULL, szBitmap, RT_BITMAP)))
    {
	lpbi = (LPBITMAPINFOHEADER)LockResource(LoadResource(NULL, h));
	if (!lpbi)
	    OutputDebugString("lock resource failed\n");
	prgb = (RGBQUAD*)((BYTE*)lpbi + lpbi->biSize);

	if (lpbi == NULL || lpbi->biSize < sizeof(BITMAPINFOHEADER))
	    n = 0;
	else if (lpbi->biBitCount > 8)
	    n = 0;
	else if (lpbi->biClrUsed == 0)
	    n = 1 << lpbi->biBitCount;
	else
	    n = lpbi->biClrUsed;

	//
	//  a DIB color table has its colors stored BGR not RGB
	//  so flip them around.
	//
	for(i=0; i<n; i++ )
	{
	    ape[i].peRed   = prgb[i].rgbRed;
	    ape[i].peGreen = prgb[i].rgbGreen;
	    ape[i].peBlue  = prgb[i].rgbBlue;
	    ape[i].peFlags = 0;
	}
    }
Mike Wiering's avatar
Mike Wiering committed
252
253
254
255
256
257
258
259
260
    else if (szBitmap &&
           //  (fh = _lopen(szBitmap, OF_READ)) != -1
           // Mike ... //
              (fh = CreateFile (szBitmap, GENERIC_READ,
                  FILE_SHARE_READ, &sa, OPEN_EXISTING,
                  FILE_ATTRIBUTE_NORMAL, NULL)
              ) != NULL
            )
           // ... Mike //
Peter Achten's avatar
Peter Achten committed
261
262
263
    {
	BITMAPFILEHEADER bf;
	BITMAPINFOHEADER bi;
Mike Wiering's avatar
Mike Wiering committed
264
    unsigned long iNumRead;
Peter Achten's avatar
Peter Achten committed
265

Mike Wiering's avatar
Mike Wiering committed
266
  /*
Peter Achten's avatar
Peter Achten committed
267
268
269
270
	_lread(fh, &bf, sizeof(bf));
	_lread(fh, &bi, sizeof(bi));
	_lread(fh, ape, sizeof(ape));
	_lclose(fh);
Mike Wiering's avatar
Mike Wiering committed
271
272
273
274
275
276
277
  */
    // Mike ... //
    ReadFile (fh, &bf, sizeof (bf), &iNumRead, NULL);
    ReadFile (fh, &bi, sizeof (bi), &iNumRead, NULL);
    ReadFile (fh, ape, sizeof (ape), &iNumRead, NULL);
	CloseHandle (fh);
    // ... Mike //
Peter Achten's avatar
Peter Achten committed
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299

	if (bi.biSize != sizeof(BITMAPINFOHEADER))
	    n = 0;
	else if (bi.biBitCount > 8)
	    n = 0;
	else if (bi.biClrUsed == 0)
	    n = 1 << bi.biBitCount;
	else
	    n = bi.biClrUsed;

	//
	//  a DIB color table has its colors stored BGR not RGB
	//  so flip them around.
	//
	for(i=0; i<n; i++ )
	{
	    BYTE r = ape[i].peRed;
	    ape[i].peRed  = ape[i].peBlue;
	    ape[i].peBlue = r;
	}
    }

Mike Wiering's avatar
Mike Wiering committed
300

Peter Achten's avatar
Peter Achten committed
301
302
303
304
305
    pdd->CreatePalette(DDPCAPS_8BIT, ape, &ddpal, NULL);

    return ddpal;
}

Mike Wiering's avatar
Mike Wiering committed
306

Peter Achten's avatar
Peter Achten committed
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*
 * DDColorMatch
 *
 * convert a RGB color to a pysical color.
 *
 * we do this by leting GDI SetPixel() do the color matching
 * then we lock the memory and see what it got mapped to.
 */
extern "C" DWORD DDColorMatch(IDirectDrawSurface *pdds, COLORREF rgb)
{
    COLORREF rgbT;
    HDC hdc;
    DWORD dw = CLR_INVALID;
    DDSURFACEDESC ddsd;
    HRESULT hres;

    //
    //  use GDI SetPixel to color match for us
    //
    if (rgb != CLR_INVALID && pdds->GetDC(&hdc) == DD_OK)
    {
	rgbT = GetPixel(hdc, 0, 0);             // save current pixel value
	SetPixel(hdc, 0, 0, rgb);               // set our value
	pdds->ReleaseDC(hdc);
    }

    //
    // now lock the surface so we can read back the converted color
    //
    ddsd.dwSize = sizeof(ddsd);
    while ((hres = pdds->Lock(NULL, &ddsd, 0, NULL)) == DDERR_WASSTILLDRAWING)
	;

    if (hres == DD_OK)
    {
	dw  = *(DWORD *)ddsd.lpSurface;                     // get DWORD
        if(ddsd.ddpfPixelFormat.dwRGBBitCount < 32)
            dw &= (1 << ddsd.ddpfPixelFormat.dwRGBBitCount)-1;  // mask it to bpp
	pdds->Unlock(NULL);
    }

    //
    //  now put the color that was there back.
    //
    if (rgb != CLR_INVALID && pdds->GetDC(&hdc) == DD_OK)
    {
	SetPixel(hdc, 0, 0, rgbT);
	pdds->ReleaseDC(hdc);
    }

    return dw;
}

/*
 * DDSetColorKey
 *
 * set a color key for a surface, given a RGB.
 * if you pass CLR_INVALID as the color key, the pixel
 * in the upper-left corner will be used.
 */
extern "C" HRESULT DDSetColorKey(IDirectDrawSurface *pdds, COLORREF rgb)
{
    DDCOLORKEY          ddck;

    ddck.dwColorSpaceLowValue  = DDColorMatch(pdds, rgb);
    ddck.dwColorSpaceHighValue = ddck.dwColorSpaceLowValue;
    return pdds->SetColorKey(DDCKEY_SRCBLT, &ddck);
}