Don't leak a list of visuals (with advice of Jacek Caban).
[wine] / dlls / ddraw / light.c
1 /* Direct3D Light
2  * Copyright (c) 1998 / 2002 Lionel ULMER
3  *
4  * This file contains the implementation of Direct3DLight.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22
23 #include <stdarg.h>
24
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
27
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winerror.h"
31 #include "objbase.h"
32 #include "wingdi.h"
33 #include "ddraw.h"
34 #include "d3d.h"
35 #include "wine/debug.h"
36
37 #include "d3d_private.h"
38 #include "opengl_private.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
41
42 /* First, the 'main' interface... */
43 HRESULT WINAPI
44 Main_IDirect3DLightImpl_1_QueryInterface(LPDIRECT3DLIGHT iface,
45                                          REFIID riid,
46                                          LPVOID* obp)
47 {
48     ICOM_THIS_FROM(IDirect3DLightImpl, IDirect3DLight, iface);
49     FIXME("(%p/%p)->(%s,%p): stub!\n", This, iface, debugstr_guid(riid), obp);
50     return DD_OK;
51 }
52
53 ULONG WINAPI
54 Main_IDirect3DLightImpl_1_AddRef(LPDIRECT3DLIGHT iface)
55 {
56     ICOM_THIS_FROM(IDirect3DLightImpl, IDirect3DLight, iface);
57     ULONG ref = InterlockedIncrement(&This->ref);
58
59     TRACE("(%p/%p)->() incrementing from %lu.\n", This, iface, ref - 1);
60
61     return ref;
62 }
63
64 ULONG WINAPI
65 Main_IDirect3DLightImpl_1_Release(LPDIRECT3DLIGHT iface)
66 {
67     ICOM_THIS_FROM(IDirect3DLightImpl, IDirect3DLight, iface);
68     ULONG ref = InterlockedDecrement(&This->ref);
69
70     TRACE("(%p/%p)->() decrementing from %lu.\n", This, iface, ref + 1);
71
72     if (!ref) {
73         HeapFree(GetProcessHeap(), 0, This);
74         return 0;
75     }
76     return ref;
77 }
78
79 HRESULT WINAPI
80 Main_IDirect3DLightImpl_1_Initialize(LPDIRECT3DLIGHT iface,
81                                      LPDIRECT3D lpDirect3D)
82 {
83     ICOM_THIS_FROM(IDirect3DLightImpl, IDirect3DLight, iface);
84     TRACE("(%p/%p)->(%p) no-op...\n", This, iface, lpDirect3D);
85     return DD_OK;
86 }
87
88 /*** IDirect3DLight methods ***/
89 static void dump_light(LPD3DLIGHT2 light)
90 {
91     DPRINTF("    - dwSize : %ld\n", light->dwSize);
92 }
93
94 static const float zero_value[] = {
95     0.0, 0.0, 0.0, 0.0
96 };
97
98 HRESULT WINAPI
99 Main_IDirect3DLightImpl_1_SetLight(LPDIRECT3DLIGHT iface,
100                                    LPD3DLIGHT lpLight)
101 {
102     ICOM_THIS_FROM(IDirect3DLightImpl, IDirect3DLight, iface);
103     LPD3DLIGHT7 light7 = &(This->light7);
104     TRACE("(%p/%p)->(%p)\n", This, iface, lpLight);
105     if (TRACE_ON(ddraw)) {
106         TRACE("  Light definition : \n");
107         dump_light((LPD3DLIGHT2) lpLight);
108     }
109
110     if ( (lpLight->dltType == 0) || (lpLight->dltType > D3DLIGHT_PARALLELPOINT) )
111          return DDERR_INVALIDPARAMS;
112     
113     if ( lpLight->dltType == D3DLIGHT_PARALLELPOINT )
114          FIXME("D3DLIGHT_PARALLELPOINT no supported\n");
115     
116     /* Translate D3DLIGH2 structure to D3DLIGHT7 */
117     light7->dltType        = lpLight->dltType;
118     light7->dcvDiffuse     = lpLight->dcvColor;
119     if ((((LPD3DLIGHT2)lpLight)->dwFlags & D3DLIGHT_NO_SPECULAR) != 0)      
120       light7->dcvSpecular    = lpLight->dcvColor;
121     else
122       light7->dcvSpecular    = *(const D3DCOLORVALUE*)zero_value;           
123     light7->dcvAmbient     = lpLight->dcvColor;
124     light7->dvPosition     = lpLight->dvPosition;
125     light7->dvDirection    = lpLight->dvDirection;
126     light7->dvRange        = lpLight->dvRange;
127     light7->dvFalloff      = lpLight->dvFalloff;
128     light7->dvAttenuation0 = lpLight->dvAttenuation0;
129     light7->dvAttenuation1 = lpLight->dvAttenuation1;
130     light7->dvAttenuation2 = lpLight->dvAttenuation2;
131     light7->dvTheta        = lpLight->dvTheta;
132     light7->dvPhi          = lpLight->dvPhi;
133
134     memcpy(&This->light, lpLight, lpLight->dwSize);
135     if ((This->light.dwFlags & D3DLIGHT_ACTIVE) != 0) {
136         This->update(This);        
137     }
138     return DD_OK;
139 }
140
141 HRESULT WINAPI
142 Main_IDirect3DLightImpl_1_GetLight(LPDIRECT3DLIGHT iface,
143                                    LPD3DLIGHT lpLight)
144 {
145     ICOM_THIS_FROM(IDirect3DLightImpl, IDirect3DLight, iface);
146     TRACE("(%p/%p)->(%p)\n", This, iface, lpLight);
147     if (TRACE_ON(ddraw)) {
148         TRACE("  Returning light definition : \n");
149         dump_light(&This->light);
150     }
151     memcpy(lpLight, &This->light, lpLight->dwSize);
152     return DD_OK;
153 }
154
155 /*******************************************************************************
156  *                              Light static functions
157  */
158
159 static void update(IDirect3DLightImpl* This) {
160     IDirect3DDeviceImpl* device;
161     if (!This->active_viewport||!This->active_viewport->active_device)
162         return;
163     device =  This->active_viewport->active_device;
164     IDirect3DDevice7_SetLight(ICOM_INTERFACE(device,IDirect3DDevice7),This->dwLightIndex,&(This->light7));
165 }
166
167 static void activate(IDirect3DLightImpl* This) {
168     IDirect3DLightGLImpl *glThis = (IDirect3DLightGLImpl *) This;
169
170     TRACE("(%p)\n", This);
171     
172     ENTER_GL();
173     update(This);
174     /* If was not active, activate it */
175     if ((glThis->parent.light.dwFlags & D3DLIGHT_ACTIVE) == 0) {
176         glEnable(glThis->light_num);
177         glThis->parent.light.dwFlags |= D3DLIGHT_ACTIVE;
178     }
179     LEAVE_GL();
180 }
181
182 static void desactivate(IDirect3DLightImpl* This) {
183     IDirect3DLightGLImpl *glThis = (IDirect3DLightGLImpl *) This;
184     
185     TRACE("(%p)\n", This);
186     
187     ENTER_GL();
188     /* If was not active, activate it */
189     if ((glThis->parent.light.dwFlags & D3DLIGHT_ACTIVE) != 0) {
190         glDisable(glThis->light_num);
191         glThis->parent.light.dwFlags &= ~D3DLIGHT_ACTIVE;
192     }
193     LEAVE_GL();
194 }
195
196 ULONG WINAPI
197 GL_IDirect3DLightImpl_1_Release(LPDIRECT3DLIGHT iface)
198 {
199     ICOM_THIS_FROM(IDirect3DLightImpl, IDirect3DLight, iface);
200     IDirect3DLightGLImpl *glThis = (IDirect3DLightGLImpl *) This;
201     ULONG ref = InterlockedDecrement(&This->ref);
202     
203     TRACE("(%p/%p)->() decrementing from %lu.\n", This, iface, ref + 1);
204
205     if (!ref) {
206         ((IDirect3DGLImpl *) This->d3d->d3d_private)->light_released(This->d3d, glThis->light_num);
207         HeapFree(GetProcessHeap(), 0, This);
208         return 0;
209     }
210     return ref;
211 }
212
213 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
214 # define XCAST(fun)     (typeof(VTABLE_IDirect3DLight.fun))
215 #else
216 # define XCAST(fun)     (void*)
217 #endif
218
219 static const IDirect3DLightVtbl VTABLE_IDirect3DLight =
220 {
221     XCAST(QueryInterface) Main_IDirect3DLightImpl_1_QueryInterface,
222     XCAST(AddRef) Main_IDirect3DLightImpl_1_AddRef,
223     XCAST(Release) GL_IDirect3DLightImpl_1_Release,
224     XCAST(Initialize) Main_IDirect3DLightImpl_1_Initialize,
225     XCAST(SetLight) Main_IDirect3DLightImpl_1_SetLight,
226     XCAST(GetLight) Main_IDirect3DLightImpl_1_GetLight,
227 };
228
229 #if !defined(__STRICT_ANSI__) && defined(__GNUC__)
230 #undef XCAST
231 #endif
232
233
234
235
236 HRESULT d3dlight_create(IDirect3DLightImpl **obj, IDirectDrawImpl *d3d, GLenum light_num)
237 {
238     IDirect3DLightImpl *object;
239     IDirect3DLightGLImpl *gl_object;
240     
241     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirect3DLightGLImpl));
242     if (object == NULL) return DDERR_OUTOFMEMORY;
243     gl_object = (IDirect3DLightGLImpl *) object;
244     
245     object->ref = 1;
246     object->d3d = d3d;
247     object->next = NULL;
248     object->activate = activate;
249     object->desactivate = desactivate;
250     object->update = update;
251     object->active_viewport = NULL;
252     gl_object->light_num = light_num;
253     
254     ICOM_INIT_INTERFACE(object, IDirect3DLight, VTABLE_IDirect3DLight);
255
256     *obj = object;
257
258     TRACE(" creating implementation at %p.\n", *obj);
259     
260     return D3D_OK;
261 }