Add support for more than 2 audio channels.
[wine] / dlls / ddraw / dsurface / 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 #define CONST_VTABLE
35
36 #include "windef.h"
37 #include "winbase.h"
38 #include "wingdi.h"
39 #include "ddraw.h"
40 #include "d3d.h"
41
42 #include "wine/debug.h"
43
44 #include "ddcomimpl.h"
45 #include "ddraw_private.h"
46 #include "d3d_private.h"
47 #include "dsurface/main.h"
48 #include "dsurface/fakezbuffer.h"
49
50 WINE_DEFAULT_DEBUG_CHANNEL(ddraw);
51
52 static const IDirectDrawSurface7Vtbl FakeZBuffer_IDirectDrawSurface7_VTable;
53
54 #ifdef HAVE_OPENGL
55 static void zbuffer_lock_update(IDirectDrawSurfaceImpl* This, LPCRECT pRect, DWORD dwFlags)
56 {
57     /* Note that this does not do anything for now... At least it's not needed for Grim Fandango :-) */
58 }
59
60 static void zbuffer_unlock_update(IDirectDrawSurfaceImpl* This, LPCRECT pRect)
61 {
62     ((FakeZBuffer_DirectDrawSurfaceImpl *) This->private)->in_memory = TRUE;
63 }
64
65 static BOOLEAN zbuffer_get_dirty_status(IDirectDrawSurfaceImpl* This, LPCRECT pRect)
66 {
67     if (((FakeZBuffer_DirectDrawSurfaceImpl *) This->private)->in_memory) {
68         ((FakeZBuffer_DirectDrawSurfaceImpl *) This->private)->in_memory = FALSE;
69         return TRUE;
70     }
71     return FALSE;
72 }
73 #endif
74
75 HRESULT FakeZBuffer_DirectDrawSurface_Construct(IDirectDrawSurfaceImpl *This,
76                                                 IDirectDrawImpl *pDD,
77                                                 const DDSURFACEDESC2 *pDDSD)
78 {
79     HRESULT hr;
80     BYTE zdepth = 16; /* Default value.. Should use the one from GL */
81     
82     assert(pDDSD->ddsCaps.dwCaps & DDSCAPS_ZBUFFER);
83
84     hr = Main_DirectDrawSurface_Construct(This, pDD, pDDSD);
85     if (FAILED(hr)) return hr;
86
87     ICOM_INIT_INTERFACE(This, IDirectDrawSurface7,
88                         FakeZBuffer_IDirectDrawSurface7_VTable);
89
90     This->final_release = FakeZBuffer_DirectDrawSurface_final_release;
91     This->duplicate_surface = FakeZBuffer_DirectDrawSurface_duplicate_surface;
92
93 #ifdef HAVE_OPENGL     
94     if (opengl_initialized) {
95         This->lock_update = zbuffer_lock_update;
96         This->unlock_update = zbuffer_unlock_update;
97         This->get_dirty_status = zbuffer_get_dirty_status;
98     }
99 #endif
100         
101     
102     /* Beginning of some D3D hacks :-) */
103     if (This->surface_desc.dwFlags & DDSD_ZBUFFERBITDEPTH) {
104         zdepth = This->surface_desc.u2.dwMipMapCount; /* This is where the Z buffer depth is stored in 'old' versions */
105     }
106     
107     if ((This->surface_desc.dwFlags & DDSD_PIXELFORMAT) == 0) {
108         This->surface_desc.dwFlags |= DDSD_PIXELFORMAT;
109         This->surface_desc.u4.ddpfPixelFormat.dwSize = sizeof(This->surface_desc.u4.ddpfPixelFormat);
110         This->surface_desc.u4.ddpfPixelFormat.dwFlags = DDPF_ZBUFFER;
111         This->surface_desc.u4.ddpfPixelFormat.u1.dwZBufferBitDepth = zdepth;
112     }
113     if ((This->surface_desc.dwFlags & DDSD_PITCH) == 0) {
114         This->surface_desc.dwFlags |= DDSD_PITCH;
115         This->surface_desc.u1.lPitch = ((zdepth + 7) / 8) * This->surface_desc.dwWidth;
116     }
117     This->surface_desc.lpSurface = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
118                                              This->surface_desc.u1.lPitch * This->surface_desc.dwHeight);
119     
120     return DD_OK;
121 }
122
123 /* Not an API */
124 HRESULT FakeZBuffer_DirectDrawSurface_Create(IDirectDrawImpl* pDD,
125                                              const DDSURFACEDESC2* pDDSD,
126                                              LPDIRECTDRAWSURFACE7* ppSurf,
127                                              IUnknown* pUnkOuter)
128 {
129     IDirectDrawSurfaceImpl* This;
130     HRESULT hr;
131     assert(pUnkOuter == NULL);
132
133     This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
134                      sizeof(*This)
135                      + sizeof(FakeZBuffer_DirectDrawSurfaceImpl));
136     if (This == NULL) return E_OUTOFMEMORY;
137
138     This->private = (FakeZBuffer_DirectDrawSurfaceImpl*)(This+1);
139
140     hr = FakeZBuffer_DirectDrawSurface_Construct(This, pDD, pDDSD);
141     if (FAILED(hr))
142         HeapFree(GetProcessHeap(), 0, This);
143     else
144         *ppSurf = ICOM_INTERFACE(This, IDirectDrawSurface7);
145
146     return hr;
147 }
148
149 void
150 FakeZBuffer_DirectDrawSurface_final_release(IDirectDrawSurfaceImpl* This)
151 {
152     Main_DirectDrawSurface_final_release(This);
153 }
154
155 HRESULT
156 FakeZBuffer_DirectDrawSurface_duplicate_surface(IDirectDrawSurfaceImpl* This,
157                                                 LPDIRECTDRAWSURFACE7* ppDup)
158 {
159     return FakeZBuffer_DirectDrawSurface_Create(This->ddraw_owner,
160                                                 &This->surface_desc, ppDup,
161                                                 NULL);
162 }
163
164 /* put your breakpoint/abort call here */
165 static HRESULT cant_do_that(const char *s)
166 {
167     FIXME("attempt to %s fake z-buffer\n", s);
168     return DDERR_UNSUPPORTED;
169 }
170
171 HRESULT WINAPI
172 FakeZBuffer_DirectDrawSurface_Blt(LPDIRECTDRAWSURFACE7 iface, LPRECT rdst,
173                                   LPDIRECTDRAWSURFACE7 src, LPRECT rsrc,
174                                   DWORD dwFlags, LPDDBLTFX lpbltfx)
175 {
176     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
177
178     if (TRACE_ON(ddraw)) {
179         TRACE("(%p)->(%p,%p,%p,%08lx,%p)\n", This,rdst,src,rsrc,dwFlags,lpbltfx);
180         if (rdst) TRACE("\tdestrect :%ldx%ld-%ldx%ld\n",rdst->left,rdst->top,rdst->right,rdst->bottom);
181         if (rsrc) TRACE("\tsrcrect  :%ldx%ld-%ldx%ld\n",rsrc->left,rsrc->top,rsrc->right,rsrc->bottom);
182         TRACE("\tflags: ");
183         DDRAW_dump_DDBLT(dwFlags);
184         if (dwFlags & DDBLT_DDFX) {
185             TRACE("\tblitfx: ");
186             DDRAW_dump_DDBLTFX(lpbltfx->dwDDFX);
187         }
188     }
189
190     /* We only support the BLT with DEPTH_FILL for now */
191     if ((dwFlags & DDBLT_DEPTHFILL) && (This->ddraw_owner->d3d_private != NULL)) {
192         if (This->ddraw_owner->current_device != NULL) {
193             D3DRECT rect;
194             if (rdst) {
195                 rect.u1.x1 = rdst->left;
196                 rect.u2.y1 = rdst->top;
197                 rect.u3.x2 = rdst->right;
198                 rect.u4.y2 = rdst->bottom;
199             }
200             This->ddraw_owner->current_device->clear(This->ddraw_owner->current_device,
201                                                      (rdst == NULL ? 0 : 1), &rect,
202                                                      D3DCLEAR_ZBUFFER,
203                                                      0x00000000,
204                                                      ((double) lpbltfx->u5.dwFillDepth) / 4294967295.0,
205                                                      0x00000000);
206             return DD_OK;
207         }
208     }
209
210     return cant_do_that("blt to a");
211 }
212
213 HRESULT WINAPI
214 FakeZBuffer_DirectDrawSurface_BltFast(LPDIRECTDRAWSURFACE7 iface, DWORD dstx,
215                                       DWORD dsty, LPDIRECTDRAWSURFACE7 src,
216                                       LPRECT rsrc, DWORD trans)
217 {
218     IDirectDrawSurfaceImpl *This = (IDirectDrawSurfaceImpl *)iface;
219
220     if (TRACE_ON(ddraw)) {
221         FIXME("(%p)->(%ld,%ld,%p,%p,%08lx)\n",
222                 This,dstx,dsty,src,rsrc,trans
223         );
224         FIXME("\ttrans:");
225         if (FIXME_ON(ddraw))
226           DDRAW_dump_DDBLTFAST(trans);
227         if (rsrc)
228           FIXME("\tsrcrect: %ldx%ld-%ldx%ld\n",rsrc->left,rsrc->top,rsrc->right,rsrc->bottom);
229         else
230           FIXME(" srcrect: NULL\n");
231     }
232
233     return cant_do_that("bltfast to a");
234 }
235
236 HRESULT WINAPI
237 FakeZBuffer_DirectDrawSurface_GetDC(LPDIRECTDRAWSURFACE7 iface, HDC *phDC)
238 {
239     return cant_do_that("get a DC for a");
240 }
241
242 HRESULT WINAPI
243 FakeZBuffer_DirectDrawSurface_ReleaseDC(LPDIRECTDRAWSURFACE7 iface, HDC hDC)
244 {
245     return cant_do_that("release a DC for a");
246 }
247
248 HRESULT WINAPI
249 FakeZBuffer_DirectDrawSurface_Restore(LPDIRECTDRAWSURFACE7 iface)
250 {
251     return DD_OK;
252 }
253
254 HRESULT WINAPI
255 FakeZBuffer_DirectDrawSurface_SetSurfaceDesc(LPDIRECTDRAWSURFACE7 iface,
256                                              LPDDSURFACEDESC2 pDDSD,
257                                              DWORD dwFlags)
258 {
259     /* XXX */
260     abort();
261     return E_FAIL;
262 }
263
264
265 static const IDirectDrawSurface7Vtbl FakeZBuffer_IDirectDrawSurface7_VTable=
266 {
267     Main_DirectDrawSurface_QueryInterface,
268     Main_DirectDrawSurface_AddRef,
269     Main_DirectDrawSurface_Release,
270     Main_DirectDrawSurface_AddAttachedSurface,
271     Main_DirectDrawSurface_AddOverlayDirtyRect,
272     FakeZBuffer_DirectDrawSurface_Blt,
273     Main_DirectDrawSurface_BltBatch,
274     FakeZBuffer_DirectDrawSurface_BltFast,
275     Main_DirectDrawSurface_DeleteAttachedSurface,
276     Main_DirectDrawSurface_EnumAttachedSurfaces,
277     Main_DirectDrawSurface_EnumOverlayZOrders,
278     Main_DirectDrawSurface_Flip,
279     Main_DirectDrawSurface_GetAttachedSurface,
280     Main_DirectDrawSurface_GetBltStatus,
281     Main_DirectDrawSurface_GetCaps,
282     Main_DirectDrawSurface_GetClipper,
283     Main_DirectDrawSurface_GetColorKey,
284     FakeZBuffer_DirectDrawSurface_GetDC,
285     Main_DirectDrawSurface_GetFlipStatus,
286     Main_DirectDrawSurface_GetOverlayPosition,
287     Main_DirectDrawSurface_GetPalette,
288     Main_DirectDrawSurface_GetPixelFormat,
289     Main_DirectDrawSurface_GetSurfaceDesc,
290     Main_DirectDrawSurface_Initialize,
291     Main_DirectDrawSurface_IsLost,
292     Main_DirectDrawSurface_Lock,
293     FakeZBuffer_DirectDrawSurface_ReleaseDC,
294     FakeZBuffer_DirectDrawSurface_Restore,
295     Main_DirectDrawSurface_SetClipper,
296     Main_DirectDrawSurface_SetColorKey,
297     Main_DirectDrawSurface_SetOverlayPosition,
298     Main_DirectDrawSurface_SetPalette,
299     Main_DirectDrawSurface_Unlock,
300     Main_DirectDrawSurface_UpdateOverlay,
301     Main_DirectDrawSurface_UpdateOverlayDisplay,
302     Main_DirectDrawSurface_UpdateOverlayZOrder,
303     Main_DirectDrawSurface_GetDDInterface,
304     Main_DirectDrawSurface_PageLock,
305     Main_DirectDrawSurface_PageUnlock,
306     FakeZBuffer_DirectDrawSurface_SetSurfaceDesc,
307     Main_DirectDrawSurface_SetPrivateData,
308     Main_DirectDrawSurface_GetPrivateData,
309     Main_DirectDrawSurface_FreePrivateData,
310     Main_DirectDrawSurface_GetUniquenessValue,
311     Main_DirectDrawSurface_ChangeUniquenessValue,
312     Main_DirectDrawSurface_SetPriority,
313     Main_DirectDrawSurface_GetPriority,
314     Main_DirectDrawSurface_SetLOD,
315     Main_DirectDrawSurface_GetLOD
316 };