102 Dalmatians: Puppies to the Rescue
Decompilation Source Reference | GitLab repository
Loading...
Searching...
No Matches
graphics_surfaces.c
Go to the documentation of this file.
1/* Recovered game implementation; original addresses identify provenance. */
9#include <string.h>
10
11// Original pcdogs.exe: 0x413bf0
12// Requests a whole-surface color fill and returns whether the immediate Blt result is successful. No completion wait is requested, and only the required size and fill-color fields are initialized.
13BOOL DDraw_FillSurfaceColor(IDirectDrawSurface7 *surface, uint32_t fill_color)
14{
15 DDBLTFX blit_effects;
16 blit_effects.dwFillColor = fill_color;
17
18 IDirectDrawSurface7Vtbl *lp_vtbl = surface->lpVtbl;
19
20 blit_effects.dwSize = 0x64;
21
22 return !(lp_vtbl->Blt(surface, NULL, NULL, NULL, DDBLT_COLORFILL, &blit_effects));
23}
24
25// Original pcdogs.exe: 0x413c30
26// Rounds texture dimensions up to powers of two, caps them at 256 and applies square-only hardware requirements before creating the surface. If no texture format is selected, it still attempts creation with an implicit format.
27IDirectDrawSurface7 *D3D_CreateTextureSurface(uint32_t width, uint32_t height)
28{
29 IDirect3DDevice7 *capabilities_device = g_d3dDevice7;
30 IDirectDrawSurface7 *surface = NULL;
31 D3DDEVICEDESC7 caps;
32
33 if (capabilities_device->lpVtbl->GetCaps(capabilities_device, &caps) < 0)
34 return 0;
35
36 DDSURFACEDESC2 texture_surface_desc = {0};
37 texture_surface_desc.dwSize = sizeof(texture_surface_desc);
38 texture_surface_desc.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_TEXTURESTAGE;
39 texture_surface_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
40 if (IsEqualGUID(&caps.deviceGUID, &D3D_IID_IDirect3DHALDevice) ||
41 IsEqualGUID(&caps.deviceGUID, &D3D_IID_IDirect3DTnLHalDevice))
42 texture_surface_desc.ddsCaps.dwCaps2 = DDSCAPS2_TEXTUREMANAGE;
43 else
44 texture_surface_desc.ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
45
46 uint32_t power_of_two_width = 1;
47
48 texture_surface_desc.dwWidth = 1;
49
50 if (width > 1)
51 {
52 do
53 power_of_two_width <<= 1;
54
55 while (width > power_of_two_width);
56
57 texture_surface_desc.dwWidth = power_of_two_width;
58 }
59
60 uint32_t power_of_two_height = 1;
61
62 texture_surface_desc.dwHeight = 1;
63
64 if (height > 1)
65 {
66 do
67 power_of_two_height <<= 1;
68
69 while (height > power_of_two_height);
70
71 texture_surface_desc.dwHeight = power_of_two_height;
72 }
73
74 if (power_of_two_width > GRAPHICS_TEXTURE_MAX_DIMENSION)
75 {
76 power_of_two_width = GRAPHICS_TEXTURE_MAX_DIMENSION;
77 texture_surface_desc.dwWidth = GRAPHICS_TEXTURE_MAX_DIMENSION;
78 }
79
80 // Enumerate texture formats to find compatible 16-bit format:
81 // - Prefers 4444 RGBA if DAT_00459ecd set
82 // - Falls back to 565 RGB or 1555 RGBA
83 // - Stores format in aiStack_134[3]
84 if (power_of_two_height > GRAPHICS_TEXTURE_MAX_DIMENSION)
85 {
86 power_of_two_height = GRAPHICS_TEXTURE_MAX_DIMENSION;
87 texture_surface_desc.dwHeight = GRAPHICS_TEXTURE_MAX_DIMENSION;
88 }
89
90 if (caps.dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_SQUAREONLY)
91 {
92 if (power_of_two_width <= power_of_two_height)
93 {
94 power_of_two_width = power_of_two_height;
95 texture_surface_desc.dwWidth = power_of_two_width;
96 }
97 else
98 {
99 power_of_two_height = power_of_two_width;
100 texture_surface_desc.dwHeight = power_of_two_height;
101 }
102 }
103
104 g_createdTextureHeight = power_of_two_height;
105 IDirect3DDevice7 *format_enumeration_device = g_d3dDevice7;
106 g_createdTextureWidth = power_of_two_width;
107 // ID3DDevice7::EnumTextureFormats(this, callback, context) - Enumerate supported texture formats
108 format_enumeration_device->lpVtbl->EnumTextureFormats(format_enumeration_device, D3D_HandleTextureFormatCallback,
109 &texture_surface_desc.ddpfPixelFormat);
110
111 int32_t green_bits_to_discard = 8;
112
113 if (texture_surface_desc.ddpfPixelFormat.dwRGBBitCount)
114 {
115 uint32_t dw_flags = texture_surface_desc.dwFlags;
116
118 dw_flags |= DDSD_PIXELFORMAT;
119 texture_surface_desc.dwFlags = dw_flags;
120 }
121 else
122 {
123 char *category;
124 char *message;
125
127 {
128 message = "Texture Error";
129 category = "No 4444 RGBA texture formats!";
130 }
131 else
132 {
133 message = "Error";
134 category = "No 16-bit RGB texture formats!";
135 }
136
137 Debug_Log(category, message);
138 __builtin_memset(&texture_surface_desc.ddpfPixelFormat, 0, 0x20);
140 }
141
142 uint32_t dw_r_bit_mask = texture_surface_desc.ddpfPixelFormat.dwRBitMask;
143
144 graphics_pixel_alpha_mask = texture_surface_desc.ddpfPixelFormat.dwRGBAlphaBitMask;
145 __builtin_memcpy(&g_lastTextureSurfaceDesc, &texture_surface_desc, 0x7c);
146
147 uint32_t dw_g_bit_mask = texture_surface_desc.ddpfPixelFormat.dwGBitMask;
148
149 graphics_pixel_blue_mask = texture_surface_desc.ddpfPixelFormat.dwBBitMask;
152
153 int32_t green_shift = 0;
154 int32_t red_shift = 0;
155 int32_t red_bits_to_discard = 8;
156 int32_t alpha_bits_to_discard = 8;
157
158 // Calculate bit shift/mask for each color channel:
159 // DAT_01a8a6ec/708 = R/G shift, _DAT_01a8a6e4/70c = R/G bits
160 // DAT_019a9630/01a8a700 = B/A shift, _DAT_019a9634/01a8a6fc = B/A bits
161 // Used for pixel format conversion during texture upload
162 graphics_pixel_red_mask = dw_r_bit_mask;
163 graphics_pixel_green_mask = dw_g_bit_mask;
167 // Calculate bit shift/mask for each color channel from pixel format. g_textureRedBitShift/Count,
168 // g_textureGreenBitShift/Count, g_textureBlueBitShift/Count, g_textureAlphaBitShift/Count used for pixel
169 // format conversion during texture upload.
173
174 if (dw_r_bit_mask)
175 {
176 while (true)
177 {
178 if ((uint8_t)dw_r_bit_mask & 1)
179 {
181
182 if (dw_r_bit_mask)
183 {
184 do
185 {
186 red_bits_to_discard -= 1;
187 dw_r_bit_mask >>= 1;
188 } while (dw_r_bit_mask);
189
190 g_redBitsToDiscard = red_bits_to_discard;
191 }
192
193 break;
194 }
195
196 red_shift += 1;
197 dw_r_bit_mask >>= 1;
198
199 if (!dw_r_bit_mask)
200 {
202
203 break;
204 }
205 }
206 }
207
208 uint32_t remaining_green_mask = dw_g_bit_mask;
209
210 if (dw_g_bit_mask)
211 {
212 while (true)
213 {
214 if ((uint8_t)remaining_green_mask & 1)
215 {
216 graphics_pixel_green_shift = green_shift;
217
218 if (remaining_green_mask)
219 {
220 do
221 {
222 green_bits_to_discard -= 1;
223 remaining_green_mask >>= 1;
224 } while (remaining_green_mask);
225
226 g_greenBitsToDiscard = green_bits_to_discard;
227 }
228
229 break;
230 }
231
232 green_shift += 1;
233 remaining_green_mask >>= 1;
234
235 if (!remaining_green_mask)
236 {
237 graphics_pixel_green_shift = green_shift;
238
239 break;
240 }
241 }
242 }
243
244 uint32_t dw_b_bit_mask = texture_surface_desc.ddpfPixelFormat.dwBBitMask;
245
246 if (dw_b_bit_mask)
247 {
248 int32_t blue_shift =
250
251 while (true)
252 {
253 if ((uint8_t)dw_b_bit_mask & 1)
254 {
256 blue_shift;
257
258 if (dw_b_bit_mask)
259 {
260 int32_t blue_bits_to_discard = g_blueBitsToDiscard;
261
262 do
263 {
264 blue_bits_to_discard -= 1;
265 dw_b_bit_mask >>= 1;
266 } while (dw_b_bit_mask);
267
268 g_blueBitsToDiscard = blue_bits_to_discard;
269 }
270
271 break;
272 }
273
274 blue_shift += 1;
275 dw_b_bit_mask >>= 1;
276
277 if (!dw_b_bit_mask)
278 {
280 blue_shift;
281
282 break;
283 }
284 }
285 }
286
287 uint32_t dw_rgb_alpha_bit_mask = texture_surface_desc.ddpfPixelFormat.dwRGBAlphaBitMask;
288
289 if (dw_rgb_alpha_bit_mask)
290 {
291 int32_t alpha_shift = graphics_pixel_alpha_shift;
292
293 while (true)
294 {
295 if ((uint8_t)dw_rgb_alpha_bit_mask & 1)
296 {
297 graphics_pixel_alpha_shift = alpha_shift;
298
299 if (dw_rgb_alpha_bit_mask)
300 {
301 do
302 {
303 alpha_bits_to_discard -= 1;
304 dw_rgb_alpha_bit_mask >>= 1;
305 } while (dw_rgb_alpha_bit_mask);
306
307 g_alphaBitsToDiscard = alpha_bits_to_discard;
308 }
309
310 break;
311 }
312
313 alpha_shift += 1;
314 dw_rgb_alpha_bit_mask >>= 1;
315
316 if (!dw_rgb_alpha_bit_mask)
317 {
318 graphics_pixel_alpha_shift = alpha_shift;
319
320 break;
321 }
322 }
323 }
324
325 // IDirectDraw7::CreateSurface - Create DirectDraw texture surface with DDSURFACEDESC2 structure. Returns
326 // IDirectDrawSurface7** in surfacePtr. Locks surface after creation for initial setup.
327 IDirectDraw7 *surface_factory = g_directDraw7;
328
329 HRESULT error_code = surface_factory->lpVtbl->CreateSurface(surface_factory, &texture_surface_desc,
330 &surface, NULL);
331
332 if (error_code >= 0)
333 {
334 DDraw_FillSurfaceColor(surface, 0);
335
336 return surface;
337 }
338
339 char *creation_error_context = "DirectX Error: CreateTexture();";
340
341 D3D_FormatDirectXError(error_code, NULL);
342 Debug_Log(g_errorMessageBuffer, creation_error_context);
343
344 return 0;
345}
346
347// Original pcdogs.exe: 0x413f60
348// Selects the first texture format satisfying the current alpha-mode predicate and copies it to the context. Alpha mode checks the 0xf000 alpha mask without requiring an exact 4444 RGB layout.
349HRESULT __stdcall D3D_HandleTextureFormatCallback(DDPIXELFORMAT *pixel_format, void *selected_format_context)
350{
352
353 if (!pixel_format || !selected_format_context)
354 return 1;
355
356 uint32_t dw_flags = pixel_format->dwFlags;
357
358 if (dw_flags & (DDPF_LUMINANCE | DDPF_BUMPLUMINANCE | DDPF_BUMPDUDV))
359 return 1;
360
361 uint32_t dw_rgb_bit_count = pixel_format->dwRGBBitCount;
362
363 if (dw_rgb_bit_count < 0x10 || pixel_format->dwFourCC)
364 return 1;
365
366 uint32_t dw_rgb_alpha_bit_mask = pixel_format->dwRGBAlphaBitMask;
367 uint8_t requires_alpha = graphics_tex_needs_alpha;
368
369 if (dw_rgb_alpha_bit_mask == 0xf000)
370 {
371 if (requires_alpha != 1)
372 return 1;
373 }
374 else if (requires_alpha || (dw_flags & DDPF_ALPHAPIXELS) || dw_rgb_bit_count != 16)
375 return 1;
376
377 /* The accepted format is copied forwards in DWORDs, including overlap. */
378 const volatile uint32_t *source = (const volatile uint32_t *)pixel_format;
379 volatile uint32_t *destination = selected_format_context;
380 for (unsigned i = 0; i < sizeof(*pixel_format) / sizeof(*destination); ++i)
381 destination[i] = source[i];
382
383 return 0;
384}
385
386// Original pcdogs.exe: 0x413ff0
387// Creates and clears a work surface, copying the source texture format when implicit-format mode is disabled. It logs creation failure and returns null.
388IDirectDrawSurface7 *D3D_CreateWorkSurface(IDirectDrawSurface7 *source_surface, uint32_t width, uint32_t height)
389{
390 DDSURFACEDESC2 create_desc;
391 memset(&create_desc, 0, 0x7c);
392 DDSURFACEDESC2 source_desc;
393 source_desc.dwSize = 0x7c;
394 create_desc.dwSize = 0x7c;
395
397 {
398 DDSURFACEDESC2 *source_description_output = &source_desc;
399 IDirectDrawSurface7Vtbl *lp_vtbl = source_surface->lpVtbl;
400 source_desc.dwFlags = DDSD_PIXELFORMAT;
401 lp_vtbl->GetSurfaceDesc(source_surface, source_description_output);
402 memcpy(&create_desc.ddpfPixelFormat, &source_desc.ddpfPixelFormat, 0x20);
403 }
404
405 uint8_t implicit_format = g_useImplicitTextureFormat;
406
407 create_desc.dwFlags = (DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH);
408
409 if (!implicit_format)
410 create_desc.dwFlags = (DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT);
411
412 create_desc.dwWidth = width;
413 create_desc.dwHeight = height;
414 IDirectDraw7 *surface_factory = g_directDraw7;
415 IUnknown *outer_unknown = NULL;
416 IDirectDrawSurface7 *surface;
417 IDirectDrawSurface7 **surface_output = &surface;
418 create_desc.ddsCaps.dwCaps = (DDSCAPS_TEXTURE | DDSCAPS_SYSTEMMEMORY);
419
420 HRESULT error_code =
421 surface_factory->lpVtbl->CreateSurface(surface_factory, &create_desc, surface_output, outer_unknown);
422
423 if (error_code >= 0)
424 {
425 DDraw_FillSurfaceColor(surface, 0);
426
427 return surface;
428 }
429
430 char *creation_error_context = "DirectXError: CreateWorkSurface();";
431
432 Debug_Log(D3D_FormatDirectXError(error_code, NULL), creation_error_context);
433
434 return 0;
435}
436
437// Original pcdogs.exe: 0x414d70
438// Updates cached blend and texture-stage state and returns the diffuse-alpha byte for drawing. The return value is render data rather than a success status.
439uint8_t D3D_SetBlendMode(int32_t blend_mode)
440{
441 char result;
442
443 if (blend_mode)
444 {
445 int32_t previous_blend_mode = g_currentBlendMode;
446
447 if (!previous_blend_mode || graphics_text_rendering_mode)
448 {
449 IDirect3DDevice7 *alpha_enable_device = g_d3dDevice7;
450
451 alpha_enable_device->lpVtbl->SetRenderState(alpha_enable_device, D3DRENDERSTATE_ALPHABLENDENABLE, 1);
452
453 IDirect3DDevice7 *alpha_operation_device = g_d3dDevice7;
454
455 DWORD alpha_argument_state;
456
457 IDirect3DDevice7 *alpha_argument_device;
458 IDirect3DDevice7Vtbl *lp_vtbl;
459
461 {
462 alpha_operation_device->lpVtbl->SetTextureStageState(alpha_operation_device, 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
463
464 alpha_argument_device = g_d3dDevice7;
465
466 alpha_argument_state = D3DTSS_ALPHAARG1;
467 lp_vtbl = alpha_argument_device->lpVtbl;
468 }
469 else
470 {
471 alpha_operation_device->lpVtbl->SetTextureStageState(alpha_operation_device, 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
472
473 IDirect3DDevice7 *alpha_first_argument_device = g_d3dDevice7;
474
475 alpha_first_argument_device->lpVtbl->SetTextureStageState(alpha_first_argument_device, 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
476 alpha_argument_device = g_d3dDevice7;
477
478 alpha_argument_state = D3DTSS_ALPHAARG2;
479 lp_vtbl = alpha_argument_device->lpVtbl;
480 }
481
482 lp_vtbl->SetTextureStageState(alpha_argument_device, 0, alpha_argument_state, D3DTA_DIFFUSE);
483 previous_blend_mode = g_currentBlendMode;
484 }
485
486 bool blend_mode_unchanged;
487
488 IDirect3DDevice7 *destination_blend_device;
489
490 if (blend_mode == GRAPHICS_BLEND_ADDITIVE || blend_mode == GRAPHICS_BLEND_ADDITIVE_DIM)
491 {
493
494 if (previous_blend_mode != blend_mode)
495 {
496 IDirect3DDevice7 *additive_source_blend_device = g_d3dDevice7;
497
498 additive_source_blend_device->lpVtbl->SetRenderState(additive_source_blend_device, D3DRENDERSTATE_SRCBLEND, D3DBLEND_SRCALPHA);
499 destination_blend_device = g_d3dDevice7;
500
501 destination_blend_device->lpVtbl->SetRenderState(destination_blend_device, D3DRENDERSTATE_DESTBLEND, D3DBLEND_ONE);
502 }
503 }
504 else
505 {
507 blend_mode_unchanged = previous_blend_mode == blend_mode;
508
509 if (!blend_mode_unchanged)
510 {
511 IDirect3DDevice7 *alpha_source_blend_device = g_d3dDevice7;
512
513 alpha_source_blend_device->lpVtbl->SetRenderState(alpha_source_blend_device, D3DRENDERSTATE_SRCBLEND, D3DBLEND_SRCALPHA);
514 destination_blend_device = g_d3dDevice7;
515
516 destination_blend_device->lpVtbl->SetRenderState(destination_blend_device, D3DRENDERSTATE_DESTBLEND, D3DBLEND_INVSRCALPHA);
517 }
518 }
519 }
520 else
521 {
522 IDirect3DDevice7 *default_alpha_enable_device = g_d3dDevice7;
523
525 {
526 default_alpha_enable_device->lpVtbl->SetRenderState(default_alpha_enable_device, D3DRENDERSTATE_ALPHABLENDENABLE, 0);
527 IDirect3DDevice7 *text_alpha_operation_device = g_d3dDevice7;
528
529 text_alpha_operation_device->lpVtbl->SetTextureStageState(text_alpha_operation_device, 0, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
530 result = GRAPHICS_ALPHA_OPAQUE;
531 }
532 else
533 {
534 default_alpha_enable_device->lpVtbl->SetRenderState(default_alpha_enable_device, D3DRENDERSTATE_ALPHABLENDENABLE, 1);
535
536 IDirect3DDevice7 *default_alpha_operation_device = g_d3dDevice7;
537
538 default_alpha_operation_device->lpVtbl->SetTextureStageState(default_alpha_operation_device, blend_mode, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
539
540 IDirect3DDevice7 *default_alpha_argument_device = g_d3dDevice7;
541
542 default_alpha_argument_device->lpVtbl->SetTextureStageState(default_alpha_argument_device, blend_mode, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
543 IDirect3DDevice7 *default_source_blend_device = g_d3dDevice7;
544
545 default_source_blend_device->lpVtbl->SetRenderState(default_source_blend_device, D3DRENDERSTATE_SRCBLEND, D3DBLEND_SRCALPHA);
546 IDirect3DDevice7 *default_destination_blend_device = g_d3dDevice7;
547
548 default_destination_blend_device->lpVtbl->SetRenderState(default_destination_blend_device, D3DRENDERSTATE_DESTBLEND, D3DBLEND_INVSRCALPHA);
549 result = GRAPHICS_ALPHA_OPAQUE;
550 }
551 }
552
553 int32_t text_rendering_mode = graphics_text_rendering_mode;
554
555 g_currentBlendMode = blend_mode;
556
557 if (text_rendering_mode)
559
560 return result;
561}
562
563// Original pcdogs.exe: 0x414f20
564// Reads stage-zero color operation and updates it only when it differs from the requested operation. COM status values are ignored.
565void D3D_SetTextureColorOperation(DWORD color_operation)
566{
567 DWORD previous_color_operation;
568
569 // Load the global Direct3D device pointer (g_d3dDevice)
570 IDirect3DDevice7 *query_device = g_d3dDevice7;
571 query_device->lpVtbl->GetTextureStageState(query_device, 0, 1, &previous_color_operation);
572
573 // Compare current state with desired state to avoid redundant D3D calls (optimization)
574 if (previous_color_operation != color_operation)
575 {
576 IDirect3DDevice7 *update_device = g_d3dDevice7;
577 // Call IDirect3DDevice7::SetTextureStageState(0, D3DTSS_COLOROP, newStateValue) to update the texture
578 // stage state only if it differs
579 update_device->lpVtbl->SetTextureStageState(update_device, 0, D3DTSS_COLOROP, color_operation);
580 }
581}
582
583// Original pcdogs.exe: 0x415b00
584// Sets Z-write enable only when graphics capability bit 0x2000 is present; unsupported devices are left unchanged.
586{
588 {
589 IDirect3DDevice7 *render_device = g_d3dDevice7;
590 render_device->lpVtbl->SetRenderState(render_device, D3DRENDERSTATE_ZWRITEENABLE, enable);
591 }
592}
593
594// Original pcdogs.exe: 0x418760
595// Clears the target and Z buffer to black and depth 1.0, returning the device HRESULT. The global device must already be valid.
597{
598 IDirect3DDevice7 *render_device = g_d3dDevice7;
599
600 return render_device->lpVtbl->Clear(render_device, 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0, 1.0f, 0);
601}
602
603// Original pcdogs.exe: 0x418cd0
604// Unlocks the entire supplied surface and returns its HRESULT; the surface must be valid.
605HRESULT DDraw_UnlockSurface(IDirectDrawSurface7 *surface)
606{
607 // IDirectDrawSurface7::Unlock(this, lpRect) - Vtable offset +0x80. Unlocks surface after CPU write access.
608 // Call IDirectDrawSurface7::Unlock(NULL) - unlock entire surface
609 return surface->lpVtbl->Unlock(surface, NULL);
610}
611
612// Original pcdogs.exe: 0x418de0
613// Builds identical 256-entry gamma channels from the effective scale and submits them through a cached gamma-control interface. NaN and scales below 0.1 become 0.1, scales above 10 become 5, and the channel formula is clamp(trunc(256*i*scale)-1,0,65535).
614void D3D_SetGammaRamp(float linear_ramp_scale)
615{
616 if (!(linear_ramp_scale >= 0.1f))
618 else if (linear_ramp_scale > 10.0f)
620 else
621 D3D_CurrentGammaScale = linear_ramp_scale;
622
624 {
625 Debug_Log("Gamma Ramp is not supported by this hardware.", "DirectX Error");
626 return;
627 }
628
629 IDirectDrawGammaControl *control = g_pGammaControl;
630 if (!control)
631 {
632 IDirectDrawSurface7 *primary = g_ddrawPrimarySurface;
633 HRESULT result = primary->lpVtbl->QueryInterface(primary, &GUID_IDirectDrawGammaControl, (void **)&g_pGammaControl);
634 if (FAILED(result))
635 {
636 Debug_Log(D3D_FormatDirectXError(result, NULL), "QueryInterface(GammaControl)");
637 return;
638 }
639 control = g_pGammaControl;
640 if (!control)
641 return;
642 }
643
644 DDGAMMARAMP ramp;
645 for (int i = 0; i < 256; ++i)
646 {
647 int intensity = __ftol_msvc((long double)(256 * i) * D3D_CurrentGammaScale) - 1;
648 if (intensity < 0)
649 intensity = 0;
650 else if (intensity > UINT16_MAX)
651 intensity = UINT16_MAX;
652
653 ramp.red[i] = ramp.green[i] = ramp.blue[i] = (WORD)intensity;
654 }
655
656 HRESULT result = control->lpVtbl->SetGammaRamp(control, 0, &ramp);
657 if (FAILED(result))
658 Debug_Log(D3D_FormatDirectXError(result, NULL), "SetGammaControl()");
659}
660
661// Original pcdogs.exe: 0x43d080
662// Compare the unrounded dimming result before passing the binary32 scale to the driver.
663void D3D_SetGammaFromMenuSetting(int32_t gamma_setting)
664{
665 const long double step = 0.2f;
666 long double scale = 1.0L;
667 if (gamma_setting < 5)
668 {
669 scale -= (5.0L - gamma_setting) * step;
670 if (scale < step)
671 scale = 0.1f;
672 }
673 else if (gamma_setting > 5)
674 scale += (gamma_setting - 5.0L) * step;
675
676 D3D_SetGammaRamp((float)scale);
677}
void Debug_Log(char const *category, char const *message)
Definition diagnostics.c:17
#define D3D_IID_IDirect3DHALDevice
#define D3D_IID_IDirect3DTnLHalDevice
#define GRAPHICS_CAP_DEPTH_BUFFER
Definition constants.h:16
#define GRAPHICS_TEXTURE_MAX_DIMENSION
Definition constants.h:28
#define GRAPHICS_ALPHA_DIM
Definition constants.h:61
@ GRAPHICS_BLEND_ADDITIVE_DIM
Definition constants.h:47
@ GRAPHICS_BLEND_ADDITIVE
Definition constants.h:45
@ GRAPHICS_BLEND_ALPHA_DIM
Definition constants.h:46
#define GRAPHICS_ALPHA_HALF
Definition constants.h:60
#define GRAPHICS_ALPHA_OPAQUE
Definition constants.h:59
#define GRAPHICS_GAMMA_RAMP_SUPPORTED
Definition constants.h:87
#define graphics_tex_needs_alpha
Definition globals.h:236
#define graphics_pixel_alpha_shift
Definition globals.h:320
#define graphics_pixel_green_shift
Definition globals.h:326
#define graphics_pixel_green_mask
Definition globals.h:314
#define graphics_pixel_blue_mask
Definition globals.h:305
#define graphics_pixel_red_mask
Definition globals.h:311
#define graphics_text_rendering_mode
Definition globals.h:293
#define graphics_pixel_alpha_mask
Definition globals.h:323
void D3D_SetGammaRamp(float linear_ramp_scale)
IDirectDrawSurface7 * D3D_CreateTextureSurface(uint32_t width, uint32_t height)
void D3D_SetZWriteEnableIfSupported(BOOL enable)
void D3D_SetGammaFromMenuSetting(int32_t gamma_setting)
BOOL DDraw_FillSurfaceColor(IDirectDrawSurface7 *surface, uint32_t fill_color)
HRESULT D3D_HandleTextureFormatCallback(DDPIXELFORMAT *pixel_format, void *selected_format_context)
IDirectDrawSurface7 * D3D_CreateWorkSurface(IDirectDrawSurface7 *source_surface, uint32_t width, uint32_t height)
uint8_t D3D_SetBlendMode(int32_t blend_mode)
HRESULT D3D_ClearViewport()
HRESULT DDraw_UnlockSurface(IDirectDrawSurface7 *surface)
void D3D_SetTextureColorOperation(DWORD color_operation)
char * D3D_FormatDirectXError(HRESULT error_code, char *out_buffer)
HRESULT D3D_HandleTextureFormatCallback(DDPIXELFORMAT *pixel_format, void *selected_format_context)
#define D3D_CreateTextureSurface_GraphicsPixelRedShift
Definition globals.h:234
#define g_ddrawPrimarySurface
Definition globals.h:225
#define GUID_IDirectDrawGammaControl
Definition globals.h:15
#define D3D_GraphicsCapabilitiesHighByte
Definition globals.h:261
#define D3D_CurrentGammaScale
Definition globals.h:21
#define D3D_CreateTextureSurface_GraphicsPixelBlueShift
Definition globals.h:228
int64_t __ftol_msvc(long double value)
#define g_greenBitsToDiscard
Definition values.h:819
#define g_createdTextureHeight
Definition values.h:798
#define g_d3dDevice7
Definition values.h:792
#define g_directDraw7
Definition values.h:810
#define g_useImplicitTextureFormat
Definition values.h:303
#define g_currentBlendMode
Definition values.h:297
#define g_pGammaControl
Definition values.h:306
#define g_alphaBitsToDiscard
Definition values.h:816
#define g_blueBitsToDiscard
Definition values.h:789
#define g_supportsGammaRamp
Definition values.h:780
#define g_lastTextureSurfaceDesc
Definition values.h:801
#define g_redBitsToDiscard
Definition values.h:807
#define g_errorMessageBuffer
Definition values.h:315
#define g_createdTextureWidth
Definition values.h:786