ddraw: More fully implement and test DirectDrawEnumerateExA.
[wine] / dlls / ddraw / material.c
1 /* Direct3D Material
2  * Copyright (c) 2002 Lionel ULMER
3  * Copyright (c) 2006 Stefan DÖSINGER
4  *
5  * This file contains the implementation of Direct3DMaterial.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #include "config.h"
23 #include "wine/port.h"
24
25 #include <assert.h>
26 #include <stdarg.h>
27 #include <string.h>
28 #include <stdlib.h>
29
30 #define COBJMACROS
31 #define NONAMELESSUNION
32
33 #include "windef.h"
34 #include "winbase.h"
35 #include "winerror.h"
36 #include "wingdi.h"
37 #include "wine/exception.h"
38
39 #include "ddraw.h"
40 #include "d3d.h"
41
42 #include "ddraw_private.h"
43 #include "wine/debug.h"
44
45 WINE_DEFAULT_DEBUG_CHANNEL(d3d7);
46 WINE_DECLARE_DEBUG_CHANNEL(ddraw_thunk);
47
48 static void dump_material(const D3DMATERIAL *mat)
49 {
50     TRACE("  dwSize : %d\n", mat->dwSize);
51 }
52
53 static inline IDirect3DMaterialImpl *material_from_material1(IDirect3DMaterial *iface)
54 {
55     return (IDirect3DMaterialImpl *)((char*)iface - FIELD_OFFSET(IDirect3DMaterialImpl, IDirect3DMaterial_vtbl));
56 }
57
58 static inline IDirect3DMaterialImpl *material_from_material2(IDirect3DMaterial2 *iface)
59 {
60     return (IDirect3DMaterialImpl *)((char*)iface - FIELD_OFFSET(IDirect3DMaterialImpl, IDirect3DMaterial2_vtbl));
61 }
62
63 /*****************************************************************************
64  * IUnknown Methods.
65  *****************************************************************************/
66
67 /*****************************************************************************
68  * IDirect3DMaterial3::QueryInterface
69  *
70  * QueryInterface for IDirect3DMaterial. Can query all IDirect3DMaterial
71  * versions.
72  *
73  * Params:
74  *  riid: Interface id queried for
75  *  obj: Address to pass the interface pointer back
76  *
77  * Returns:
78  *  S_OK on success
79  *  E_NOINTERFACE if the requested interface wasn't found
80  *
81  *****************************************************************************/
82 static HRESULT WINAPI
83 IDirect3DMaterialImpl_QueryInterface(IDirect3DMaterial3 *iface,
84                                      REFIID riid,
85                                      LPVOID* obp)
86 {
87     IDirect3DMaterialImpl *This = (IDirect3DMaterialImpl *)iface;
88     TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), obp);
89
90     *obp = NULL;
91
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;
97     }
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;
103     }
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;
109     }
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;
115     }
116     FIXME("(%p): interface for IID %s NOT found!\n", This, debugstr_guid(riid));
117     return E_NOINTERFACE;
118 }
119
120 /*****************************************************************************
121  * IDirect3DMaterial3::AddRef
122  *
123  * Increases the refcount.
124  *
125  * Returns:
126  *  The new refcount
127  *
128  *****************************************************************************/
129 static ULONG WINAPI
130 IDirect3DMaterialImpl_AddRef(IDirect3DMaterial3 *iface)
131 {
132     IDirect3DMaterialImpl *This = (IDirect3DMaterialImpl *)iface;
133     ULONG ref = InterlockedIncrement(&This->ref);
134
135     TRACE("(%p)->() incrementing from %u.\n", This, ref - 1);
136
137     return ref;
138 }
139
140 /*****************************************************************************
141  * IDirect3DMaterial3::Release
142  *
143  * Reduces the refcount by one. If the refcount falls to 0, the object
144  * is destroyed
145  *
146  * Returns:
147  *  The new refcount
148  *
149  *****************************************************************************/
150 static ULONG WINAPI
151 IDirect3DMaterialImpl_Release(IDirect3DMaterial3 *iface)
152 {
153     IDirect3DMaterialImpl *This = (IDirect3DMaterialImpl *)iface;
154     ULONG ref = InterlockedDecrement(&This->ref);
155
156     TRACE("(%p)->() decrementing from %u.\n", This, ref + 1);
157
158     if (!ref)
159     {
160         if(This->Handle)
161         {
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);
166         }
167
168         HeapFree(GetProcessHeap(), 0, This);
169         return 0;
170     }
171     return ref;
172 }
173
174 /*****************************************************************************
175  * IDirect3DMaterial Methods
176  *****************************************************************************/
177
178 /*****************************************************************************
179  * IDirect3DMaterial::Initialize
180  *
181  * A no-op initialization
182  *
183  * Params:
184  *  Direct3D: Pointer to a Direct3D interface
185  *
186  * Returns:
187  *  D3D_OK
188  *
189  *****************************************************************************/
190 static HRESULT WINAPI
191 IDirect3DMaterialImpl_Initialize(IDirect3DMaterial *iface,
192                                   IDirect3D *Direct3D)
193 {
194     IDirect3DMaterialImpl *This = material_from_material1(iface);
195
196     TRACE("(%p)->(%p) no-op...!\n", This, Direct3D);
197
198     return D3D_OK;
199 }
200
201 /*****************************************************************************
202  * IDirect3DMaterial::Reserve
203  *
204  * DirectX 5 sdk: "The IDirect3DMaterial2::Reserve method is not implemented"
205  * Odd. They seem to have mixed their interfaces.
206  *
207  * Returns:
208  *  DDERR_UNSUPPORTED
209  *
210  *****************************************************************************/
211 static HRESULT WINAPI
212 IDirect3DMaterialImpl_Reserve(IDirect3DMaterial *iface)
213 {
214     IDirect3DMaterialImpl *This = material_from_material1(iface);
215     TRACE("(%p)->() not implemented\n", This);
216
217     return DDERR_UNSUPPORTED;
218 }
219
220 /*****************************************************************************
221  * IDirect3DMaterial::Unreserve
222  *
223  * Not supported too
224  *
225  * Returns:
226  *  DDERR_UNSUPPORTED
227  *
228  *****************************************************************************/
229 static HRESULT WINAPI
230 IDirect3DMaterialImpl_Unreserve(IDirect3DMaterial *iface)
231 {
232     IDirect3DMaterialImpl *This = material_from_material1(iface);
233     TRACE("(%p)->() not implemented.\n", This);
234
235     return DDERR_UNSUPPORTED;
236 }
237
238 /*****************************************************************************
239  * IDirect3DMaterial3::SetMaterial
240  *
241  * Sets the material description
242  *
243  * Params:
244  *  Mat: Material to set
245  *
246  * Returns:
247  *  D3D_OK on success
248  *  DDERR_INVALIDPARAMS if Mat is NULL
249  *
250  *****************************************************************************/
251 static HRESULT WINAPI
252 IDirect3DMaterialImpl_SetMaterial(IDirect3DMaterial3 *iface,
253                                   D3DMATERIAL *lpMat)
254 {
255     IDirect3DMaterialImpl *This = (IDirect3DMaterialImpl *)iface;
256     TRACE("(%p)->(%p)\n", This, lpMat);
257     if (TRACE_ON(d3d7))
258         dump_material(lpMat);
259
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);
265
266     return DD_OK;
267 }
268
269 /*****************************************************************************
270  * IDirect3DMaterial3::GetMaterial
271  *
272  * Returns the material assigned to this interface
273  *
274  * Params:
275  *  Mat: Pointer to a D3DMATERIAL structure to store the material description
276  *
277  * Returns:
278  *  D3D_OK on success
279  *  DDERR_INVALIDPARAMS if Mat is NULL
280  *
281  *****************************************************************************/
282 static HRESULT WINAPI
283 IDirect3DMaterialImpl_GetMaterial(IDirect3DMaterial3 *iface,
284                                   D3DMATERIAL *lpMat)
285 {
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);
292     }
293
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);
300
301     return DD_OK;
302 }
303
304 /*****************************************************************************
305  * IDirect3DMaterial3::GetHandle
306  *
307  * Returns a handle for the material interface. The handle is simply a
308  * pointer to the material implementation
309  *
310  * Params:
311  *  Direct3DDevice3: The device this handle is assigned to
312  *  Handle: Address to write the handle to
313  *
314  * Returns:
315  *  D3D_OK on success
316  *  DDERR_INVALIDPARAMS if Handle is NULL
317  *
318  *****************************************************************************/
319 static HRESULT WINAPI
320 IDirect3DMaterialImpl_GetHandle(IDirect3DMaterial3 *iface,
321                                 IDirect3DDevice3 *lpDirect3DDevice3,
322                                 D3DMATERIALHANDLE *lpHandle)
323 {
324     IDirect3DMaterialImpl *This = (IDirect3DMaterialImpl *)iface;
325     IDirect3DDeviceImpl *device = device_from_device3(lpDirect3DDevice3);
326     TRACE("(%p/%p)->(%p,%p)\n", This, iface, device, lpHandle);
327
328     EnterCriticalSection(&ddraw_cs);
329     This->active_device = device;
330     if(!This->Handle)
331     {
332         This->Handle = IDirect3DDeviceImpl_CreateHandle(device);
333         if(!This->Handle)
334         {
335             ERR("Error creating a handle\n");
336             LeaveCriticalSection(&ddraw_cs);
337             return DDERR_INVALIDPARAMS;   /* Unchecked */
338         }
339         device->Handles[This->Handle - 1].ptr = This;
340         device->Handles[This->Handle - 1].type = DDrawHandle_Material;
341     }
342     *lpHandle = This->Handle;
343     TRACE(" returning handle %08x.\n", *lpHandle);
344     LeaveCriticalSection(&ddraw_cs);
345
346     return D3D_OK;
347 }
348
349 static HRESULT WINAPI
350 Thunk_IDirect3DMaterialImpl_2_GetHandle(LPDIRECT3DMATERIAL2 iface,
351                                         LPDIRECT3DDEVICE2 lpDirect3DDevice2,
352                                         LPD3DMATERIALHANDLE lpHandle)
353 {
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);
357 }
358
359 static HRESULT WINAPI
360 Thunk_IDirect3DMaterialImpl_1_GetHandle(LPDIRECT3DMATERIAL iface,
361                                         LPDIRECT3DDEVICE lpDirect3DDevice,
362                                         LPD3DMATERIALHANDLE lpHandle)
363 {
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);
367 }
368
369 static HRESULT WINAPI
370 Thunk_IDirect3DMaterialImpl_2_QueryInterface(LPDIRECT3DMATERIAL2 iface,
371                                              REFIID riid,
372                                              LPVOID* obp)
373 {
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);
376 }
377
378 static HRESULT WINAPI
379 Thunk_IDirect3DMaterialImpl_1_QueryInterface(LPDIRECT3DMATERIAL iface,
380                                              REFIID riid,
381                                              LPVOID* obp)
382 {
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);
385 }
386
387 static ULONG WINAPI
388 Thunk_IDirect3DMaterialImpl_2_AddRef(LPDIRECT3DMATERIAL2 iface)
389 {
390     TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DMaterial3 interface.\n", iface);
391     return IDirect3DMaterial3_AddRef((IDirect3DMaterial3 *)material_from_material2(iface));
392 }
393
394 static ULONG WINAPI
395 Thunk_IDirect3DMaterialImpl_1_AddRef(LPDIRECT3DMATERIAL iface)
396 {
397     TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DMaterial3 interface.\n", iface);
398     return IDirect3DMaterial3_AddRef((IDirect3DMaterial3 *)material_from_material1(iface));
399 }
400
401 static ULONG WINAPI
402 Thunk_IDirect3DMaterialImpl_2_Release(LPDIRECT3DMATERIAL2 iface)
403 {
404     TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DMaterial3 interface.\n", iface);
405     return IDirect3DMaterial3_Release((IDirect3DMaterial3 *)material_from_material2(iface));
406 }
407
408 static ULONG WINAPI
409 Thunk_IDirect3DMaterialImpl_1_Release(LPDIRECT3DMATERIAL iface)
410 {
411     TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DMaterial3 interface.\n", iface);
412     return IDirect3DMaterial3_Release((IDirect3DMaterial3 *)material_from_material1(iface));
413 }
414
415 static HRESULT WINAPI
416 Thunk_IDirect3DMaterialImpl_2_SetMaterial(LPDIRECT3DMATERIAL2 iface,
417                                           LPD3DMATERIAL lpMat)
418 {
419     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpMat);
420     return IDirect3DMaterial3_SetMaterial((IDirect3DMaterial3 *)material_from_material2(iface), lpMat);
421 }
422
423 static HRESULT WINAPI
424 Thunk_IDirect3DMaterialImpl_1_SetMaterial(LPDIRECT3DMATERIAL iface,
425                                           LPD3DMATERIAL lpMat)
426 {
427     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpMat);
428     return IDirect3DMaterial3_SetMaterial((IDirect3DMaterial3 *)material_from_material1(iface), lpMat);
429 }
430
431 static HRESULT WINAPI
432 Thunk_IDirect3DMaterialImpl_2_GetMaterial(LPDIRECT3DMATERIAL2 iface,
433                                           LPD3DMATERIAL lpMat)
434 {
435     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpMat);
436     return IDirect3DMaterial3_GetMaterial((IDirect3DMaterial3 *)material_from_material2(iface), lpMat);
437 }
438
439 static HRESULT WINAPI
440 Thunk_IDirect3DMaterialImpl_1_GetMaterial(LPDIRECT3DMATERIAL iface,
441                                           LPD3DMATERIAL lpMat)
442 {
443     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpMat);
444     return IDirect3DMaterial3_GetMaterial((IDirect3DMaterial3 *)material_from_material1(iface), lpMat);
445 }
446
447
448 /*****************************************************************************
449  * material_activate
450  *
451  * Uses IDirect3DDevice7::SetMaterial to activate the material
452  *
453  * Params:
454  *  This: Pointer to the material implementation to activate
455  *
456  *****************************************************************************/
457 void material_activate(IDirect3DMaterialImpl* This)
458 {
459     D3DMATERIAL7 d3d7mat;
460
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;
467
468     IDirect3DDevice7_SetMaterial((IDirect3DDevice7 *)This->active_device, &d3d7mat);
469 }
470
471 const IDirect3DMaterial3Vtbl IDirect3DMaterial3_Vtbl =
472 {
473     /*** IUnknown Methods ***/
474     IDirect3DMaterialImpl_QueryInterface,
475     IDirect3DMaterialImpl_AddRef,
476     IDirect3DMaterialImpl_Release,
477     /*** IDirect3DMaterial3 Methods ***/
478     IDirect3DMaterialImpl_SetMaterial,
479     IDirect3DMaterialImpl_GetMaterial,
480     IDirect3DMaterialImpl_GetHandle,
481 };
482
483 const IDirect3DMaterial2Vtbl IDirect3DMaterial2_Vtbl =
484 {
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,
493 };
494
495 const IDirect3DMaterialVtbl IDirect3DMaterial_Vtbl =
496 {
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
508 };