jscript.idl: Added IActiveScriptDebug declaration.
[wine/multimedia.git] / dlls / ddraw / material.c
blob6f162f7c828a6cf829c23d0c59b33541444aae18
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 <assert.h>
26 #include <stdarg.h>
27 #include <string.h>
28 #include <stdlib.h>
30 #define COBJMACROS
31 #define NONAMELESSUNION
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winerror.h"
36 #include "wingdi.h"
37 #include "wine/exception.h"
39 #include "ddraw.h"
40 #include "d3d.h"
42 #include "ddraw_private.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(d3d7);
46 WINE_DECLARE_DEBUG_CHANNEL(ddraw_thunk);
48 static void dump_material(const D3DMATERIAL *mat)
50 TRACE(" dwSize : %d\n", mat->dwSize);
53 static inline IDirect3DMaterialImpl *material_from_material1(IDirect3DMaterial *iface)
55 return (IDirect3DMaterialImpl *)((char*)iface - FIELD_OFFSET(IDirect3DMaterialImpl, IDirect3DMaterial_vtbl));
58 static inline IDirect3DMaterialImpl *material_from_material2(IDirect3DMaterial2 *iface)
60 return (IDirect3DMaterialImpl *)((char*)iface - FIELD_OFFSET(IDirect3DMaterialImpl, IDirect3DMaterial2_vtbl));
63 /*****************************************************************************
64 * IUnknown Methods.
65 *****************************************************************************/
67 /*****************************************************************************
68 * IDirect3DMaterial3::QueryInterface
70 * QueryInterface for IDirect3DMaterial. Can query all IDirect3DMaterial
71 * versions.
73 * Params:
74 * riid: Interface id queried for
75 * obj: Address to pass the interface pointer back
77 * Returns:
78 * S_OK on success
79 * E_NOINTERFACE if the requested interface wasn't found
81 *****************************************************************************/
82 static HRESULT WINAPI
83 IDirect3DMaterialImpl_QueryInterface(IDirect3DMaterial3 *iface,
84 REFIID riid,
85 LPVOID* obp)
87 IDirect3DMaterialImpl *This = (IDirect3DMaterialImpl *)iface;
88 TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), obp);
90 *obp = NULL;
92 if ( IsEqualGUID( &IID_IUnknown, riid ) ) {
93 IUnknown_AddRef(iface);
94 *obp = iface;
95 TRACE(" Creating IUnknown interface at %p.\n", *obp);
96 return S_OK;
98 if ( IsEqualGUID( &IID_IDirect3DMaterial, riid ) ) {
99 IDirect3DMaterial_AddRef((IDirect3DMaterial *)&This->IDirect3DMaterial_vtbl);
100 *obp = &This->IDirect3DMaterial_vtbl;
101 TRACE(" Creating IDirect3DMaterial interface %p\n", *obp);
102 return S_OK;
104 if ( IsEqualGUID( &IID_IDirect3DMaterial2, riid ) ) {
105 IDirect3DMaterial_AddRef((IDirect3DMaterial2 *)&This->IDirect3DMaterial2_vtbl);
106 *obp = &This->IDirect3DMaterial2_vtbl;
107 TRACE(" Creating IDirect3DMaterial2 interface %p\n", *obp);
108 return S_OK;
110 if ( IsEqualGUID( &IID_IDirect3DMaterial3, riid ) ) {
111 IDirect3DMaterial3_AddRef((IDirect3DMaterial3 *)This);
112 *obp = This;
113 TRACE(" Creating IDirect3DMaterial3 interface %p\n", *obp);
114 return S_OK;
116 FIXME("(%p): interface for IID %s NOT found!\n", This, debugstr_guid(riid));
117 return E_NOINTERFACE;
120 /*****************************************************************************
121 * IDirect3DMaterial3::AddRef
123 * Increases the refcount.
125 * Returns:
126 * The new refcount
128 *****************************************************************************/
129 static ULONG WINAPI
130 IDirect3DMaterialImpl_AddRef(IDirect3DMaterial3 *iface)
132 IDirect3DMaterialImpl *This = (IDirect3DMaterialImpl *)iface;
133 ULONG ref = InterlockedIncrement(&This->ref);
135 TRACE("(%p)->() incrementing from %u.\n", This, ref - 1);
137 return ref;
140 /*****************************************************************************
141 * IDirect3DMaterial3::Release
143 * Reduces the refcount by one. If the refcount falls to 0, the object
144 * is destroyed
146 * Returns:
147 * The new refcount
149 *****************************************************************************/
150 static ULONG WINAPI
151 IDirect3DMaterialImpl_Release(IDirect3DMaterial3 *iface)
153 IDirect3DMaterialImpl *This = (IDirect3DMaterialImpl *)iface;
154 ULONG ref = InterlockedDecrement(&This->ref);
156 TRACE("(%p)->() decrementing from %u.\n", This, ref + 1);
158 if (!ref)
160 if(This->Handle)
162 EnterCriticalSection(&ddraw_cs);
163 This->ddraw->d3ddevice->Handles[This->Handle - 1].ptr = NULL;
164 This->ddraw->d3ddevice->Handles[This->Handle - 1].type = DDrawHandle_Unknown;
165 LeaveCriticalSection(&ddraw_cs);
168 HeapFree(GetProcessHeap(), 0, This);
169 return 0;
171 return ref;
174 /*****************************************************************************
175 * IDirect3DMaterial Methods
176 *****************************************************************************/
178 /*****************************************************************************
179 * IDirect3DMaterial::Initialize
181 * A no-op initialization
183 * Params:
184 * Direct3D: Pointer to a Direct3D interface
186 * Returns:
187 * D3D_OK
189 *****************************************************************************/
190 static HRESULT WINAPI
191 IDirect3DMaterialImpl_Initialize(IDirect3DMaterial *iface,
192 IDirect3D *Direct3D)
194 IDirect3DMaterialImpl *This = material_from_material1(iface);
196 TRACE("(%p)->(%p) no-op...!\n", This, Direct3D);
198 return D3D_OK;
201 /*****************************************************************************
202 * IDirect3DMaterial::Reserve
204 * DirectX 5 sdk: "The IDirect3DMaterial2::Reserve method is not implemented"
205 * Odd. They seem to have mixed their interfaces.
207 * Returns:
208 * DDERR_UNSUPPORTED
210 *****************************************************************************/
211 static HRESULT WINAPI
212 IDirect3DMaterialImpl_Reserve(IDirect3DMaterial *iface)
214 IDirect3DMaterialImpl *This = material_from_material1(iface);
215 TRACE("(%p)->() not implemented\n", This);
217 return DDERR_UNSUPPORTED;
220 /*****************************************************************************
221 * IDirect3DMaterial::Unreserve
223 * Not supported too
225 * Returns:
226 * DDERR_UNSUPPORTED
228 *****************************************************************************/
229 static HRESULT WINAPI
230 IDirect3DMaterialImpl_Unreserve(IDirect3DMaterial *iface)
232 IDirect3DMaterialImpl *This = material_from_material1(iface);
233 TRACE("(%p)->() not implemented.\n", This);
235 return DDERR_UNSUPPORTED;
238 /*****************************************************************************
239 * IDirect3DMaterial3::SetMaterial
241 * Sets the material description
243 * Params:
244 * Mat: Material to set
246 * Returns:
247 * D3D_OK on success
248 * DDERR_INVALIDPARAMS if Mat is NULL
250 *****************************************************************************/
251 static HRESULT WINAPI
252 IDirect3DMaterialImpl_SetMaterial(IDirect3DMaterial3 *iface,
253 D3DMATERIAL *lpMat)
255 IDirect3DMaterialImpl *This = (IDirect3DMaterialImpl *)iface;
256 TRACE("(%p)->(%p)\n", This, lpMat);
257 if (TRACE_ON(d3d7))
258 dump_material(lpMat);
260 /* Stores the material */
261 EnterCriticalSection(&ddraw_cs);
262 memset(&This->mat, 0, sizeof(This->mat));
263 memcpy(&This->mat, lpMat, lpMat->dwSize);
264 LeaveCriticalSection(&ddraw_cs);
266 return DD_OK;
269 /*****************************************************************************
270 * IDirect3DMaterial3::GetMaterial
272 * Returns the material assigned to this interface
274 * Params:
275 * Mat: Pointer to a D3DMATERIAL structure to store the material description
277 * Returns:
278 * D3D_OK on success
279 * DDERR_INVALIDPARAMS if Mat is NULL
281 *****************************************************************************/
282 static HRESULT WINAPI
283 IDirect3DMaterialImpl_GetMaterial(IDirect3DMaterial3 *iface,
284 D3DMATERIAL *lpMat)
286 IDirect3DMaterialImpl *This = (IDirect3DMaterialImpl *)iface;
287 DWORD dwSize;
288 TRACE("(%p)->(%p)\n", This, lpMat);
289 if (TRACE_ON(d3d7)) {
290 TRACE(" Returning material : ");
291 dump_material(&This->mat);
294 /* Copies the material structure */
295 EnterCriticalSection(&ddraw_cs);
296 dwSize = lpMat->dwSize;
297 memset(lpMat, 0, dwSize);
298 memcpy(lpMat, &This->mat, dwSize);
299 LeaveCriticalSection(&ddraw_cs);
301 return DD_OK;
304 /*****************************************************************************
305 * IDirect3DMaterial3::GetHandle
307 * Returns a handle for the material interface. The handle is simply a
308 * pointer to the material implementation
310 * Params:
311 * Direct3DDevice3: The device this handle is assigned to
312 * Handle: Address to write the handle to
314 * Returns:
315 * D3D_OK on success
316 * DDERR_INVALIDPARAMS if Handle is NULL
318 *****************************************************************************/
319 static HRESULT WINAPI
320 IDirect3DMaterialImpl_GetHandle(IDirect3DMaterial3 *iface,
321 IDirect3DDevice3 *lpDirect3DDevice3,
322 D3DMATERIALHANDLE *lpHandle)
324 IDirect3DMaterialImpl *This = (IDirect3DMaterialImpl *)iface;
325 IDirect3DDeviceImpl *device = device_from_device3(lpDirect3DDevice3);
326 TRACE("(%p/%p)->(%p,%p)\n", This, iface, device, lpHandle);
328 EnterCriticalSection(&ddraw_cs);
329 This->active_device = device;
330 if(!This->Handle)
332 This->Handle = IDirect3DDeviceImpl_CreateHandle(device);
333 if(!This->Handle)
335 ERR("Error creating a handle\n");
336 LeaveCriticalSection(&ddraw_cs);
337 return DDERR_INVALIDPARAMS; /* Unchecked */
339 device->Handles[This->Handle - 1].ptr = This;
340 device->Handles[This->Handle - 1].type = DDrawHandle_Material;
342 *lpHandle = This->Handle;
343 TRACE(" returning handle %08x.\n", *lpHandle);
344 LeaveCriticalSection(&ddraw_cs);
346 return D3D_OK;
349 static HRESULT WINAPI
350 Thunk_IDirect3DMaterialImpl_2_GetHandle(LPDIRECT3DMATERIAL2 iface,
351 LPDIRECT3DDEVICE2 lpDirect3DDevice2,
352 LPD3DMATERIALHANDLE lpHandle)
354 TRACE_(ddraw_thunk)("(%p)->(%p,%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpDirect3DDevice2, lpHandle);
355 return IDirect3DMaterial3_GetHandle((IDirect3DMaterial3 *)material_from_material2(iface), lpDirect3DDevice2 ?
356 (IDirect3DDevice3 *)&device_from_device2(lpDirect3DDevice2)->IDirect3DDevice3_vtbl : NULL, lpHandle);
359 static HRESULT WINAPI
360 Thunk_IDirect3DMaterialImpl_1_GetHandle(LPDIRECT3DMATERIAL iface,
361 LPDIRECT3DDEVICE lpDirect3DDevice,
362 LPD3DMATERIALHANDLE lpHandle)
364 TRACE_(ddraw_thunk)("(%p)->(%p,%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpDirect3DDevice, lpHandle);
365 return IDirect3DMaterial3_GetHandle((IDirect3DMaterial3 *)material_from_material1(iface), lpDirect3DDevice ?
366 (IDirect3DDevice3 *)&device_from_device1(lpDirect3DDevice)->IDirect3DDevice3_vtbl : NULL, lpHandle);
369 static HRESULT WINAPI
370 Thunk_IDirect3DMaterialImpl_2_QueryInterface(LPDIRECT3DMATERIAL2 iface,
371 REFIID riid,
372 LPVOID* obp)
374 TRACE_(ddraw_thunk)("(%p)->(%s,%p) thunking to IDirect3DMaterial3 interface.\n", iface, debugstr_guid(riid), obp);
375 return IDirect3DMaterial3_QueryInterface((IDirect3DMaterial3 *)material_from_material2(iface), riid, obp);
378 static HRESULT WINAPI
379 Thunk_IDirect3DMaterialImpl_1_QueryInterface(LPDIRECT3DMATERIAL iface,
380 REFIID riid,
381 LPVOID* obp)
383 TRACE_(ddraw_thunk)("(%p)->(%s,%p) thunking to IDirect3DMaterial3 interface.\n", iface, debugstr_guid(riid), obp);
384 return IDirect3DMaterial3_QueryInterface((IDirect3DMaterial3 *)material_from_material1(iface), riid, obp);
387 static ULONG WINAPI
388 Thunk_IDirect3DMaterialImpl_2_AddRef(LPDIRECT3DMATERIAL2 iface)
390 TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DMaterial3 interface.\n", iface);
391 return IDirect3DMaterial3_AddRef((IDirect3DMaterial3 *)material_from_material2(iface));
394 static ULONG WINAPI
395 Thunk_IDirect3DMaterialImpl_1_AddRef(LPDIRECT3DMATERIAL iface)
397 TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DMaterial3 interface.\n", iface);
398 return IDirect3DMaterial3_AddRef((IDirect3DMaterial3 *)material_from_material1(iface));
401 static ULONG WINAPI
402 Thunk_IDirect3DMaterialImpl_2_Release(LPDIRECT3DMATERIAL2 iface)
404 TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DMaterial3 interface.\n", iface);
405 return IDirect3DMaterial3_Release((IDirect3DMaterial3 *)material_from_material2(iface));
408 static ULONG WINAPI
409 Thunk_IDirect3DMaterialImpl_1_Release(LPDIRECT3DMATERIAL iface)
411 TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DMaterial3 interface.\n", iface);
412 return IDirect3DMaterial3_Release((IDirect3DMaterial3 *)material_from_material1(iface));
415 static HRESULT WINAPI
416 Thunk_IDirect3DMaterialImpl_2_SetMaterial(LPDIRECT3DMATERIAL2 iface,
417 LPD3DMATERIAL lpMat)
419 TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpMat);
420 return IDirect3DMaterial3_SetMaterial((IDirect3DMaterial3 *)material_from_material2(iface), lpMat);
423 static HRESULT WINAPI
424 Thunk_IDirect3DMaterialImpl_1_SetMaterial(LPDIRECT3DMATERIAL iface,
425 LPD3DMATERIAL lpMat)
427 TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpMat);
428 return IDirect3DMaterial3_SetMaterial((IDirect3DMaterial3 *)material_from_material1(iface), lpMat);
431 static HRESULT WINAPI
432 Thunk_IDirect3DMaterialImpl_2_GetMaterial(LPDIRECT3DMATERIAL2 iface,
433 LPD3DMATERIAL lpMat)
435 TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpMat);
436 return IDirect3DMaterial3_GetMaterial((IDirect3DMaterial3 *)material_from_material2(iface), lpMat);
439 static HRESULT WINAPI
440 Thunk_IDirect3DMaterialImpl_1_GetMaterial(LPDIRECT3DMATERIAL iface,
441 LPD3DMATERIAL lpMat)
443 TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpMat);
444 return IDirect3DMaterial3_GetMaterial((IDirect3DMaterial3 *)material_from_material1(iface), lpMat);
448 /*****************************************************************************
449 * material_activate
451 * Uses IDirect3DDevice7::SetMaterial to activate the material
453 * Params:
454 * This: Pointer to the material implementation to activate
456 *****************************************************************************/
457 void material_activate(IDirect3DMaterialImpl* This)
459 D3DMATERIAL7 d3d7mat;
461 TRACE("Activating material %p\n", This);
462 d3d7mat.u.diffuse = This->mat.u.diffuse;
463 d3d7mat.u1.ambient = This->mat.u1.ambient;
464 d3d7mat.u2.specular = This->mat.u2.specular;
465 d3d7mat.u3.emissive = This->mat.u3.emissive;
466 d3d7mat.u4.power = This->mat.u4.power;
468 IDirect3DDevice7_SetMaterial((IDirect3DDevice7 *)This->active_device, &d3d7mat);
471 const IDirect3DMaterial3Vtbl IDirect3DMaterial3_Vtbl =
473 /*** IUnknown Methods ***/
474 IDirect3DMaterialImpl_QueryInterface,
475 IDirect3DMaterialImpl_AddRef,
476 IDirect3DMaterialImpl_Release,
477 /*** IDirect3DMaterial3 Methods ***/
478 IDirect3DMaterialImpl_SetMaterial,
479 IDirect3DMaterialImpl_GetMaterial,
480 IDirect3DMaterialImpl_GetHandle,
483 const IDirect3DMaterial2Vtbl IDirect3DMaterial2_Vtbl =
485 /*** IUnknown Methods ***/
486 Thunk_IDirect3DMaterialImpl_2_QueryInterface,
487 Thunk_IDirect3DMaterialImpl_2_AddRef,
488 Thunk_IDirect3DMaterialImpl_2_Release,
489 /*** IDirect3DMaterial2 Methods ***/
490 Thunk_IDirect3DMaterialImpl_2_SetMaterial,
491 Thunk_IDirect3DMaterialImpl_2_GetMaterial,
492 Thunk_IDirect3DMaterialImpl_2_GetHandle,
495 const IDirect3DMaterialVtbl IDirect3DMaterial_Vtbl =
497 /*** IUnknown Methods ***/
498 Thunk_IDirect3DMaterialImpl_1_QueryInterface,
499 Thunk_IDirect3DMaterialImpl_1_AddRef,
500 Thunk_IDirect3DMaterialImpl_1_Release,
501 /*** IDirect3DMaterial1 Methods ***/
502 IDirect3DMaterialImpl_Initialize,
503 Thunk_IDirect3DMaterialImpl_1_SetMaterial,
504 Thunk_IDirect3DMaterialImpl_1_GetMaterial,
505 Thunk_IDirect3DMaterialImpl_1_GetHandle,
506 IDirect3DMaterialImpl_Reserve,
507 IDirect3DMaterialImpl_Unreserve