ddraw: Acquire/release the focus window from the correct location.
[wine] / dlls / ddraw / viewport.c
1 /* Direct3D Viewport
2  * Copyright (c) 1998 Lionel ULMER
3  * Copyright (c) 2006-2007 Stefan DÖSINGER
4  *
5  * This file contains the implementation of Direct3DViewport2.
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 "winerror.h"
36 #include "wingdi.h"
37 #include "wine/exception.h"
38
39 #include "ddraw.h"
40 #include "d3d.h"
41
42 #include "ddraw_private.h"
43 #include "wine/debug.h"
44
45 WINE_DEFAULT_DEBUG_CHANNEL(d3d7);
46
47 /*****************************************************************************
48  * Helper functions
49  *****************************************************************************/
50
51 /*****************************************************************************
52  * viewport_activate
53  *
54  * activates the viewport using IDirect3DDevice7::SetViewport
55  *
56  *****************************************************************************/
57 void viewport_activate(IDirect3DViewportImpl* This, BOOL ignore_lights) {
58     IDirect3DLightImpl* light;
59     D3DVIEWPORT7 vp;
60
61     if (!ignore_lights) {
62         /* Activate all the lights associated with this context */
63         light = This->lights;
64
65         while (light != NULL) {
66             light->activate(light);
67             light = light->next;
68         }
69     }
70
71     /* And copy the values in the structure used by the device */
72     if (This->use_vp2) {
73         vp.dwX = This->viewports.vp2.dwX;
74         vp.dwY = This->viewports.vp2.dwY;
75         vp.dwHeight = This->viewports.vp2.dwHeight;
76         vp.dwWidth = This->viewports.vp2.dwWidth;
77         vp.dvMinZ = This->viewports.vp2.dvMinZ;
78         vp.dvMaxZ = This->viewports.vp2.dvMaxZ;
79     } else {
80         vp.dwX = This->viewports.vp1.dwX;
81         vp.dwY = This->viewports.vp1.dwY;
82         vp.dwHeight = This->viewports.vp1.dwHeight;
83         vp.dwWidth = This->viewports.vp1.dwWidth;
84         vp.dvMinZ = This->viewports.vp1.dvMinZ;
85         vp.dvMaxZ = This->viewports.vp1.dvMaxZ;
86     }
87
88     /* And also set the viewport */
89     IDirect3DDevice7_SetViewport((IDirect3DDevice7 *)This->active_device, &vp);
90 }
91
92 /*****************************************************************************
93  * _dump_D3DVIEWPORT, _dump_D3DVIEWPORT2
94  *
95  * Writes viewport information to TRACE
96  *
97  *****************************************************************************/
98 static void _dump_D3DVIEWPORT(const D3DVIEWPORT *lpvp)
99 {
100     TRACE("    - dwSize = %d   dwX = %d   dwY = %d\n",
101           lpvp->dwSize, lpvp->dwX, lpvp->dwY);
102     TRACE("    - dwWidth = %d   dwHeight = %d\n",
103           lpvp->dwWidth, lpvp->dwHeight);
104     TRACE("    - dvScaleX = %f   dvScaleY = %f\n",
105           lpvp->dvScaleX, lpvp->dvScaleY);
106     TRACE("    - dvMaxX = %f   dvMaxY = %f\n",
107           lpvp->dvMaxX, lpvp->dvMaxY);
108     TRACE("    - dvMinZ = %f   dvMaxZ = %f\n",
109           lpvp->dvMinZ, lpvp->dvMaxZ);
110 }
111
112 static void _dump_D3DVIEWPORT2(const D3DVIEWPORT2 *lpvp)
113 {
114     TRACE("    - dwSize = %d   dwX = %d   dwY = %d\n",
115           lpvp->dwSize, lpvp->dwX, lpvp->dwY);
116     TRACE("    - dwWidth = %d   dwHeight = %d\n",
117           lpvp->dwWidth, lpvp->dwHeight);
118     TRACE("    - dvClipX = %f   dvClipY = %f\n",
119           lpvp->dvClipX, lpvp->dvClipY);
120     TRACE("    - dvClipWidth = %f   dvClipHeight = %f\n",
121           lpvp->dvClipWidth, lpvp->dvClipHeight);
122     TRACE("    - dvMinZ = %f   dvMaxZ = %f\n",
123           lpvp->dvMinZ, lpvp->dvMaxZ);
124 }
125
126 /*****************************************************************************
127  * IUnknown Methods.
128  *****************************************************************************/
129
130 /*****************************************************************************
131  * IDirect3DViewport3::QueryInterface
132  *
133  * A normal QueryInterface. Can query all interface versions and the
134  * IUnknown interface. The VTables of the different versions
135  * are equal
136  *
137  * Params:
138  *  refiid: Interface id queried for
139  *  obj: Address to write the interface pointer to
140  *
141  * Returns:
142  *  S_OK on success.
143  *  E_NOINTERFACE if the requested interface wasn't found
144  *
145  *****************************************************************************/
146 static HRESULT WINAPI
147 IDirect3DViewportImpl_QueryInterface(IDirect3DViewport3 *iface,
148                                      REFIID riid,
149                                      void **obp)
150 {
151     TRACE("(%p)->(%s,%p)\n", iface, debugstr_guid(riid), obp);
152
153     *obp = NULL;
154
155     if ( IsEqualGUID(&IID_IUnknown,  riid) ||
156          IsEqualGUID(&IID_IDirect3DViewport, riid) ||
157          IsEqualGUID(&IID_IDirect3DViewport2, riid) ||
158          IsEqualGUID(&IID_IDirect3DViewport3, riid) ) {
159         IDirect3DViewport3_AddRef(iface);
160         *obp = iface;
161         TRACE("  Creating IDirect3DViewport1/2/3 interface %p\n", *obp);
162         return S_OK;
163     }
164     FIXME("(%p): interface for IID %s NOT found!\n", iface, debugstr_guid(riid));
165     return E_NOINTERFACE;
166 }
167
168 /*****************************************************************************
169  * IDirect3DViewport3::AddRef
170  *
171  * Increases the refcount.
172  *
173  * Returns:
174  *  The new refcount
175  *
176  *****************************************************************************/
177 static ULONG WINAPI
178 IDirect3DViewportImpl_AddRef(IDirect3DViewport3 *iface)
179 {
180     IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
181     ULONG ref = InterlockedIncrement(&This->ref);
182
183     TRACE("(%p)->() incrementing from %u.\n", This, ref - 1);
184
185     return ref;
186 }
187
188 /*****************************************************************************
189  * IDirect3DViewport3::Release
190  *
191  * Reduces the refcount. If it falls to 0, the interface is released
192  *
193  * Returns:
194  *  The new refcount
195  *
196  *****************************************************************************/
197 static ULONG WINAPI
198 IDirect3DViewportImpl_Release(IDirect3DViewport3 *iface)
199 {
200     IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
201     ULONG ref = InterlockedDecrement(&This->ref);
202
203     TRACE("(%p)->() decrementing from %u.\n", This, ref + 1);
204
205     if (!ref) {
206         HeapFree(GetProcessHeap(), 0, This);
207         return 0;
208     }
209     return ref;
210 }
211
212 /*****************************************************************************
213  * IDirect3DViewport Methods.
214  *****************************************************************************/
215
216 /*****************************************************************************
217  * IDirect3DViewport3::Initialize
218  *
219  * No-op initialization.
220  *
221  * Params:
222  *  Direct3D: The direct3D device this viewport is assigned to
223  *
224  * Returns:
225  *  DDERR_ALREADYINITIALIZED
226  *
227  *****************************************************************************/
228 static HRESULT WINAPI
229 IDirect3DViewportImpl_Initialize(IDirect3DViewport3 *iface,
230                                  IDirect3D *Direct3D)
231 {
232     TRACE("(%p)->(%p) no-op...\n", iface, Direct3D);
233     return DDERR_ALREADYINITIALIZED;
234 }
235
236 /*****************************************************************************
237  * IDirect3DViewport3::GetViewport
238  *
239  * Returns the viewport data assigned to this viewport interface
240  *
241  * Params:
242  *  Data: Address to store the data
243  *
244  * Returns:
245  *  D3D_OK on success
246  *  DDERR_INVALIDPARAMS if Data is NULL
247  *
248  *****************************************************************************/
249 static HRESULT WINAPI
250 IDirect3DViewportImpl_GetViewport(IDirect3DViewport3 *iface,
251                                   D3DVIEWPORT *lpData)
252 {
253     IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
254     DWORD dwSize;
255     TRACE("(%p/%p)->(%p)\n", This, iface, lpData);
256
257     EnterCriticalSection(&ddraw_cs);
258     dwSize = lpData->dwSize;
259     memset(lpData, 0, dwSize);
260     if (!This->use_vp2)
261         memcpy(lpData, &(This->viewports.vp1), dwSize);
262     else {
263         D3DVIEWPORT vp1;
264         vp1.dwSize = sizeof(vp1);
265         vp1.dwX = This->viewports.vp2.dwX;
266         vp1.dwY = This->viewports.vp2.dwY;
267         vp1.dwWidth = This->viewports.vp2.dwWidth;
268         vp1.dwHeight = This->viewports.vp2.dwHeight;
269         vp1.dvMaxX = 0.0;
270         vp1.dvMaxY = 0.0;
271         vp1.dvScaleX = 0.0;
272         vp1.dvScaleY = 0.0;
273         vp1.dvMinZ = This->viewports.vp2.dvMinZ;
274         vp1.dvMaxZ = This->viewports.vp2.dvMaxZ;
275         memcpy(lpData, &vp1, dwSize);
276     }
277
278     if (TRACE_ON(d3d7)) {
279         TRACE("  returning D3DVIEWPORT :\n");
280         _dump_D3DVIEWPORT(lpData);
281     }
282     LeaveCriticalSection(&ddraw_cs);
283
284     return DD_OK;
285 }
286
287 /*****************************************************************************
288  * IDirect3DViewport3::SetViewport
289  *
290  * Sets the viewport information for this interface
291  *
292  * Params:
293  *  lpData: Viewport to set
294  *
295  * Returns:
296  *  D3D_OK on success
297  *  DDERR_INVALIDPARAMS if Data is NULL
298  *
299  *****************************************************************************/
300 static HRESULT WINAPI
301 IDirect3DViewportImpl_SetViewport(IDirect3DViewport3 *iface,
302                                   D3DVIEWPORT *lpData)
303 {
304     IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
305     LPDIRECT3DVIEWPORT3 current_viewport;
306     TRACE("(%p/%p)->(%p)\n", This, iface, lpData);
307
308     if (TRACE_ON(d3d7)) {
309         TRACE("  getting D3DVIEWPORT :\n");
310         _dump_D3DVIEWPORT(lpData);
311     }
312
313     EnterCriticalSection(&ddraw_cs);
314     This->use_vp2 = 0;
315     memset(&(This->viewports.vp1), 0, sizeof(This->viewports.vp1));
316     memcpy(&(This->viewports.vp1), lpData, lpData->dwSize);
317
318     /* Tests on two games show that these values are never used properly so override
319        them with proper ones :-)
320     */
321     This->viewports.vp1.dvMinZ = 0.0;
322     This->viewports.vp1.dvMaxZ = 1.0;
323
324     if (This->active_device) {
325         IDirect3DDevice3 *d3d_device3 = (IDirect3DDevice3 *)&This->active_device->IDirect3DDevice3_vtbl;
326         IDirect3DDevice3_GetCurrentViewport(d3d_device3, &current_viewport);
327         if (current_viewport) {
328             if ((IDirect3DViewportImpl *)current_viewport == This) This->activate(This, FALSE);
329             IDirect3DViewport3_Release(current_viewport);
330         }
331     }
332     LeaveCriticalSection(&ddraw_cs);
333
334     return DD_OK;
335 }
336
337 /*****************************************************************************
338  * IDirect3DViewport3::TransformVertices
339  *
340  * Transforms vertices by the transformation matrix.
341  *
342  * This function is pretty similar to IDirect3DVertexBuffer7::ProcessVertices,
343  * so it's tempting to forward it to there. However, there are some
344  * tiny differences. First, the lpOffscreen flag that is reported back,
345  * then there is the homogeneous vertex that is generated. Also there's a lack
346  * of FVFs, but still a custom stride. Last, the d3d1 - d3d3 viewport has some
347  * settings (scale) that d3d7 and wined3d do not have. All in all wrapping to
348  * ProcessVertices doesn't pay of in terms of wrapper code needed and code
349  * reused.
350  *
351  * Params:
352  *  dwVertexCount: The number of vertices to be transformed
353  *  lpData: Pointer to the vertex data
354  *  dwFlags: D3DTRANSFORM_CLIPPED or D3DTRANSFORM_UNCLIPPED
355  *  lpOffScreen: Set to the clipping plane clipping the vertex, if only one
356  *               vertex is transformed and clipping is on. 0 otherwise
357  *
358  * Returns:
359  *  D3D_OK on success
360  *  D3DERR_VIEWPORTHASNODEVICE if the viewport is not assigned to a device
361  *  DDERR_INVALIDPARAMS if no clipping flag is specified
362  *
363  *****************************************************************************/
364 static HRESULT WINAPI
365 IDirect3DViewportImpl_TransformVertices(IDirect3DViewport3 *iface,
366                                         DWORD dwVertexCount,
367                                         D3DTRANSFORMDATA *lpData,
368                                         DWORD dwFlags,
369                                         DWORD *lpOffScreen)
370 {
371     IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
372     D3DMATRIX view_mat, world_mat, proj_mat, mat;
373     float *in;
374     float *out;
375     float x, y, z, w;
376     unsigned int i;
377     D3DVIEWPORT vp = This->viewports.vp1;
378     D3DHVERTEX *outH;
379     TRACE("(%p)->(%08x,%p,%08x,%p)\n", This, dwVertexCount, lpData, dwFlags, lpOffScreen);
380
381     /* Tests on windows show that Windows crashes when this occurs,
382      * so don't return the (intuitive) return value
383     if(!This->active_device)
384     {
385         WARN("No device active, returning D3DERR_VIEWPORTHASNODEVICE\n");
386         return D3DERR_VIEWPORTHASNODEVICE;
387     }
388      */
389
390     if(!(dwFlags & (D3DTRANSFORM_UNCLIPPED | D3DTRANSFORM_CLIPPED)))
391     {
392         WARN("No clipping flag passed, returning DDERR_INVALIDPARAMS\n");
393         return DDERR_INVALIDPARAMS;
394     }
395
396
397     EnterCriticalSection(&ddraw_cs);
398     IWineD3DDevice_GetTransform(This->active_device->wineD3DDevice,
399                                 D3DTRANSFORMSTATE_VIEW,
400                                 (WINED3DMATRIX*) &view_mat);
401
402     IWineD3DDevice_GetTransform(This->active_device->wineD3DDevice,
403                                 D3DTRANSFORMSTATE_PROJECTION,
404                                 (WINED3DMATRIX*) &proj_mat);
405
406     IWineD3DDevice_GetTransform(This->active_device->wineD3DDevice,
407                                 WINED3DTS_WORLDMATRIX(0),
408                                 (WINED3DMATRIX*) &world_mat);
409     multiply_matrix(&mat,&view_mat,&world_mat);
410     multiply_matrix(&mat,&proj_mat,&mat);
411
412     in = lpData->lpIn;
413     out = lpData->lpOut;
414     outH = lpData->lpHOut;
415     for(i = 0; i < dwVertexCount; i++)
416     {
417         x = (in[0] * mat._11) + (in[1] * mat._21) + (in[2] * mat._31) + (1.0 * mat._41);
418         y = (in[0] * mat._12) + (in[1] * mat._22) + (in[2] * mat._32) + (1.0 * mat._42);
419         z = (in[0] * mat._13) + (in[1] * mat._23) + (in[2] * mat._33) + (1.0 * mat._43);
420         w = (in[0] * mat._14) + (in[1] * mat._24) + (in[2] * mat._34) + (1.0 * mat._44);
421
422         if(dwFlags & D3DTRANSFORM_CLIPPED)
423         {
424             /* If clipping is enabled, Windows assumes that outH is
425              * a valid pointer
426              */
427             outH[i].u1.hx = x; outH[i].u2.hy = y; outH[i].u3.hz = z;
428
429             outH[i].dwFlags = 0;
430             if(x * vp.dvScaleX > ((float) vp.dwWidth * 0.5))
431                 outH[i].dwFlags |= D3DCLIP_RIGHT;
432             if(x * vp.dvScaleX <= -((float) vp.dwWidth) * 0.5)
433                 outH[i].dwFlags |= D3DCLIP_LEFT;
434             if(y * vp.dvScaleY > ((float) vp.dwHeight * 0.5))
435                 outH[i].dwFlags |= D3DCLIP_TOP;
436             if(y * vp.dvScaleY <= -((float) vp.dwHeight) * 0.5)
437                 outH[i].dwFlags |= D3DCLIP_BOTTOM;
438             if(z < 0.0)
439                 outH[i].dwFlags |= D3DCLIP_FRONT;
440             if(z > 1.0)
441                 outH[i].dwFlags |= D3DCLIP_BACK;
442
443             if(outH[i].dwFlags)
444             {
445                 /* Looks like native just drops the vertex, leaves whatever data
446                  * it has in the output buffer and goes on with the next vertex.
447                  * The exact scheme hasn't been figured out yet, but windows
448                  * definitely writes something there.
449                  */
450                 out[0] = x;
451                 out[1] = y;
452                 out[2] = z;
453                 out[3] = w;
454                 in = (float *) ((char *) in + lpData->dwInSize);
455                 out = (float *) ((char *) out + lpData->dwOutSize);
456                 continue;
457             }
458         }
459
460         w = 1 / w;
461         x *= w; y *= w; z *= w;
462
463         out[0] = vp.dwWidth / 2 + vp.dwX + x * vp.dvScaleX;
464         out[1] = vp.dwHeight / 2 + vp.dwY - y * vp.dvScaleY;
465         out[2] = z;
466         out[3] = w;
467         in = (float *) ((char *) in + lpData->dwInSize);
468         out = (float *) ((char *) out + lpData->dwOutSize);
469     }
470
471     /* According to the d3d test, the offscreen flag is set only
472      * if exactly one vertex is transformed. Its not documented,
473      * but the test shows that the lpOffscreen flag is set to the
474      * flag combination of clipping planes that clips the vertex.
475      *
476      * If clipping is requested, Windows assumes that the offscreen
477      * param is a valid pointer.
478      */
479     if(dwVertexCount == 1 && dwFlags & D3DTRANSFORM_CLIPPED)
480     {
481         *lpOffScreen = outH[0].dwFlags;
482     }
483     else if(*lpOffScreen)
484     {
485         *lpOffScreen = 0;
486     }
487     LeaveCriticalSection(&ddraw_cs);
488
489     TRACE("All done\n");
490     return DD_OK;
491 }
492
493 /*****************************************************************************
494  * IDirect3DViewport3::LightElements
495  *
496  * The DirectX 5.0 sdk says that it's not implemented
497  *
498  * Params:
499  *  ?
500  *
501  * Returns:
502  *  DDERR_UNSUPPORTED
503  *
504  *****************************************************************************/
505 static HRESULT WINAPI
506 IDirect3DViewportImpl_LightElements(IDirect3DViewport3 *iface,
507                                     DWORD dwElementCount,
508                                     LPD3DLIGHTDATA lpData)
509 {
510     IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
511     TRACE("(%p)->(%08x,%p): Unimplemented!\n", This, dwElementCount, lpData);
512     return DDERR_UNSUPPORTED;
513 }
514
515 /*****************************************************************************
516  * IDirect3DViewport3::SetBackground
517  *
518  * Sets tje background material
519  *
520  * Params:
521  *  hMat: Handle from a IDirect3DMaterial interface
522  *
523  * Returns:
524  *  D3D_OK on success
525  *
526  *****************************************************************************/
527 static HRESULT WINAPI
528 IDirect3DViewportImpl_SetBackground(IDirect3DViewport3 *iface,
529                                     D3DMATERIALHANDLE hMat)
530 {
531     IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
532     TRACE("(%p)->(%d)\n", This, hMat);
533
534     EnterCriticalSection(&ddraw_cs);
535     if(hMat && hMat > This->ddraw->d3ddevice->numHandles)
536     {
537         WARN("Specified Handle %d out of range\n", hMat);
538         LeaveCriticalSection(&ddraw_cs);
539         return DDERR_INVALIDPARAMS;
540     }
541     else if(hMat && This->ddraw->d3ddevice->Handles[hMat - 1].type != DDrawHandle_Material)
542     {
543         WARN("Handle %d is not a material handle\n", hMat);
544         LeaveCriticalSection(&ddraw_cs);
545         return DDERR_INVALIDPARAMS;
546     }
547
548     if(hMat)
549     {
550         This->background = This->ddraw->d3ddevice->Handles[hMat - 1].ptr;
551         TRACE(" setting background color : %f %f %f %f\n",
552               This->background->mat.u.diffuse.u1.r,
553               This->background->mat.u.diffuse.u2.g,
554               This->background->mat.u.diffuse.u3.b,
555               This->background->mat.u.diffuse.u4.a);
556     }
557     else
558     {
559         This->background = NULL;
560         TRACE("Setting background to NULL\n");
561     }
562
563     LeaveCriticalSection(&ddraw_cs);
564     return D3D_OK;
565 }
566
567 /*****************************************************************************
568  * IDirect3DViewport3::GetBackground
569  *
570  * Returns the material handle assigned to the background of the viewport
571  *
572  * Params:
573  *  lphMat: Address to store the handle
574  *  lpValid: is set to FALSE if no background is set, TRUE if one is set
575  *
576  * Returns:
577  *  D3D_OK
578  *
579  *****************************************************************************/
580 static HRESULT WINAPI
581 IDirect3DViewportImpl_GetBackground(IDirect3DViewport3 *iface,
582                                     D3DMATERIALHANDLE *lphMat,
583                                     BOOL *lpValid)
584 {
585     IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
586     TRACE("(%p)->(%p,%p)\n", This, lphMat, lpValid);
587
588     EnterCriticalSection(&ddraw_cs);
589     if(lpValid)
590     {
591         *lpValid = This->background != NULL;
592     }
593     if(lphMat)
594     {
595         if(This->background)
596         {
597             *lphMat = This->background->Handle;
598         }
599         else
600         {
601             *lphMat = 0;
602         }
603     }
604     LeaveCriticalSection(&ddraw_cs);
605
606     return D3D_OK;
607 }
608
609 /*****************************************************************************
610  * IDirect3DViewport3::SetBackgroundDepth
611  *
612  * Sets a surface that represents the background depth. It's contents are
613  * used to set the depth buffer in IDirect3DViewport3::Clear
614  *
615  * Params:
616  *  lpDDSurface: Surface to set
617  *
618  * Returns: D3D_OK, because it's a stub
619  *
620  *****************************************************************************/
621 static HRESULT WINAPI
622 IDirect3DViewportImpl_SetBackgroundDepth(IDirect3DViewport3 *iface,
623                                          IDirectDrawSurface *lpDDSurface)
624 {
625     IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
626     FIXME("(%p)->(%p): stub!\n", This, lpDDSurface);
627     return D3D_OK;
628 }
629
630 /*****************************************************************************
631  * IDirect3DViewport3::GetBackgroundDepth
632  *
633  * Returns the surface that represents the depth field
634  *
635  * Params:
636  *  lplpDDSurface: Address to store the interface pointer
637  *  lpValid: Set to TRUE if a depth is assigned, FALSE otherwise
638  *
639  * Returns:
640  *  D3D_OK, because it's a stub
641  *  (DDERR_INVALIDPARAMS if DDSurface of Valid is NULL)
642  *
643  *****************************************************************************/
644 static HRESULT WINAPI
645 IDirect3DViewportImpl_GetBackgroundDepth(IDirect3DViewport3 *iface,
646                                          IDirectDrawSurface **lplpDDSurface,
647                                          LPBOOL lpValid)
648 {
649     IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
650     FIXME("(%p)->(%p,%p): stub!\n", This, lplpDDSurface, lpValid);
651     return DD_OK;
652 }
653
654 /*****************************************************************************
655  * IDirect3DViewport3::Clear
656  *
657  * Clears the render target and / or the z buffer
658  *
659  * Params:
660  *  dwCount: The amount of rectangles to clear. If 0, the whole buffer is
661  *           cleared
662  *  lpRects: Pointer to the array of rectangles. If NULL, Count must be 0
663  *  dwFlags: D3DCLEAR_ZBUFFER and / or D3DCLEAR_TARGET
664  *
665  * Returns:
666  *  D3D_OK on success
667  *  D3DERR_VIEWPORTHASNODEVICE if there's no active device
668  *  The return value of IDirect3DDevice7::Clear
669  *
670  *****************************************************************************/
671 static HRESULT WINAPI IDirect3DViewportImpl_Clear(IDirect3DViewport3 *iface,
672         DWORD dwCount, D3DRECT *lpRects, DWORD dwFlags)
673 {
674     IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
675     DWORD color = 0x00000000;
676     HRESULT hr;
677     LPDIRECT3DVIEWPORT3 current_viewport;
678     IDirect3DDevice3 *d3d_device3;
679
680     TRACE("(%p/%p)->(%08x,%p,%08x)\n", This, iface, dwCount, lpRects, dwFlags);
681     if (This->active_device == NULL) {
682         ERR(" Trying to clear a viewport not attached to a device !\n");
683         return D3DERR_VIEWPORTHASNODEVICE;
684     }
685     d3d_device3 = (IDirect3DDevice3 *)&This->active_device->IDirect3DDevice3_vtbl;
686
687     EnterCriticalSection(&ddraw_cs);
688     if (dwFlags & D3DCLEAR_TARGET) {
689         if (This->background == NULL) {
690             ERR(" Trying to clear the color buffer without background material !\n");
691         }
692         else
693         {
694             color = ((int)((This->background->mat.u.diffuse.u1.r) * 255) << 16)
695                     | ((int) ((This->background->mat.u.diffuse.u2.g) * 255) <<  8)
696                     | ((int) ((This->background->mat.u.diffuse.u3.b) * 255) <<  0)
697                     | ((int) ((This->background->mat.u.diffuse.u4.a) * 255) << 24);
698         }
699     }
700
701     /* Need to temporarily activate viewport to clear it. Previously active one will be restored
702         afterwards. */
703     This->activate(This, TRUE);
704
705     hr = IDirect3DDevice7_Clear((IDirect3DDevice7 *)This->active_device, dwCount, lpRects,
706             dwFlags & (D3DCLEAR_ZBUFFER | D3DCLEAR_TARGET), color, 1.0, 0x00000000);
707
708     IDirect3DDevice3_GetCurrentViewport(d3d_device3, &current_viewport);
709     if(current_viewport) {
710         IDirect3DViewportImpl *vp = (IDirect3DViewportImpl *)current_viewport;
711         vp->activate(vp, TRUE);
712         IDirect3DViewport3_Release(current_viewport);
713     }
714
715     LeaveCriticalSection(&ddraw_cs);
716     return hr;
717 }
718
719 /*****************************************************************************
720  * IDirect3DViewport3::AddLight
721  *
722  * Adds an light to the viewport
723  *
724  * Params:
725  *  lpDirect3DLight: Interface of the light to add
726  *
727  * Returns:
728  *  D3D_OK on success
729  *  DDERR_INVALIDPARAMS if Direct3DLight is NULL
730  *  DDERR_INVALIDPARAMS if there are 8 lights or more
731  *
732  *****************************************************************************/
733 static HRESULT WINAPI
734 IDirect3DViewportImpl_AddLight(IDirect3DViewport3 *iface,
735                                IDirect3DLight *lpDirect3DLight)
736 {
737     IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
738     IDirect3DLightImpl *lpDirect3DLightImpl = (IDirect3DLightImpl *)lpDirect3DLight;
739     DWORD i = 0;
740     DWORD map = This->map_lights;
741
742     TRACE("(%p)->(%p)\n", This, lpDirect3DLight);
743
744     EnterCriticalSection(&ddraw_cs);
745     if (This->num_lights >= 8)
746     {
747         LeaveCriticalSection(&ddraw_cs);
748         return DDERR_INVALIDPARAMS;
749     }
750
751     /* Find a light number and update both light and viewports objects accordingly */
752     while(map&1) {
753         map>>=1;
754         i++;
755     }
756     lpDirect3DLightImpl->dwLightIndex = i;
757     This->num_lights++;
758     This->map_lights |= 1<<i;
759
760     /* Add the light in the 'linked' chain */
761     lpDirect3DLightImpl->next = This->lights;
762     This->lights = lpDirect3DLightImpl;
763     IDirect3DLight_AddRef(lpDirect3DLight);
764
765     /* Attach the light to the viewport */
766     lpDirect3DLightImpl->active_viewport = This;
767
768     /* If active, activate the light */
769     if (This->active_device != NULL) {
770         lpDirect3DLightImpl->activate(lpDirect3DLightImpl);
771     }
772
773     LeaveCriticalSection(&ddraw_cs);
774     return D3D_OK;
775 }
776
777 /*****************************************************************************
778  * IDirect3DViewport3::DeleteLight
779  *
780  * Deletes a light from the viewports' light list
781  *
782  * Params:
783  *  lpDirect3DLight: Light to delete
784  *
785  * Returns:
786  *  D3D_OK on success
787  *  DDERR_INVALIDPARAMS if the light wasn't found
788  *
789  *****************************************************************************/
790 static HRESULT WINAPI
791 IDirect3DViewportImpl_DeleteLight(IDirect3DViewport3 *iface,
792                                   IDirect3DLight *lpDirect3DLight)
793 {
794     IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
795     IDirect3DLightImpl *lpDirect3DLightImpl = (IDirect3DLightImpl *)lpDirect3DLight;
796     IDirect3DLightImpl *cur_light, *prev_light = NULL;
797
798     TRACE("(%p)->(%p)\n", This, lpDirect3DLight);
799
800     EnterCriticalSection(&ddraw_cs);
801     cur_light = This->lights;
802     while (cur_light != NULL) {
803         if (cur_light == lpDirect3DLightImpl) {
804             lpDirect3DLightImpl->desactivate(lpDirect3DLightImpl);
805             if (prev_light == NULL) This->lights = cur_light->next;
806             else prev_light->next = cur_light->next;
807             /* Detach the light to the viewport */
808             cur_light->active_viewport = NULL;
809             IDirect3DLight_Release( (IDirect3DLight *)cur_light );
810             This->num_lights--;
811             This->map_lights &= ~(1<<lpDirect3DLightImpl->dwLightIndex);
812             LeaveCriticalSection(&ddraw_cs);
813             return D3D_OK;
814         }
815         prev_light = cur_light;
816         cur_light = cur_light->next;
817     }
818     LeaveCriticalSection(&ddraw_cs);
819
820     return DDERR_INVALIDPARAMS;
821 }
822
823 /*****************************************************************************
824  * IDirect3DViewport::NextLight
825  *
826  * Enumerates the lights associated with the viewport
827  *
828  * Params:
829  *  lpDirect3DLight: Light to start with
830  *  lplpDirect3DLight: Address to store the successor to
831  *
832  * Returns:
833  *  D3D_OK, because it's a stub
834  *
835  *****************************************************************************/
836 static HRESULT WINAPI
837 IDirect3DViewportImpl_NextLight(IDirect3DViewport3 *iface,
838                                 IDirect3DLight *lpDirect3DLight,
839                                 IDirect3DLight **lplpDirect3DLight,
840                                 DWORD dwFlags)
841 {
842     IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
843     IDirect3DLightImpl *cur_light, *prev_light = NULL;
844
845     TRACE("(%p)->(%p,%p,%08x)\n", This, lpDirect3DLight, lplpDirect3DLight, dwFlags);
846
847     if (!lplpDirect3DLight)
848         return DDERR_INVALIDPARAMS;
849
850     *lplpDirect3DLight = NULL;
851
852     EnterCriticalSection(&ddraw_cs);
853
854     cur_light = This->lights;
855
856     switch (dwFlags) {
857         case D3DNEXT_NEXT:
858             if (!lpDirect3DLight) {
859                 LeaveCriticalSection(&ddraw_cs);
860                 return DDERR_INVALIDPARAMS;
861             }
862             while (cur_light != NULL) {
863                 if (cur_light == (IDirect3DLightImpl *)lpDirect3DLight) {
864                     *lplpDirect3DLight = (IDirect3DLight*)cur_light->next;
865                     break;
866                 }
867                 cur_light = cur_light->next;
868             }
869             break;
870         case D3DNEXT_HEAD:
871             *lplpDirect3DLight = (IDirect3DLight*)This->lights;
872             break;
873         case D3DNEXT_TAIL:
874             while (cur_light != NULL) {
875                 prev_light = cur_light;
876                 cur_light = cur_light->next;
877             }
878             *lplpDirect3DLight = (IDirect3DLight*)prev_light;
879             break;
880         default:
881             ERR("Unknown flag %d\n", dwFlags);
882             break;
883     }
884
885     if (*lplpDirect3DLight)
886         IDirect3DLight_AddRef(*lplpDirect3DLight);
887
888     LeaveCriticalSection(&ddraw_cs);
889
890     return *lplpDirect3DLight ? D3D_OK : DDERR_INVALIDPARAMS;
891 }
892
893 /*****************************************************************************
894  * IDirect3DViewport2 Methods.
895  *****************************************************************************/
896
897 /*****************************************************************************
898  * IDirect3DViewport3::GetViewport2
899  *
900  * Returns the currently set viewport in a D3DVIEWPORT2 structure.
901  * Similar to IDirect3DViewport3::GetViewport
902  *
903  * Params:
904  *  lpData: Pointer to the structure to fill
905  *
906  * Returns:
907  *  D3D_OK on success
908  *  DDERR_INVALIDPARAMS if the viewport was set with
909  *                      IDirect3DViewport3::SetViewport
910  *  DDERR_INVALIDPARAMS if Data is NULL
911  *
912  *****************************************************************************/
913 static HRESULT WINAPI
914 IDirect3DViewportImpl_GetViewport2(IDirect3DViewport3 *iface,
915                                    D3DVIEWPORT2 *lpData)
916 {
917     IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
918     DWORD dwSize;
919     TRACE("(%p)->(%p)\n", This, lpData);
920
921     EnterCriticalSection(&ddraw_cs);
922     dwSize = lpData->dwSize;
923     memset(lpData, 0, dwSize);
924     if (This->use_vp2)
925         memcpy(lpData, &(This->viewports.vp2), dwSize);
926     else {
927         D3DVIEWPORT2 vp2;
928         vp2.dwSize = sizeof(vp2);
929         vp2.dwX = This->viewports.vp1.dwX;
930         vp2.dwY = This->viewports.vp1.dwY;
931         vp2.dwWidth = This->viewports.vp1.dwWidth;
932         vp2.dwHeight = This->viewports.vp1.dwHeight;
933         vp2.dvClipX = 0.0;
934         vp2.dvClipY = 0.0;
935         vp2.dvClipWidth = 0.0;
936         vp2.dvClipHeight = 0.0;
937         vp2.dvMinZ = This->viewports.vp1.dvMinZ;
938         vp2.dvMaxZ = This->viewports.vp1.dvMaxZ;
939         memcpy(lpData, &vp2, dwSize);
940     }
941
942     if (TRACE_ON(d3d7)) {
943         TRACE("  returning D3DVIEWPORT2 :\n");
944         _dump_D3DVIEWPORT2(lpData);
945     }
946
947     LeaveCriticalSection(&ddraw_cs);
948     return D3D_OK;
949 }
950
951 /*****************************************************************************
952  * IDirect3DViewport3::SetViewport2
953  *
954  * Sets the viewport from a D3DVIEWPORT2 structure
955  *
956  * Params:
957  *  lpData: Viewport to set
958  *
959  * Returns:
960  *  D3D_OK on success
961  *
962  *****************************************************************************/
963 static HRESULT WINAPI
964 IDirect3DViewportImpl_SetViewport2(IDirect3DViewport3 *iface,
965                                    D3DVIEWPORT2 *lpData)
966 {
967     IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
968     LPDIRECT3DVIEWPORT3 current_viewport;
969     TRACE("(%p/%p)->(%p)\n", This, iface, lpData);
970
971     if (TRACE_ON(d3d7)) {
972         TRACE("  getting D3DVIEWPORT2 :\n");
973         _dump_D3DVIEWPORT2(lpData);
974     }
975
976     EnterCriticalSection(&ddraw_cs);
977     This->use_vp2 = 1;
978     memset(&(This->viewports.vp2), 0, sizeof(This->viewports.vp2));
979     memcpy(&(This->viewports.vp2), lpData, lpData->dwSize);
980
981     if (This->active_device) {
982         IDirect3DDevice3 *d3d_device3 = (IDirect3DDevice3 *)&This->active_device->IDirect3DDevice3_vtbl;
983         IDirect3DDevice3_GetCurrentViewport(d3d_device3, &current_viewport);
984         if (current_viewport) {
985             if ((IDirect3DViewportImpl *)current_viewport == This) This->activate(This, FALSE);
986             IDirect3DViewport3_Release(current_viewport);
987         }
988     }
989     LeaveCriticalSection(&ddraw_cs);
990
991     return D3D_OK;
992 }
993
994 /*****************************************************************************
995  * IDirect3DViewport3 Methods.
996  *****************************************************************************/
997
998 /*****************************************************************************
999  * IDirect3DViewport3::SetBackgroundDepth2
1000  *
1001  * Sets a IDirectDrawSurface4 surface as the background depth surface
1002  *
1003  * Params:
1004  *  lpDDS: Surface to set
1005  *
1006  * Returns:
1007  *  D3D_OK, because it's stub
1008  *
1009  *****************************************************************************/
1010 static HRESULT WINAPI
1011 IDirect3DViewportImpl_SetBackgroundDepth2(IDirect3DViewport3 *iface,
1012                                           IDirectDrawSurface4 *lpDDS)
1013 {
1014     IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
1015     FIXME("(%p)->(%p): stub!\n", This, lpDDS);
1016     return D3D_OK;
1017 }
1018
1019 /*****************************************************************************
1020  * IDirect3DViewport3::GetBackgroundDepth2
1021  *
1022  * Returns the IDirect3DSurface4 interface to the background depth surface
1023  *
1024  * Params:
1025  *  lplpDDS: Address to store the interface pointer at
1026  *  lpValid: Set to true if a surface is assigned
1027  *
1028  * Returns:
1029  *  D3D_OK because it's a stub
1030  *
1031  *****************************************************************************/
1032 static HRESULT WINAPI
1033 IDirect3DViewportImpl_GetBackgroundDepth2(IDirect3DViewport3 *iface,
1034                                           IDirectDrawSurface4 **lplpDDS,
1035                                           BOOL *lpValid)
1036 {
1037     IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
1038     FIXME("(%p/%p)->(%p,%p): stub!\n", This, iface, lplpDDS, lpValid);
1039     return D3D_OK;
1040 }
1041
1042 /*****************************************************************************
1043  * IDirect3DViewport3::Clear2
1044  *
1045  * Another clearing method
1046  *
1047  * Params:
1048  *  Count: Number of rectangles to clear
1049  *  Rects: Rectangle array to clear
1050  *  Flags: Some flags :)
1051  *  Color: Color to fill the render target with
1052  *  Z: Value to fill the depth buffer with
1053  *  Stencil: Value to fill the stencil bits with
1054  *
1055  * Returns:
1056  *
1057  *****************************************************************************/
1058 static HRESULT WINAPI
1059 IDirect3DViewportImpl_Clear2(IDirect3DViewport3 *iface,
1060                              DWORD dwCount,
1061                              LPD3DRECT lpRects,
1062                              DWORD dwFlags,
1063                              DWORD dwColor,
1064                              D3DVALUE dvZ,
1065                              DWORD dwStencil)
1066 {
1067     IDirect3DViewportImpl *This = (IDirect3DViewportImpl *)iface;
1068     HRESULT hr;
1069     LPDIRECT3DVIEWPORT3 current_viewport;
1070     IDirect3DDevice3 *d3d_device3;
1071     TRACE("(%p)->(%08x,%p,%08x,%08x,%f,%08x)\n", This, dwCount, lpRects, dwFlags, dwColor, dvZ, dwStencil);
1072
1073     EnterCriticalSection(&ddraw_cs);
1074     if (This->active_device == NULL) {
1075         ERR(" Trying to clear a viewport not attached to a device !\n");
1076         LeaveCriticalSection(&ddraw_cs);
1077         return D3DERR_VIEWPORTHASNODEVICE;
1078     }
1079     d3d_device3 = (IDirect3DDevice3 *)&This->active_device->IDirect3DDevice3_vtbl;
1080     /* Need to temporarily activate viewport to clear it. Previously active one will be restored
1081         afterwards. */
1082     This->activate(This, TRUE);
1083
1084     hr = IDirect3DDevice7_Clear((IDirect3DDevice7 *)This->active_device,
1085             dwCount, lpRects, dwFlags, dwColor, dvZ, dwStencil);
1086     IDirect3DDevice3_GetCurrentViewport(d3d_device3, &current_viewport);
1087     if(current_viewport) {
1088         IDirect3DViewportImpl *vp = (IDirect3DViewportImpl *)current_viewport;
1089         vp->activate(vp, TRUE);
1090         IDirect3DViewport3_Release(current_viewport);
1091     }
1092     LeaveCriticalSection(&ddraw_cs);
1093     return hr;
1094 }
1095
1096 /*****************************************************************************
1097  * The VTable
1098  *****************************************************************************/
1099
1100 const IDirect3DViewport3Vtbl IDirect3DViewport3_Vtbl =
1101 {
1102     /*** IUnknown Methods ***/
1103     IDirect3DViewportImpl_QueryInterface,
1104     IDirect3DViewportImpl_AddRef,
1105     IDirect3DViewportImpl_Release,
1106     /*** IDirect3DViewport Methods */
1107     IDirect3DViewportImpl_Initialize,
1108     IDirect3DViewportImpl_GetViewport,
1109     IDirect3DViewportImpl_SetViewport,
1110     IDirect3DViewportImpl_TransformVertices,
1111     IDirect3DViewportImpl_LightElements,
1112     IDirect3DViewportImpl_SetBackground,
1113     IDirect3DViewportImpl_GetBackground,
1114     IDirect3DViewportImpl_SetBackgroundDepth,
1115     IDirect3DViewportImpl_GetBackgroundDepth,
1116     IDirect3DViewportImpl_Clear,
1117     IDirect3DViewportImpl_AddLight,
1118     IDirect3DViewportImpl_DeleteLight,
1119     IDirect3DViewportImpl_NextLight,
1120     /*** IDirect3DViewport2 Methods ***/
1121     IDirect3DViewportImpl_GetViewport2,
1122     IDirect3DViewportImpl_SetViewport2,
1123     /*** IDirect3DViewport3 Methods ***/
1124     IDirect3DViewportImpl_SetBackgroundDepth2,
1125     IDirect3DViewportImpl_GetBackgroundDepth2,
1126     IDirect3DViewportImpl_Clear2,
1127 };