2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
11 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
18 #define WIN32_LEAN_AND_MEAN
22 #include "win\ddraw.h"
35 #define MAX_GFX_BITMAPS 512
40 static BOOL dd_gfx_initialized = 0;
41 static unsigned short gfxBitmapHandleCur = 1;
43 static struct _gfxBitmap {
44 LPDIRECTDRAWSURFACE lpdds;
47 } gfxBitmap[MAX_GFX_BITMAPS];
56 if (dd_gfx_initialized) return 1;
58 for (i = 0; i < MAX_GFX_BITMAPS; i++)
59 gfxBitmap[i].lpdds = NULL;
61 gfxBitmapHandleCur = 1;
66 if (FindArg("-disallowgfx")) dd_gfx_initialized = 0;
67 else if (FindArg("-forcegfx")) dd_gfx_initialized = 1;
69 if (ddDriverCaps.hwcolorkey)
70 logentry("Card supports HW colorkeying.\n");
72 if (ddDriverCaps.hwbltstretch)
73 logentry("Card supports HW bitmap stretching.\n");
75 if (ddDriverCaps.hwcolorkey) dd_gfx_initialized = 1;
76 else dd_gfx_initialized = 0;
87 if (!dd_gfx_initialized) return 1;
89 for (i = 0; i < MAX_GFX_BITMAPS; i++)
91 if (gfxBitmap[i].lpdds)
92 IDirectDrawSurface_Release(gfxBitmap[i].lpdds);
95 dd_gfx_initialized =0;
101 unsigned short dd_gfx_createbitmap2D(int w, int h)
104 unsigned short handle;
106 if (!dd_gfx_initialized) return 0;
108 for (i = 0; i < MAX_GFX_BITMAPS; i++)
109 if (!gfxBitmap[i].lpdds) {
110 handle = (unsigned short)(i);
114 if (i == MAX_GFX_BITMAPS) return 0;
116 // Only do this if we can benefit from it.
117 // if (ddDriverCaps.hwcolorkey && ddDriverCaps.hwbltstretch)
119 if (FindArg("-forcegfx")) force = 1;
122 if (ddDriverCaps.hwcolorkey || force)
124 LPDIRECTDRAWSURFACE lpdds;
129 memset(&ddsd, 0, sizeof(ddsd));
130 ddsd.dwSize = sizeof(ddsd);
131 ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
132 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY;
136 ddresult = IDirectDraw_CreateSurface(_lpDD, &ddsd, &lpdds, NULL);
138 if (ddresult != DD_OK) {
139 mprintf((0, "DDGFX: Failed to create vidmem 2d bitmap (%x).\n", ddresult));
142 ddck.dwColorSpaceLowValue = TRANSPARENCY_COLOR;
143 ddck.dwColorSpaceHighValue = TRANSPARENCY_COLOR;
144 IDirectDrawSurface_SetColorKey(lpdds, DDCKEY_SRCBLT, &ddck);
146 gfxBitmap[handle].lpdds = lpdds;
147 gfxBitmap[handle].w = w;
148 gfxBitmap[handle].h = h;
149 handle++; // Make it a valid handle
157 int dd_gfx_loadbitmap2D(unsigned short handle, void *buf, int rleflag)
167 if (!dd_gfx_initialized) return 0;
169 handle--; // Convert to valid handle
173 memset(&ddsd, 0, sizeof(ddsd));
174 ddsd.dwSize = sizeof(ddsd);
176 ddresult = IDirectDrawSurface_Lock(gfxBitmap[handle].lpdds, NULL, &ddsd, DDLOCK_WAIT, NULL);
177 if (ddresult != DD_OK) {
178 if (ddresult == DDERR_SURFACELOST) {
179 ddresult = IDirectDrawSurface_Restore(gfxBitmap[handle].lpdds);
180 if (ddresult != DD_OK) {
181 if (ddresult != DDERR_WRONGMODE) {
182 logentry("DDGFX::Restore::Surface err: %x\n", ddresult);
186 LPDIRECTDRAWSURFACE lpdds;
189 IDirectDrawSurface_Release(lpdds);
190 gfxBitmap[handle].lpdds = NULL;
192 memset(&ddsd, 0, sizeof(ddsd));
193 ddsd.dwSize = sizeof(ddsd);
194 ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH;
195 ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | DDSCAPS_VIDEOMEMORY;
196 ddsd.dwWidth = gfxBitmap[handle].w;
197 ddsd.dwHeight = gfxBitmap[handle].h;
199 ddresult = IDirectDraw_CreateSurface(_lpDD, &ddsd, &lpdds, NULL);
201 if (ddresult != DD_OK) {
202 mprintf((0, "DDGFX: Failed to restore vidmem 2d bitmap.\n"));
205 ddck.dwColorSpaceLowValue = TRANSPARENCY_COLOR;
206 ddck.dwColorSpaceHighValue = TRANSPARENCY_COLOR;
207 IDirectDrawSurface_SetColorKey(lpdds, DDCKEY_SRCBLT, &ddck);
208 gfxBitmap[handle].lpdds = lpdds;
215 mprintf((0, "DDGFX:Unable to lock surface!!\n"));
221 ptr = ddsd.lpSurface;
224 gr_init_bitmap(&tbmp, BM_LINEAR, 0,0,gfxBitmap[handle].w, gfxBitmap[handle].h, gfxBitmap[handle].w, buf);
225 if (rleflag) tbmp.bm_flags = BM_FLAG_RLE;
227 for(i = 0; i < gfxBitmap[handle].h; i++)
229 extern ubyte scale_rle_data[];
232 memcpy(ptr+(i*pitch), scale_rle_data, gfxBitmap[handle].w);
234 else memcpy(ptr+(i*pitch), (ubyte*)(buf)+(i*gfxBitmap[handle].w), gfxBitmap[handle].w);
237 IDirectDrawSurface_Unlock(gfxBitmap[handle].lpdds, ptr);
245 int dd_gfx_destroybitmap2D(unsigned short handle)
247 if (!dd_gfx_initialized) return 0;
249 if (handle == 0) return 1;
250 handle--; // Convert to valid handle
252 if (gfxBitmap[handle].lpdds) {
253 IDirectDrawSurface_Release(gfxBitmap[handle].lpdds);
254 gfxBitmap[handle].lpdds = NULL;
261 int dd_gfx_resetbitmap2Dcache()
265 if (!dd_gfx_initialized) return 0;
267 for (i = 0; i < MAX_GFX_BITMAPS; i++)
268 dd_gfx_destroybitmap2D(i+1);
274 void dd_gfx_blt2D(unsigned short handle, int x, int y, int x2, int y2,
275 fix u0, fix v0, fix u1, fix v1)
279 if (!dd_gfx_initialized) return;
282 handle--; // Convert to valid handle
284 SetRect(&drect, x,y,x2,y2);
285 SetRect(&srect, f2i(u0), f2i(v0), f2i(u1), f2i(v1));
287 IDirectDrawSurface_Blt(dd_grd_curcanv->lpdds, &drect,
288 gfxBitmap[handle].lpdds,
289 &srect, DDBLT_WAIT| DDBLT_KEYSRC,