mmsystem.dll16: Fix the pointer arithmetic and memory leak issues when unmapping.
[wine.git] / dlls / ddraw / material.c
blobe137bfcbed5e4cd5b52e8b72ffdec2ca28b0a5fe
1 /* Direct3D Material
2 * Copyright (c) 2002 Lionel ULMER
3 * Copyright (c) 2006 Stefan DÖSINGER
5 * This file contains the implementation of Direct3DMaterial.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
23 #include "wine/port.h"
25 #include "ddraw_private.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
29 static void dump_material(const D3DMATERIAL *mat)
31 TRACE(" dwSize : %d\n", mat->dwSize);
34 static inline IDirect3DMaterialImpl *material_from_material1(IDirect3DMaterial *iface)
36 return (IDirect3DMaterialImpl *)((char*)iface - FIELD_OFFSET(IDirect3DMaterialImpl, IDirect3DMaterial_vtbl));
39 static inline IDirect3DMaterialImpl *material_from_material2(IDirect3DMaterial2 *iface)
41 return (IDirect3DMaterialImpl *)((char*)iface - FIELD_OFFSET(IDirect3DMaterialImpl, IDirect3DMaterial2_vtbl));
44 /*****************************************************************************
45 * IUnknown Methods.
46 *****************************************************************************/
48 /*****************************************************************************
49 * IDirect3DMaterial3::QueryInterface
51 * QueryInterface for IDirect3DMaterial. Can query all IDirect3DMaterial
52 * versions.
54 * Params:
55 * riid: Interface id queried for
56 * obj: Address to pass the interface pointer back
58 * Returns:
59 * S_OK on success
60 * E_NOINTERFACE if the requested interface wasn't found
62 *****************************************************************************/
63 static HRESULT WINAPI
64 IDirect3DMaterialImpl_QueryInterface(IDirect3DMaterial3 *iface,
65 REFIID riid,
66 LPVOID* obp)
68 IDirect3DMaterialImpl *This = (IDirect3DMaterialImpl *)iface;
70 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), obp);
72 *obp = NULL;
74 if ( IsEqualGUID( &IID_IUnknown, riid ) ) {
75 IUnknown_AddRef(iface);
76 *obp = iface;
77 TRACE(" Creating IUnknown interface at %p.\n", *obp);
78 return S_OK;
80 if ( IsEqualGUID( &IID_IDirect3DMaterial, riid ) ) {
81 IDirect3DMaterial_AddRef((IDirect3DMaterial *)&This->IDirect3DMaterial_vtbl);
82 *obp = &This->IDirect3DMaterial_vtbl;
83 TRACE(" Creating IDirect3DMaterial interface %p\n", *obp);
84 return S_OK;
86 if ( IsEqualGUID( &IID_IDirect3DMaterial2, riid ) ) {
87 IDirect3DMaterial_AddRef((IDirect3DMaterial2 *)&This->IDirect3DMaterial2_vtbl);
88 *obp = &This->IDirect3DMaterial2_vtbl;
89 TRACE(" Creating IDirect3DMaterial2 interface %p\n", *obp);
90 return S_OK;
92 if ( IsEqualGUID( &IID_IDirect3DMaterial3, riid ) ) {
93 IDirect3DMaterial3_AddRef((IDirect3DMaterial3 *)This);
94 *obp = This;
95 TRACE(" Creating IDirect3DMaterial3 interface %p\n", *obp);
96 return S_OK;
98 FIXME("(%p): interface for IID %s NOT found!\n", This, debugstr_guid(riid));
99 return E_NOINTERFACE;
102 /*****************************************************************************
103 * IDirect3DMaterial3::AddRef
105 * Increases the refcount.
107 * Returns:
108 * The new refcount
110 *****************************************************************************/
111 static ULONG WINAPI
112 IDirect3DMaterialImpl_AddRef(IDirect3DMaterial3 *iface)
114 IDirect3DMaterialImpl *This = (IDirect3DMaterialImpl *)iface;
115 ULONG ref = InterlockedIncrement(&This->ref);
117 TRACE("%p increasing refcount to %u.\n", This, ref);
119 return ref;
122 /*****************************************************************************
123 * IDirect3DMaterial3::Release
125 * Reduces the refcount by one. If the refcount falls to 0, the object
126 * is destroyed
128 * Returns:
129 * The new refcount
131 *****************************************************************************/
132 static ULONG WINAPI
133 IDirect3DMaterialImpl_Release(IDirect3DMaterial3 *iface)
135 IDirect3DMaterialImpl *This = (IDirect3DMaterialImpl *)iface;
136 ULONG ref = InterlockedDecrement(&This->ref);
138 TRACE("%p decreasing refcount to %u.\n", This, ref);
140 if (!ref)
142 if(This->Handle)
144 EnterCriticalSection(&ddraw_cs);
145 ddraw_free_handle(&This->ddraw->d3ddevice->handle_table, This->Handle - 1, DDRAW_HANDLE_MATERIAL);
146 LeaveCriticalSection(&ddraw_cs);
149 HeapFree(GetProcessHeap(), 0, This);
150 return 0;
152 return ref;
155 /*****************************************************************************
156 * IDirect3DMaterial Methods
157 *****************************************************************************/
159 /*****************************************************************************
160 * IDirect3DMaterial::Initialize
162 * A no-op initialization
164 * Params:
165 * Direct3D: Pointer to a Direct3D interface
167 * Returns:
168 * D3D_OK
170 *****************************************************************************/
171 static HRESULT WINAPI
172 IDirect3DMaterialImpl_Initialize(IDirect3DMaterial *iface,
173 IDirect3D *Direct3D)
175 TRACE("iface %p, d3d %p.\n", iface, Direct3D);
177 return D3D_OK;
180 /*****************************************************************************
181 * IDirect3DMaterial::Reserve
183 * DirectX 5 sdk: "The IDirect3DMaterial2::Reserve method is not implemented"
184 * Odd. They seem to have mixed their interfaces.
186 * Returns:
187 * DDERR_UNSUPPORTED
189 *****************************************************************************/
190 static HRESULT WINAPI
191 IDirect3DMaterialImpl_Reserve(IDirect3DMaterial *iface)
193 TRACE("iface %p.\n", iface);
195 return DDERR_UNSUPPORTED;
198 /*****************************************************************************
199 * IDirect3DMaterial::Unreserve
201 * Not supported too
203 * Returns:
204 * DDERR_UNSUPPORTED
206 *****************************************************************************/
207 static HRESULT WINAPI
208 IDirect3DMaterialImpl_Unreserve(IDirect3DMaterial *iface)
210 TRACE("iface %p.\n", iface);
212 return DDERR_UNSUPPORTED;
215 /*****************************************************************************
216 * IDirect3DMaterial3::SetMaterial
218 * Sets the material description
220 * Params:
221 * Mat: Material to set
223 * Returns:
224 * D3D_OK on success
225 * DDERR_INVALIDPARAMS if Mat is NULL
227 *****************************************************************************/
228 static HRESULT WINAPI
229 IDirect3DMaterialImpl_SetMaterial(IDirect3DMaterial3 *iface,
230 D3DMATERIAL *lpMat)
232 IDirect3DMaterialImpl *This = (IDirect3DMaterialImpl *)iface;
234 TRACE("iface %p, material %p.\n", iface, lpMat);
235 if (TRACE_ON(ddraw))
236 dump_material(lpMat);
238 /* Stores the material */
239 EnterCriticalSection(&ddraw_cs);
240 memset(&This->mat, 0, sizeof(This->mat));
241 memcpy(&This->mat, lpMat, lpMat->dwSize);
242 LeaveCriticalSection(&ddraw_cs);
244 return DD_OK;
247 /*****************************************************************************
248 * IDirect3DMaterial3::GetMaterial
250 * Returns the material assigned to this interface
252 * Params:
253 * Mat: Pointer to a D3DMATERIAL structure to store the material description
255 * Returns:
256 * D3D_OK on success
257 * DDERR_INVALIDPARAMS if Mat is NULL
259 *****************************************************************************/
260 static HRESULT WINAPI
261 IDirect3DMaterialImpl_GetMaterial(IDirect3DMaterial3 *iface,
262 D3DMATERIAL *lpMat)
264 IDirect3DMaterialImpl *This = (IDirect3DMaterialImpl *)iface;
265 DWORD dwSize;
267 TRACE("iface %p, material %p.\n", iface, lpMat);
268 if (TRACE_ON(ddraw))
270 TRACE(" Returning material : ");
271 dump_material(&This->mat);
274 /* Copies the material structure */
275 EnterCriticalSection(&ddraw_cs);
276 dwSize = lpMat->dwSize;
277 memcpy(lpMat, &This->mat, dwSize);
278 LeaveCriticalSection(&ddraw_cs);
280 return DD_OK;
283 /*****************************************************************************
284 * IDirect3DMaterial3::GetHandle
286 * Returns a handle for the material interface. The handle is simply a
287 * pointer to the material implementation
289 * Params:
290 * Direct3DDevice3: The device this handle is assigned to
291 * Handle: Address to write the handle to
293 * Returns:
294 * D3D_OK on success
295 * DDERR_INVALIDPARAMS if Handle is NULL
297 *****************************************************************************/
298 static HRESULT WINAPI
299 IDirect3DMaterialImpl_GetHandle(IDirect3DMaterial3 *iface,
300 IDirect3DDevice3 *lpDirect3DDevice3,
301 D3DMATERIALHANDLE *lpHandle)
303 IDirect3DMaterialImpl *This = (IDirect3DMaterialImpl *)iface;
304 IDirect3DDeviceImpl *device = device_from_device3(lpDirect3DDevice3);
306 TRACE("iface %p, device %p, handle %p.\n", iface, lpDirect3DDevice3, lpHandle);
308 EnterCriticalSection(&ddraw_cs);
309 This->active_device = device;
310 if(!This->Handle)
312 DWORD h = ddraw_allocate_handle(&device->handle_table, This, DDRAW_HANDLE_MATERIAL);
313 if (h == DDRAW_INVALID_HANDLE)
315 ERR("Failed to allocate a material handle.\n");
316 LeaveCriticalSection(&ddraw_cs);
317 return DDERR_INVALIDPARAMS; /* Unchecked */
320 This->Handle = h + 1;
322 *lpHandle = This->Handle;
323 TRACE(" returning handle %08x.\n", *lpHandle);
324 LeaveCriticalSection(&ddraw_cs);
326 return D3D_OK;
329 static HRESULT WINAPI IDirect3DMaterialImpl_2_GetHandle(IDirect3DMaterial2 *iface,
330 IDirect3DDevice2 *lpDirect3DDevice2, D3DMATERIALHANDLE *lpHandle)
332 TRACE("iface %p, device %p, handle %p.\n", iface, lpDirect3DDevice2, lpHandle);
334 return IDirect3DMaterial3_GetHandle((IDirect3DMaterial3 *)material_from_material2(iface), lpDirect3DDevice2 ?
335 (IDirect3DDevice3 *)&device_from_device2(lpDirect3DDevice2)->IDirect3DDevice3_vtbl : NULL, lpHandle);
338 static HRESULT WINAPI IDirect3DMaterialImpl_1_GetHandle(IDirect3DMaterial *iface,
339 IDirect3DDevice *lpDirect3DDevice, D3DMATERIALHANDLE *lpHandle)
341 TRACE("iface %p, device %p, handle %p.\n", iface, lpDirect3DDevice, lpHandle);
343 return IDirect3DMaterial3_GetHandle((IDirect3DMaterial3 *)material_from_material1(iface), lpDirect3DDevice ?
344 (IDirect3DDevice3 *)&device_from_device1(lpDirect3DDevice)->IDirect3DDevice3_vtbl : NULL, lpHandle);
347 static HRESULT WINAPI IDirect3DMaterialImpl_2_QueryInterface(LPDIRECT3DMATERIAL2 iface, REFIID riid,
348 LPVOID* obp)
350 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), obp);
352 return IDirect3DMaterial3_QueryInterface((IDirect3DMaterial3 *)material_from_material2(iface), riid, obp);
355 static HRESULT WINAPI IDirect3DMaterialImpl_1_QueryInterface(LPDIRECT3DMATERIAL iface, REFIID riid,
356 LPVOID* obp)
358 TRACE("iface %p, riid %s, object %p.\n", iface, debugstr_guid(riid), obp);
360 return IDirect3DMaterial3_QueryInterface((IDirect3DMaterial3 *)material_from_material1(iface), riid, obp);
363 static ULONG WINAPI IDirect3DMaterialImpl_2_AddRef(LPDIRECT3DMATERIAL2 iface)
365 TRACE("iface %p.\n", iface);
367 return IDirect3DMaterial3_AddRef((IDirect3DMaterial3 *)material_from_material2(iface));
370 static ULONG WINAPI IDirect3DMaterialImpl_1_AddRef(LPDIRECT3DMATERIAL iface)
372 TRACE("iface %p.\n", iface);
374 return IDirect3DMaterial3_AddRef((IDirect3DMaterial3 *)material_from_material1(iface));
377 static ULONG WINAPI IDirect3DMaterialImpl_2_Release(LPDIRECT3DMATERIAL2 iface)
379 TRACE("iface %p.\n", iface);
381 return IDirect3DMaterial3_Release((IDirect3DMaterial3 *)material_from_material2(iface));
384 static ULONG WINAPI IDirect3DMaterialImpl_1_Release(LPDIRECT3DMATERIAL iface)
386 TRACE("iface %p.\n", iface);
388 return IDirect3DMaterial3_Release((IDirect3DMaterial3 *)material_from_material1(iface));
391 static HRESULT WINAPI IDirect3DMaterialImpl_2_SetMaterial(LPDIRECT3DMATERIAL2 iface,
392 LPD3DMATERIAL lpMat)
394 TRACE("iface %p, material %p.\n", iface, lpMat);
396 return IDirect3DMaterial3_SetMaterial((IDirect3DMaterial3 *)material_from_material2(iface), lpMat);
399 static HRESULT WINAPI IDirect3DMaterialImpl_1_SetMaterial(LPDIRECT3DMATERIAL iface,
400 LPD3DMATERIAL lpMat)
402 TRACE("iface %p, material %p.\n", iface, lpMat);
404 return IDirect3DMaterial3_SetMaterial((IDirect3DMaterial3 *)material_from_material1(iface), lpMat);
407 static HRESULT WINAPI IDirect3DMaterialImpl_2_GetMaterial(LPDIRECT3DMATERIAL2 iface,
408 LPD3DMATERIAL lpMat)
410 TRACE("iface %p, material %p.\n", iface, lpMat);
412 return IDirect3DMaterial3_GetMaterial((IDirect3DMaterial3 *)material_from_material2(iface), lpMat);
415 static HRESULT WINAPI IDirect3DMaterialImpl_1_GetMaterial(LPDIRECT3DMATERIAL iface,
416 LPD3DMATERIAL lpMat)
418 TRACE("iface %p, material %p.\n", iface, lpMat);
420 return IDirect3DMaterial3_GetMaterial((IDirect3DMaterial3 *)material_from_material1(iface), lpMat);
424 /*****************************************************************************
425 * material_activate
427 * Uses IDirect3DDevice7::SetMaterial to activate the material
429 * Params:
430 * This: Pointer to the material implementation to activate
432 *****************************************************************************/
433 void material_activate(IDirect3DMaterialImpl* This)
435 D3DMATERIAL7 d3d7mat;
437 TRACE("Activating material %p\n", This);
438 d3d7mat.u.diffuse = This->mat.u.diffuse;
439 d3d7mat.u1.ambient = This->mat.u1.ambient;
440 d3d7mat.u2.specular = This->mat.u2.specular;
441 d3d7mat.u3.emissive = This->mat.u3.emissive;
442 d3d7mat.u4.power = This->mat.u4.power;
444 IDirect3DDevice7_SetMaterial((IDirect3DDevice7 *)This->active_device, &d3d7mat);
447 static const struct IDirect3DMaterial3Vtbl d3d_material3_vtbl =
449 /*** IUnknown Methods ***/
450 IDirect3DMaterialImpl_QueryInterface,
451 IDirect3DMaterialImpl_AddRef,
452 IDirect3DMaterialImpl_Release,
453 /*** IDirect3DMaterial3 Methods ***/
454 IDirect3DMaterialImpl_SetMaterial,
455 IDirect3DMaterialImpl_GetMaterial,
456 IDirect3DMaterialImpl_GetHandle,
459 static const struct IDirect3DMaterial2Vtbl d3d_material2_vtbl =
461 /*** IUnknown Methods ***/
462 IDirect3DMaterialImpl_2_QueryInterface,
463 IDirect3DMaterialImpl_2_AddRef,
464 IDirect3DMaterialImpl_2_Release,
465 /*** IDirect3DMaterial2 Methods ***/
466 IDirect3DMaterialImpl_2_SetMaterial,
467 IDirect3DMaterialImpl_2_GetMaterial,
468 IDirect3DMaterialImpl_2_GetHandle,
471 static const struct IDirect3DMaterialVtbl d3d_material1_vtbl =
473 /*** IUnknown Methods ***/
474 IDirect3DMaterialImpl_1_QueryInterface,
475 IDirect3DMaterialImpl_1_AddRef,
476 IDirect3DMaterialImpl_1_Release,
477 /*** IDirect3DMaterial1 Methods ***/
478 IDirect3DMaterialImpl_Initialize,
479 IDirect3DMaterialImpl_1_SetMaterial,
480 IDirect3DMaterialImpl_1_GetMaterial,
481 IDirect3DMaterialImpl_1_GetHandle,
482 IDirect3DMaterialImpl_Reserve,
483 IDirect3DMaterialImpl_Unreserve
486 void d3d_material_init(IDirect3DMaterialImpl *material, IDirectDrawImpl *ddraw)
488 material->lpVtbl = &d3d_material3_vtbl;
489 material->IDirect3DMaterial2_vtbl = &d3d_material2_vtbl;
490 material->IDirect3DMaterial_vtbl = &d3d_material1_vtbl;
491 material->ref = 1;
492 material->ddraw = ddraw;