ReadConsoleA: use ir as pointer to (req+1).
[wine] / graphics / d3dlight.c
1 /* Direct3D Light
2    (c) 1998 Lionel ULMER
3    
4    This files contains the implementation of Direct3DLight. */
5
6
7 #include "config.h"
8 #include "windef.h"
9 #include "winerror.h"
10 #include "wine/obj_base.h"
11 #include "heap.h"
12 #include "ddraw.h"
13 #include "d3d.h"
14 #include "debugtools.h"
15
16 #include "d3d_private.h"
17
18 DEFAULT_DEBUG_CHANNEL(ddraw)
19
20 #ifdef HAVE_MESAGL
21
22 static ICOM_VTABLE(IDirect3DLight) light_vtable;
23
24 enum {
25   D3D_1,
26   D3D_2
27 };
28
29 /*******************************************************************************
30  *                              Light static functions
31  */
32 static const float zero_value[] = {
33   0.0, 0.0, 0.0, 0.0
34 };
35
36 static void update(IDirect3DLightImpl* This) {
37   switch (This->light.dltType) {
38   case D3DLIGHT_POINT:         /* 1 */
39     TRACE("Activating POINT\n");
40     break;
41     
42   case D3DLIGHT_SPOT:          /* 2 */
43     TRACE("Activating SPOT\n");
44     break;
45     
46   case D3DLIGHT_DIRECTIONAL: {  /* 3 */
47     float direction[4];
48     
49     TRACE("Activating DIRECTIONAL\n");
50     TRACE("  direction : %f %f %f\n",
51           This->light.dvDirection.x.x,
52           This->light.dvDirection.y.y,
53           This->light.dvDirection.z.z);
54     _dump_colorvalue(" color    ", This->light.dcvColor);
55
56     glLightfv(This->light_num,
57               GL_AMBIENT,
58               (float *) zero_value);
59
60     glLightfv(This->light_num,
61               GL_DIFFUSE,
62               (float *) &(This->light.dcvColor));
63
64     direction[0] = -This->light.dvDirection.x.x;
65     direction[1] = -This->light.dvDirection.y.y;
66     direction[2] = -This->light.dvDirection.z.z;
67     direction[3] = 0.0; /* This is a directional light */
68     glLightfv(This->light_num,
69               GL_POSITION,
70               (float *) direction);
71   } break;
72     
73   case D3DLIGHT_PARALLELPOINT:  /* 4 */
74     TRACE("Activating PARRALLEL-POINT\n");
75     break;
76
77   default:
78     TRACE("Not a know Light Type\n");
79     break;
80   }
81 }
82
83 static void activate(IDirect3DLightImpl* This) {
84   ENTER_GL();
85   update(This);
86
87   /* If was not active, activate it */
88   if (This->is_active == 0) {
89     glEnable(This->light_num);
90
91     This->is_active = 1;
92   }
93   LEAVE_GL();
94
95   return ;
96 }
97
98 /*******************************************************************************
99  *                              Light Creation functions
100  */
101 LPDIRECT3DLIGHT d3dlight_create(IDirect3D2Impl* d3d2)
102 {
103   IDirect3DLightImpl* light;
104   
105   light = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDirect3DLightImpl));
106   light->ref = 1;
107   light->lpvtbl = &light_vtable;
108   light->d3d.d3d2 = d3d2;
109   light->type = D3D_2;
110
111   light->next = NULL;
112   light->prev = NULL;
113   light->activate = activate;
114   light->is_active = 0;
115   
116   return (LPDIRECT3DLIGHT)light;
117 }
118
119 LPDIRECT3DLIGHT d3dlight_create_dx3(IDirect3DImpl* d3d1)
120 {
121   IDirect3DLightImpl* light;
122   
123   light = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(IDirect3DLightImpl));
124   light->ref = 1;
125   light->lpvtbl = &light_vtable;
126   
127   light->d3d.d3d1 = d3d1;
128   light->type = D3D_1;
129   
130   light->next = NULL;
131   light->prev = NULL;
132   light->activate = activate;
133   light->is_active = 0;
134   
135   return (LPDIRECT3DLIGHT)light;
136 }
137
138 /*******************************************************************************
139  *                              IDirect3DLight methods
140  */
141
142 static HRESULT WINAPI IDirect3DLightImpl_QueryInterface(LPDIRECT3DLIGHT iface,
143                                                     REFIID riid,
144                                                     LPVOID* ppvObj)
145 {
146   ICOM_THIS(IDirect3DLightImpl,iface);
147   char xrefiid[50];
148   
149   WINE_StringFromCLSID((LPCLSID)riid,xrefiid);
150   FIXME("(%p)->(%s,%p): stub\n", This, xrefiid,ppvObj);
151   
152   return S_OK;
153 }
154
155
156
157 static ULONG WINAPI IDirect3DLightImpl_AddRef(LPDIRECT3DLIGHT iface)
158 {
159   ICOM_THIS(IDirect3DLightImpl,iface);
160   TRACE("(%p)->()incrementing from %lu.\n", This, This->ref );
161   
162   return ++(This->ref);
163 }
164
165
166
167 static ULONG WINAPI IDirect3DLightImpl_Release(LPDIRECT3DLIGHT iface)
168 {
169   ICOM_THIS(IDirect3DLightImpl,iface);
170   FIXME("(%p)->() decrementing from %lu.\n", This, This->ref );
171   
172   if (!--(This->ref)) {
173     HeapFree(GetProcessHeap(),0,This);
174     return 0;
175   }
176   
177   return This->ref;
178 }
179
180 /*** IDirect3DLight methods ***/
181 static void dump_light(LPD3DLIGHT light)
182 {
183   fprintf(stderr, "  dwSize : %ld\n", light->dwSize);
184 }
185
186 static HRESULT WINAPI IDirect3DLightImpl_GetLight(LPDIRECT3DLIGHT iface,
187                                               LPD3DLIGHT lpLight)
188 {
189   ICOM_THIS(IDirect3DLightImpl,iface);
190   TRACE("(%p)->(%p)\n", This, lpLight);
191   if (TRACE_ON(ddraw))
192     dump_light(lpLight);
193   
194   /* Copies the light structure */
195   switch (This->type) {
196   case D3D_1:
197     *((LPD3DLIGHT)lpLight) = *((LPD3DLIGHT) &(This->light));
198     break;
199   case D3D_2:
200     *((LPD3DLIGHT2)lpLight) = *((LPD3DLIGHT2) &(This->light));
201     break;
202   }
203   
204   return DD_OK;
205 }
206
207 static HRESULT WINAPI IDirect3DLightImpl_SetLight(LPDIRECT3DLIGHT iface,
208                                               LPD3DLIGHT lpLight)
209 {
210   ICOM_THIS(IDirect3DLightImpl,iface);
211   TRACE("(%p)->(%p)\n", This, lpLight);
212   if (TRACE_ON(ddraw))
213     dump_light(lpLight);
214   
215   /* Stores the light */
216   switch (This->type) {
217   case D3D_1:
218     *((LPD3DLIGHT) &(This->light)) = *((LPD3DLIGHT)lpLight);
219     break;
220   case D3D_2:
221     *((LPD3DLIGHT2) &(This->light)) = *((LPD3DLIGHT2)lpLight);
222     break;
223   }
224   
225   ENTER_GL();
226   if (This->is_active)
227     update(This);
228   LEAVE_GL();
229   
230   return DD_OK;
231 }
232
233 static HRESULT WINAPI IDirect3DLightImpl_Initialize(LPDIRECT3DLIGHT iface,
234                                                 LPDIRECT3D lpDirect3D)
235
236 {
237   ICOM_THIS(IDirect3DLightImpl,iface);
238   TRACE("(%p)->(%p)\n", This, lpDirect3D);
239
240   return DDERR_ALREADYINITIALIZED;
241 }
242
243
244 /*******************************************************************************
245  *                              IDirect3DLight VTable
246  */
247 static ICOM_VTABLE(IDirect3DLight) light_vtable = 
248 {
249   ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
250   /*** IUnknown methods ***/
251   IDirect3DLightImpl_QueryInterface,
252   IDirect3DLightImpl_AddRef,
253   IDirect3DLightImpl_Release,
254   /*** IDirect3DLight methods ***/
255   IDirect3DLightImpl_Initialize,
256   IDirect3DLightImpl_SetLight,
257   IDirect3DLightImpl_GetLight
258 };
259
260 #else /* HAVE_MESAGL */
261
262 /* These function should never be called if MesaGL is not present */
263 LPDIRECT3DLIGHT d3dlight_create_dx3(IDirect3DImpl* d3d1) {
264   ERR("Should not be called...\n");
265   return NULL;
266 }
267
268 LPDIRECT3DLIGHT d3dlight_create(IDirect3D2Impl* d3d2) {
269   ERR("Should not be called...\n");
270   return NULL;
271 }
272
273 #endif /* HAVE_MESAGL */