3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS DirectX
5 * FILE: ddraw/ddraw/ddraw_main.c
6 * PURPOSE: IDirectDraw7 Implementation
7 * PROGRAMMER: Magnus Olsen, Maarten Bosma
12 * add warper functions for dx 1 - 6
13 * map the DirectDraw4_Vtable, DirectDraw2_Vtable, DirectDraw_Vtable
14 * table to right version of the functions
23 LPDDRAWI_DIRECTDRAW_INT
24 internal_directdraw_int_alloc(LPDDRAWI_DIRECTDRAW_INT This
)
26 LPDDRAWI_DIRECTDRAW_INT newThis
;
27 DxHeapMemAlloc(newThis
, sizeof(DDRAWI_DIRECTDRAW_INT
));
30 newThis
->lpLcl
= This
->lpLcl
;
31 newThis
->lpLink
= This
;
38 Main_DirectDraw_QueryInterface (LPDDRAWI_DIRECTDRAW_INT This
,
42 HRESULT retVal
= DD_OK
;
49 the D3D object can be optained from here
52 if (IsEqualGUID(&IID_IDirectDraw7
, id
))
54 if (This
->lpVtbl
!= &DirectDraw7_Vtable
)
56 This
= internal_directdraw_int_alloc(This
);
59 retVal
= DDERR_OUTOFVIDEOMEMORY
;
64 This
->lpVtbl
= &DirectDraw7_Vtable
;
66 Main_DirectDraw_AddRef(This
);
68 else if (IsEqualGUID(&IID_IDirectDraw4
, id
))
70 if (This
->lpVtbl
!= &DirectDraw4_Vtable
)
72 This
= internal_directdraw_int_alloc(This
);
75 retVal
= DDERR_OUTOFVIDEOMEMORY
;
80 This
->lpVtbl
= &DirectDraw4_Vtable
;
82 Main_DirectDraw_AddRef(This
);
85 else if (IsEqualGUID(&IID_IDirectDraw2
, id
))
87 if (This
->lpVtbl
!= &DirectDraw2_Vtable
)
89 This
= internal_directdraw_int_alloc(This
);
92 retVal
= DDERR_OUTOFVIDEOMEMORY
;
97 This
->lpVtbl
= &DirectDraw2_Vtable
;
99 Main_DirectDraw_AddRef(This
);
101 else if (IsEqualGUID(&IID_IDirectDraw
, id
))
103 if (This
->lpVtbl
!= &DirectDraw_Vtable
)
105 This
= internal_directdraw_int_alloc(This
);
108 retVal
= DDERR_OUTOFVIDEOMEMORY
;
113 This
->lpVtbl
= &DirectDraw_Vtable
;
115 Main_DirectDraw_AddRef(This
);
120 DX_STUB_str("E_NOINTERFACE");
121 retVal
= E_NOINTERFACE
;
124 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
133 * @name DDraw->AddRef
136 * The function DDraw->AddRef manages all ref counters in the COM object DDraw->
139 * Returns the local Ref counter value for the COM object
146 Main_DirectDraw_AddRef (LPDDRAWI_DIRECTDRAW_INT This
)
152 /* Lock the thread */
153 AcquireDDThreadLock();
157 /* Increment the internal ref counter */
160 /* Increment the local internal ref counter */
161 This
->lpLcl
->dwLocalRefCnt
++;
163 if (This
->lpLcl
->lpGbl
!= NULL
)
165 /* Increment the gobal internal ref counter */
166 This
->lpLcl
->lpGbl
->dwRefCnt
++;
169 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
176 retValue
= This
->dwIntRefCnt
;
178 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
184 /* Release the thread lock */
185 ReleaseDDThreadLock();
187 /* Return the local Ref counter */
195 Main_DirectDraw_Release (LPDDRAWI_DIRECTDRAW_INT This
)
201 /* Lock the thread */
202 AcquireDDThreadLock();
208 This
->lpLcl
->dwLocalRefCnt
--;
211 if (This
->lpLcl
->lpGbl
!= NULL
)
213 This
->lpLcl
->lpGbl
->dwRefCnt
--;
216 if ( This
->lpLcl
->lpGbl
->dwRefCnt
== 0)
218 // set resoltion back to the one in registry
219 /*if(This->cooperative_level & DDSCL_EXCLUSIVE)
221 ChangeDisplaySettings(NULL, 0);
227 /* FIXME cleanup being not call why ?? */
228 Counter
= This
->dwIntRefCnt
;
232 Counter
= This
->dwIntRefCnt
;
235 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
240 /* Release the thread lock */
241 ReleaseDDThreadLock();
248 Main_DirectDraw_Initialize (LPDDRAWI_DIRECTDRAW_INT This
, LPGUID lpGUID
)
250 return DDERR_ALREADYINITIALIZED
;
255 * @name DDraw->Compact
258 * In exlusive mode the function DDraw->Compact returns DERR_NOEXCLUSIVEMODE, otherwise it returns DD_OK
261 * Returns only error code DD_OK or DERR_NOEXCLUSIVEMODE
264 * Microsoft says Compact is not implemented in ddraw.dll, but it returns DDERR_NOEXCLUSIVEMODE or DD_OK
268 Main_DirectDraw_Compact(LPDDRAWI_DIRECTDRAW_INT This
)
270 HRESULT retVal
= DD_OK
;
274 /* Lock the thread */
275 AcquireDDThreadLock();
279 /* Check if Exclusive mode has been activated */
280 if (This
->lpLcl
->lpGbl
->lpExclusiveOwner
!= This
->lpLcl
)
282 retVal
= DDERR_NOEXCLUSIVEMODE
;
285 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
290 /* Release the thread lock */
291 ReleaseDDThreadLock();
297 Main_DirectDraw_GetAvailableVidMem(LPDDRAWI_DIRECTDRAW_INT This
, LPDDSCAPS ddscaps
, LPDWORD dwTotal
, LPDWORD dwFree
)
300 HRESULT retValue
= DD_OK
;
302 ZeroMemory(&myddscaps
, sizeof(DDSCAPS2
));
306 myddscaps
.dwCaps
= ddscaps
->dwCaps
;
307 retValue
= Main_DirectDraw_GetAvailableVidMem4(This
, &myddscaps
, dwTotal
, dwFree
);
309 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
311 retValue
= DDERR_INVALIDPARAMS
;
319 Main_DirectDraw_GetAvailableVidMem4(LPDDRAWI_DIRECTDRAW_INT This
, LPDDSCAPS2 ddscaps
,
320 LPDWORD dwTotal
, LPDWORD dwFree
)
322 HRESULT retVal
= DD_OK
;
323 DDHAL_GETAVAILDRIVERMEMORYDATA memdata
;
329 // There is no HEL implentation of this api
330 if (!(This
->lpLcl
->lpDDCB
->HALDDMiscellaneous
.dwFlags
& DDHAL_MISCCB32_GETAVAILDRIVERMEMORY
) ||
331 (This
->lpLcl
->lpGbl
->dwFlags
& DDRAWI_NOHARDWARE
) )
333 retVal
= DDERR_NODIRECTDRAWHW
;
337 if ((!dwTotal
&& !dwFree
) || !ddscaps
)
339 retVal
= DDERR_INVALIDPARAMS
;
343 if ( ddscaps
->dwCaps
& (DDSCAPS_BACKBUFFER
| DDSCAPS_COMPLEX
| DDSCAPS_FLIP
|
344 DDSCAPS_FRONTBUFFER
| DDSCAPS_PALETTE
| DDSCAPS_SYSTEMMEMORY
|
345 DDSCAPS_VISIBLE
| DDSCAPS_WRITEONLY
| DDSCAPS_OWNDC
))
347 retVal
= DDERR_INVALIDPARAMS
;
352 /* ddscaps->dwCaps2 & 0x01
353 this flag is outdate and are
354 set to 0 in ms dxsdk the name of
355 this flag is DDSCAPS2_HARDWAREDEINTERLACE
358 if ( ddscaps
->dwCaps2
& 0x01)
360 retVal
= DDERR_INVALIDCAPS
;
364 if ( ddscaps
->dwCaps3
& ~( DDSCAPS3_MULTISAMPLE_QUALITY_MASK
| DDSCAPS3_MULTISAMPLE_MASK
|
365 DDSCAPS3_RESERVED1
| DDSCAPS3_RESERVED2
|
366 DDSCAPS3_LIGHTWEIGHTMIPMAP
| DDSCAPS3_AUTOGENMIPMAP
|
369 retVal
= DDERR_INVALIDCAPS
;
373 if ( ddscaps
->dwCaps4
)
375 retVal
= DDERR_INVALIDCAPS
;
379 ZeroMemory(&memdata
, sizeof(DDHAL_GETAVAILDRIVERMEMORYDATA
));
380 memdata
.lpDD
= This
->lpLcl
->lpGbl
;
381 memdata
.ddRVal
= DDERR_INVALIDPARAMS
;
383 memdata
.ddsCapsEx
.dwCaps2
= ddscaps
->dwCaps2
;
384 memdata
.ddsCapsEx
.dwCaps3
= ddscaps
->dwCaps3
;
386 This
->lpLcl
->lpGbl
->hDD
= This
->lpLcl
->hDD
;
388 if (This
->lpLcl
->lpDDCB
->HALDDMiscellaneous
.GetAvailDriverMemory(&memdata
) == DDHAL_DRIVER_NOTHANDLED
)
390 retVal
= DDERR_NODIRECTDRAWHW
;
401 *dwTotal
= memdata
.dwTotal
;
404 *dwFree
= memdata
.dwFree
;
406 retVal
= memdata
.ddRVal
;
410 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
419 Main_DirectDraw_GetFourCCCodes(LPDDRAWI_DIRECTDRAW_INT This
, LPDWORD lpNumCodes
, LPDWORD lpCodes
)
421 HRESULT retVal
= DD_OK
;
426 // EnterCriticalSection(&ddcs);
430 if(IsBadWritePtr(lpNumCodes
,sizeof(LPDWORD
)))
432 retVal
= DDERR_INVALIDPARAMS
;
436 if(!(IsBadWritePtr(lpNumCodes
,sizeof(LPDWORD
))))
440 if (*lpNumCodes
> This
->lpLcl
->lpGbl
->dwNumFourCC
)
442 *lpNumCodes
= This
->lpLcl
->lpGbl
->dwNumFourCC
;
445 size
= *lpNumCodes
* sizeof(DWORD
);
447 if(!IsBadWritePtr(lpCodes
, size
))
449 memcpy(lpCodes
, This
->lpLcl
->lpGbl
->lpdwFourCC
, size
);
453 *lpNumCodes
= This
->lpLcl
->lpGbl
->dwNumFourCC
;
458 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
463 //LeaveCriticalSection(&ddcs);
469 * We can optain the version of the directdraw object by compare the
470 * vtl table pointer from iface we do not need pass which version
473 * Main_DirectDraw_CreateSurface is dead at moment we do only support
474 * directdraw 7 at moment
477 /* For DirectDraw 1 - 3 */
479 Main_DirectDraw_CreateSurface (LPDDRAWI_DIRECTDRAW_INT This
, LPDDSURFACEDESC pDDSD
,
480 LPDDRAWI_DDRAWSURFACE_INT
*ppSurf
, IUnknown
*pUnkOuter
)
482 HRESULT ret
= DDERR_GENERIC
;
483 DDSURFACEDESC2 dd_desc_v2
;
487 EnterCriticalSection(&ddcs
);
492 if (pDDSD
->dwSize
== sizeof(DDSURFACEDESC
))
494 CopyDDSurfDescToDDSurfDesc2(&dd_desc_v2
, (LPDDSURFACEDESC
)pDDSD
);
495 ret
= Internal_CreateSurface(This
,
502 ret
= DDERR_INVALIDPARAMS
;
505 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
507 ret
= DDERR_INVALIDPARAMS
;
510 LeaveCriticalSection(&ddcs
);
515 /* For DirectDraw 4 - 7 */
517 Main_DirectDraw_CreateSurface4(LPDDRAWI_DIRECTDRAW_INT This
, LPDDSURFACEDESC2 pDDSD
,
518 LPDDRAWI_DDRAWSURFACE_INT
*ppSurf
, IUnknown
*pUnkOuter
)
523 EnterCriticalSection(&ddcs
);
528 ret
= Internal_CreateSurface(This
, pDDSD
, ppSurf
, pUnkOuter
);
530 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
532 ret
= DDERR_INVALIDPARAMS
;
537 Main_DirectDraw_AddRef(This
);
539 LeaveCriticalSection(&ddcs
);
543 /* 5 of 31 DirectDraw7_Vtable api are working simluare to windows */
544 /* 8 of 31 DirectDraw7_Vtable api are under devloping / testing */
546 HRESULT WINAPI
Main_DirectDraw_CreatePalette(LPDDRAWI_DIRECTDRAW_INT This
, DWORD dwFlags
,
547 LPPALETTEENTRY palent
, LPDIRECTDRAWPALETTE
* ppPalette
, LPUNKNOWN pUnkOuter
)
552 EnterCriticalSection(&ddcs
);
557 ret
= Internal_CreatePalette(This
, dwFlags
, palent
, ppPalette
, pUnkOuter
);
559 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
561 ret
= DDERR_INVALIDPARAMS
;
565 //Versions 7 and 4 are addref'ed
566 if((This
->lpVtbl
== &DirectDraw7_Vtable
|| This
->lpVtbl
== &DirectDraw4_Vtable
) && *ppPalette
!= NULL
)
567 Main_DirectDraw_AddRef(This
) ;
569 LeaveCriticalSection(&ddcs
);