Refactored d3d9 a bit
[reactos.git] / reactos / dll / directx / d3d9 / d3d9_create.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS ReactX
4 * FILE: dll/directx/d3d9/d3d9_create.c
5 * PURPOSE: d3d9.dll internal create functions and data
6 * PROGRAMERS: Gregor Brunmar <gregor (dot) brunmar (at) home (dot) se>
7 */
8
9 typedef struct IDirect3D9 *LPDIRECT3D9;
10
11 #include "d3d9_create.h"
12 #include "d3d9_helpers.h"
13 #include <debug.h>
14 #include <ddrawi.h>
15
16 static const GUID DISPLAY_GUID = { 0x67685559, 0x3106, 0x11D0, { 0xB9, 0x71, 0x00, 0xAA, 0x00, 0x34, 0x2F, 0x9F } };
17
18 static CHAR D3D9_PrimaryDeviceName[CCHDEVICENAME];
19
20 static BOOL GetDisplayDeviceInfo(IN OUT LPDIRECT3D9_INT pDirect3D9)
21 {
22 DISPLAY_DEVICEA DisplayDevice;
23 DWORD AdapterIndex;
24 DWORD Planes;
25 DWORD Bpp;
26 HDC hDC;
27
28 memset(&DisplayDevice, 0, sizeof(DISPLAY_DEVICEA));
29 DisplayDevice.cb = sizeof(DISPLAY_DEVICEA);
30
31 pDirect3D9->dwNumDisplayAdapters = 0;
32 D3D9_PrimaryDeviceName[0] = '\0';
33
34 AdapterIndex = 0;
35 while (EnumDisplayDevicesA(NULL, AdapterIndex, &DisplayDevice, 0) == TRUE)
36 {
37 if ((DisplayDevice.StateFlags & (DISPLAY_DEVICE_DISCONNECT | DISPLAY_DEVICE_MIRRORING_DRIVER)) == 0 &&
38 (DisplayDevice.StateFlags & (DISPLAY_DEVICE_PRIMARY_DEVICE | DISPLAY_DEVICE_ATTACHED_TO_DESKTOP)) != 0)
39 {
40 memcpy(&pDirect3D9->DisplayAdapters[0].DisplayGuid, &DISPLAY_GUID, sizeof(GUID));
41
42 lstrcpynA(pDirect3D9->DisplayAdapters[0].szDeviceName, DisplayDevice.DeviceName, MAX_PATH);
43
44 if (pDirect3D9->dwNumDisplayAdapters == 0)
45 lstrcpynA(D3D9_PrimaryDeviceName, DisplayDevice.DeviceName, sizeof(D3D9_PrimaryDeviceName));
46
47 pDirect3D9->DisplayAdapters[0].dwStateFlags = DisplayDevice.StateFlags;
48 pDirect3D9->DisplayAdapters[0].bInUseFlag = TRUE;
49
50 ++pDirect3D9->dwNumDisplayAdapters;
51 break;
52 }
53 }
54
55 AdapterIndex = 0;
56 while (EnumDisplayDevicesA(NULL, AdapterIndex, &DisplayDevice, 0) == TRUE)
57 {
58 if ((DisplayDevice.StateFlags & DISPLAY_DEVICE_ATTACHED_TO_DESKTOP) != 0 &&
59 (DisplayDevice.StateFlags & (DISPLAY_DEVICE_MIRRORING_DRIVER | DISPLAY_DEVICE_PRIMARY_DEVICE)) == 0)
60 {
61 memcpy(&pDirect3D9->DisplayAdapters[pDirect3D9->dwNumDisplayAdapters].DisplayGuid, &DISPLAY_GUID, sizeof(GUID));
62
63 lstrcpynA(pDirect3D9->DisplayAdapters[pDirect3D9->dwNumDisplayAdapters].szDeviceName, DisplayDevice.DeviceName, MAX_PATH);
64
65 pDirect3D9->DisplayAdapters[pDirect3D9->dwNumDisplayAdapters].dwStateFlags = DisplayDevice.StateFlags;
66 pDirect3D9->DisplayAdapters[pDirect3D9->dwNumDisplayAdapters].bInUseFlag = TRUE;
67
68 ++pDirect3D9->dwNumDisplayAdapters;
69 }
70 }
71
72 hDC = GetDC(NULL);
73 Planes = GetDeviceCaps(hDC, PLANES);
74 Bpp = GetDeviceCaps(hDC, BITSPIXEL);
75 ReleaseDC(NULL, hDC);
76
77 if (Planes * Bpp < 8)
78 return FALSE;
79
80 hDC = CreateDCA(NULL, D3D9_PrimaryDeviceName, NULL, NULL);
81
82 if (hDC == NULL)
83 return FALSE;
84
85 return TRUE;
86 }
87
88 HRESULT CreateD3D9(OUT LPDIRECT3D9 *ppDirect3D9)
89 {
90 LPDIRECT3D9_INT pDirect3D9;
91
92 if (ppDirect3D9 == 0)
93 return DDERR_INVALIDPARAMS;
94
95 if (AlignedAlloc((LPVOID *)&pDirect3D9, sizeof(DIRECT3D9_INT)) != S_OK)
96 return DDERR_OUTOFMEMORY;
97
98 if (pDirect3D9 == 0)
99 return DDERR_OUTOFMEMORY;
100
101 pDirect3D9->unknown000007 = 0;
102 pDirect3D9->lpInt = 0;
103
104 pDirect3D9->lpVtbl = &Direct3D9_Vtbl;
105 //pDirect3D9->dwProcessId = GetCurrentThreadId();
106 pDirect3D9->dwRefCnt = 1;
107
108 pDirect3D9->unknown004576 = 0;
109 pDirect3D9->unknown004578 = 0;
110 pDirect3D9->unknown004579 = 0;
111 pDirect3D9->unknown004580 = 0;
112 pDirect3D9->unknown004581 = 0;
113 pDirect3D9->unknown004582 = 0;
114 pDirect3D9->unknown004583 = 0;
115 pDirect3D9->unknown004589 = 0x20;
116
117 pDirect3D9->lpInt = pDirect3D9;
118 pDirect3D9->unknown000007 = 1;
119
120 //InitializeCriticalSection(&pDirect3D9->d3d9_cs);
121
122 //memset(pDirect3D9->DisplayAdapters, 0, sizeof(pDirect3D9->DisplayAdapters));
123 GetDisplayDeviceInfo(pDirect3D9);
124
125 *ppDirect3D9 = (LPDIRECT3D9)&pDirect3D9->lpVtbl;
126
127 return ERROR_SUCCESS;
128 }