kernel32: Handle the SECURITY_* flags passed into CreateFileW by filling out the...
[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 "winnls.h"
36 #include "winerror.h"
37 #include "wingdi.h"
38 #include "wine/exception.h"
39 #include "excpt.h"
40
41 #include "ddraw.h"
42 #include "d3d.h"
43
44 #include "ddraw_private.h"
45 #include "wine/debug.h"
46
47 WINE_DEFAULT_DEBUG_CHANNEL(d3d7);
48 WINE_DECLARE_DEBUG_CHANNEL(ddraw_thunk);
49
50 static void dump_material(LPD3DMATERIAL mat)
51 {
52     DPRINTF("  dwSize : %d\n", mat->dwSize);
53 }
54
55 /*****************************************************************************
56  * IUnknown Methods.
57  *****************************************************************************/
58
59 /*****************************************************************************
60  * IDirect3DMaterial3::QueryInterface
61  *
62  * QueryInterface for IDirect3DMaterial. Can query all IDirect3DMaterial
63  * versions.
64  *
65  * Params:
66  *  riid: Interface id queried for
67  *  obj: Address to pass the interface pointer back
68  *
69  * Returns:
70  *  S_OK on success
71  *  E_NOINTERFACE if the requested interface wasn't found
72  *
73  *****************************************************************************/
74 static HRESULT WINAPI
75 IDirect3DMaterialImpl_QueryInterface(IDirect3DMaterial3 *iface,
76                                      REFIID riid,
77                                      LPVOID* obp)
78 {
79     ICOM_THIS_FROM(IDirect3DMaterialImpl, IDirect3DMaterial3, iface);
80     TRACE("(%p)->(%s,%p)\n", This, debugstr_guid(riid), obp);
81
82     *obp = NULL;
83
84     if ( IsEqualGUID( &IID_IUnknown,  riid ) ) {
85         IDirect3DMaterial_AddRef(ICOM_INTERFACE(This, IDirect3DMaterial));
86         *obp = iface;
87         TRACE("  Creating IUnknown interface at %p.\n", *obp);
88         return S_OK;
89     }
90     if ( IsEqualGUID( &IID_IDirect3DMaterial, riid ) ) {
91         IDirect3DMaterial_AddRef(ICOM_INTERFACE(This, IDirect3DMaterial));
92         *obp = ICOM_INTERFACE(This, IDirect3DMaterial);
93         TRACE("  Creating IDirect3DMaterial interface %p\n", *obp);
94         return S_OK;
95     }
96     if ( IsEqualGUID( &IID_IDirect3DMaterial2, riid ) ) {
97         IDirect3DMaterial_AddRef(ICOM_INTERFACE(This, IDirect3DMaterial));
98         *obp = ICOM_INTERFACE(This, IDirect3DMaterial2);
99         TRACE("  Creating IDirect3DMaterial2 interface %p\n", *obp);
100         return S_OK;
101     }
102     if ( IsEqualGUID( &IID_IDirect3DMaterial3, riid ) ) {
103         IDirect3DMaterial_AddRef(ICOM_INTERFACE(This, IDirect3DMaterial));
104         *obp = ICOM_INTERFACE(This, IDirect3DMaterial3);
105         TRACE("  Creating IDirect3DMaterial3 interface %p\n", *obp);
106         return S_OK;
107     }
108     FIXME("(%p): interface for IID %s NOT found!\n", This, debugstr_guid(riid));
109     return E_NOINTERFACE;
110 }
111
112 /*****************************************************************************
113  * IDirect3DMaterial3::AddRef
114  *
115  * Increases the refcount.
116  *
117  * Returns:
118  *  The new refcount
119  *
120  *****************************************************************************/
121 static ULONG WINAPI
122 IDirect3DMaterialImpl_AddRef(IDirect3DMaterial3 *iface)
123 {
124     ICOM_THIS_FROM(IDirect3DMaterialImpl, IDirect3DMaterial3, iface);
125     ULONG ref = InterlockedIncrement(&This->ref);
126
127     TRACE("(%p)->() incrementing from %u.\n", This, ref - 1);
128
129     return ref;
130 }
131
132 /*****************************************************************************
133  * IDirect3DMaterial3::Release
134  *
135  * Reduces the refcount by one. If the refcount falls to 0, the object
136  * is destroyed
137  *
138  * Returns:
139  *  The new refcount
140  *
141  *****************************************************************************/
142 static ULONG WINAPI
143 IDirect3DMaterialImpl_Release(IDirect3DMaterial3 *iface)
144 {
145     ICOM_THIS_FROM(IDirect3DMaterialImpl, IDirect3DMaterial3, iface);
146     ULONG ref = InterlockedDecrement(&This->ref);
147
148     TRACE("(%p)->() decrementing from %u.\n", This, ref + 1);
149
150     if (!ref)
151     {
152         if(This->Handle)
153         {
154             This->ddraw->d3ddevice->Handles[This->Handle - 1].ptr = NULL;
155             This->ddraw->d3ddevice->Handles[This->Handle - 1].type = DDrawHandle_Unknown;
156         }
157
158         HeapFree(GetProcessHeap(), 0, This);
159         return 0;
160     }
161     return ref;
162 }
163
164 /*****************************************************************************
165  * IDirect3DMaterial Methods
166  *****************************************************************************/
167
168 /*****************************************************************************
169  * IDirect3DMaterial::Initialize
170  *
171  * A no-op initialization
172  *
173  * Params:
174  *  Direct3D: Pointer to a Direct3D interface
175  *
176  * Returns:
177  *  D3D_OK
178  *
179  *****************************************************************************/
180 static HRESULT WINAPI
181 IDirect3DMaterialImpl_Initialize(IDirect3DMaterial *iface,
182                                   IDirect3D *Direct3D)
183 {
184     ICOM_THIS_FROM(IDirect3DMaterialImpl, IDirect3DMaterial, iface);
185
186     TRACE("(%p)->(%p) no-op...!\n", This, Direct3D);
187
188     return D3D_OK;
189 }
190
191 /*****************************************************************************
192  * IDirect3DMaterial::Reserve
193  *
194  * DirectX 5 sdk: "The IDirect3DMaterial2::Reserve method is not implemented"
195  * Odd. They seem to have mixed their interfaces.
196  *
197  * Returns:
198  *  DDERR_UNSUPPORTED
199  *
200  *****************************************************************************/
201 static HRESULT WINAPI
202 IDirect3DMaterialImpl_Reserve(IDirect3DMaterial *iface)
203 {
204     ICOM_THIS_FROM(IDirect3DMaterialImpl, IDirect3DMaterial, iface);
205     TRACE("(%p)->() not implemented\n", This);
206
207     return DDERR_UNSUPPORTED;
208 }
209
210 /*****************************************************************************
211  * IDirect3DMaterial::Unreserve
212  *
213  * Not supported too
214  *
215  * Returns:
216  *  DDERR_UNSUPPORTED
217  *
218  *****************************************************************************/
219 static HRESULT WINAPI
220 IDirect3DMaterialImpl_Unreserve(IDirect3DMaterial *iface)
221 {
222     ICOM_THIS_FROM(IDirect3DMaterialImpl, IDirect3DMaterial, iface);
223     TRACE("(%p)->() not implemented.\n", This);
224
225     return DDERR_UNSUPPORTED;
226 }
227
228 /*****************************************************************************
229  * IDirect3DMaterial3::SetMaterial
230  *
231  * Sets the material description
232  *
233  * Params:
234  *  Mat: Material to set
235  *
236  * Returns:
237  *  D3D_OK on success
238  *  DDERR_INVALIDPARAMS if Mat is NULL
239  *
240  *****************************************************************************/
241 static HRESULT WINAPI
242 IDirect3DMaterialImpl_SetMaterial(IDirect3DMaterial3 *iface,
243                                   D3DMATERIAL *lpMat)
244 {
245     ICOM_THIS_FROM(IDirect3DMaterialImpl, IDirect3DMaterial3, iface);
246     TRACE("(%p)->(%p)\n", This, lpMat);
247     if (TRACE_ON(d3d7))
248         dump_material(lpMat);
249
250     /* Stores the material */
251     memset(&This->mat, 0, sizeof(This->mat));
252     memcpy(&This->mat, lpMat, lpMat->dwSize);
253     
254     return DD_OK;
255 }
256
257 /*****************************************************************************
258  * IDirect3DMaterial3::GetMaterial
259  *
260  * Returns the material assigned to this interface
261  *
262  * Params:
263  *  Mat: Pointer to a D3DMATERIAL structure to store the material description
264  *
265  * Returns:
266  *  D3D_OK on success
267  *  DDERR_INVALIDPARAMS if Mat is NULL
268  *
269  *****************************************************************************/
270 static HRESULT WINAPI
271 IDirect3DMaterialImpl_GetMaterial(IDirect3DMaterial3 *iface,
272                                   D3DMATERIAL *lpMat)
273 {
274     ICOM_THIS_FROM(IDirect3DMaterialImpl, IDirect3DMaterial3, iface);
275     DWORD dwSize;
276     TRACE("(%p)->(%p)\n", This, lpMat);
277     if (TRACE_ON(d3d7)) {
278         TRACE("  Returning material : ");
279         dump_material(&This->mat);
280     }
281
282     /* Copies the material structure */
283     dwSize = lpMat->dwSize;
284     memset(lpMat, 0, dwSize);
285     memcpy(lpMat, &This->mat, dwSize);
286
287     return DD_OK;
288 }
289
290 /*****************************************************************************
291  * IDirect3DMaterial3::GetHandle
292  *
293  * Returns a handle for the material interface. The handle is simply a
294  * pointer to the material implementation
295  *
296  * Params:
297  *  Direct3DDevice3: The device this handle is assigned to
298  *  Handle: Address to write the handle to
299  *
300  * Returns:
301  *  D3D_OK on success
302  *  DDERR_INVALIDPARAMS if Handle is NULL
303  *
304  *****************************************************************************/
305 static HRESULT WINAPI
306 IDirect3DMaterialImpl_GetHandle(IDirect3DMaterial3 *iface,
307                                 IDirect3DDevice3 *lpDirect3DDevice3,
308                                 D3DMATERIALHANDLE *lpHandle)
309 {
310     ICOM_THIS_FROM(IDirect3DMaterialImpl, IDirect3DMaterial3, iface);
311     IDirect3DDeviceImpl *device = ICOM_OBJECT(IDirect3DDeviceImpl, IDirect3DDevice3, lpDirect3DDevice3);
312     TRACE("(%p/%p)->(%p,%p)\n", This, iface, device, lpHandle);
313
314     This->active_device = device;
315     if(!This->Handle)
316     {
317         This->Handle = IDirect3DDeviceImpl_CreateHandle(device);
318         if(!This->Handle)
319         {
320             ERR("Error creating a handle\n");
321             return DDERR_INVALIDPARAMS;   /* Unchecked */
322         }
323         device->Handles[This->Handle - 1].ptr = This;
324         device->Handles[This->Handle - 1].type = DDrawHandle_Material;
325     }
326     *lpHandle = This->Handle;
327     TRACE(" returning handle %08x.\n", *lpHandle);
328
329     return D3D_OK;
330 }
331
332 static HRESULT WINAPI
333 Thunk_IDirect3DMaterialImpl_2_GetHandle(LPDIRECT3DMATERIAL2 iface,
334                                         LPDIRECT3DDEVICE2 lpDirect3DDevice2,
335                                         LPD3DMATERIALHANDLE lpHandle)
336 {
337     TRACE_(ddraw_thunk)("(%p)->(%p,%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpDirect3DDevice2, lpHandle);
338     return IDirect3DMaterial3_GetHandle(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial2, IDirect3DMaterial3, iface),
339                                         COM_INTERFACE_CAST(IDirect3DDeviceImpl, IDirect3DDevice2, IDirect3DDevice3, lpDirect3DDevice2),
340                                         lpHandle);
341 }
342
343 static HRESULT WINAPI
344 Thunk_IDirect3DMaterialImpl_1_GetHandle(LPDIRECT3DMATERIAL iface,
345                                         LPDIRECT3DDEVICE lpDirect3DDevice,
346                                         LPD3DMATERIALHANDLE lpHandle)
347 {
348     TRACE_(ddraw_thunk)("(%p)->(%p,%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpDirect3DDevice, lpHandle);
349     return IDirect3DMaterial3_GetHandle(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial, IDirect3DMaterial3, iface),
350                                         COM_INTERFACE_CAST(IDirect3DDeviceImpl, IDirect3DDevice, IDirect3DDevice3, lpDirect3DDevice),
351                                         lpHandle);
352 }
353
354 static HRESULT WINAPI
355 Thunk_IDirect3DMaterialImpl_2_QueryInterface(LPDIRECT3DMATERIAL2 iface,
356                                              REFIID riid,
357                                              LPVOID* obp)
358 {
359     TRACE_(ddraw_thunk)("(%p)->(%s,%p) thunking to IDirect3DMaterial3 interface.\n", iface, debugstr_guid(riid), obp);
360     return IDirect3DMaterial3_QueryInterface(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial2, IDirect3DMaterial3, iface),
361                                              riid,
362                                              obp);
363 }
364
365 static HRESULT WINAPI
366 Thunk_IDirect3DMaterialImpl_1_QueryInterface(LPDIRECT3DMATERIAL iface,
367                                              REFIID riid,
368                                              LPVOID* obp)
369 {
370     TRACE_(ddraw_thunk)("(%p)->(%s,%p) thunking to IDirect3DMaterial3 interface.\n", iface, debugstr_guid(riid), obp);
371     return IDirect3DMaterial3_QueryInterface(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial, IDirect3DMaterial3, iface),
372                                              riid,
373                                              obp);
374 }
375
376 static ULONG WINAPI
377 Thunk_IDirect3DMaterialImpl_2_AddRef(LPDIRECT3DMATERIAL2 iface)
378 {
379     TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DMaterial3 interface.\n", iface);
380     return IDirect3DMaterial3_AddRef(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial2, IDirect3DMaterial3, iface));
381 }
382
383 static ULONG WINAPI
384 Thunk_IDirect3DMaterialImpl_1_AddRef(LPDIRECT3DMATERIAL iface)
385 {
386     TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DMaterial3 interface.\n", iface);
387     return IDirect3DMaterial3_AddRef(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial, IDirect3DMaterial3, iface));
388 }
389
390 static ULONG WINAPI
391 Thunk_IDirect3DMaterialImpl_2_Release(LPDIRECT3DMATERIAL2 iface)
392 {
393     TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DMaterial3 interface.\n", iface);
394     return IDirect3DMaterial3_Release(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial2, IDirect3DMaterial3, iface));
395 }
396
397 static ULONG WINAPI
398 Thunk_IDirect3DMaterialImpl_1_Release(LPDIRECT3DMATERIAL iface)
399 {
400     TRACE_(ddraw_thunk)("(%p)->() thunking to IDirect3DMaterial3 interface.\n", iface);
401     return IDirect3DMaterial3_Release(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial, IDirect3DMaterial3, iface));
402 }
403
404 static HRESULT WINAPI
405 Thunk_IDirect3DMaterialImpl_2_SetMaterial(LPDIRECT3DMATERIAL2 iface,
406                                           LPD3DMATERIAL lpMat)
407 {
408     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpMat);
409     return IDirect3DMaterial3_SetMaterial(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial2, IDirect3DMaterial3, iface),
410                                           lpMat);
411 }
412
413 static HRESULT WINAPI
414 Thunk_IDirect3DMaterialImpl_1_SetMaterial(LPDIRECT3DMATERIAL iface,
415                                           LPD3DMATERIAL lpMat)
416 {
417     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpMat);
418     return IDirect3DMaterial3_SetMaterial(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial, IDirect3DMaterial3, iface),
419                                           lpMat);
420 }
421
422 static HRESULT WINAPI
423 Thunk_IDirect3DMaterialImpl_2_GetMaterial(LPDIRECT3DMATERIAL2 iface,
424                                           LPD3DMATERIAL lpMat)
425 {
426     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpMat);
427     return IDirect3DMaterial3_GetMaterial(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial2, IDirect3DMaterial3, iface),
428                                           lpMat);
429 }
430
431 static HRESULT WINAPI
432 Thunk_IDirect3DMaterialImpl_1_GetMaterial(LPDIRECT3DMATERIAL iface,
433                                           LPD3DMATERIAL lpMat)
434 {
435     TRACE_(ddraw_thunk)("(%p)->(%p) thunking to IDirect3DMaterial3 interface.\n", iface, lpMat);
436     return IDirect3DMaterial3_GetMaterial(COM_INTERFACE_CAST(IDirect3DMaterialImpl, IDirect3DMaterial, IDirect3DMaterial3, iface),
437                                           lpMat);
438 }
439
440
441 /*****************************************************************************
442  * material_activate
443  *
444  * Uses IDirect3DDevice7::SetMaterial to activate the material
445  *
446  * Params:
447  *  This: Pointer to the material implementation to activate
448  *
449  *****************************************************************************/
450 void material_activate(IDirect3DMaterialImpl* This)
451 {
452     D3DMATERIAL7 d3d7mat;
453
454     TRACE("Activating material %p\n", This);
455     d3d7mat.u.diffuse = This->mat.u.diffuse;
456     d3d7mat.u1.ambient = This->mat.u1.ambient;
457     d3d7mat.u2.specular = This->mat.u2.specular;
458     d3d7mat.u3.emissive = This->mat.u3.emissive;
459     d3d7mat.u4.power = This->mat.u4.power;
460
461     IDirect3DDevice7_SetMaterial(ICOM_INTERFACE(This->active_device, IDirect3DDevice7),
462                                  &d3d7mat);
463 }
464
465 const IDirect3DMaterial3Vtbl IDirect3DMaterial3_Vtbl =
466 {
467     /*** IUnknown Methods ***/
468     IDirect3DMaterialImpl_QueryInterface,
469     IDirect3DMaterialImpl_AddRef,
470     IDirect3DMaterialImpl_Release,
471     /*** IDirect3DMaterial3 Methods ***/
472     IDirect3DMaterialImpl_SetMaterial,
473     IDirect3DMaterialImpl_GetMaterial,
474     IDirect3DMaterialImpl_GetHandle,
475 };
476
477 const IDirect3DMaterial2Vtbl IDirect3DMaterial2_Vtbl =
478 {
479     /*** IUnknown Methods ***/
480     Thunk_IDirect3DMaterialImpl_2_QueryInterface,
481     Thunk_IDirect3DMaterialImpl_2_AddRef,
482     Thunk_IDirect3DMaterialImpl_2_Release,
483     /*** IDirect3DMaterial2 Methods ***/
484     Thunk_IDirect3DMaterialImpl_2_SetMaterial,
485     Thunk_IDirect3DMaterialImpl_2_GetMaterial,
486     Thunk_IDirect3DMaterialImpl_2_GetHandle,
487 };
488
489 const IDirect3DMaterialVtbl IDirect3DMaterial_Vtbl =
490 {
491     /*** IUnknown Methods ***/
492     Thunk_IDirect3DMaterialImpl_1_QueryInterface,
493     Thunk_IDirect3DMaterialImpl_1_AddRef,
494     Thunk_IDirect3DMaterialImpl_1_Release,
495     /*** IDirect3DMaterial1 Methods ***/
496     IDirect3DMaterialImpl_Initialize,
497     Thunk_IDirect3DMaterialImpl_1_SetMaterial,
498     Thunk_IDirect3DMaterialImpl_1_GetMaterial,
499     Thunk_IDirect3DMaterialImpl_1_GetHandle,
500     IDirect3DMaterialImpl_Reserve,
501     IDirect3DMaterialImpl_Unreserve
502 };