102 Patches: Detours to the Rescue
C reference for DttR maintainers and modders.
Loading...
Searching...
No Matches
com_directdraw7.c
Go to the documentation of this file.
1// IDirectDraw7 COM translator that handles the DDraw7 interface and creates D3D7
2// https://learn.microsoft.com/en-us/windows/win32/api/ddraw/nn-ddraw-idirectdraw7
3
5#include "graphics_private.h"
6#include <dttr_log.h>
7#include <stdlib.h>
8#include <string.h>
9
11
19
20static HRESULT __stdcall ddraw7_queryinterface(
22 void *riid,
23 void **ppv
24) {
26
27 if (ppv) {
28 *ppv = direct3d;
29 }
30
31 return S_OK;
32}
33
35
37
39
40static HRESULT __stdcall ddraw7_createclipper(
42 DWORD f,
43 void **clip,
44 void *outer
45) {
46 if (clip) {
47 *clip = NULL;
48 }
49
50 return S_OK;
51}
52
53static HRESULT __stdcall ddraw7_createpalette(
55 DWORD f,
56 void *colors,
57 void **pal,
58 void *outer
59) {
60 if (pal) {
61 *pal = NULL;
62 }
63
64 return S_OK;
65}
66
67static HRESULT __stdcall ddraw7_createsurface(
69 void *desc,
70 void **surf,
71 void *outer
72) {
73 if (surf) {
74 *surf = NULL;
75 } else {
76 return DDERR_INVALIDPARAMS;
77 }
78
79 // Use 640x480 RGB565 when the game does not provide surface metadata
80 uint32_t width = 640, height = 480, bpp = 16;
81 uint32_t r_mask = 0xF800, g_mask = 0x07E0, b_mask = 0x001F, a_mask = 0;
82 bool updates_logical_resolution = false;
83
84 if (desc) {
85 const DDSURFACEDESC2 *desc2 = (const DDSURFACEDESC2 *)desc;
86 const DWORD flags = desc2->dwFlags;
87
88 if (flags & DDSD_WIDTH) {
89 width = desc2->dwWidth;
90 }
91
92 if (flags & DDSD_HEIGHT) {
93 height = desc2->dwHeight;
94 }
95
96 if (flags & DDSD_PIXELFORMAT) {
97 bpp = desc2->ddpfPixelFormat.dwRGBBitCount;
98 r_mask = desc2->ddpfPixelFormat.dwRBitMask;
99 g_mask = desc2->ddpfPixelFormat.dwGBitMask;
100 b_mask = desc2->ddpfPixelFormat.dwBBitMask;
101 a_mask = desc2->ddpfPixelFormat.dwRGBAlphaBitMask;
102 }
103
104 if (flags & DDSD_CAPS) {
105 const DWORD caps = desc2->ddsCaps.dwCaps;
106 if (caps & (DDSCAPS_PRIMARYSURFACE | DDSCAPS_3DDEVICE | DDSCAPS_BACKBUFFER)) {
107 updates_logical_resolution = true;
108 }
109 }
110 }
111
113 width,
114 height,
115 bpp,
116 NULL,
117 NULL
118 );
119 if (validation != S_OK) {
120 return validation;
121 }
122
125 width,
126 height,
127 bpp,
128 r_mask,
129 g_mask,
130 b_mask,
131 a_mask
132 );
133 if (!created) {
134 return DDERR_OUTOFMEMORY;
135 }
136
137 if (updates_logical_resolution) {
138 dttr_graphics_set_logical_resolution((int)width, (int)height);
139 }
140
141 *surf = created;
142
143 return S_OK;
144}
145
147 ddraw7_duplicatesurface,
148 void *,
149 NULL,
151 void *src
152)
153
155 ddraw7_enumdisplaymodes,
158 void *desc,
159 void *ctx,
160 void *cb
161)
162
164 ddraw7_enumsurfaces,
166 DWORD f,
167 void *desc,
168 void *ctx,
169 void *cb
170)
171
173
175 ddraw7_getcaps,
177 void *drvCaps,
178 void *helCaps
179)
180
182 ddraw7_getdisplaymode,
184 void *desc
185)
186
187static HRESULT __stdcall ddraw7_getfourcccodes(
191) {
192 if (num) {
193 *num = 0;
194 }
195
196 return S_OK;
197}
198
199DTTR_COM_STUB_SET(ddraw7_getgdisurface, void *, NULL, DTTR_Graphics_COM_DirectDraw7 *self)
200
202 ddraw7_getmonitorfrequency,
203 DWORD,
204 60,
206)
207
209
211 ddraw7_getverticalblankstatus,
213 0,
215)
216
217DTTR_COM_NOOP_HRESULT(ddraw7_initialize, DTTR_Graphics_COM_DirectDraw7 *self, void *guid)
218
220
222 ddraw7_setcooperativelevel,
224 HWND hwnd,
226)
227
228static HRESULT __stdcall ddraw7_setdisplaymode(
234 DWORD f
235) {
236 if (!self || w == 0 || h == 0) {
237 return DDERR_INVALIDPARAMS;
238 }
239
240 if (bpp == 0) {
241 bpp = 16;
242 }
243
244 if (hz == 0) {
245 hz = 60;
246 }
247
249 "SetDisplayMode request: %lux%lu %lu-bpp @ %luHz flags=0x%lx",
250 (unsigned long)w,
251 (unsigned long)h,
252 (unsigned long)bpp,
253 (unsigned long)hz,
254 (unsigned long)f
255 );
257 return S_OK;
258}
259
261 ddraw7_waitforverticalblank,
263 DWORD f,
264 HANDLE evt
265)
266
267static HRESULT __stdcall ddraw7_getavailablevidmem(
269 void *caps,
272) {
273 if (tot) {
274 *tot = 128 * 1024 * 1024;
275 }
276
277 if (free) {
278 *free = 64 * 1024 * 1024;
279 }
280
281 return S_OK;
282}
283
285 ddraw7_getsurfacefromdc,
286 void *,
287 NULL,
289 HDC dc
290)
291
293
294DTTR_COM_NOOP_HRESULT(ddraw7_testcooperativelevel, DTTR_Graphics_COM_DirectDraw7 *self)
295
296static HRESULT __stdcall ddraw7_getdeviceidentifier(
298 void *id,
299 DWORD f
300) {
301 if (id) {
302 memset(id, 0, DTTR_SIZEOF_DDDEVICEIDENTIFIER2);
303 }
304
305 return S_OK;
306}
307
309 ddraw7_startmodetest,
311 void *modes,
312 DWORD n,
313 DWORD f
314)
315
317 ddraw7_evaluatemode,
319 DWORD f,
320 DWORD *timeout
321)
322
324 .QueryInterface = ddraw7_queryinterface,
325 .AddRef = ddraw7_addref,
326 .Release = ddraw7_release,
327 .Compact = ddraw7_compact,
328 .CreateClipper = ddraw7_createclipper,
329 .CreatePalette = ddraw7_createpalette,
330 .CreateSurface = ddraw7_createsurface,
331 .DuplicateSurface = ddraw7_duplicatesurface,
332 .EnumDisplayModes = ddraw7_enumdisplaymodes,
333 .EnumSurfaces = ddraw7_enumsurfaces,
334 .FlipToGDISurface = ddraw7_fliptogdisurface,
335 .GetCaps = ddraw7_getcaps,
336 .GetDisplayMode = ddraw7_getdisplaymode,
337 .GetFourCCCodes = ddraw7_getfourcccodes,
338 .GetGDISurface = ddraw7_getgdisurface,
339 .GetMonitorFrequency = ddraw7_getmonitorfrequency,
340 .GetScanLine = ddraw7_getscanline,
341 .GetVerticalBlankStatus = ddraw7_getverticalblankstatus,
342 .Initialize = ddraw7_initialize,
343 .RestoreDisplayMode = ddraw7_restoredisplaymode,
344 .SetCooperativeLevel = ddraw7_setcooperativelevel,
345 .SetDisplayMode = ddraw7_setdisplaymode,
346 .WaitForVerticalBlank = ddraw7_waitforverticalblank,
347 .GetAvailableVidMem = ddraw7_getavailablevidmem,
348 .GetSurfaceFromDC = ddraw7_getsurfacefromdc,
349 .RestoreAllSurfaces = ddraw7_restoreallsurfaces,
350 .TestCooperativeLevel = ddraw7_testcooperativelevel,
351 .GetDeviceIdentifier = ddraw7_getdeviceidentifier,
352 .StartModeTest = ddraw7_startmodetest,
353 .EvaluateMode = ddraw7_evaluatemode,
354};
355
358 if (dd7) {
359 dd7->vtbl = &vtbl;
361 "Created DDraw7 translator at %p, vtbl=%p, QueryInterface=%p",
362 dd7,
363 dd7->vtbl,
364 dd7->vtbl->QueryInterface
365 );
366 }
367
368 return dd7;
369}
static DTTR_Graphics_COM_Direct3D7_VT vtbl
DTTR_Graphics_COM_Direct3D7 * dttr_graphics_com_create_direct3d7()
void * cb
DTTR_Graphics_COM_Direct3DDevice7 * self
return S_OK
void void * ctx
const uint8_t * src
DTTR_Graphics_COM_Direct3DDevice7 void DWORD flags DWORD void DWORD flags
DTTR_Graphics_COM_Direct3DDevice7 void *status DTTR_Graphics_COM_Direct3DDevice7 DWORD DWORD void DWORD n
DTTR_Graphics_COM_DirectDraw7 *self DWORD DWORD DWORD bpp
DTTR_Graphics_COM_DirectDraw7 DWORD void void void *cb DTTR_Graphics_COM_DirectDraw7 void * drvCaps
void * caps
static HRESULT ddraw7_createclipper(DTTR_Graphics_COM_DirectDraw7 *self, DWORD f, void **clip, void *outer)
void DWORD * tot
static HRESULT ddraw7_createpalette(DTTR_Graphics_COM_DirectDraw7 *self, DWORD f, void *colors, void **pal, void *outer)
DTTR_Graphics_COM_DirectDraw7 *self DWORD DWORD h
dttr_graphics_set_logical_resolution((int) w,(int) h)
DTTR_Graphics_COM_DirectDraw7 DWORD void void void *cb DTTR_Graphics_COM_DirectDraw7 void void *helCaps DWORD * num
DTTR_Graphics_COM_DirectDraw7 DWORD f
DTTR_Graphics_COM_DirectDraw7 *self DWORD DWORD DWORD DWORD hz
DTTR_Graphics_COM_DirectDraw7 * dttr_graphics_com_create_directdraw7()
DTTR_Graphics_COM_DirectDraw7 DWORD void void void *cb DTTR_Graphics_COM_DirectDraw7 void void *helCaps DWORD DWORD * codes
void DWORD DWORD * free
static DTTR_Graphics_COM_Direct3D7 * ddraw7_get_direct3d()
static DTTR_Graphics_COM_Direct3D7 * ddraw7_d3d7
DTTR_Graphics_COM_DirectDraw7 *self DWORD w
static HRESULT ddraw7_createsurface(DTTR_Graphics_COM_DirectDraw7 *self, void *desc, void **surf, void *outer)
DTTR_Graphics_COM_DirectDraw7 DWORD void * desc
static HRESULT ddraw7_queryinterface(DTTR_Graphics_COM_DirectDraw7 *self, void *riid, void **ppv)
DTTR_Graphics_COM_DirectDrawSurface7 * dttr_graphics_com_create_directdrawsurface7(uint32_t width, uint32_t height, uint32_t bpp, uint32_t r_mask, uint32_t g_mask, uint32_t b_mask, uint32_t a_mask)
DTTR_Graphics_COM_DirectDrawSurface7 DWORD flags void NULL
HRESULT dttr_graphics_com_validate_directdrawsurface7(uint32_t width, uint32_t height, uint32_t bpp, uint32_t *out_pitch, size_t *out_pixel_size)
#define DTTR_LOG_DEBUG(...)
Definition dttr_log.h:28
#define DTTR_COM_STUB_SET(fn, out_type, val,...)
#define DTTR_COM_NOOP_HRESULT(fn,...)
#define DTTR_COM_ADDREF(fn, type)
#define DTTR_COM_RELEASE(fn, type)
#define DTTR_SIZEOF_DDDEVICEIDENTIFIER2
HRESULT(* QueryInterface)(DTTR_Graphics_COM_DirectDraw7 *self, void *riid, void **ppv)
DTTR_Graphics_COM_DirectDraw7_VT * vtbl