msvcrt: Move btowc implementation to mbcs.c file.
[wine.git] / dlls / ddraw / light.c
bloba8c3a93f738063329d9cecb7b52ec32c33b22837
1 /* Direct3D Light
2 * Copyright (c) 1998 / 2002 Lionel ULMER
3 * Copyright (c) 2006 Stefan DÖSINGER
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "ddraw_private.h"
22 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
24 /*****************************************************************************
25 * light_update
27 * Updates the Direct3DDevice7 lighting parameters
29 *****************************************************************************/
30 static void light_update(struct d3d_light *light)
32 struct d3d_device *device;
34 TRACE("light %p.\n", light);
36 if (!light->active_viewport || !light->active_viewport->active_device) return;
37 device = light->active_viewport->active_device;
39 IDirect3DDevice7_SetLight(&device->IDirect3DDevice7_iface, light->active_light_index, &light->light7);
42 /*****************************************************************************
43 * light_activate
45 * Uses the Direct3DDevice7::LightEnable method to active the light
47 *****************************************************************************/
48 void light_activate(struct d3d_light *light)
50 struct d3d_device *device;
52 TRACE("light %p.\n", light);
54 if (!light->active_viewport || !light->active_viewport->active_device
55 || light->active_viewport->active_device->current_viewport != light->active_viewport)
56 return;
57 device = light->active_viewport->active_device;
59 if (light->light.dwFlags & D3DLIGHT_ACTIVE)
61 viewport_alloc_active_light_index(light);
62 light_update(light);
63 IDirect3DDevice7_LightEnable(&device->IDirect3DDevice7_iface, light->active_light_index, TRUE);
67 /*****************************************************************************
69 * light_deactivate
71 * Uses the Direct3DDevice7::LightEnable method to deactivate the light
73 *****************************************************************************/
74 void light_deactivate(struct d3d_light *light)
76 struct d3d_device *device;
78 TRACE("light %p.\n", light);
80 if (!light->active_viewport || !light->active_viewport->active_device
81 || light->active_viewport->active_device->current_viewport != light->active_viewport)
83 assert(!light->active_light_index);
84 return;
87 device = light->active_viewport->active_device;
88 if (light->active_light_index)
90 IDirect3DDevice7_LightEnable(&device->IDirect3DDevice7_iface, light->active_light_index, FALSE);
91 viewport_free_active_light_index(light);
95 static inline struct d3d_light *impl_from_IDirect3DLight(IDirect3DLight *iface)
97 return CONTAINING_RECORD(iface, struct d3d_light, IDirect3DLight_iface);
100 /*****************************************************************************
101 * IDirect3DLight::QueryInterface
103 * Queries the object for different interfaces. Unimplemented for this
104 * object at the moment
106 * Params:
107 * riid: Interface id asked for
108 * obj: Address to return the resulting pointer at.
110 * Returns:
111 * E_NOINTERFACE, because it's a stub
112 *****************************************************************************/
113 static HRESULT WINAPI d3d_light_QueryInterface(IDirect3DLight *iface, REFIID riid, void **object)
115 FIXME("iface %p, riid %s, object %p stub!\n", iface, debugstr_guid(riid), object);
117 *object = NULL;
118 return E_NOINTERFACE;
121 static ULONG WINAPI d3d_light_AddRef(IDirect3DLight *iface)
123 struct d3d_light *light = impl_from_IDirect3DLight(iface);
124 ULONG ref = InterlockedIncrement(&light->ref);
126 TRACE("%p increasing refcount to %u.\n", light, ref);
128 return ref;
131 static ULONG WINAPI d3d_light_Release(IDirect3DLight *iface)
133 struct d3d_light *light = impl_from_IDirect3DLight(iface);
134 ULONG ref = InterlockedDecrement(&light->ref);
136 TRACE("%p decreasing refcount to %u.\n", light, ref);
138 if (!ref)
140 heap_free(light);
141 return 0;
143 return ref;
146 /*****************************************************************************
147 * IDirect3DLight Methods.
148 *****************************************************************************/
150 /*****************************************************************************
151 * IDirect3DLight::Initialize
153 * Initializes the interface. This implementation is a no-op, because
154 * initialization takes place at creation time
156 * Params:
157 * Direct3D: Pointer to an IDirect3D interface.
159 * Returns:
160 * D3D_OK
162 *****************************************************************************/
163 static HRESULT WINAPI d3d_light_Initialize(IDirect3DLight *iface, IDirect3D *d3d)
165 TRACE("iface %p, d3d %p.\n", iface, d3d);
167 return D3D_OK;
170 static HRESULT WINAPI d3d_light_SetLight(IDirect3DLight *iface, D3DLIGHT *data)
172 static const D3DCOLORVALUE zero_value = {{0.0f}, {0.0f}, {0.0f}, {0.0f}};
173 struct d3d_light *light = impl_from_IDirect3DLight(iface);
174 DWORD flags = data->dwSize >= sizeof(D3DLIGHT2) ? ((D3DLIGHT2 *)data)->dwFlags : D3DLIGHT_ACTIVE;
175 D3DLIGHT7 *light7 = &light->light7;
177 TRACE("iface %p, data %p.\n", iface, data);
179 if ((!data->dltType) || (data->dltType > D3DLIGHT_PARALLELPOINT))
180 return DDERR_INVALIDPARAMS;
182 /* Translate D3DLIGHT2 structure to D3DLIGHT7. */
183 light7->dltType = data->dltType;
184 light7->dcvDiffuse = data->dcvColor;
185 if (flags & D3DLIGHT_NO_SPECULAR)
186 light7->dcvSpecular = zero_value;
187 else
188 light7->dcvSpecular = data->dcvColor;
189 light7->dcvAmbient = zero_value;
190 light7->dvPosition = data->dvPosition;
191 light7->dvDirection = data->dvDirection;
192 light7->dvRange = data->dvRange;
193 light7->dvFalloff = data->dvFalloff;
194 light7->dvAttenuation0 = data->dvAttenuation0;
195 light7->dvAttenuation1 = data->dvAttenuation1;
196 light7->dvAttenuation2 = data->dvAttenuation2;
197 light7->dvTheta = data->dvTheta;
198 light7->dvPhi = data->dvPhi;
200 wined3d_mutex_lock();
201 memcpy(&light->light, data, sizeof(*data));
203 if (!(flags & D3DLIGHT_ACTIVE))
204 light_deactivate(light);
206 light->light.dwFlags = flags;
207 light_activate(light);
208 wined3d_mutex_unlock();
210 return D3D_OK;
213 /*****************************************************************************
214 * IDirect3DLight::GetLight
216 * Returns the parameters currently assigned to the IDirect3DLight object
218 * Params:
219 * Light: Pointer to an D3DLIGHT structure to store the parameters
221 * Returns:
222 * D3D_OK on success
223 * DDERR_INVALIDPARAMS if Light is NULL
224 *****************************************************************************/
225 static HRESULT WINAPI d3d_light_GetLight(IDirect3DLight *iface, D3DLIGHT *lpLight)
227 struct d3d_light *light = impl_from_IDirect3DLight(iface);
229 TRACE("iface %p, light %p.\n", iface, lpLight);
231 wined3d_mutex_lock();
232 memcpy(lpLight, &light->light, lpLight->dwSize);
233 wined3d_mutex_unlock();
235 return DD_OK;
238 static const struct IDirect3DLightVtbl d3d_light_vtbl =
240 /*** IUnknown Methods ***/
241 d3d_light_QueryInterface,
242 d3d_light_AddRef,
243 d3d_light_Release,
244 /*** IDirect3DLight Methods ***/
245 d3d_light_Initialize,
246 d3d_light_SetLight,
247 d3d_light_GetLight
250 void d3d_light_init(struct d3d_light *light, struct ddraw *ddraw)
252 light->IDirect3DLight_iface.lpVtbl = &d3d_light_vtbl;
253 light->ref = 1;
254 light->ddraw = ddraw;
257 struct d3d_light *unsafe_impl_from_IDirect3DLight(IDirect3DLight *iface)
259 if (!iface)
260 return NULL;
261 assert(iface->lpVtbl == &d3d_light_vtbl);
263 return impl_from_IDirect3DLight(iface);