d0167a4e1b14f63d1ca4e26b14a62e171a7213fd
[reactos.git] / reactos / dll / directx / wine / d3dx9_36 / core.c
1 /*
2 *
3 * Copyright 2002 Raphael Junqueira
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
18 */
19
20 #include "d3dx9_36_private.h"
21
22 struct ID3DXBufferImpl
23 {
24 ID3DXBuffer ID3DXBuffer_iface;
25 LONG ref;
26
27 void *buffer;
28 DWORD size;
29 };
30
31 static inline struct ID3DXBufferImpl *impl_from_ID3DXBuffer(ID3DXBuffer *iface)
32 {
33 return CONTAINING_RECORD(iface, struct ID3DXBufferImpl, ID3DXBuffer_iface);
34 }
35
36 static HRESULT WINAPI ID3DXBufferImpl_QueryInterface(ID3DXBuffer *iface, REFIID riid, void **ppobj)
37 {
38 TRACE("iface %p, riid %s, object %p\n", iface, debugstr_guid(riid), ppobj);
39
40 if (IsEqualGUID(riid, &IID_IUnknown)
41 || IsEqualGUID(riid, &IID_ID3DXBuffer))
42 {
43 IUnknown_AddRef(iface);
44 *ppobj = iface;
45 return D3D_OK;
46 }
47
48 WARN("%s not implemented, returning E_NOINTERFACE\n", debugstr_guid(riid));
49
50 return E_NOINTERFACE;
51 }
52
53 static ULONG WINAPI ID3DXBufferImpl_AddRef(ID3DXBuffer *iface)
54 {
55 struct ID3DXBufferImpl *This = impl_from_ID3DXBuffer(iface);
56 ULONG ref = InterlockedIncrement(&This->ref);
57
58 TRACE("%p increasing refcount to %u\n", This, ref);
59
60 return ref;
61 }
62
63 static ULONG WINAPI ID3DXBufferImpl_Release(ID3DXBuffer *iface)
64 {
65 struct ID3DXBufferImpl *This = impl_from_ID3DXBuffer(iface);
66 ULONG ref = InterlockedDecrement(&This->ref);
67
68 TRACE("%p decreasing refcount to %u\n", This, ref);
69
70 if (ref == 0)
71 {
72 HeapFree(GetProcessHeap(), 0, This->buffer);
73 HeapFree(GetProcessHeap(), 0, This);
74 }
75
76 return ref;
77 }
78
79 static LPVOID WINAPI ID3DXBufferImpl_GetBufferPointer(ID3DXBuffer *iface)
80 {
81 struct ID3DXBufferImpl *This = impl_from_ID3DXBuffer(iface);
82
83 TRACE("iface %p\n", iface);
84
85 return This->buffer;
86 }
87
88 static DWORD WINAPI ID3DXBufferImpl_GetBufferSize(ID3DXBuffer *iface)
89 {
90 struct ID3DXBufferImpl *This = impl_from_ID3DXBuffer(iface);
91
92 TRACE("iface %p\n", iface);
93
94 return This->size;
95 }
96
97 static const struct ID3DXBufferVtbl ID3DXBufferImpl_Vtbl =
98 {
99 /* IUnknown methods */
100 ID3DXBufferImpl_QueryInterface,
101 ID3DXBufferImpl_AddRef,
102 ID3DXBufferImpl_Release,
103 /* ID3DXBuffer methods */
104 ID3DXBufferImpl_GetBufferPointer,
105 ID3DXBufferImpl_GetBufferSize
106 };
107
108 static HRESULT d3dx9_buffer_init(struct ID3DXBufferImpl *buffer, DWORD size)
109 {
110 buffer->ID3DXBuffer_iface.lpVtbl = &ID3DXBufferImpl_Vtbl;
111 buffer->ref = 1;
112 buffer->size = size;
113
114 buffer->buffer = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
115 if (!buffer->buffer)
116 {
117 ERR("Failed to allocate buffer memory\n");
118 return E_OUTOFMEMORY;
119 }
120
121 return D3D_OK;
122 }
123
124 HRESULT WINAPI D3DXCreateBuffer(DWORD size, ID3DXBuffer **buffer)
125 {
126 struct ID3DXBufferImpl *object;
127 HRESULT hr;
128
129 if (!buffer)
130 {
131 WARN("Invalid buffer specified.\n");
132 return D3DERR_INVALIDCALL;
133 }
134
135 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
136 if (!object)
137 return E_OUTOFMEMORY;
138
139 hr = d3dx9_buffer_init(object, size);
140 if (FAILED(hr))
141 {
142 WARN("Failed to initialize buffer, hr %#x.\n", hr);
143 HeapFree(GetProcessHeap(), 0, object);
144 return hr;
145 }
146
147 *buffer = &object->ID3DXBuffer_iface;
148
149 TRACE("Created ID3DBuffer %p\n", *buffer);
150
151 return D3D_OK;
152 }