ddraw: Properly clear the clip list if ddraw_clipper_SetClipList() is called with...
[wine] / dlls / ddraw / light.c
1 /* Direct3D Light
2  * Copyright (c) 1998 / 2002 Lionel ULMER
3  * Copyright (c) 2006        Stefan DÖSINGER
4  *
5  * This file contains the implementation of Direct3DLight.
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 "ddraw_private.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
28
29 /*****************************************************************************
30  * light_update
31  *
32  * Updates the Direct3DDevice7 lighting parameters
33  *
34  *****************************************************************************/
35 static void light_update(IDirect3DLightImpl *light)
36 {
37     IDirect3DDeviceImpl *device;
38
39     TRACE("light %p.\n", light);
40
41     if (!light->active_viewport || !light->active_viewport->active_device) return;
42     device = light->active_viewport->active_device;
43
44     IDirect3DDevice7_SetLight(&device->IDirect3DDevice7_iface, light->dwLightIndex, &light->light7);
45 }
46
47 /*****************************************************************************
48  * light_activate
49  *
50  * Uses the Direct3DDevice7::LightEnable method to active the light
51  *
52  *****************************************************************************/
53 void light_activate(IDirect3DLightImpl *light)
54 {
55     IDirect3DDeviceImpl *device;
56
57     TRACE("light %p.\n", light);
58
59     if (!light->active_viewport || !light->active_viewport->active_device) return;
60     device = light->active_viewport->active_device;
61
62     light_update(light);
63     if (!(light->light.dwFlags & D3DLIGHT_ACTIVE))
64     {
65         IDirect3DDevice7_LightEnable(&device->IDirect3DDevice7_iface, light->dwLightIndex, TRUE);
66         light->light.dwFlags |= D3DLIGHT_ACTIVE;
67     }
68 }
69
70 /*****************************************************************************
71  *
72  * light_deactivate
73  *
74  * Uses the Direct3DDevice7::LightEnable method to deactivate the light
75  *
76  *****************************************************************************/
77 void light_deactivate(IDirect3DLightImpl *light)
78 {
79     IDirect3DDeviceImpl *device;
80
81     TRACE("light %p.\n", light);
82
83     if (!light->active_viewport || !light->active_viewport->active_device) return;
84     device = light->active_viewport->active_device;
85
86     /* If was not active, activate it */
87     if (light->light.dwFlags & D3DLIGHT_ACTIVE)
88     {
89         IDirect3DDevice7_LightEnable(&device->IDirect3DDevice7_iface, light->dwLightIndex, FALSE);
90         light->light.dwFlags &= ~D3DLIGHT_ACTIVE;
91     }
92 }
93
94 static inline IDirect3DLightImpl *impl_from_IDirect3DLight(IDirect3DLight *iface)
95 {
96     return CONTAINING_RECORD(iface, IDirect3DLightImpl, IDirect3DLight_iface);
97 }
98
99 /*****************************************************************************
100  * IDirect3DLight::QueryInterface
101  *
102  * Queries the object for different interfaces. Unimplemented for this
103  * object at the moment
104  *
105  * Params:
106  *  riid: Interface id asked for
107  *  obj: Address to return the resulting pointer at.
108  *
109  * Returns:
110  *  E_NOINTERFACE, because it's a stub
111  *****************************************************************************/
112 static HRESULT WINAPI IDirect3DLightImpl_QueryInterface(IDirect3DLight *iface, REFIID riid, void **object)
113 {
114     FIXME("iface %p, riid %s, object %p stub!\n", iface, debugstr_guid(riid), object);
115
116     *object = NULL;
117     return E_NOINTERFACE;
118 }
119
120 static ULONG WINAPI IDirect3DLightImpl_AddRef(IDirect3DLight *iface)
121 {
122     IDirect3DLightImpl *This = impl_from_IDirect3DLight(iface);
123     ULONG ref = InterlockedIncrement(&This->ref);
124
125     TRACE("%p increasing refcount to %u.\n", This, ref);
126
127     return ref;
128 }
129
130 static ULONG WINAPI IDirect3DLightImpl_Release(IDirect3DLight *iface)
131 {
132     IDirect3DLightImpl *This = impl_from_IDirect3DLight(iface);
133     ULONG ref = InterlockedDecrement(&This->ref);
134
135     TRACE("%p decreasing refcount to %u.\n", This, ref);
136
137     if (!ref) {
138         HeapFree(GetProcessHeap(), 0, This);
139         return 0;
140     }
141     return ref;
142 }
143
144 /*****************************************************************************
145  * IDirect3DLight Methods.
146  *****************************************************************************/
147
148 /*****************************************************************************
149  * IDirect3DLight::Initialize
150  *
151  * Initializes the interface. This implementation is a no-op, because
152  * initialization takes place at creation time
153  *
154  * Params:
155  *  Direct3D: Pointer to an IDirect3D interface.
156  *
157  * Returns:
158  *  D3D_OK
159  *
160  *****************************************************************************/
161 static HRESULT WINAPI IDirect3DLightImpl_Initialize(IDirect3DLight *iface, IDirect3D *d3d)
162 {
163     TRACE("iface %p, d3d %p.\n", iface, d3d);
164
165     return D3D_OK;
166 }
167
168 /*****************************************************************************
169  * IDirect3DLight::SetLight
170  *
171  * Assigns a lighting value to this object
172  *
173  * Params:
174  *  Light: Lighting parameter to set
175  *
176  * Returns:
177  *  D3D_OK on success
178  *  DDERR_INVALIDPARAMS if Light is NULL
179  *
180  *****************************************************************************/
181 static void dump_light(const D3DLIGHT2 *light)
182 {
183     TRACE("    - dwSize : %d\n", light->dwSize);
184 }
185
186 static const float zero_value[] = {
187     0.0, 0.0, 0.0, 0.0
188 };
189
190 static HRESULT WINAPI IDirect3DLightImpl_SetLight(IDirect3DLight *iface, D3DLIGHT *lpLight)
191 {
192     IDirect3DLightImpl *This = impl_from_IDirect3DLight(iface);
193     LPD3DLIGHT7 light7 = &This->light7;
194
195     TRACE("iface %p, light %p.\n", iface, lpLight);
196
197     if (TRACE_ON(ddraw))
198     {
199         TRACE("  Light definition :\n");
200         dump_light((LPD3DLIGHT2) lpLight);
201     }
202
203     if ( (lpLight->dltType == 0) || (lpLight->dltType > D3DLIGHT_PARALLELPOINT) )
204          return DDERR_INVALIDPARAMS;
205
206     if ( lpLight->dltType == D3DLIGHT_PARALLELPOINT )
207         FIXME("D3DLIGHT_PARALLELPOINT no supported\n");
208
209     /* Translate D3DLIGH2 structure to D3DLIGHT7 */
210     light7->dltType        = lpLight->dltType;
211     light7->dcvDiffuse     = lpLight->dcvColor;
212     if ((((LPD3DLIGHT2)lpLight)->dwFlags & D3DLIGHT_NO_SPECULAR) != 0)
213       light7->dcvSpecular    = lpLight->dcvColor;
214     else
215       light7->dcvSpecular    = *(const D3DCOLORVALUE*)zero_value;
216     light7->dcvAmbient     = lpLight->dcvColor;
217     light7->dvPosition     = lpLight->dvPosition;
218     light7->dvDirection    = lpLight->dvDirection;
219     light7->dvRange        = lpLight->dvRange;
220     light7->dvFalloff      = lpLight->dvFalloff;
221     light7->dvAttenuation0 = lpLight->dvAttenuation0;
222     light7->dvAttenuation1 = lpLight->dvAttenuation1;
223     light7->dvAttenuation2 = lpLight->dvAttenuation2;
224     light7->dvTheta        = lpLight->dvTheta;
225     light7->dvPhi          = lpLight->dvPhi;
226
227     wined3d_mutex_lock();
228     memcpy(&This->light, lpLight, lpLight->dwSize);
229     if (This->light.dwFlags & D3DLIGHT_ACTIVE)
230         light_update(This);
231     wined3d_mutex_unlock();
232
233     return D3D_OK;
234 }
235
236 /*****************************************************************************
237  * IDirect3DLight::GetLight
238  *
239  * Returns the parameters currently assigned to the IDirect3DLight object
240  *
241  * Params:
242  *  Light: Pointer to an D3DLIGHT structure to store the parameters
243  *
244  * Returns:
245  *  D3D_OK on success
246  *  DDERR_INVALIDPARAMS if Light is NULL
247  *****************************************************************************/
248 static HRESULT WINAPI IDirect3DLightImpl_GetLight(IDirect3DLight *iface, D3DLIGHT *lpLight)
249 {
250     IDirect3DLightImpl *This = impl_from_IDirect3DLight(iface);
251
252     TRACE("iface %p, light %p.\n", iface, lpLight);
253
254     if (TRACE_ON(ddraw))
255     {
256         TRACE("  Returning light definition :\n");
257         dump_light(&This->light);
258     }
259
260     wined3d_mutex_lock();
261     memcpy(lpLight, &This->light, lpLight->dwSize);
262     wined3d_mutex_unlock();
263
264     return DD_OK;
265 }
266
267 static const struct IDirect3DLightVtbl d3d_light_vtbl =
268 {
269     /*** IUnknown Methods ***/
270     IDirect3DLightImpl_QueryInterface,
271     IDirect3DLightImpl_AddRef,
272     IDirect3DLightImpl_Release,
273     /*** IDirect3DLight Methods ***/
274     IDirect3DLightImpl_Initialize,
275     IDirect3DLightImpl_SetLight,
276     IDirect3DLightImpl_GetLight
277 };
278
279 void d3d_light_init(IDirect3DLightImpl *light, IDirectDrawImpl *ddraw)
280 {
281     light->IDirect3DLight_iface.lpVtbl = &d3d_light_vtbl;
282     light->ref = 1;
283     light->ddraw = ddraw;
284 }
285
286 IDirect3DLightImpl *unsafe_impl_from_IDirect3DLight(IDirect3DLight *iface)
287 {
288     if (!iface)
289         return NULL;
290     assert(iface->lpVtbl == &d3d_light_vtbl);
291
292     return impl_from_IDirect3DLight(iface);
293 }