msvcrt: Added _sscanf_l implementation.
[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     memcpy(lpMat, &This->mat, dwSize);
298     LeaveCriticalSection(&ddraw_cs);
299
300     return DD_OK;
301 }
302
303 /*****************************************************************************
304  * IDirect3DMaterial3::GetHandle
305  *
306  * Returns a handle for the material interface. The handle is simply a
307  * pointer to the material implementation
308  *
309  * Params:
310  *  Direct3DDevice3: The device this handle is assigned to
311  *  Handle: Address to write the handle to
312  *
313  * Returns:
314  *  D3D_OK on success
315  *  DDERR_INVALIDPARAMS if Handle is NULL
316  *
317  *****************************************************************************/
318 static HRESULT WINAPI
319 IDirect3DMaterialImpl_GetHandle(IDirect3DMaterial3 *iface,
320                                 IDirect3DDevice3 *lpDirect3DDevice3,
321                                 D3DMATERIALHANDLE *lpHandle)
322 {
323     IDirect3DMaterialImpl *This = (IDirect3DMaterialImpl *)iface;
324     IDirect3DDeviceImpl *device = device_from_device3(lpDirect3DDevice3);
325     TRACE("(%p/%p)->(%p,%p)\n", This, iface, device, lpHandle);
326
327     EnterCriticalSection(&ddraw_cs);
328     This->active_device = device;
329     if(!This->Handle)
330     {
331         This->Handle = IDirect3DDeviceImpl_CreateHandle(device);
332         if(!This->Handle)
333         {
334             ERR("Error creating a handle\n");
335             LeaveCriticalSection(&ddraw_cs);
336             return DDERR_INVALIDPARAMS;   /* Unchecked */
337         }
338         device->Handles[This->Handle - 1].ptr = This;
339         device->Handles[This->Handle - 1].type = DDrawHandle_Material;
340     }
341     *lpHandle = This->Handle;
342     TRACE(" returning handle %08x.\n", *lpHandle);
343     LeaveCriticalSection(&ddraw_cs);
344
345     return D3D_OK;
346 }
347
348 static HRESULT WINAPI
349 Thunk_IDirect3DMaterialImpl_2_GetHandle(LPDIRECT3DMATERIAL2 iface,
350                                         LPDIRECT3DDEVICE2 lpDirect3DDevice2,
351                                         LPD3DMATERIALHANDLE lpHandle)
352 {
353     TRACE_(ddraw_thunk)("(%p)->(%p,%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpDirect3DDevice2, lpHandle);
354     return IDirect3DMaterial3_GetHandle((IDirect3DMaterial3 *)material_from_material2(iface), lpDirect3DDevice2 ?
355             (IDirect3DDevice3 *)&device_from_device2(lpDirect3DDevice2)->IDirect3DDevice3_vtbl : NULL, lpHandle);
356 }
357
358 static HRESULT WINAPI
359 Thunk_IDirect3DMaterialImpl_1_GetHandle(LPDIRECT3DMATERIAL iface,
360                                         LPDIRECT3DDEVICE lpDirect3DDevice,
361                                         LPD3DMATERIALHANDLE lpHandle)
362 {
363     TRACE_(ddraw_thunk)("(%p)->(%p,%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpDirect3DDevice, lpHandle);
364     return IDirect3DMaterial3_GetHandle((IDirect3DMaterial3 *)material_from_material1(iface), lpDirect3DDevice ?
365             (IDirect3DDevice3 *)&device_from_device1(lpDirect3DDevice)->IDirect3DDevice3_vtbl : NULL, lpHandle);
366 }
367
368 static HRESULT WINAPI
369 Thunk_IDirect3DMaterialImpl_2_QueryInterface(LPDIRECT3DMATERIAL2 iface,
370                                              REFIID riid,
371                                              LPVOID* obp)
372 {
373     TRACE_(ddraw_thunk)("(%p)->(%s,%p) thunking to IDirect3DMaterial3 interface.\n", iface, debugstr_guid(riid), obp);
374     return IDirect3DMaterial3_QueryInterface((IDirect3DMaterial3 *)material_from_material2(iface), riid, obp);
375 }
376
377 static HRESULT WINAPI
378 Thunk_IDirect3DMaterialImpl_1_QueryInterface(LPDIRECT3DMATERIAL iface,
379                                              REFIID riid,
380                                              LPVOID* obp)
381 {
382     TRACE_(ddraw_thunk)("(%p)->(%s,%p) thunking to IDirect3DMaterial3 interface.\n", iface, debugstr_guid(riid), obp);
383     return IDirect3DMaterial3_QueryInterface((IDirect3DMaterial3 *)material_from_material1(iface), riid, obp);
384 }
385
386 static ULONG WINAPI
387 Thunk_IDirect3DMaterialImpl_2_AddRef(LPDIRECT3DMATERIAL2 iface)
388 {
389     TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DMaterial3 interface.\n", iface);
390     return IDirect3DMaterial3_AddRef((IDirect3DMaterial3 *)material_from_material2(iface));
391 }
392
393 static ULONG WINAPI
394 Thunk_IDirect3DMaterialImpl_1_AddRef(LPDIRECT3DMATERIAL iface)
395 {
396     TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DMaterial3 interface.\n", iface);
397     return IDirect3DMaterial3_AddRef((IDirect3DMaterial3 *)material_from_material1(iface));
398 }
399
400 static ULONG WINAPI
401 Thunk_IDirect3DMaterialImpl_2_Release(LPDIRECT3DMATERIAL2 iface)
402 {
403     TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DMaterial3 interface.\n", iface);
404     return IDirect3DMaterial3_Release((IDirect3DMaterial3 *)material_from_material2(iface));
405 }
406
407 static ULONG WINAPI
408 Thunk_IDirect3DMaterialImpl_1_Release(LPDIRECT3DMATERIAL iface)
409 {
410     TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DMaterial3 interface.\n", iface);
411     return IDirect3DMaterial3_Release((IDirect3DMaterial3 *)material_from_material1(iface));
412 }
413
414 static HRESULT WINAPI
415 Thunk_IDirect3DMaterialImpl_2_SetMaterial(LPDIRECT3DMATERIAL2 iface,
416                                           LPD3DMATERIAL lpMat)
417 {
418     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpMat);
419     return IDirect3DMaterial3_SetMaterial((IDirect3DMaterial3 *)material_from_material2(iface), lpMat);
420 }
421
422 static HRESULT WINAPI
423 Thunk_IDirect3DMaterialImpl_1_SetMaterial(LPDIRECT3DMATERIAL iface,
424                                           LPD3DMATERIAL lpMat)
425 {
426     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpMat);
427     return IDirect3DMaterial3_SetMaterial((IDirect3DMaterial3 *)material_from_material1(iface), lpMat);
428 }
429
430 static HRESULT WINAPI
431 Thunk_IDirect3DMaterialImpl_2_GetMaterial(LPDIRECT3DMATERIAL2 iface,
432                                           LPD3DMATERIAL lpMat)
433 {
434     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpMat);
435     return IDirect3DMaterial3_GetMaterial((IDirect3DMaterial3 *)material_from_material2(iface), lpMat);
436 }
437
438 static HRESULT WINAPI
439 Thunk_IDirect3DMaterialImpl_1_GetMaterial(LPDIRECT3DMATERIAL iface,
440                                           LPD3DMATERIAL lpMat)
441 {
442     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpMat);
443     return IDirect3DMaterial3_GetMaterial((IDirect3DMaterial3 *)material_from_material1(iface), lpMat);
444 }
445
446
447 /*****************************************************************************
448  * material_activate
449  *
450  * Uses IDirect3DDevice7::SetMaterial to activate the material
451  *
452  * Params:
453  *  This: Pointer to the material implementation to activate
454  *
455  *****************************************************************************/
456 void material_activate(IDirect3DMaterialImpl* This)
457 {
458     D3DMATERIAL7 d3d7mat;
459
460     TRACE("Activating material %p\n", This);
461     d3d7mat.u.diffuse = This->mat.u.diffuse;
462     d3d7mat.u1.ambient = This->mat.u1.ambient;
463     d3d7mat.u2.specular = This->mat.u2.specular;
464     d3d7mat.u3.emissive = This->mat.u3.emissive;
465     d3d7mat.u4.power = This->mat.u4.power;
466
467     IDirect3DDevice7_SetMaterial((IDirect3DDevice7 *)This->active_device, &d3d7mat);
468 }
469
470 const IDirect3DMaterial3Vtbl IDirect3DMaterial3_Vtbl =
471 {
472     /*** IUnknown Methods ***/
473     IDirect3DMaterialImpl_QueryInterface,
474     IDirect3DMaterialImpl_AddRef,
475     IDirect3DMaterialImpl_Release,
476     /*** IDirect3DMaterial3 Methods ***/
477     IDirect3DMaterialImpl_SetMaterial,
478     IDirect3DMaterialImpl_GetMaterial,
479     IDirect3DMaterialImpl_GetHandle,
480 };
481
482 const IDirect3DMaterial2Vtbl IDirect3DMaterial2_Vtbl =
483 {
484     /*** IUnknown Methods ***/
485     Thunk_IDirect3DMaterialImpl_2_QueryInterface,
486     Thunk_IDirect3DMaterialImpl_2_AddRef,
487     Thunk_IDirect3DMaterialImpl_2_Release,
488     /*** IDirect3DMaterial2 Methods ***/
489     Thunk_IDirect3DMaterialImpl_2_SetMaterial,
490     Thunk_IDirect3DMaterialImpl_2_GetMaterial,
491     Thunk_IDirect3DMaterialImpl_2_GetHandle,
492 };
493
494 const IDirect3DMaterialVtbl IDirect3DMaterial_Vtbl =
495 {
496     /*** IUnknown Methods ***/
497     Thunk_IDirect3DMaterialImpl_1_QueryInterface,
498     Thunk_IDirect3DMaterialImpl_1_AddRef,
499     Thunk_IDirect3DMaterialImpl_1_Release,
500     /*** IDirect3DMaterial1 Methods ***/
501     IDirect3DMaterialImpl_Initialize,
502     Thunk_IDirect3DMaterialImpl_1_SetMaterial,
503     Thunk_IDirect3DMaterialImpl_1_GetMaterial,
504     Thunk_IDirect3DMaterialImpl_1_GetHandle,
505     IDirect3DMaterialImpl_Reserve,
506     IDirect3DMaterialImpl_Unreserve
507 };