Better windows handling.
[wine] / dlls / ddraw / surface_fakezbuffer.c
1 /*              DirectDraw/Direct3D Z-Buffer stand in
2  *
3  * Copyright 2000 TransGaming Technologies Inc.
4  *
5  * This class provides a DirectDrawSurface implementation that represents
6  * a Z-Buffer surface. However it does not store an image and does not
7  * support Lock/Unlock or GetDC. It is merely a placeholder required by the
8  * Direct3D architecture.
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  */
24
25 #include "config.h"
26
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <assert.h>
30
31 #define NONAMELESSUNION
32 #define NONAMELESSSTRUCT
33
34 #include "windef.h"
35 #include "winbase.h"
36 #include "wingdi.h"
37 #include "ddraw.h"
38 #include "d3d.h"
39
40 #include "wine/debug.h"
41
42 #include "ddcomimpl.h"
43 #include "ddraw_private.h"
44 #include "d3d_private.h"
45
46 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
47
48 static const IDirectDrawSurface7Vtbl FakeZBuffer_IDirectDrawSurface7_VTable;
49
50 #ifdef HAVE_OPENGL
51 static void zbuffer_lock_update(IDirectDrawSurfaceImpl* This, LPCRECT pRect, DWORD dwFlags)
52 {
53     /* Note that this does not do anything for now... At least it's not needed for Grim Fandango :-) */
54 }
55
56 static void zbuffer_unlock_update(IDirectDrawSurfaceImpl* This, LPCRECT pRect)
57 {
58     ((FakeZBuffer_DirectDrawSurfaceImpl *) This->private)->in_memory = TRUE;
59 }
60
61 static BOOLEAN zbuffer_get_dirty_status(IDirectDrawSurfaceImpl* This, LPCRECT pRect)
62 {
63     if (((FakeZBuffer_DirectDrawSurfaceImpl *) This->private)->in_memory) {
64         ((FakeZBuffer_DirectDrawSurfaceImpl *) This->private)->in_memory = FALSE;
65         return TRUE;
66     }
67     return FALSE;
68 }
69 #endif
70
71 HRESULT FakeZBuffer_DirectDrawSurface_Construct(IDirectDrawSurfaceImpl *This,
72                                                 IDirectDrawImpl *pDD,
73                                                 const DDSURFACEDESC2 *pDDSD)
74 {
75     HRESULT hr;
76     BYTE zdepth = 16; /* Default value.. Should use the one from GL */
77     
78     assert(pDDSD->ddsCaps.dwCaps & DDSCAPS_ZBUFFER);
79
80     hr = Main_DirectDrawSurface_Construct(This, pDD, pDDSD);
81     if (FAILED(hr)) return hr;
82
83     ICOM_INIT_INTERFACE(This, IDirectDrawSurface7,
84                         FakeZBuffer_IDirectDrawSurface7_VTable);
85
86     This->final_release = FakeZBuffer_DirectDrawSurface_final_release;
87     This->duplicate_surface = FakeZBuffer_DirectDrawSurface_duplicate_surface;
88
89 #ifdef HAVE_OPENGL     
90     if (opengl_initialized) {
91         This->lock_update = zbuffer_lock_update;
92         This->unlock_update = zbuffer_unlock_update;
93         This->get_dirty_status = zbuffer_get_dirty_status;
94     }
95 #endif
96         
97     
98     /* Beginning of some D3D hacks :-) */
99     if (This->surface_desc.dwFlags & DDSD_ZBUFFERBITDEPTH) {
100         zdepth = This->surface_desc.u2.dwMipMapCount; /* This is where the Z buffer depth is stored in 'old' versions */
101     }
102     
103     if ((This->surface_desc.dwFlags & DDSD_PIXELFORMAT) == 0) {
104         This->surface_desc.dwFlags |= DDSD_PIXELFORMAT;
105         This->surface_desc.u4.ddpfPixelFormat.dwSize = sizeof(This->surface_desc.u4.ddpfPixelFormat);
106         This->surface_desc.u4.ddpfPixelFormat.dwFlags = DDPF_ZBUFFER;
107         This->surface_desc.u4.ddpfPixelFormat.u1.dwZBufferBitDepth = zdepth;
108     }
109     if ((This->surface_desc.dwFlags & DDSD_PITCH) == 0) {
110         This->surface_desc.dwFlags |= DDSD_PITCH;
111         This->surface_desc.u1.lPitch = ((zdepth + 7) / 8) * This->surface_desc.dwWidth;
112     }
113     This->surface_desc.lpSurface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
114                                              This->surface_desc.u1.lPitch * This->surface_desc.dwHeight);
115     
116     return DD_OK;
117 }
118
119 /* Not an API */
120 HRESULT FakeZBuffer_DirectDrawSurface_Create(IDirectDrawImpl* pDD,
121                                              const DDSURFACEDESC2* pDDSD,
122                                              LPDIRECTDRAWSURFACE7* ppSurf,
123                                              IUnknown* pUnkOuter)
124 {
125     IDirectDrawSurfaceImpl* This;
126     HRESULT hr;
127     assert(pUnkOuter == NULL);
128
129     This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
130                      sizeof(*This)
131                      + sizeof(FakeZBuffer_DirectDrawSurfaceImpl));
132     if (This == NULL) return E_OUTOFMEMORY;
133
134     This->private = (FakeZBuffer_DirectDrawSurfaceImpl*)(This+1);
135
136     hr = FakeZBuffer_DirectDrawSurface_Construct(This, pDD, pDDSD);
137     if (FAILED(hr))
138         HeapFree(GetProcessHeap(), 0, This);
139     else
140         *ppSurf = ICOM_INTERFACE(This, IDirectDrawSurface7);
141
142     return hr;
143 }
144
145 void
146 FakeZBuffer_DirectDrawSurface_final_release(IDirectDrawSurfaceImpl* This)
147 {
148     Main_DirectDrawSurface_final_release(This);
149 }
150
151 HRESULT
152 FakeZBuffer_DirectDrawSurface_duplicate_surface(IDirectDrawSurfaceImpl* This,
153                                                 LPDIRECTDRAWSURFACE7* ppDup)
154 {
155     return FakeZBuffer_DirectDrawSurface_Create(This->ddraw_owner,
156                                                 &This->surface_desc, ppDup,
157                                                 NULL);
158 }
159
160 /* put your breakpoint/abort call here */
161 static HRESULT cant_do_that(const char *s)
162 {
163     FIXME("attempt to %s fake z-buffer\n", s);
164     return DDERR_UNSUPPORTED;
165 }
166
167 HRESULT WINAPI
168 FakeZBuffer_DirectDrawSurface_Blt(LPDIRECTDRAWSURFACE7 iface, LPRECT rdst,
169                                   LPDIRECTDRAWSURFACE7 src, LPRECT rsrc,
170                                   DWORD dwFlags, LPDDBLTFX lpbltfx)
171 {
172     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
173
174     if (TRACE_ON(ddraw)) {
175         TRACE("(%p)->(%p,%p,%p,%08lx,%p)\n", This,rdst,src,rsrc,dwFlags,lpbltfx);
176         if (rdst) TRACE("\tdestrect :%ldx%ld-%ldx%ld\n",rdst->left,rdst->top,rdst->right,rdst->bottom);
177         if (rsrc) TRACE("\tsrcrect  :%ldx%ld-%ldx%ld\n",rsrc->left,rsrc->top,rsrc->right,rsrc->bottom);
178         TRACE("\tflags: ");
179         DDRAW_dump_DDBLT(dwFlags);
180         if (dwFlags & DDBLT_DDFX) {
181             TRACE("\tblitfx: ");
182             DDRAW_dump_DDBLTFX(lpbltfx->dwDDFX);
183         }
184     }
185
186     /* We only support the BLT with DEPTH_FILL for now */
187     if ((dwFlags & DDBLT_DEPTHFILL) && (This->ddraw_owner->d3d_private != NULL)) {
188         if (This->ddraw_owner->current_device != NULL) {
189             D3DRECT rect;
190             if (rdst) {
191                 rect.u1.x1 = rdst->left;
192                 rect.u2.y1 = rdst->top;
193                 rect.u3.x2 = rdst->right;
194                 rect.u4.y2 = rdst->bottom;
195             }
196             This->ddraw_owner->current_device->clear(This->ddraw_owner->current_device,
197                                                      (rdst == NULL ? 0 : 1), &rect,
198                                                      D3DCLEAR_ZBUFFER,
199                                                      0x00000000,
200                                                      ((double) lpbltfx->u5.dwFillDepth) / 4294967295.0,
201                                                      0x00000000);
202             return DD_OK;
203         }
204     }
205
206     return cant_do_that("blt to a");
207 }
208
209 HRESULT WINAPI
210 FakeZBuffer_DirectDrawSurface_BltFast(LPDIRECTDRAWSURFACE7 iface, DWORD dstx,
211                                       DWORD dsty, LPDIRECTDRAWSURFACE7 src,
212                                       LPRECT rsrc, DWORD trans)
213 {
214     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
215
216     if (TRACE_ON(ddraw)) {
217         FIXME("(%p)->(%ld,%ld,%p,%p,%08lx)\n",
218                 This,dstx,dsty,src,rsrc,trans
219         );
220         FIXME("\ttrans:");
221         if (FIXME_ON(ddraw))
222           DDRAW_dump_DDBLTFAST(trans);
223         if (rsrc)
224           FIXME("\tsrcrect: %ldx%ld-%ldx%ld\n",rsrc->left,rsrc->top,rsrc->right,rsrc->bottom);
225         else
226           FIXME(" srcrect: NULL\n");
227     }
228
229     return cant_do_that("bltfast to a");
230 }
231
232 HRESULT WINAPI
233 FakeZBuffer_DirectDrawSurface_GetDC(LPDIRECTDRAWSURFACE7 iface, HDC *phDC)
234 {
235     return cant_do_that("get a DC for a");
236 }
237
238 HRESULT WINAPI
239 FakeZBuffer_DirectDrawSurface_ReleaseDC(LPDIRECTDRAWSURFACE7 iface, HDC hDC)
240 {
241     return cant_do_that("release a DC for a");
242 }
243
244 HRESULT WINAPI
245 FakeZBuffer_DirectDrawSurface_Restore(LPDIRECTDRAWSURFACE7 iface)
246 {
247     return DD_OK;
248 }
249
250 HRESULT WINAPI
251 FakeZBuffer_DirectDrawSurface_SetSurfaceDesc(LPDIRECTDRAWSURFACE7 iface,
252                                              LPDDSURFACEDESC2 pDDSD,
253                                              DWORD dwFlags)
254 {
255     /* XXX */
256     abort();
257     return E_FAIL;
258 }
259
260
261 static const IDirectDrawSurface7Vtbl FakeZBuffer_IDirectDrawSurface7_VTable=
262 {
263     Main_DirectDrawSurface_QueryInterface,
264     Main_DirectDrawSurface_AddRef,
265     Main_DirectDrawSurface_Release,
266     Main_DirectDrawSurface_AddAttachedSurface,
267     Main_DirectDrawSurface_AddOverlayDirtyRect,
268     FakeZBuffer_DirectDrawSurface_Blt,
269     Main_DirectDrawSurface_BltBatch,
270     FakeZBuffer_DirectDrawSurface_BltFast,
271     Main_DirectDrawSurface_DeleteAttachedSurface,
272     Main_DirectDrawSurface_EnumAttachedSurfaces,
273     Main_DirectDrawSurface_EnumOverlayZOrders,
274     Main_DirectDrawSurface_Flip,
275     Main_DirectDrawSurface_GetAttachedSurface,
276     Main_DirectDrawSurface_GetBltStatus,
277     Main_DirectDrawSurface_GetCaps,
278     Main_DirectDrawSurface_GetClipper,
279     Main_DirectDrawSurface_GetColorKey,
280     FakeZBuffer_DirectDrawSurface_GetDC,
281     Main_DirectDrawSurface_GetFlipStatus,
282     Main_DirectDrawSurface_GetOverlayPosition,
283     Main_DirectDrawSurface_GetPalette,
284     Main_DirectDrawSurface_GetPixelFormat,
285     Main_DirectDrawSurface_GetSurfaceDesc,
286     Main_DirectDrawSurface_Initialize,
287     Main_DirectDrawSurface_IsLost,
288     Main_DirectDrawSurface_Lock,
289     FakeZBuffer_DirectDrawSurface_ReleaseDC,
290     FakeZBuffer_DirectDrawSurface_Restore,
291     Main_DirectDrawSurface_SetClipper,
292     Main_DirectDrawSurface_SetColorKey,
293     Main_DirectDrawSurface_SetOverlayPosition,
294     Main_DirectDrawSurface_SetPalette,
295     Main_DirectDrawSurface_Unlock,
296     Main_DirectDrawSurface_UpdateOverlay,
297     Main_DirectDrawSurface_UpdateOverlayDisplay,
298     Main_DirectDrawSurface_UpdateOverlayZOrder,
299     Main_DirectDrawSurface_GetDDInterface,
300     Main_DirectDrawSurface_PageLock,
301     Main_DirectDrawSurface_PageUnlock,
302     FakeZBuffer_DirectDrawSurface_SetSurfaceDesc,
303     Main_DirectDrawSurface_SetPrivateData,
304     Main_DirectDrawSurface_GetPrivateData,
305     Main_DirectDrawSurface_FreePrivateData,
306     Main_DirectDrawSurface_GetUniquenessValue,
307     Main_DirectDrawSurface_ChangeUniquenessValue,
308     Main_DirectDrawSurface_SetPriority,
309     Main_DirectDrawSurface_GetPriority,
310     Main_DirectDrawSurface_SetLOD,
311     Main_DirectDrawSurface_GetLOD
312 };