Isle
Loading...
Searching...
No Matches
mxdirect3d.cpp
Go to the documentation of this file.
1#include "mxdirect3d.h"
2
3#include <assert.h>
4
6
7#if !defined(MXDIRECTX_FOR_CONFIG)
8#define RELEASE(x) \
9 if (x != NULL) { \
10 x->Release(); \
11 x = NULL; \
12 }
13
14// FUNCTION: LEGO1 0x1009b0a0
15// FUNCTION: BETA10 0x1011b220
17{
18 m_pDirect3d = NULL;
19 m_pDirect3dDevice = NULL;
20 m_bTexturesDisabled = FALSE;
21 m_currentDeviceInfo = NULL;
22}
23
24// FUNCTION: LEGO1 0x1009b140
25// FUNCTION: BETA10 0x1011b2c3
27{
28 Destroy();
29}
30
31// FUNCTION: LEGO1 0x1009b1a0
32// FUNCTION: BETA10 0x1011b333
34 HWND hWnd,
35 BOOL fullscreen_1,
36 BOOL surface_fullscreen,
37 BOOL onlySystemMemory,
38 int width,
39 int height,
40 int bpp,
41 const PALETTEENTRY* pPaletteEntries,
42 int paletteEntryCount
43)
44{
45 BOOL success = FALSE;
46 assert(m_currentDeviceInfo);
47
49 hWnd,
50 fullscreen_1,
51 surface_fullscreen,
52 onlySystemMemory,
53 width,
54 height,
55 bpp,
56 pPaletteEntries,
57 paletteEntryCount
58 )) {
59 goto done;
60 }
61
62 if (!D3DCreate()) {
63 goto done;
64 }
65
66 if (!D3DSetMode()) {
67 goto done;
68 }
69
70 success = TRUE;
71
72done:
73 if (!success) {
75 }
76
77 return success;
78}
79
80// FUNCTION: LEGO1 0x1009b210
81// FUNCTION: BETA10 0x1011b41d
83{
84 RELEASE(m_pDirect3dDevice);
85 RELEASE(m_pDirect3d);
86
87 if (m_currentDeviceInfo) {
88 delete m_currentDeviceInfo;
89 m_currentDeviceInfo = NULL;
90 }
91
92 if (m_currentDevInfo) {
94 }
95
97}
98
99// FUNCTION: LEGO1 0x1009b290
100// FUNCTION: BETA10 0x1011b50a
102{
103 RELEASE(m_pDirect3dDevice);
104 RELEASE(m_pDirect3d);
106}
107
108// FUNCTION: LEGO1 0x1009b2d0
109// FUNCTION: BETA10 0x1011b592
111{
112 HRESULT result;
113
114 result = DirectDraw()->QueryInterface(IID_IDirect3D2, (LPVOID*) &m_pDirect3d);
115 if (result != DD_OK) {
116 Error("Creation of IDirect3D failed", result);
117 return FALSE;
118 }
119 return TRUE;
120}
121
122// FUNCTION: LEGO1 0x1009b310
123// FUNCTION: BETA10 0x1011b617
125{
126 assert(m_currentDeviceInfo);
127
128 if (m_currentDeviceInfo->m_flags & MxAssignedDevice::c_hardwareMode) {
129 if (m_bOnlySoftRender) {
130 Error("Failed to place vital surfaces in video memory for hardware driver", DDERR_GENERIC);
131 return FALSE;
132 }
133
134 if (m_currentDeviceInfo->m_desc.dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_PERSPECTIVE) {
135 m_bTexturesDisabled = FALSE;
136 }
137 else {
138 m_bTexturesDisabled = TRUE;
139 }
140
141 if (!CreateZBuffer(DDSCAPS_VIDEOMEMORY, ZBufferDepth(m_currentDeviceInfo))) {
142 return FALSE;
143 }
144 }
145 else {
146 if (m_currentDeviceInfo->m_desc.dpcTriCaps.dwTextureCaps & D3DPTEXTURECAPS_PERSPECTIVE) {
147 m_bTexturesDisabled = FALSE;
148 }
149 else {
150 m_bTexturesDisabled = TRUE;
151 }
152
153 if (!CreateZBuffer(DDSCAPS_SYSTEMMEMORY, ZBufferDepth(m_currentDeviceInfo))) {
154 return FALSE;
155 }
156 }
157
159 HRESULT result = m_pDirect3d->CreateDevice(m_currentDeviceInfo->m_guid, backBuf, &m_pDirect3dDevice);
160
161 if (result != DD_OK) {
162 Error("Create D3D device failed", result);
163 return FALSE;
164 }
165
167
168 if (IsFullScreen()) {
169 if (!IsSupportedMode(mode.width, mode.height, mode.bitsPerPixel)) {
170 Error("This device cannot support the current display mode", DDERR_GENERIC);
171 return FALSE;
172 }
173 }
174
175 LPDIRECTDRAWSURFACE frontBuffer = FrontBuffer();
176 LPDIRECTDRAWSURFACE backBuffer = BackBuffer();
177
178 DDSURFACEDESC desc;
179 memset(&desc, 0, sizeof(desc));
180 desc.dwSize = sizeof(desc);
181
182 if (backBuffer->Lock(NULL, &desc, DDLOCK_WAIT, NULL) == DD_OK) {
183 unsigned char* surface = (unsigned char*) desc.lpSurface;
184
185 for (int i = 0; i < mode.height; i++) {
186 memset(surface, 0, mode.width * desc.ddpfPixelFormat.dwRGBBitCount / 8);
187 surface += desc.lPitch;
188 }
189
190 backBuffer->Unlock(desc.lpSurface);
191 }
192 else {
193 OutputDebugString("MxDirect3D::D3DSetMode() back lock failed\n");
194 }
195
196 if (IsFullScreen()) {
197 memset(&desc, 0, sizeof(desc));
198 desc.dwSize = sizeof(desc);
199
200 if (frontBuffer->Lock(NULL, &desc, DDLOCK_WAIT, NULL) == DD_OK) {
201 unsigned char* surface = (unsigned char*) desc.lpSurface;
202
203 for (int i = 0; i < mode.height; i++) {
204 memset(surface, 0, mode.width * desc.ddpfPixelFormat.dwRGBBitCount / 8);
205 surface += desc.lPitch;
206 }
207
208 frontBuffer->Unlock(desc.lpSurface);
209 }
210 else {
211 OutputDebugString("MxDirect3D::D3DSetMode() front lock failed\n");
212 }
213 }
214
215 return TRUE;
216}
217
218// FUNCTION: LEGO1 0x1009b5a0
219// FUNCTION: BETA10 0x1011babb
221{
222 int depth;
223 DWORD deviceDepth;
224
225 if (p_assignedDevice->m_desc.dwFlags & D3DDD_DEVICEZBUFFERBITDEPTH) {
226 deviceDepth = p_assignedDevice->m_desc.dwDeviceZBufferBitDepth;
227 }
228 else {
229 deviceDepth = 0;
230 }
231
232 if (deviceDepth & DDBD_32) {
233 depth = 32;
234 }
235 else if (deviceDepth & DDBD_24) {
236 depth = 24;
237 }
238 else if (deviceDepth & DDBD_16) {
239 depth = 16;
240 }
241 else if (deviceDepth & DDBD_8) {
242 depth = 8;
243 }
244 else {
245 depth = -1;
246 }
247
248 return depth;
249}
250
251// FUNCTION: LEGO1 0x1009b5f0
252// FUNCTION: BETA10 0x1011bbca
254{
255 if (m_currentDeviceInfo) {
256 delete m_currentDeviceInfo;
257 m_currentDeviceInfo = NULL;
259 }
260
262 assert(d);
263 int i = 0;
264
265 for (list<MxDriver>::iterator it = p_deviceEnumerate.m_list.begin(); it != p_deviceEnumerate.m_list.end();
266 it++, i++) {
267 MxDriver& driver = *it;
268
269 if (&driver == p_driver) {
270 d->m_deviceInfo = new DeviceModesInfo;
271
272 if (driver.m_guid) {
273 d->m_deviceInfo->m_guid = new GUID;
274 *d->m_deviceInfo->m_guid = *driver.m_guid;
275 }
276
277 d->m_deviceInfo->m_count = driver.m_displayModes.size();
278
279 if (d->m_deviceInfo->m_count > 0) {
280 int j = 0;
281 d->m_deviceInfo->m_modeArray = new DeviceModesInfo::Mode[d->m_deviceInfo->m_count];
282
283 for (list<MxDisplayMode>::iterator it2 = driver.m_displayModes.begin();
284 it2 != driver.m_displayModes.end();
285 it2++, j++) {
286 d->m_deviceInfo->m_modeArray[j].width = (*it2).m_width;
287 d->m_deviceInfo->m_modeArray[j].height = (*it2).m_height;
288 d->m_deviceInfo->m_modeArray[j].bitsPerPixel = (*it2).m_bitsPerPixel;
289 }
290 }
291
292 d->m_deviceInfo->m_ddcaps = driver.m_ddCaps;
293
294 if (i == 0) {
296 }
297
298 for (list<Direct3DDeviceInfo>::iterator it2 = driver.m_devices.begin(); it2 != driver.m_devices.end();
299 it2++) {
300 Direct3DDeviceInfo& device = *it2;
301 if (&device == p_device) {
302 memcpy(&d->m_guid, device.m_guid, sizeof(d->m_guid));
303
304 if (device.m_HWDesc.dcmColorModel) {
306 d->m_desc = device.m_HWDesc;
307 }
308 else {
309 d->m_desc = device.m_HELDesc;
310 }
311
312 m_currentDeviceInfo = d;
313 m_currentDevInfo = d->m_deviceInfo;
314 break;
315 }
316 }
317 }
318 }
319
320 if (!m_currentDeviceInfo) {
321 delete d;
322 return FALSE;
323 }
324 else {
325 return TRUE;
326 }
327}
328
329#endif
[AI] Holds assignment and description details for a Direct3D rendering device in use.
Definition: mxdirectxinfo.h:65
@ c_primaryDevice
[AI] Flags the device as the system's primary device.
Definition: mxdirectxinfo.h:69
@ c_hardwareMode
[AI] Indicates that the device is a hardware rendering device.
Definition: mxdirectxinfo.h:68
[AI] Enumerates DirectDraw/Direct3D drivers, devices, and display modes on the system.
list< MxDriver > m_list
[AI] List of all discovered DirectDraw drivers and their device/mode info.
[AI] MxDirect3D provides Direct3D (D3D) rendering capabilities layered on top of MxDirectDraw,...
Definition: mxdirect3d.h:24
~MxDirect3D() override
[AI] Destroys the MxDirect3D object, releasing associated Direct3D interfaces and memory for assigned...
Definition: mxdirect3d.cpp:26
BOOL SetDevice(MxDeviceEnumerate &p_deviceEnumerate, MxDriver *p_driver, Direct3DDeviceInfo *p_device)
[AI] Selects which Direct3D device to be used for rendering, given an enumeration of available device...
Definition: mxdirect3d.cpp:253
BOOL D3DCreate()
[AI] Internal D3D initialization helper: acquires the IDirect3D2 interface from DirectDraw.
Definition: mxdirect3d.cpp:110
void DestroyButNotDirectDraw() override
[AI] Like Destroy(), but preserves the DirectDraw context; only Direct3D/3DDevice and D3D-specific ob...
Definition: mxdirect3d.cpp:101
int ZBufferDepth(MxAssignedDevice *p_assignedDevice)
[AI] Determines the suitable ZBuffer depth for a selected rendering device, according to its capabili...
Definition: mxdirect3d.cpp:220
BOOL D3DSetMode()
[AI] Internal method to initialize D3D video mode, create devices, ZBuffer, and clear surfaces as nee...
Definition: mxdirect3d.cpp:124
MxDirect3D()
[AI] Constructs a new MxDirect3D object.
Definition: mxdirect3d.cpp:16
BOOL Create(HWND hWnd, BOOL fullscreen_1, BOOL surface_fullscreen, BOOL onlySystemMemory, int width, int height, int bpp, const PALETTEENTRY *pPaletteEntries, int paletteEntryCount) override
[AI] Initializes DirectDraw and Direct3D, prepares primary surfaces and selects the video mode,...
Definition: mxdirect3d.cpp:33
void Destroy() override
[AI] Tears down the Direct3D environment, including releasing D3D and D3DDevice objects,...
Definition: mxdirect3d.cpp:82
void FUN_1009d920()
Internal: Restores original palette and resets display mode and cooperative level.
BOOL IsSupportedMode(int width, int height, int bpp)
Checks if the given mode (resolution, bpp) is supported by the current device.
BOOL m_bOnlySoftRender
If TRUE, restricts rendering to software only; disables hardware acceleration. [AI].
Definition: mxdirectdraw.h:286
IDirectDrawSurface * BackBuffer()
Returns the back buffer surface.
Definition: mxdirectdraw.h:94
IDirectDraw * DirectDraw()
Returns the DirectDraw device interface pointer.
Definition: mxdirectdraw.h:82
DeviceModesInfo * m_currentDevInfo
Device info array for storing supported display modes. [AI].
Definition: mxdirectdraw.h:313
virtual void Destroy()
Shuts down DirectDraw and releases all resources, including device and surfaces.
BOOL CreateZBuffer(DWORD memorytype, DWORD depth)
Creates a z-buffer with the given memory type/capabilities and attaches it to the back buffer.
virtual void DestroyButNotDirectDraw()
Releases all DirectDraw-related resources except the DirectDraw device itself.
void Error(const char *p_message, int p_error)
Invokes the error handler, optionally destroying the device, and outputs the given error message/code...
BOOL IsFullScreen()
Returns TRUE if DirectDraw is currently set in exclusive fullscreen mode.
Definition: mxdirectdraw.h:111
DeviceModesInfo::Mode * CurrentMode()
Provides access to the current selected display mode (width, height, bpp).
Definition: mxdirectdraw.h:106
IDirectDrawSurface * FrontBuffer()
Returns the surface used as the primary/front buffer.
Definition: mxdirectdraw.h:88
virtual BOOL Create(HWND hWnd, BOOL fullscreen_1, BOOL surface_fullscreen, BOOL onlySystemMemory, int width, int height, int bpp, const PALETTEENTRY *pPaletteEntries, int paletteEntryCount)
Creates and initializes the DirectDraw device and associated surfaces/windows/state.
#define D3DDD_DEVICEZBUFFERBITDEPTH
Definition: d3dcaps.h:228
#define D3DPTEXTURECAPS_PERSPECTIVE
Definition: d3dcaps.h:152
LPSTR LPSTR LPVOID
Definition: d3dcaps.h:216
#define TRUE
Definition: d3drmdef.h:28
#define FALSE
Definition: d3drmdef.h:27
struct IDirectDrawSurface FAR * LPDIRECTDRAWSURFACE
Definition: ddraw.h:74
#define DDBD_8
Definition: ddraw.h:2192
typedef DWORD(FAR PASCAL *LPCLIPPERCALLBACK)(LPDIRECTDRAWCLIPPER lpDDClipper
HWND hWnd
Definition: ddraw.h:425
#define DD_OK
Definition: ddraw.h:3166
#define DDBD_24
Definition: ddraw.h:2202
long HRESULT
Definition: ddraw.h:115
#define DDERR_GENERIC
Definition: ddraw.h:3224
#define DDSCAPS_VIDEOMEMORY
Definition: ddraw.h:1437
#define DDLOCK_WAIT
Definition: ddraw.h:2916
#define DDSCAPS_SYSTEMMEMORY
Definition: ddraw.h:1419
#define DDBD_32
Definition: ddraw.h:2207
#define DDBD_16
Definition: ddraw.h:2197
#define DECOMP_SIZE_ASSERT(T, S)
Definition: decomp.h:19
typedef BOOL(FAR PASCAL *LPDIENUMDEVICEOBJECTSCALLBACKA)(LPCDIDEVICEOBJECTINSTANCEA
#define NULL
[AI] Null pointer value (C/C++ semantics).
Definition: legotypes.h:26
#define RELEASE(x)
Definition: mxdirect3d.cpp:8
[AI] Represents a specific display mode supported by the device (width, height, bits per pixel).
Definition: mxdirectxinfo.h:22
int bitsPerPixel
[AI] Color depth (bits per pixel)
Definition: mxdirectxinfo.h:35
int width
[AI] Horizontal resolution in pixels
Definition: mxdirectxinfo.h:33
int height
[AI] Vertical resolution in pixels
Definition: mxdirectxinfo.h:34
[AI] Contains detailed information about a Direct3D device's supported display modes and capabilities...
Definition: mxdirectxinfo.h:16
DDCAPS m_ddcaps
[AI] Capabilities of the DirectDraw device as filled by GetCaps.
Definition: mxdirectxinfo.h:51
Mode * m_modeArray
[AI] Array of supported display modes; dynamically allocated and owned.
Definition: mxdirectxinfo.h:49
int m_count
[AI] Number of display modes in m_modeArray.
Definition: mxdirectxinfo.h:50
GUID * m_guid
[AI] GUID of the video device (heap-allocated and owned by this struct).
Definition: mxdirectxinfo.h:48
[AI] Encapsulates Direct3D device enumeration information and capability structures.
D3DDEVICEDESC m_HWDesc
[AI] Hardware Direct3D device capability description.
D3DDEVICEDESC m_HELDesc
[AI] Software (HEL) emulation device capability description.
LPGUID m_guid
[AI] GUID uniquely identifying this 3D device. [AI]
[AI] Holds data about a DirectDraw driver including devices and supported display modes.
LPGUID m_guid
[AI] GUID for this DirectDraw driver (heap-allocated and owned).
list< MxDisplayMode > m_displayModes
[AI] List of all display modes reported by the driver.
DDCAPS m_ddCaps
[AI] Capabilities structure as reported by the driver.
list< Direct3DDeviceInfo > m_devices
[AI] List of all Direct3D devices provided by this driver.
D3DPRIMCAPS dpcTriCaps
Definition: d3dcaps.h:200
DWORD dwDeviceZBufferBitDepth
Definition: d3dcaps.h:202
DWORD dwFlags
Definition: d3dcaps.h:193
D3DCOLORMODEL dcmColorModel
Definition: d3dcaps.h:194
DWORD dwTextureCaps
Definition: d3dcaps.h:57
DWORD dwRGBBitCount
Definition: ddraw.h:348
LPVOID lpSurface
Definition: ddraw.h:1152
DDPIXELFORMAT ddpfPixelFormat
Definition: ddraw.h:1157
LONG lPitch
Definition: ddraw.h:1140
DWORD dwSize
Definition: ddraw.h:1134