* Forgot the struct name change in self-pointer
[reactos.git] / reactos / dll / directx / d3d9 / d3d9_helpers.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS ReactX
4 * FILE: dll/directx/d3d9/d3d9_helpers.c
5 * PURPOSE: d3d9.dll helper functions
6 * PROGRAMERS: Gregor Brunmar <gregor (dot) brunmar (at) home (dot) se>
7 */
8
9 #include "d3d9_helpers.h"
10 #include <stdio.h>
11 #include <ddraw.h>
12
13 #include "d3d9_private.h"
14
15 static LPCSTR D3dDebugRegPath = "Software\\Microsoft\\Direct3D";
16
17 LPDIRECT3D9_INT impl_from_IDirect3D9(LPDIRECT3D9 iface)
18 {
19 return (LPDIRECT3D9_INT)((ULONG_PTR)iface - FIELD_OFFSET(DIRECT3D9_INT, lpVtbl));
20 }
21
22 BOOL ReadRegistryValue(IN DWORD ValueType, IN LPCSTR ValueName, OUT LPBYTE DataBuffer, IN OUT LPDWORD DataBufferSize)
23 {
24 HKEY hKey;
25 DWORD Type;
26 LONG Ret;
27
28 if (ERROR_SUCCESS != RegOpenKeyEx(HKEY_LOCAL_MACHINE, D3dDebugRegPath, 0, KEY_QUERY_VALUE, &hKey))
29 return FALSE;
30
31 Ret = RegQueryValueEx(hKey, ValueName, 0, &Type, DataBuffer, DataBufferSize);
32
33 RegCloseKey(hKey);
34
35 if (ERROR_SUCCESS != Ret)
36 return FALSE;
37
38 if (Type != ValueType)
39 return FALSE;
40
41 return TRUE;
42 }
43
44 HRESULT FormatDebugString(IN OUT LPSTR Buffer, IN LONG BufferSize, IN LPCSTR FormatString, ... )
45 {
46 int BytesWritten;
47 va_list vargs;
48
49 if (BufferSize == 0)
50 return DDERR_INVALIDPARAMS;
51
52 va_start(vargs, FormatString);
53 BytesWritten = _vsnprintf(Buffer, BufferSize-1, FormatString, vargs);
54
55 if (BytesWritten < BufferSize)
56 return DDERR_GENERIC;
57
58 Buffer[BufferSize-1] = '\0';
59
60 return 0;
61 }
62
63 HRESULT CreateD3D9(OUT LPDIRECT3D9 *ppDirect3D9)
64 {
65 LPDIRECT3D9_INT pDirect3D9;
66
67 if (ppDirect3D9 == 0)
68 return DDERR_INVALIDPARAMS;
69
70 pDirect3D9 = HeapAlloc(GetProcessHeap(), 0, sizeof(DIRECT3D9_INT));
71
72 if (0 == pDirect3D9)
73 return DDERR_OUTOFMEMORY;
74
75 pDirect3D9->unknown000007 = 0;
76 pDirect3D9->lpInt = 0;
77
78 pDirect3D9->lpVtbl = &Direct3D9_Vtbl;
79 pDirect3D9->dwProcessId = GetCurrentThreadId();
80 pDirect3D9->dwRefCnt = 1;
81
82 pDirect3D9->unknown004576 = 0;
83 pDirect3D9->unknown004578 = 0;
84 pDirect3D9->unknown004579 = 0;
85 pDirect3D9->unknown004580 = 0;
86 pDirect3D9->unknown004581 = 0;
87 pDirect3D9->unknown004582 = 0;
88 pDirect3D9->unknown004583 = 0;
89 pDirect3D9->unknown004589 = 0;
90
91 pDirect3D9->lpInt = pDirect3D9;
92
93 *ppDirect3D9 = (LPDIRECT3D9)&pDirect3D9->lpVtbl;
94
95 return ERROR_SUCCESS;
96 }