Debian Codegraph @

dd_grwin32/ddgrWin32GDI.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/ddgr_win32/ddgrWin32GDI.cpp $
21 * $Revision: 10 $
22 * $Date: 10/02/98 11:15a $
23 * $Author: Jeff $
24 *
25 * DDGR v2.0 GDI(X) implementation
26 *
27 * $Log: /DescentIII/Main/ddgr_win32/ddgrWin32GDI.cpp $
28 *
29 * 10 10/02/98 11:15a Jeff
30 * added HBITMAP, HFONT and HBRUSH type casts where needed to satisfy the
31 * compiler
32 *
33 * 9 6/02/98 12:46p Samir
34 * removed restriction of ddgr system when going to true-color
35 * resolutions.
36 *
37 * 8 4/23/98 6:38p Jason
38 * made bitmaps use 1555 format
39 *
40 * 7 4/09/98 11:00a Samir
41 * took out assertion in ddgr_gdi_Close that caused errors and assertions
42 * to 'crap' out.
43 *
44 * 6 11/14/97 12:32p Samir
45 * DirectDraw works under GDIX windowed subsystem (NORMAL cooperative
46 * mode).
47 *
48 * 5 9/23/97 12:15p Jason
49 * fixed color bug
50 *
51 * 4 9/12/97 4:13p Samir
52 * Added some private data access functions and more DirectX
53 * functionality.
54 *
55 * 3 8/12/97 10:43p Matt
56 * Fixed RGB conversion bug
57 *
58 * 5 6/16/97 4:46p Samir
59 * OpenGL works in window too.
60 *
61 * 4 6/13/97 3:01p Samir
62 * Fixed flip assertions and OpenGL imp.
63 *
64 * 3 6/13/97 1:24p Samir
65 * FlipVideo doesn't flip when no backbuffer, so just do nothing.
66 *
67 * 2 6/13/97 12:24p Samir
68 * Fixed OpenGLDC hack and removed assert in lock if data ptr = NULL
69 *
70 * 1 6/12/97 6:33p Samir
71 * Initial revision
72 *
73 * $NoKeywords: $
74 */
75
76 #include "ddgrWin32.h"
77 #include "ddgrWin32GDI.h"
78 #include "Application.h"
79 #include "mono.h"
80 #include "pserror.h"
81 #include "gr.h"
82
83 /* Global Library Data
84 */
85 tDDGRGDIInternalData DDGR_GDI_lib_data;
86
87 /* Primary Interface Functions
88 */
89
90 // ---------------------------------------------------------------------------
91 // initializes the objects needed to use GDI
92
93 bool ddgr_gdi_Init(oeApplication *app, bool fullscreen, bool ddraw) {
94 HRESULT hres;
95
96 GDI_DATA(fullscreen) = fullscreen;
97 GDI_DATA(hPrimaryWnd) = (HWND)((oeWin32Application *)app)->m_hWnd;
98
99 GDI_DATA(lpDD) = NULL;
100 if (ddraw) {
101 hres = DirectDrawCreate(NULL, &GDI_DATA(lpDD), NULL);
102 if (hres != DD_OK) {
103 ddgr_PushError("Failure to initialize DirectDraw driver (%d)", LOWORD(hres));
104 return false;
105 }
106 }
107
108 // Initialize DirectDraw exclusive system if we're going fullscreen, and GDIX system
109 if (GDI_DATA(fullscreen)) {
110 if (ddraw) {
111 hres = GDI_DATA(lpDD)->SetCooperativeLevel(GDI_DATA(hPrimaryWnd),
112 DDSCL_ALLOWREBOOT | DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
113
114 if (hres != DD_OK) {
115 ddgr_PushError("Failed to set DirectDraw fullscreen access level (%d)", LOWORD(hres));
116 return false;
117 }
118 } else {
119 // grab all device modes for the current display device.
120 unsigned i;
121 for (i = 0; i < GDI_MAX_DEVMODES; i++) {
122 if (EnumDisplaySettings(NULL, i, &GDI_DATA(devmodes[i])) == FALSE) {
123 break;
124 }
125 HDC hdc = GetDC(GDI_DATA(hPrimaryWnd));
126
127 if (GDI_DATA(devmodes[i].dmBitsPerPel) == (uint32_t)GetDeviceCaps(hdc, BITSPIXEL) &&
128 GDI_DATA(devmodes[i].dmPelsWidth) == (uint32_t)GetDeviceCaps(hdc, HORZRES) &&
129 GDI_DATA(devmodes[i].dmPelsHeight) == (uint32_t)GetDeviceCaps(hdc, VERTRES))
130 GDI_DATA(olddispmode) = i;
131
132 ReleaseDC(GDI_DATA(hPrimaryWnd), hdc);
133 }
134 }
135
136 GDI_DATA(ddraw) = ddraw;
137 } else {
138 if (ddraw) {
139 hres = GDI_DATA(lpDD)->SetCooperativeLevel(GDI_DATA(hPrimaryWnd), DDSCL_NORMAL);
140 if (hres != DD_OK) {
141 ddgr_PushError("Failed to set DirectDraw normal access level (%d)", LOWORD(hres));
142 return false;
143 }
144 }
145 }
146
147 GDI_DATA(hOffscreenDC) = NULL;
148 GDI_DATA(vidrefs) = 0; // reset video reference count.
149 GDI_DATA(init) = true;
150
151 mprintf(0, "GDI system initialized.\n");
152
153 return true;
154 }
155
156 // ---------------------------------------------------------------------------
157 // Deinitialized GDI objects
158
159 void ddgr_gdi_Close() {
160 if (GDI_DATA(hOffscreenDC)) {
161 DeleteDC(GDI_DATA(hOffscreenDC));
162 }
163
164 if (GDI_DATA(fullscreen)) {
165 if (GDI_DATA(ddraw)) {
166 // For full screen, deinitialize directdraw/
167 if (GDI_DATA(lpDD)) {
168 GDI_DATA(lpDD)->RestoreDisplayMode();
169 GDI_DATA(lpDD)->Release();
170 }
171 GDI_DATA(lpDD) = NULL;
172 } else {
173 // do a ChangeDisplaySettings to old mode.
174 LONG lres;
175 GDI_DATA(devmodes[GDI_DATA(olddispmode)]).dmFields =
176 DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFLAGS | DM_DISPLAYFREQUENCY;
177
178 lres = ChangeDisplaySettings(&GDI_DATA(devmodes[GDI_DATA(olddispmode)]), 0);
179 ASSERT(lres == DISP_CHANGE_SUCCESSFUL);
180 }
181 }
182
183 GDI_DATA(hPrimaryWnd) = NULL;
184 GDI_DATA(init) = false;
185
186 mprintf(0, "GDI system closed.\n");
187 }
188
189 // ---------------------------------------------------------------------------
190 // Initializes the video surface for blting
191 // note that is the surface specified is a back buffer by sf->flags, then we need
192 // to create a GDI bitmap for the back buffer, and our visible screen is the front buffer
193
194 bool ddgr_gdi_surf_InitVideo(ddgr_surface *sf) {
195 tGDISurface *gdisf, *gdisf2;
196
197 // we need to create a DC for our GDI bitmaps if this is first ref to video surface.
198 if (GDI_DATA(vidrefs) == 0) {
199 // if we are in fullscreen, do display mode change
200 HDC hdc;
201
202 if (GDI_DATA(fullscreen)) {
203 if (GDI_DATA(ddraw)) {
204 HRESULT hres;
205
206 hres = GDI_DATA(lpDD)->SetDisplayMode(sf->w, sf->h, sf->bpp);
207 if (hres != DD_OK) {
208 ddgr_PushError("Unable to set DirectDraw display mode in fullscreen (%d)\n", LOWORD(hres));
209 return false;
210 }
211 } else {
212 // grab all device modes for the current display device.
213 unsigned i;
214 for (i = 0; i < GDI_MAX_DEVMODES; i++) {
215 LONG lres;
216 if ((int)GDI_DATA(devmodes[i].dmBitsPerPel) == sf->bpp && (int)GDI_DATA(devmodes[i].dmPelsWidth) == sf->w &&
217 (int)GDI_DATA(devmodes[i].dmPelsHeight) == sf->h) {
218 // do a ChangeDisplaySettings to old mode.
219 GDI_DATA(devmodes[i]).dmFields =
220 DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFLAGS | DM_DISPLAYFREQUENCY;
221
222 lres = ChangeDisplaySettings(&GDI_DATA(devmodes[i]), 0);
223 ASSERT(lres == DISP_CHANGE_SUCCESSFUL);
224 break;
225 }
226 }
227 }
228 }
229 // HACKS FOR OPENGL, WILL CHANGE SOON.
230 hdc = CreateCompatibleDC(NULL);
231 if (hdc == NULL) {
232 ddgr_PushError("Unable to create offscreen device context (%x).", GetLastError());
233 return false;
234 }
235 GDI_DATA(hOffscreenDC) = hdc;
236 }
237
238 GDI_DATA(vidrefs)++; // increment vidref count
239
240 gdisf = new tGDISurface;
241 gdisf->hbm = NULL;
242 gdisf->data = NULL;
243 gdisf->rowsize = NULL;
244 gdisf->hwnd = NULL;
245
246 // this means that we are capturing the screen, not creating a screen.
247 // get the screen's width and height and bitdepth
248 if (sf->flags == 0) {
249 sf->w = (int)GetDeviceCaps(GDI_DATA(hOffscreenDC), HORZRES);
250 sf->h = (int)GetDeviceCaps(GDI_DATA(hOffscreenDC), VERTRES);
251 sf->bpp = (int)GetDeviceCaps(GDI_DATA(hOffscreenDC), BITSPIXEL);
252 }
253
254 HDC hdc = GetDC(NULL);
255 int tbpp = GetDeviceCaps(hdc, BITSPIXEL);
256 ReleaseDC(NULL, hdc);
257
258 if (tbpp < sf->bpp) {
259 ddgr_PushError("Desktop color depth must be set to a %d-bit color mode or greater.\n", sf->bpp);
260 return false;
261 }
262
263 // we create a dummy surface for the screen's back buffer, or our accessable buffer
264 // specifying SURFFLAG_BACKBUFFER ensures that we create a GDI bitmap of size requested
265 // in 'sf' and it will be our renderable bitmap, or OUR primary render buffer
266 if (sf->flags & SURFFLAG_BACKBUFFER) {
267 ddgr_surface back_sf;
268 back_sf.w = sf->w;
269 back_sf.h = sf->h;
270 back_sf.bpp = sf->bpp;
271 back_sf.type = SURFTYPE_GENERIC;
272
273 if (!ddgr_gdi_surf_Create(&back_sf)) {
274 ddgr_PushError((0, "Failed to create back buffer <%s>", sf->name));
275 delete gdisf;
276 return false;
277 }
278
279 // set our front buffer to this newly created surface and delete the gdi bitmap object
280 gdisf2 = (tGDISurface *)back_sf.obj;
281 gdisf->hbm = gdisf2->hbm;
282 gdisf->data = gdisf2->data;
283 gdisf->rowsize = gdisf2->rowsize;
284 gdisf->backbuffer = true;
285 delete gdisf2;
286 } else {
287 gdisf->hbm = NULL; // this is really just the screen.
288 gdisf->backbuffer = false;
289 }
290
291 gdisf->hwnd = GDI_DATA(hPrimaryWnd);
292
293 sf->obj = (void *)gdisf;
294
295 return true;
296 }
297
298 // ---------------------------------------------------------------------------
299 // deinitializes the DC created in InitVideo
300
301 void ddgr_gdi_surf_CloseVideo(ddgr_surface *sf) {
302 tGDISurface *bm = (tGDISurface *)sf->obj;
303
304 ASSERT(GDI_DATA(vidrefs) > 0);
305
306 GDI_DATA(vidrefs)--;
307 if (GDI_DATA(vidrefs) == 0) {
308 if (GDI_DATA(fullscreen)) {
309 }
310 // HACK for OPENGL support.
311 DeleteDC(GDI_DATA(hOffscreenDC));
312 }
313
314 if (sf->flags & SURFFLAG_BACKBUFFER)
315 DeleteObject(bm->hbm); // eliminate back buffer
316
317 delete bm;
318 }
319
320 // ---------------------------------------------------------------------------
321 // flip, does absolutely nothing
322
323 bool ddgr_gdi_surf_FlipVideo(ddgr_surface *sf) {
324 HBITMAP old_bmp, h_sbm;
325 HDC hdc_dest;
326 RECT rect;
327 int dw, dh;
328 tGDISurface *bm = (tGDISurface *)sf->obj;
329
330 ASSERT(bm->hwnd != NULL);
331
332 // don't give an error if there's no backbuffer.
333 if (!bm->backbuffer)
334 return true;
335
336 h_sbm = bm->hbm;
337
338 // if we associated another window with our screem, then make sure their DC is the dest.
339 // other wise, use our base window's DC for destination, and select our front buffer as the
340 // source bitmap into our driver's DC.
341 // if (bm->hwnd == NULL) {
342 // hdc_dest = GetDC(GDI_DATA(hwnd));
343 // GetClientRect(GDI_DATA(hwnd),&rect);
344 // }
345 // else {
346 hdc_dest = GetDC(bm->hwnd);
347 GetClientRect(bm->hwnd, &rect);
348 // }
349
350 dw = rect.right - rect.left;
351 dh = rect.bottom - rect.top;
352
353 old_bmp = (HBITMAP)SelectObject(GDI_DATA(hOffscreenDC), h_sbm);
354
355 BOOL bltres = BitBlt(hdc_dest, 0, 0, dw, dh, GDI_DATA(hOffscreenDC), 0, 0, SRCCOPY);
356
357 SelectObject(GDI_DATA(hOffscreenDC), old_bmp);
358
359 ReleaseDC(bm->hwnd, hdc_dest);
360
361 return (bltres) ? true : false;
362 }
363
364 // ---------------------------------------------------------------------------
365 // Creates a GDI DIBitmap
366
367 bool ddgr_gdi_surf_Create(ddgr_surface *sf) {
368 tGDISurface *bm;
369
370 // set up bitmap header
371 int nw = sf->w;
372 if (nw % 4)
373 nw = ((sf->w / 4) * 4) + 4;
374
375 bm = new tGDISurface;
376
377 if (sf->bpp == BPP_8) {
378 tDIBHeader8 header;
379
380 header.bmi.biSize = sizeof(BITMAPINFOHEADER);
381 header.bmi.biWidth = nw;
382 header.bmi.biHeight = -sf->h; // Always a top down bitmap!!
383 header.bmi.biPlanes = 1;
384 header.bmi.biBitCount = sf->bpp;
385 header.bmi.biCompression = BI_BITFIELDS;
386 header.bmi.biSizeImage = 0;
387 header.bmi.biXPelsPerMeter = 0;
388 header.bmi.biYPelsPerMeter = 0;
389 header.bmi.biClrUsed = 0;
390 header.bmi.biClrImportant = 0;
391
392 bm->hbm = CreateDIBSection(GDI_DATA(hOffscreenDC), (BITMAPINFO *)&header, DIB_RGB_COLORS, &bm->data, NULL, 0);
393 } else {
394 tDIBHeader header;
395 header.bmi.biSize = sizeof(BITMAPINFOHEADER);
396 header.bmi.biWidth = nw;
397 header.bmi.biHeight = -sf->h; // Always a top down bitmap!!
398 header.bmi.biPlanes = 1;
399 header.bmi.biBitCount = sf->bpp;
400 header.bmi.biCompression = BI_BITFIELDS;
401 header.bmi.biSizeImage = 0;
402 header.bmi.biXPelsPerMeter = 0;
403 header.bmi.biYPelsPerMeter = 0;
404 header.bmi.biClrUsed = 0;
405 header.bmi.biClrImportant = 0;
406
407 // setup RGB bit masks
408 if (sf->bpp == BPP_16) {
409 header.red_mask = 0x7c00;
410 header.green_mask = 0x03e0;
411 header.blue_mask = 0x001f;
412 bm->rowsize = nw * 2;
413 } else if (sf->bpp == BPP_32 || sf->bpp == BPP_24) {
414 header.red_mask = 0x00ff0000;
415 header.green_mask = 0x0000ff00;
416 header.blue_mask = 0x000000ff;
417 bm->rowsize = nw * 4;
418 }
419
420 bm->hbm = CreateDIBSection(GDI_DATA(hOffscreenDC), (BITMAPINFO *)&header, DIB_RGB_COLORS, &bm->data, NULL, 0);
421 }
422
423 if (!bm->hbm) {
424 ddgr_PushError("CreateDIBSection failed.");
425 delete bm;
426 return false;
427 }
428 bm->hwnd = NULL; // This is set by application
429
430 sf->obj = (void *)bm;
431
432 return true;
433 }
434
435 // ---------------------------------------------------------------------------
436 // destroys the DIB allocated from create
437
438 void ddgr_gdi_surf_Destroy(ddgr_surface *sf) {
439 tGDISurface *bm = (tGDISurface *)sf->obj;
440
441 ASSERT(bm->hbm);
442
443 DeleteObject(bm->hbm);
444
445 delete bm;
446 }
447
448 // Clears a GDI surface.
449 void ddgr_gdi_surf_Clear(ddgr_surface *dsf, ddgr_color col, int l, int t, int w, int h) {
450 HBITMAP old_bmp;
451 HBRUSH clr_brush, old_brush;
452 HDC hdc;
453 tGDISurface *dbm = (tGDISurface *)dsf->obj;
454 int red, green, blue;
455 ddgr_color color = col;
456
457 switch (dsf->bpp) {
458 case BPP_16:
459 red = GR_COLOR_RED(color);
460 green = GR_COLOR_GREEN(color);
461 blue = GR_COLOR_BLUE(color);
462 break;
463
464 case BPP_32:
465 case BPP_24:
466 red = (color & 0x00ff0000) >> 16;
467 green = (color & 0x0000ff00) >> 8;
468 blue = (color & 0x000000ff);
469 break;
470
471 default:
472 Int3(); // BAD
473 }
474
475 clr_brush = CreateSolidBrush(RGB((BYTE)red, (BYTE)green, (BYTE)blue));
476
477 // Are we clearing the primary display?
478 if (dbm->hbm == NULL) {
479 if (dbm->hwnd)
480 hdc = GetDC(dbm->hwnd);
481 else
482 hdc = GetDC(GDI_DATA(hPrimaryWnd));
483 } else {
484 hdc = GDI_DATA(hOffscreenDC);
485 old_bmp = (HBITMAP)SelectObject(hdc, dbm->hbm);
486 }
487 old_brush = (HBRUSH)SelectObject(hdc, clr_brush);
488
489 Rectangle(hdc, l, t, l + w, t + h);
490
491 SelectObject(hdc, old_brush);
492
493 if (dbm->hbm == NULL) {
494 // do if clearing was done on primary display.
495 if (dbm->hwnd)
496 ReleaseDC(dbm->hwnd, hdc);
497 else
498 ReleaseDC(GDI_DATA(hPrimaryWnd), hdc);
499 } else {
500 SelectObject(hdc, old_bmp);
501 }
502
503 DeleteObject(clr_brush);
504 }
505
506 // ---------------------------------------------------------------------------
507 // blts one GDI bitmap to another. Ability to blt directly to the screen is also
508 // supported.
509
510 bool ddgr_gdi_surf_Blt(ddgr_surface *dsf, int dx, int dy, ddgr_surface *ssf, int sx, int sy, int sw, int sh) {
511 HBITMAP old_bmp;
512 HBITMAP old_bmp2;
513 HDC hdc_dest;
514 tGDISurface *dbm = (tGDISurface *)dsf->obj;
515 tGDISurface *sbm = (tGDISurface *)ssf->obj;
516
517 // note that if our destination bitmap is the 'screen', then we use the GDI hwnd DC
518 // else we use the DC of the window specified in the destination.
519 if (dbm->hbm == NULL) {
520 if (dbm->hwnd == NULL)
521 hdc_dest = GetDC(GDI_DATA(hPrimaryWnd));
522 else
523 hdc_dest = GetDC(dbm->hwnd);
524 } else {
525 hdc_dest = CreateCompatibleDC(NULL);
526 old_bmp2 = (HBITMAP)SelectObject(hdc_dest, dbm->hbm);
527 }
528
529 old_bmp = (HBITMAP)SelectObject(GDI_DATA(hOffscreenDC), sbm->hbm);
530
531 BOOL bltres = BitBlt(hdc_dest, dx, dy, sw, sh, GDI_DATA(hOffscreenDC), sx, sy, SRCCOPY);
532
533 SelectObject(GDI_DATA(hOffscreenDC), old_bmp);
534
535 if (dbm->hbm == NULL) {
536 if (dbm->hwnd == NULL)
537 ReleaseDC(GDI_DATA(hPrimaryWnd), hdc_dest);
538 else
539 ReleaseDC(dbm->hwnd, hdc_dest);
540 } else {
541 SelectObject(hdc_dest, old_bmp2);
542 DeleteDC(hdc_dest);
543 }
544
545 return (bltres) ? true : false;
546 }
547
548 // ---------------------------------------------------------------------------
549 // simply extracts the data and rowsize from the DDGR object in 'sf'
550
551 bool ddgr_gdi_surf_Lock(ddgr_surface *sf, void **ptr, int *rowsize) {
552 tGDISurface *bm = (tGDISurface *)sf->obj;
553
554 *ptr = bm->data;
555 *rowsize = bm->rowsize;
556
557 return true;
558 }
559
560 // ---------------------------------------------------------------------------
561 // invalidates the data and rowsize in 'sf'
562
563 bool ddgr_gdi_surf_Unlock(ddgr_surface *sf, void *ptr) { return true; }
564
565 // ---------------------------------------------------------------------------
566 // attaches a window handle to this surface. useful in clearing and blting to
567 // screens.
568
569 void ddgr_gdi_surf_AttachHandle(ddgr_surface *sf, unsigned handle) {
570 tGDISurface *gbm = (tGDISurface *)sf->obj;
571
572 gbm->hwnd = (HWND)handle;
573 }