102 Dalmatians: Puppies to the Rescue
Decompilation Source Reference | GitLab repository
Loading...
Searching...
No Matches
graphics_system.c
Go to the documentation of this file.
1/* Recovered game implementation; original addresses identify provenance. */
16#include <math.h>
17#include <stdarg.h>
18#include <string.h>
19
20// Original pcdogs.exe: 0x417740
21// Initializes exclusive fullscreen DirectDraw and Direct3D surfaces, depth buffer, device and render state, returning success. The 640x480x16 display-mode request can fail without stopping later initialization.
23{
24 IDirectDraw7 *cooperative_level_device = g_directDraw7;
26
27 HRESULT error_code =
28 // IDirectDraw7::SetCooperativeLevel(hwnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN)
29 cooperative_level_device->lpVtbl->SetCooperativeLevel(cooperative_level_device, Window_MainHandle, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE);
30
31 if (error_code < 0)
32 {
33 MESSAGEBOX_STYLE u_type = MB_OK;
34
35 char *lp_caption = "DirectX Error: SetCooperativeLevel()";
36
37 // UINT uType for MessageBoxA
38 // LPCSTR lpCaption for MessageBoxA
39 D3D_FormatDirectXError(error_code, NULL);
40 MessageBoxA(Window_MainHandle, g_errorMessageBuffer, lp_caption, u_type);
41
42 return 0;
43 }
44
45 int32_t window_height = Graphics_WindowHeight;
46 int32_t window_width = Graphics_WindowWidth;
47
48 g_displayRectBottom = window_height;
49 g_clientScreenRectBottom = window_height;
50 POINT *lp_point = (POINT *)&g_clientScreenRectLeft;
53 g_displayRectRight = window_width;
56 g_clientScreenRectRight = window_width;
57 ClientToScreen(hwnd, lp_point);
58 ClientToScreen(hwnd, (POINT *)&g_clientScreenRectRight);
59
60 // IDirectDraw7::SetDisplayMode(640, 480, 16, 0, 0) - Set to 640x480 16-bit color
61 DWORD display_mode_flags = 0;
62
63 DDSURFACEDESC2 surface_description;
64 __builtin_memset(&surface_description, 0, 0x7c);
65 IDirectDraw7 *display_mode_device = g_directDraw7;
66
67 DWORD refresh_rate = 0;
68 DWORD display_bit_depth = GRAPHICS_DEFAULT_COLOR_BITS;
69
70 surface_description.dwSize = 0x7c;
71 surface_description.ddsCaps.dwCaps2 = 0;
72 surface_description.dwFlags = (DDSD_CAPS | DDSD_BACKBUFFERCOUNT);
73 surface_description.dwBackBufferCount = 1;
74 // IDirectDraw7::CreateSurface() - Create primary surface with back buffer (DDSCAPS_FLIP | DDSCAPS_COMPLEX |
75 // DDSCAPS_PRIMARYSURFACE)
76 surface_description.ddsCaps.dwCaps = (DDSCAPS_COMPLEX | DDSCAPS_FLIP | DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE);
77 IDirectDraw7Vtbl *lp_vtbl = display_mode_device->lpVtbl;
78
79 DWORD display_height = GRAPHICS_DEFAULT_HEIGHT;
80 DWORD display_width = GRAPHICS_DEFAULT_WIDTH;
81 HRESULT display_mode_error =
82 lp_vtbl->SetDisplayMode(display_mode_device, display_width, display_height, display_bit_depth, refresh_rate, display_mode_flags);
83
84 // Set display mode to 640x480x16 (width=0x280, height=0x1E0)
85 if (display_mode_error < 0)
86 {
87 char *message = "DirectX Error: SetDisplayMode()";
88
89 D3D_FormatDirectXError(display_mode_error, NULL);
91 }
92
93 IDirectDraw7 *primary_surface_factory = g_directDraw7;
94
95 HRESULT resource_setup_error = primary_surface_factory->lpVtbl->CreateSurface(primary_surface_factory, &surface_description,
97 char *resource_error_context;
98
99 // Create primary surface with one back buffer for page flipping
100 if (resource_setup_error < 0)
101 {
102 resource_error_context = "DirectX Error: CreateSurface()";
103 D3D_FormatDirectXError(resource_setup_error, NULL);
104 // IDirect3DDevice7::SetRenderState(D3DRENDERSTATE_ZENABLE, TRUE) - Enable Z-buffer depth testing
105 Debug_Log(g_errorMessageBuffer, resource_error_context);
106
107 return 0;
108 }
109
110 IDirectDrawSurface7 **back_buffer_output = &g_ddrawBackBuffer;
111 __builtin_memset(&surface_description, 0, 0x7c);
112 IDirectDrawSurface7 *primary_surface = g_ddrawPrimarySurface;
113 surface_description.dwSize = 0x7c;
114 // IDirect3D7::EnumZBufferFormats() - Find compatible Z-buffer pixel format for depth testing
115 surface_description.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;
116 resource_setup_error =
117 primary_surface->lpVtbl->GetAttachedSurface(primary_surface, &surface_description.ddsCaps, back_buffer_output);
118
119 if (resource_setup_error < 0)
120 {
121 resource_error_context = "DirectXError: GetAttachedSurface();";
122 D3D_FormatDirectXError(resource_setup_error, NULL);
123 Debug_Log(g_errorMessageBuffer, resource_error_context);
124
125 return 0;
126 }
127
128 IDirectDraw7 *direct3_d_query_device = g_directDraw7;
129 // Enumerate Z-buffer formats to find compatible depth buffer format
130 resource_setup_error = direct3_d_query_device->lpVtbl->QueryInterface(direct3_d_query_device, &data_44d0e8, (void **)&g_direct3D7);
131
132 if (resource_setup_error < 0)
133 {
134 resource_error_context = "DirectX Error: QueryInterface(IID_IDirect3D7)";
135 D3D_FormatDirectXError(resource_setup_error, NULL);
136 Debug_Log(g_errorMessageBuffer, resource_error_context);
137
138 return 0;
139 }
140
141 IDirect3D7 *depth_format_enumerator = g_direct3D7;
142
143 union D3D_InitFormatOrViewport format_or_viewport;
144
145 depth_format_enumerator->lpVtbl->EnumZBufferFormats(depth_format_enumerator,
147 &format_or_viewport);
148
149 if (format_or_viewport.zBufferFormat.dwSize != 0x20)
150 {
151 // Select D3D device type: HAL (hardware), TnL HAL (hardware T&L), or RGB (software)
152 Debug_Log("Cannot create Z-Buffer!", "Error");
153
154 return 0;
155 }
156
157 __builtin_memset(&surface_description, 0, 0x7c);
158
159 int32_t selected_driver_index = g_selectedDriverIndex;
160
161 surface_description.dwSize = 0x7c;
162 surface_description.dwFlags = (DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT);
163 surface_description.ddsCaps.dwCaps = DDSCAPS_ZBUFFER;
164 // IDirectDraw7::CreateSurface() - Create Z-buffer depth surface with enumerated pixel format
165 surface_description.dwWidth = GRAPHICS_DEFAULT_WIDTH;
166 surface_description.dwHeight = GRAPHICS_DEFAULT_HEIGHT;
167 __builtin_memcpy(&surface_description.ddpfPixelFormat, &format_or_viewport, 0x20);
168
169 const GUID *device_guid = D3D_InitializationRecords[selected_driver_index].device_guid;
170 const char *device_type;
171
172 if (IsEqualGUID(device_guid, &D3D_IID_IDirect3DHALDevice))
173 {
174 surface_description.ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY;
175 device_type = "3DHALDevice";
176 }
177 else if (IsEqualGUID(device_guid, &D3D_IID_IDirect3DTnLHalDevice))
178 {
179 surface_description.ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY;
180 device_type = "3DTnLDevice";
181 }
182 else
183 {
184 surface_description.ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
185 device_type = "Software Device";
186 }
187
188 Debug_Log(device_type, D3D_EmptyLogMessage);
189 IDirectDraw7 *depth_surface_factory = g_directDraw7;
190 resource_setup_error =
191 depth_surface_factory->lpVtbl->CreateSurface(depth_surface_factory, &surface_description, &g_ddrawZBuffer, NULL);
192
193 if (resource_setup_error < 0)
194 {
195 resource_error_context = "DirectX Error: CreateSurface()";
196 D3D_FormatDirectXError(resource_setup_error, NULL);
197 Debug_Log(g_errorMessageBuffer, resource_error_context);
198
199 return 0;
200 }
201
202 resource_setup_error =
203 g_ddrawPrimarySurface->lpVtbl->AddAttachedSurface(g_ddrawBackBuffer, g_ddrawZBuffer);
204
205 if (resource_setup_error < 0)
206 {
207 resource_error_context = "DirectX Error: AddAttachedSurface()";
208 D3D_FormatDirectXError(resource_setup_error, NULL);
209 Debug_Log(g_errorMessageBuffer, resource_error_context);
210
211 return 0;
212 }
213
214 Debug_Log("IDirect3D7_CreateDevice();", NULL);
215 IDirect3D7 *selected_device_factory = g_direct3D7;
216
217 HRESULT selected_device_error = selected_device_factory->lpVtbl->CreateDevice(selected_device_factory,
219
220 if (selected_device_error < 0)
221 {
222 // IDirect3DDevice7::SetRenderState(D3DRENDERSTATE_SHADEMODE, D3DSHADE_GOURAUD) - Gouraud shading
223 char *selected_device_error_context = "DirectX Error: CreateDevice(Drivers[].d3dGuid);";
224
225 D3D_FormatDirectXError(selected_device_error, NULL);
226 // IDirect3DDevice7::SetRenderState(D3DRENDERSTATE_FILLMODE, D3DFILL_SOLID) - Solid fill mode
227 Debug_Log(g_errorMessageBuffer, selected_device_error_context);
228 IDirect3D7 *hal_fallback_factory = g_direct3D7;
229
230 HRESULT hal_fallback_error = hal_fallback_factory->lpVtbl->CreateDevice(hal_fallback_factory, &D3D_IID_IDirect3DHALDevice,
232
233 if (hal_fallback_error < 0)
234 {
235 char *hal_fallback_error_context = "DirectX Error: CreateDevice(IID_IDirect3DHALDevice);";
236
237 D3D_FormatDirectXError(hal_fallback_error, NULL);
238 Debug_Log(g_errorMessageBuffer, hal_fallback_error_context);
239 IDirect3D7 *rgb_fallback_factory = g_direct3D7;
240 resource_setup_error = rgb_fallback_factory->lpVtbl->CreateDevice(rgb_fallback_factory, &data_44d0d8, g_ddrawBackBuffer,
241 &g_d3dDevice7);
242
243 if (resource_setup_error < 0)
244 {
245 resource_error_context = "DirectX Error: CreateDevice(IID_IDirect3DRGBDevice);";
246 D3D_FormatDirectXError(resource_setup_error, NULL);
247 Debug_Log(g_errorMessageBuffer, resource_error_context);
248
249 return 0;
250 }
251 }
252 }
253
254 int32_t client_bottom = g_clientScreenRectBottom;
255
256 format_or_viewport.viewport.dwWidth =
258 IDirect3DDevice7 *viewport_device = g_d3dDevice7;
259 format_or_viewport.viewport.dwHeight =
260 client_bottom - g_clientScreenRectTop;
261 format_or_viewport.viewport.dwX = 0;
262 format_or_viewport.viewport.dwY = 0;
263 format_or_viewport.viewport.dvMinZ = 0.0f;
264 format_or_viewport.viewport.dvMaxZ = 1.0f;
265
266 HRESULT viewport_error = viewport_device->lpVtbl->SetViewport(viewport_device, &format_or_viewport.viewport);
267
268 if (viewport_error < 0)
269 {
270 MESSAGEBOX_STYLE viewport_message_style = MB_OK;
271
272 char *viewport_error_caption = "DirectX Error: SetViewport()";
273
274 // UINT uType for MessageBoxA
275 // LPCSTR lpCaption for MessageBoxA
276 D3D_FormatDirectXError(viewport_error, NULL);
277 MessageBoxA(Window_GetMainHandle(), g_errorMessageBuffer, viewport_error_caption, viewport_message_style);
278
279 return 0;
280 }
281
282 IDirect3DDevice7 *render_target_device = g_d3dDevice7;
283 render_target_device->lpVtbl->SetRenderTarget(render_target_device, g_ddrawBackBuffer, 0);
284 IDirect3DDevice7 *cull_mode_device = g_d3dDevice7;
285 cull_mode_device->lpVtbl->SetRenderState(cull_mode_device, D3DRENDERSTATE_CULLMODE, D3DCULL_NONE);
286 IDirect3DDevice7 *shade_mode_device = g_d3dDevice7;
287 shade_mode_device->lpVtbl->SetRenderState(shade_mode_device, D3DRENDERSTATE_SHADEMODE, D3DSHADE_GOURAUD);
288 IDirect3DDevice7 *fill_mode_device = g_d3dDevice7;
289 fill_mode_device->lpVtbl->SetRenderState(fill_mode_device, D3DRENDERSTATE_FILLMODE, D3DFILL_SOLID);
290 IDirect3DDevice7 *lighting_device = g_d3dDevice7;
291 lighting_device->lpVtbl->SetRenderState(lighting_device, D3DRENDERSTATE_LIGHTING, 0);
292 IDirect3DDevice7 *color_vertex_device = g_d3dDevice7;
293 color_vertex_device->lpVtbl->SetRenderState(color_vertex_device, D3DRENDERSTATE_COLORVERTEX, 1);
294 IDirect3DDevice7 *diffuse_material_device = g_d3dDevice7;
295 diffuse_material_device->lpVtbl->SetRenderState(diffuse_material_device, D3DRENDERSTATE_DIFFUSEMATERIALSOURCE, D3DMCS_COLOR1);
296 IDirect3DDevice7 *alpha_blend_device = g_d3dDevice7;
297 alpha_blend_device->lpVtbl->SetRenderState(alpha_blend_device, D3DRENDERSTATE_ALPHABLENDENABLE, 1);
299 IDirect3DDevice7 *texture_binding_device = g_d3dDevice7;
301 texture_binding_device->lpVtbl->SetTexture(texture_binding_device, 0, NULL);
302 IDirect3DDevice7 *color_operation_device = g_d3dDevice7;
303 color_operation_device->lpVtbl->SetTextureStageState(color_operation_device, 0, D3DTSS_COLOROP, D3DTOP_MODULATE);
304 IDirect3DDevice7 *color_first_argument_device = g_d3dDevice7;
305 color_first_argument_device->lpVtbl->SetTextureStageState(color_first_argument_device, 0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
306 IDirect3DDevice7 *color_second_argument_device = g_d3dDevice7;
307 color_second_argument_device->lpVtbl->SetTextureStageState(color_second_argument_device, 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
308 IDirect3DDevice7 *alpha_operation_device = g_d3dDevice7;
309 alpha_operation_device->lpVtbl->SetTextureStageState(alpha_operation_device, 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
310 IDirect3DDevice7 *alpha_first_argument_device = g_d3dDevice7;
311 alpha_first_argument_device->lpVtbl->SetTextureStageState(alpha_first_argument_device, 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
312 IDirect3DDevice7 *alpha_second_argument_device = g_d3dDevice7;
313 alpha_second_argument_device->lpVtbl->SetTextureStageState(alpha_second_argument_device, 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
314 IDirect3DDevice7 *texture_address_device = g_d3dDevice7;
315 texture_address_device->lpVtbl->SetTextureStageState(texture_address_device, 0, D3DTSS_ADDRESS, D3DTADDRESS_CLAMP);
316 IDirect3DDevice7 *depth_enable_device = g_d3dDevice7;
317
318 HRESULT depth_enable_error = depth_enable_device->lpVtbl->SetRenderState(depth_enable_device, D3DRENDERSTATE_ZENABLE, D3DZB_TRUE);
319
320 if (depth_enable_error < 0)
321 {
322 char *depth_enable_error_context = "ZENABLE failed!";
323
324 D3D_FormatDirectXError(depth_enable_error, NULL);
325 Debug_Log(g_errorMessageBuffer, depth_enable_error_context);
326 }
327
328 IDirect3DDevice7 *minification_filter_device = g_d3dDevice7;
329 minification_filter_device->lpVtbl->SetTextureStageState(minification_filter_device, 0, D3DTSS_MINFILTER, D3DTFN_LINEAR);
330 IDirect3DDevice7 *magnification_filter_device = g_d3dDevice7;
331 magnification_filter_device->lpVtbl->SetTextureStageState(magnification_filter_device, 0, D3DTSS_MAGFILTER, D3DTFG_LINEAR);
332 IDirect3DDevice7 *perspective_device = g_d3dDevice7;
333 perspective_device->lpVtbl->SetRenderState(perspective_device, D3DRENDERSTATE_TEXTUREPERSPECTIVE, 1);
334 IDirect3DDevice7 *clipping_device = g_d3dDevice7;
335 clipping_device->lpVtbl->SetRenderState(clipping_device, D3DRENDERSTATE_CLIPPING, 0);
336 IDirect3DDevice7 *initial_clear_device = g_d3dDevice7;
337
338 DWORD clear_stencil = 0;
339 float clear_depth = 1.0f;
340 DWORD clear_color = 0;
341
342 IDirect3DDevice7Vtbl *clear_vtable = initial_clear_device->lpVtbl;
343
344 DWORD clear_flags = D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER;
345
346 clear_vtable->Clear(initial_clear_device, 0, NULL, clear_flags, clear_color, clear_depth, clear_stencil);
347
348 char *success_log_message = NULL;
349 char *success_log_category = "Initialize D3D successfull.";
350
352 Debug_Log(success_log_category, success_log_message);
353
354 return 1;
355}
356
357// Original pcdogs.exe: 0x417da0
358// Releases and clears the Direct3D device, interface and surfaces, restores display mode when needed and reports remaining textures. The DirectDraw interface itself is released elsewhere.
360{
361 IDirect3DDevice7 *render_device = g_d3dDevice7;
362
363 // Release D3D Device if valid - calls IDirect3DDevice7::Release
364 if (render_device)
365 {
366 render_device->lpVtbl->Release(render_device);
367 g_d3dDevice7 = NULL;
368 }
369
370 IDirect3D7 *direct3_d_interface = g_direct3D7;
371
372 if (direct3_d_interface)
373 {
374 direct3_d_interface->lpVtbl->Release(direct3_d_interface);
375 g_direct3D7 = NULL;
376 }
377
378 // Release Z-buffer surface if valid - calls IDirectDrawSurface7::Release
379 IDirectDrawSurface7 *depth_surface = g_ddrawZBuffer;
380
381 if (depth_surface)
382 {
383 depth_surface->lpVtbl->Release(depth_surface);
384 g_ddrawZBuffer = NULL;
385 }
386
387 IDirectDrawSurface7 *back_buffer = g_ddrawBackBuffer;
388
389 if (back_buffer)
390 {
391 back_buffer->lpVtbl->Release(back_buffer);
392 g_ddrawBackBuffer = NULL;
393 }
394
395 IDirectDrawSurface7 *primary_surface = g_ddrawPrimarySurface;
396
397 if (primary_surface)
398 {
399 // Restore display mode if DirectDraw is valid and cooperative level was set
400 primary_surface->lpVtbl->Release(primary_surface);
402 }
403
404 IDirectDraw7 *display_device = g_directDraw7;
405
406 if (display_device && g_cooperativeLevelSet)
407 {
408 display_device->lpVtbl->RestoreDisplayMode(display_device);
409 ReleaseCapture();
410 }
411
412 CRT_printf("%d textures still active\n", g_activeTextureCount);
413}
414
415// Original pcdogs.exe: 0x4187a0
416// Writes the closing log marker, flushes and closes the global log stream. It neither validates nor clears that stream, so repeated calls can close a stale handle.
424
425// Original pcdogs.exe: 0x4187e0
426// Opens the graphics log, selects a device, creates DirectDraw and records gamma support. Log and selection failures are unchecked, DirectDraw creation failure does not stop dereferencing the interface, and GetCaps failure can leave capability data uninitialized.
428{
430 g_debugLogFile = CRT_fopen("D3D.log", "wt");
432 String_FormatCRTString(Debug_LogMessageBuffer, "%s\n", "-------------Open log-------------");
433 // Enumerate all DirectDraw devices and select the best driver
434 // Mark driver as initialized (index 1 = first real driver after enumeration)
436 // Set vertex format 1 (untextured)
438
439 struct D3D_DriverInfo *selected_driver = D3D_EnumerateAndSelectDefaultDevice();
442 D3D_InitializationRecords[1].driver_guid = selected_driver->driverGuid;
443 D3D_InitializationRecords[1].device_guid = &selected_driver->deviceGuid;
444 String_FormatCRTString(Debug_LogMessageBuffer, "%s\n", "----------------Selected Driver --------------");
445 // DrawPrimitive: TRIANGLEFAN, 4 vertices, FVF 0x44
453 // Call IDirectDraw7::GetCaps (vtable offset +0x2C) to query capabilities
455 // Check DDCAPS.dwCaps2 for DDCAPS2_PRIMARYGAMMA (0x20000).
456 CRT_fprintf(g_debugLogFile, Debug_LogMessageBuffer); // Gamma-control capability is read below.
458
459 char color_key_log_message[0x100];
460
461 String_FormatCRTString(color_key_log_message, "Can Use ColorKey = %d",
463 String_FormatCRTString(Debug_LogMessageBuffer, "%s\n", color_key_log_message);
466 String_FormatCRTString(Debug_LogMessageBuffer, "%s\n", "----------------------------------------------");
469
470 HRESULT error_code = DDraw_CreateEx(&D3D_InitializationRecords[g_selectedDriverIndex].driver_guid,
471 (void **)&g_directDraw7, (GUID *)&IID_IDirectDraw7, NULL);
472
473 if (error_code < 0)
474 {
475 Debug_Log(D3D_FormatDirectXError(error_code, NULL), "DirectX Error");
476 }
477
478 IDirectDraw7 *capabilities_device = g_directDraw7;
479 DDCAPS_DX7 direct_draw_caps;
480 direct_draw_caps.dwSize = 0x17c;
481 capabilities_device->lpVtbl->GetCaps(capabilities_device, &direct_draw_caps, NULL);
482
483 uint32_t dw_caps2 = direct_draw_caps.dwCaps2;
484
485 g_supportsGammaRamp = (dw_caps2 & DDCAPS2_PRIMARYGAMMA) != 0;
486}
487
488// Original pcdogs.exe: 0x418a60
489// Releases and clears the global DirectDraw interface, returning its remaining reference count or zero if no interface was present.
491{
492 IDirectDraw7 *device_or_remaining_references = g_directDraw7;
493
494 // Release DirectDraw7 interface if valid and set to NULL
495 if (device_or_remaining_references)
496 {
497 // Call IDirectDraw7::Release to decrement reference count
498 device_or_remaining_references = (IDirectDraw7 *)(device_or_remaining_references->lpVtbl->Release(device_or_remaining_references));
499 g_directDraw7 = NULL;
500 }
501
502 return (ULONG)(uintptr_t)device_or_remaining_references;
503}
504
505// Original pcdogs.exe: 0x418f20
506// Compares display modes in ascending unsigned width, height and bit-depth order.
507int32_t DDraw_CompareDisplayModes(const void *left_record, const void *right_record)
508{
509 const DDSURFACEDESC2 *left = left_record;
510 const DDSURFACEDESC2 *right = right_record;
511
512 uint32_t left_width = left->dwWidth;
513 uint32_t right_width = right->dwWidth;
514
515 if (left_width < right_width)
516 return ((int32_t)0xffffffff);
517
518 if (left_width > right_width)
519 return 1;
520
521 uint32_t left_height = left->dwHeight;
522 uint32_t right_height = right->dwHeight;
523
524 if (left_height < right_height)
525 return ((int32_t)0xffffffff);
526
527 if (left_height > right_height)
528 return 1;
529
530 uint32_t left_bit_depth = left->ddpfPixelFormat.dwRGBBitCount;
531 uint32_t right_bit_depth = right->ddpfPixelFormat.dwRGBBitCount;
532 bool left_has_greater_bit_depth = right_bit_depth < left_bit_depth;
533
534 if (right_bit_depth == left_bit_depth || left_has_greater_bit_depth)
535 return !!(left_has_greater_bit_depth);
536
537 return ((int32_t)0xffffffff);
538}
539
540/* The original switch passes fixed diagnostic strings to the formatter. A few
541 * messages end at a newline because it passed their continuation as unused
542 * variadic arguments. Keep the text that actually reaches the game's log. */
543static const char *direct_draw_error_message(HRESULT error)
544{
545 switch (error)
546 {
547 case DD_OK: return "Action successful.";
548 case DDERR_UNSUPPORTED: return "Action not supported.";
549 case DDERR_GENERIC: return "Generic failure.";
550 case DDERR_NOTINITIALIZED: return "An attempt was made to invoke an interface member of a DirectDraw object created by CoCreateInstance() before it was initialized.";
551 case DDERR_OUTOFMEMORY: return "DirectDraw does not have enough memory to perform the operation.";
552 case DDERR_INVALIDPARAMS: return "One or more of the parameters passed to the callback function are incorrect.";
553 case DDERR_ALREADYINITIALIZED: return "This object is already initialized.";
554 case DDERR_CANNOTATTACHSURFACE: return "This surface can not be attached to the requested surface.";
555 case DDERR_CANNOTDETACHSURFACE: return "This surface can not be detached from the requested surface.";
556 case DDERR_CURRENTLYNOTAVAIL: return "Support is currently not available.";
557 case DDERR_EXCEPTION: return "An exception was encountered while performing the requested operation.";
558 case DDERR_HEIGHTALIGN: return "Height of rectangle provided is not a multiple of reqd alignment.";
559 case DDERR_INCOMPATIBLEPRIMARY: return "Unable to match primary surface creation request with existing primary surface.";
560 case DDERR_INVALIDCAPS: return "One or more of the caps bits passed to the callback are incorrect.";
561 case DDERR_INVALIDCLIPLIST: return "DirectDraw does not support provided Cliplist.";
562 case DDERR_INVALIDMODE: return "DirectDraw does not support the requested mode.";
563 case DDERR_INVALIDOBJECT: return "DirectDraw received a pointer that was an invalid DIRECTDRAW object.";
564 case DDERR_INVALIDPIXELFORMAT: return "Pixel format was invalid as specified.";
565 case DDERR_INVALIDRECT: return "Rectangle provided was invalid.";
566 case DDERR_LOCKEDSURFACES: return "Operation could not be carried out because one or more surfaces are locked.";
567 case DDERR_NO3D: return "There is no 3D present.";
568 case DDERR_NOALPHAHW: return "Operation could not be carried out because there is no alpha accleration hardware present or available.";
569 case DDERR_NOSTEREOHARDWARE: return "Operation could not be carried out because there is no stereo hardware present or available.";
570 case DDERR_NOSURFACELEFT: return "Operation could not be carried out because there is no hardware present which supports stereo surfaces.";
571 case DDERR_NOCLIPLIST: return "No clip list available.";
572 case DDERR_NOCOLORCONVHW: return "Operation could not be carried out because there is no color conversion hardware present or available.";
573 case DDERR_NOCOOPERATIVELEVELSET: return "Create function called without DirectDraw object method SetCooperativeLevel being called.";
574 case DDERR_NOCOLORKEY: return "Surface doesn't currently have a color key";
575 case DDERR_NOCOLORKEYHW: return "Operation could not be carried out because there is no hardware support of the dest color key.";
576 case DDERR_NODIRECTDRAWSUPPORT: return "No DirectDraw support possible with current display driver.";
577 case DDERR_NOEXCLUSIVEMODE: return "Operation requires the application to have exclusive mode but the application does not have exclusive mode.";
578 case DDERR_NOFLIPHW: return "Flipping visible surfaces is not supported.";
579 case DDERR_NOGDI: return "There is no GDI present.";
580 case DDERR_NOMIRRORHW: return "Operation could not be carried out because there is no hardware present or available.";
581 case DDERR_NOTFOUND: return "Requested item was not found.";
582 case DDERR_NOOVERLAYHW: return "Operation could not be carried out because there is no overlay hardware present or available.";
583 case DDERR_OVERLAPPINGRECTS: return "Operation could not be carried out because the source and destination rectangles are on the same surface and overlap each other.";
584 case DDERR_NORASTEROPHW: return "Operation could not be carried out because there is no appropriate raster op hardware present or available.";
585 case DDERR_NOROTATIONHW: return "Operation could not be carried out because there is no rotation hardware present or available.";
586 case DDERR_NOSTRETCHHW: return "Operation could not be carried out because there is no hardware support for stretching.";
587 case DDERR_NOT4BITCOLOR: return "DirectDrawSurface is not in 4 bit color palette and the requested operation requires 4 bit color palette.";
588 case DDERR_NOT4BITCOLORINDEX: return "DirectDrawSurface is not in 4 bit color index palette and the requested operation requires 4 bit color index palette.";
589 case DDERR_NOT8BITCOLOR: return "DirectDraw Surface is not in 8 bit color mode and the requested operation requires 8 bit color.";
590 case DDERR_NOTEXTUREHW: return "Operation could not be carried out because there is no texture mapping hardware present or available.";
591 case DDERR_NOVSYNCHW: return "Operation could not be carried out because there is no hardware support for vertical blank synchronized operations.";
592 case DDERR_NOZBUFFERHW: return "Operation could not be carried out because there is no hardware support for zbuffer blting.";
593 case DDERR_NOZOVERLAYHW: return "Overlay surfaces could not be z layered based on their BltOrder because the hardware does not support z layering of overlays.";
594 case DDERR_OUTOFCAPS: return "The hardware needed for the requested operation has already been allocated.";
595 case DDERR_OUTOFVIDEOMEMORY: return "DirectDraw does not have enough memory to perform the operation.";
596 case DDERR_OVERLAYCANTCLIP: return "Hardware does not support clipped overlays.";
597 case DDERR_OVERLAYCOLORKEYONLYONEACTIVE: return "Can only have ony color key active at one time for overlays.";
598 case DDERR_PALETTEBUSY: return "Access to this palette is being refused because the palette is already locked by another thread.";
599 case DDERR_COLORKEYNOTSET: return "No src color key specified for this operation.";
600 case DDERR_SURFACEALREADYATTACHED: return "This surface is already attached to the surface it is being attached to.";
601 case DDERR_SURFACEALREADYDEPENDENT: return "This surface is already a dependency of the surface it is being made a dependency of.";
602 case DDERR_SURFACEBUSY: return "Access to this surface is being refused because the surface is already locked by another thread.";
603 case DDERR_CANTLOCKSURFACE: return "Access to this surface is being refused because no driver exists\n";
604 case DDERR_SURFACEISOBSCURED: return "Access to Surface refused because Surface is obscured.";
605 case DDERR_SURFACELOST: return "Access to this surface is being refused because the surface is gone.\n";
606 case DDERR_SURFACENOTATTACHED: return "The requested surface is not attached.";
607 case DDERR_TOOBIGHEIGHT: return "Height requested by DirectDraw is too large.";
608 case DDERR_TOOBIGSIZE: return "Size requested by DirectDraw is too large -- The individual height and width are OK.";
609 case DDERR_TOOBIGWIDTH: return "Width requested by DirectDraw is too large.";
610 case DDERR_UNSUPPORTEDFORMAT: return "Pixel format requested is unsupported by DirectDraw.";
611 case DDERR_UNSUPPORTEDMASK: return "Bitmask in the pixel format requested is unsupported by DirectDraw.";
612 case DDERR_INVALIDSTREAM: return "The specified stream contains invalid data.";
613 case DDERR_VERTICALBLANKINPROGRESS: return "Vertical blank is in progress.";
614 case DDERR_WASSTILLDRAWING: return "Informs DirectDraw that the previous Blt which is transfering information\n";
615 case DDERR_DDSCAPSCOMPLEXREQUIRED: return "The specified surface type requires specification of the COMPLEX flag.";
616 case DDERR_XALIGN: return "Rectangle provided was not horizontally aligned on reqd. boundary.";
617 case DDERR_INVALIDDIRECTDRAWGUID: return "The GUID passed to DirectDrawCreate is not a valid DirectDraw driver identifier.";
618 case DDERR_DIRECTDRAWALREADYCREATED: return "A DirectDraw object representing this driver has already been created for this process.";
619 case DDERR_NODIRECTDRAWHW: return "A hardware only DirectDraw object creation was attempted but the driver did not support any hardware.";
620 case DDERR_PRIMARYSURFACEALREADYEXISTS: return "This process already has created a primary surface.";
621 case DDERR_NOEMULATION: return "Software emulation not available.";
622 case DDERR_REGIONTOOSMALL: return "Region passed to Clipper::GetClipList is too small.";
623 case DDERR_CLIPPERISUSINGHWND: return "An attempt was made to set a clip list for a clipper object that is already monitoring an hwnd.";
624 case DDERR_NOCLIPPERATTACHED: return "No clipper object attached to surface object.";
625 case DDERR_NOHWND: return "Clipper notification requires an HWND or no HWND has previously been set as the CooperativeLevel HWND.";
626 case DDERR_HWNDSUBCLASSED: return "HWND used by DirectDraw CooperativeLevel has been subclassed, this prevents DirectDraw from restoring state.";
627 case DDERR_HWNDALREADYSET: return "The CooperativeLevel HWND has already been set. It can not be reset while the process has surfaces or palettes created.";
628 case DDERR_NOPALETTEATTACHED: return "No palette object attached to this surface.";
629 case DDERR_NOPALETTEHW: return "No hardware support for 16 or 256 color palettes.";
630 case DDERR_BLTFASTCANTCLIP: return "A clipper object is attached to the source surface passed into a BltFast call.";
631 case DDERR_NOBLTHW: return "No blitter.";
632 case DDERR_NODDROPSHW: return "No DirectDraw ROP hardware.";
633 case DDERR_OVERLAYNOTVISIBLE: return "Returned when GetOverlayPosition is called on a hidden overlay.";
634 case DDERR_NOOVERLAYDEST: return "Returned when GetOverlayPosition is called on a overlay that UpdateOverlay has never been called on to establish a destionation.";
635 case DDERR_INVALIDPOSITION: return "Returned when the position of the overlay on the destionation is no longer legal for that destionation.";
636 case DDERR_NOTAOVERLAYSURFACE: return "Returned when an overlay member is called for a non-overlay surface.";
637 case DDERR_EXCLUSIVEMODEALREADYSET: return "An attempt was made to set the cooperative level when it was already set to exclusive.";
638 case DDERR_NOTFLIPPABLE: return "An attempt has been made to flip a surface that is not flippable.";
639 case DDERR_CANTDUPLICATE: return "Can't duplicate primary & 3D surfaces, or surfaces that are implicitly created.";
640 case DDERR_NOTLOCKED: return "Surface was not locked. An attempt to unlock a surface that was not locked at all, or by this process, has been attempted.";
641 case DDERR_CANTCREATEDC: return "Windows can not create any more DCs, or a DC was requested for a paltte-indexed\n";
642 case DDERR_NODC: return "No DC was ever created for this surface.";
643 case DDERR_WRONGMODE: return "This surface can not be restored because it was created in a different mode.";
644 case DDERR_IMPLICITLYCREATED: return "This surface can not be restored because it is an implicitly created surface.";
645 case DDERR_NOTPALETTIZED: return "The surface being used is not a palette-based surface.";
646 case DDERR_UNSUPPORTEDMODE: return "The display is currently in an unsupported mode.";
647 case DDERR_NOMIPMAPHW: return "Operation could not be carried out because there is no mip-map texture mapping hardware present or available.";
648 case DDERR_INVALIDSURFACETYPE: return "The requested action could not be performed because the surface was of the wrong type.";
649 case DDERR_NOOPTIMIZEHW: return "Device does not support optimized surfaces, therefore no video memory optimized surfaces.";
650 case DDERR_NOTLOADED: return "Surface is an optimized surface, but has not yet been allocated any memory.";
651 case DDERR_NOFOCUSWINDOW: return "Attempt was made to create or set a device window without first setting the focus window.";
652 case DDERR_NOTONMIPMAPSUBLEVEL: return "Attempt was made to set a palette on a mipmap sublevel.";
653 case DDERR_DCALREADYCREATED: return "A DC has already been returned for this surface. Only one DC can be retrieved per surface.";
654 case DDERR_NONONLOCALVIDMEM: return "An attempt was made to allocate non-local video memory from a device that does not support non-local video memory.";
655 case DDERR_CANTPAGELOCK: return "The attempt to page lock a surface failed.";
656 case DDERR_CANTPAGEUNLOCK: return "The attempt to page unlock a surface failed.";
657 case DDERR_NOTPAGELOCKED: return "An attempt was made to page unlock a surface with no outstanding page locks.";
658 case DDERR_MOREDATA: return "There is more data available than the specified buffer size could hold.";
659 case DDERR_EXPIRED: return "The data has expired and is therefore no longer valid.";
660 case DDERR_TESTFINISHED: return "The mode test has finished executing.";
661 case DDERR_NEWMODE: return "The mode test has switched to a new mode.";
662 case DDERR_D3DNOTINITIALIZED: return "D3D has not yet been initialized.";
663 case DDERR_VIDEONOTACTIVE: return "The video port is not active.";
664 case DDERR_NOMONITORINFORMATION: return "The monitor does not have EDID data.";
665 case DDERR_NODRIVERSUPPORT: return "The driver does not enumerate display mode refresh rates.";
666 case DDERR_DEVICEDOESNTOWNSURFACE: return "Surfaces created by one direct draw device cannot be used directly by another direct draw device.";
667 default: return "Unknown Error.";
668 }
669}
670
671// Original pcdogs.exe: 0x4195c0
672char *D3D_FormatDirectXError(HRESULT error_code, char *out_buffer)
673{
675 if (out_buffer)
676 {
677 size_t length = strlen(g_errorMessageBuffer) + 1;
678 size_t offset = 0;
679
680 /* The executable measures the whole string, then copies forward in
681 * DWORDs followed by the remaining bytes. */
682 for (; length - offset >= sizeof(uint32_t); offset += sizeof(uint32_t))
683 {
684 uint32_t word;
685 memcpy(&word, g_errorMessageBuffer + offset, sizeof(word));
686 memcpy(out_buffer + offset, &word, sizeof(word));
687 }
688 for (; offset < length; ++offset)
689 out_buffer[offset] = g_errorMessageBuffer[offset];
690 }
692}
693
694// Original pcdogs.exe: 0x445120
695// Forwards the GUID, output pointer, interface ID and outer object to DirectDrawCreateEx and returns its HRESULT.
696HRESULT __stdcall DDraw_CreateEx(GUID *lp_guid, void **lplp_dd, GUID *iid, IUnknown *p_unk_outer)
697{
698 /* tailcall */
699 return DirectDrawCreateEx(lp_guid, lplp_dd, iid, p_unk_outer);
700}
void Debug_Log(char const *category, char const *message)
Definition diagnostics.c:17
#define g_debugLogFile
Definition globals.h:12
#define Debug_LogMessageBuffer
Definition globals.h:18
#define D3D_IID_IDirect3DHALDevice
#define D3D_IID_IDirect3DTnLHalDevice
#define GRAPHICS_DEFAULT_WIDTH
Definition constants.h:25
#define GRAPHICS_DEFAULT_COLOR_BITS
Definition constants.h:27
#define GRAPHICS_DEFAULT_HEIGHT
Definition constants.h:26
#define Graphics_WindowWidth
Definition globals.h:140
#define Graphics_WindowHeight
Definition globals.h:143
void D3D_CloseDebugLog()
HRESULT DDraw_CreateEx(GUID *lp_guid, void **lplp_dd, GUID *iid, IUnknown *p_unk_outer)
char * D3D_FormatDirectXError(HRESULT error_code, char *out_buffer)
ULONG D3D_ReleaseDirectDrawDevice()
void D3D_ReleaseAllAndReportLeaks()
static const char * direct_draw_error_message(HRESULT error)
void D3D_InitializeDirectDraw()
int32_t DDraw_CompareDisplayModes(const void *left_record, const void *right_record)
BOOL D3D_InitDirectDrawAndDirect3D(HWND hwnd)
#define g_cooperativeLevelSet
Definition globals.h:21
#define data_44d0e8
Definition opaque_data.h:21
#define data_44d0d8
Definition opaque_data.h:18
void D3D_CloseDebugLog()
struct D3D_DriverInfo * D3D_EnumerateAndSelectDefaultDevice()
HRESULT D3D_EnumZBufferFormatCallback(DDPIXELFORMAT *pixel_format, void *selected_format_context)
uint8_t D3D_SetBlendMode(int32_t blend_mode)
HWND Window_GetMainHandle(void)
Definition window.c:94
#define Window_MainHandle
Definition globals.h:213
#define g_displayRectTop
Definition globals.h:162
#define D3D_InitializationRecords
Definition globals.h:264
#define g_ddrawPrimarySurface
Definition globals.h:225
#define g_displayRectLeft
Definition globals.h:159
#define g_ddrawBackBuffer
Definition globals.h:222
#define g_ddrawZBuffer
Definition globals.h:231
#define g_displayRectRight
Definition globals.h:165
#define D3D_EmptyLogMessage
Definition globals.h:186
#define g_displayRectBottom
Definition globals.h:168
int32_t CRT_fclose(struct File_Handle *stream)
Definition stdio.c:80
int32_t CRT_printf(char const *format,...)
Definition formatting.c:63
int32_t CRT_fprintf(struct File_Handle *stream, char const *format,...)
Definition formatting.c:47
int32_t CRT_fflush(struct File_Handle *stream)
Definition stdio.c:17
int32_t CRT_atexit(void(*callback)())
Definition startup.c:99
struct File_Handle * CRT_fopen(char const *filename, char const *mode)
Definition stdio.c:134
int32_t String_FormatCRTString(char *buffer, char const *format,...)
Definition formatting.c:18
struct _GUID driverGuid
Definition types.h:40
struct _GUID deviceGuid
Definition types.h:39
DDPIXELFORMAT zBufferFormat
Definition types.h:57
D3DVIEWPORT7 viewport
Definition types.h:58
#define g_d3dDevice7
Definition values.h:792
#define g_directDraw7
Definition values.h:810
#define g_clientScreenRectTop
Definition values.h:258
#define g_driverInitialized
Definition values.h:243
#define g_clientScreenRectRight
Definition values.h:261
#define g_direct3D7
Definition values.h:783
#define g_supportsGammaRamp
Definition values.h:780
#define g_errorMessageBuffer
Definition values.h:315
#define g_clientScreenRectLeft
Definition values.h:255
#define g_activeTextureCount
Definition values.h:267
#define g_selectedDriverIndex
Definition values.h:300
#define g_clientScreenRectBottom
Definition values.h:264
#define g_currentBoundTextureSurface
Definition values.h:294
uint32_t MESSAGEBOX_STYLE
#define MB_OK