2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS DirectX
4 * FILE: dll/directx/ddraw/Ddraw/ddraw_main.c
5 * PURPOSE: IDirectDraw7 Implementation
6 * PROGRAMMER: Magnus Olsen, Maarten Bosma
11 * add warper functions for dx 1 - 6
12 * map the DirectDraw4_Vtable, DirectDraw2_Vtable, DirectDraw_Vtable
13 * table to right version of the functions
22 LPDDRAWI_DIRECTDRAW_INT
23 internal_directdraw_int_alloc(LPDDRAWI_DIRECTDRAW_INT This
)
25 LPDDRAWI_DIRECTDRAW_INT newThis
;
26 DxHeapMemAlloc(newThis
, sizeof(DDRAWI_DIRECTDRAW_INT
));
29 newThis
->lpLcl
= This
->lpLcl
;
30 newThis
->lpLink
= This
;
37 Main_DirectDraw_QueryInterface (LPDDRAWI_DIRECTDRAW_INT This
,
41 HRESULT retVal
= DD_OK
;
48 the D3D object can be optained from here
51 if (IsEqualGUID(&IID_IDirectDraw7
, id
))
53 if (This
->lpVtbl
!= &DirectDraw7_Vtable
)
55 This
= internal_directdraw_int_alloc(This
);
58 retVal
= DDERR_OUTOFVIDEOMEMORY
;
63 This
->lpVtbl
= &DirectDraw7_Vtable
;
65 Main_DirectDraw_AddRef(This
);
67 else if (IsEqualGUID(&IID_IDirectDraw4
, id
))
69 if (This
->lpVtbl
!= &DirectDraw4_Vtable
)
71 This
= internal_directdraw_int_alloc(This
);
74 retVal
= DDERR_OUTOFVIDEOMEMORY
;
79 This
->lpVtbl
= &DirectDraw4_Vtable
;
81 Main_DirectDraw_AddRef(This
);
84 else if (IsEqualGUID(&IID_IDirectDraw2
, id
))
86 if (This
->lpVtbl
!= &DirectDraw2_Vtable
)
88 This
= internal_directdraw_int_alloc(This
);
91 retVal
= DDERR_OUTOFVIDEOMEMORY
;
96 This
->lpVtbl
= &DirectDraw2_Vtable
;
98 Main_DirectDraw_AddRef(This
);
100 else if (IsEqualGUID(&IID_IDirectDraw
, id
))
102 if (This
->lpVtbl
!= &DirectDraw_Vtable
)
104 This
= internal_directdraw_int_alloc(This
);
107 retVal
= DDERR_OUTOFVIDEOMEMORY
;
112 This
->lpVtbl
= &DirectDraw_Vtable
;
114 Main_DirectDraw_AddRef(This
);
119 DX_STUB_str("E_NOINTERFACE");
120 retVal
= E_NOINTERFACE
;
123 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
132 * @name DDraw->AddRef
135 * The function DDraw->AddRef manages all ref counters in the COM object DDraw->
138 * Returns the local Ref counter value for the COM object
145 Main_DirectDraw_AddRef (LPDDRAWI_DIRECTDRAW_INT This
)
151 /* Lock the thread */
152 AcquireDDThreadLock();
156 /* Increment the internal ref counter */
159 /* Increment the local internal ref counter */
160 This
->lpLcl
->dwLocalRefCnt
++;
162 if (This
->lpLcl
->lpGbl
!= NULL
)
164 /* Increment the gobal internal ref counter */
165 This
->lpLcl
->lpGbl
->dwRefCnt
++;
168 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
175 retValue
= This
->dwIntRefCnt
;
177 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
183 /* Release the thread lock */
184 ReleaseDDThreadLock();
186 /* Return the local Ref counter */
194 Main_DirectDraw_Release (LPDDRAWI_DIRECTDRAW_INT This
)
200 /* Lock the thread */
201 AcquireDDThreadLock();
207 This
->lpLcl
->dwLocalRefCnt
--;
210 if (This
->lpLcl
->lpGbl
!= NULL
)
212 This
->lpLcl
->lpGbl
->dwRefCnt
--;
215 if ( This
->lpLcl
->lpGbl
->dwRefCnt
== 0)
217 // set resoltion back to the one in registry
218 /*if(This->cooperative_level & DDSCL_EXCLUSIVE)
220 ChangeDisplaySettings(NULL, 0);
226 /* FIXME cleanup being not call why ?? */
227 Counter
= This
->dwIntRefCnt
;
231 Counter
= This
->dwIntRefCnt
;
234 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
239 /* Release the thread lock */
240 ReleaseDDThreadLock();
247 Main_DirectDraw_Initialize (LPDDRAWI_DIRECTDRAW_INT This
, LPGUID lpGUID
)
249 return DDERR_ALREADYINITIALIZED
;
254 * @name DDraw->Compact
257 * In exlusive mode the function DDraw->Compact returns DERR_NOEXCLUSIVEMODE, otherwise it returns DD_OK
260 * Returns only error code DD_OK or DERR_NOEXCLUSIVEMODE
263 * Microsoft says Compact is not implemented in ddraw.dll, but it returns DDERR_NOEXCLUSIVEMODE or DD_OK
267 Main_DirectDraw_Compact(LPDDRAWI_DIRECTDRAW_INT This
)
269 HRESULT retVal
= DD_OK
;
273 /* Lock the thread */
274 AcquireDDThreadLock();
278 /* Check if Exclusive mode has been activated */
279 if (This
->lpLcl
->lpGbl
->lpExclusiveOwner
!= This
->lpLcl
)
281 retVal
= DDERR_NOEXCLUSIVEMODE
;
284 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
289 /* Release the thread lock */
290 ReleaseDDThreadLock();
296 Main_DirectDraw_GetAvailableVidMem(LPDDRAWI_DIRECTDRAW_INT This
, LPDDSCAPS ddscaps
, LPDWORD dwTotal
, LPDWORD dwFree
)
299 HRESULT retValue
= DD_OK
;
301 ZeroMemory(&myddscaps
, sizeof(DDSCAPS2
));
305 myddscaps
.dwCaps
= ddscaps
->dwCaps
;
306 retValue
= Main_DirectDraw_GetAvailableVidMem4(This
, &myddscaps
, dwTotal
, dwFree
);
308 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
310 retValue
= DDERR_INVALIDPARAMS
;
318 Main_DirectDraw_GetAvailableVidMem4(LPDDRAWI_DIRECTDRAW_INT This
, LPDDSCAPS2 ddscaps
,
319 LPDWORD dwTotal
, LPDWORD dwFree
)
321 HRESULT retVal
= DD_OK
;
322 DDHAL_GETAVAILDRIVERMEMORYDATA memdata
;
328 // There is no HEL implentation of this api
329 if (!(This
->lpLcl
->lpDDCB
->HALDDMiscellaneous
.dwFlags
& DDHAL_MISCCB32_GETAVAILDRIVERMEMORY
) ||
330 (This
->lpLcl
->lpGbl
->dwFlags
& DDRAWI_NOHARDWARE
) )
332 retVal
= DDERR_NODIRECTDRAWHW
;
336 if ((!dwTotal
&& !dwFree
) || !ddscaps
)
338 retVal
= DDERR_INVALIDPARAMS
;
342 if ( ddscaps
->dwCaps
& (DDSCAPS_BACKBUFFER
| DDSCAPS_COMPLEX
| DDSCAPS_FLIP
|
343 DDSCAPS_FRONTBUFFER
| DDSCAPS_PALETTE
| DDSCAPS_SYSTEMMEMORY
|
344 DDSCAPS_VISIBLE
| DDSCAPS_WRITEONLY
| DDSCAPS_OWNDC
))
346 retVal
= DDERR_INVALIDPARAMS
;
351 /* ddscaps->dwCaps2 & 0x01
352 this flag is outdate and are
353 set to 0 in ms dxsdk the name of
354 this flag is DDSCAPS2_HARDWAREDEINTERLACE
357 if ( ddscaps
->dwCaps2
& 0x01)
359 retVal
= DDERR_INVALIDCAPS
;
363 if ( ddscaps
->dwCaps3
& ~( DDSCAPS3_MULTISAMPLE_QUALITY_MASK
| DDSCAPS3_MULTISAMPLE_MASK
|
364 DDSCAPS3_RESERVED1
| DDSCAPS3_RESERVED2
|
365 DDSCAPS3_LIGHTWEIGHTMIPMAP
| DDSCAPS3_AUTOGENMIPMAP
|
368 retVal
= DDERR_INVALIDCAPS
;
372 if ( ddscaps
->dwCaps4
)
374 retVal
= DDERR_INVALIDCAPS
;
378 ZeroMemory(&memdata
, sizeof(DDHAL_GETAVAILDRIVERMEMORYDATA
));
379 memdata
.lpDD
= This
->lpLcl
->lpGbl
;
380 memdata
.ddRVal
= DDERR_INVALIDPARAMS
;
382 memdata
.ddsCapsEx
.dwCaps2
= ddscaps
->dwCaps2
;
383 memdata
.ddsCapsEx
.dwCaps3
= ddscaps
->dwCaps3
;
385 This
->lpLcl
->lpGbl
->hDD
= This
->lpLcl
->hDD
;
387 if (This
->lpLcl
->lpDDCB
->HALDDMiscellaneous
.GetAvailDriverMemory(&memdata
) == DDHAL_DRIVER_NOTHANDLED
)
389 retVal
= DDERR_NODIRECTDRAWHW
;
400 *dwTotal
= memdata
.dwTotal
;
403 *dwFree
= memdata
.dwFree
;
405 retVal
= memdata
.ddRVal
;
409 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
418 Main_DirectDraw_GetFourCCCodes(LPDDRAWI_DIRECTDRAW_INT This
, LPDWORD lpNumCodes
, LPDWORD lpCodes
)
420 HRESULT retVal
= DD_OK
;
425 // EnterCriticalSection(&ddcs);
429 if(IsBadWritePtr(lpNumCodes
,sizeof(LPDWORD
)))
431 retVal
= DDERR_INVALIDPARAMS
;
435 if(!(IsBadWritePtr(lpNumCodes
,sizeof(LPDWORD
))))
439 if (*lpNumCodes
> This
->lpLcl
->lpGbl
->dwNumFourCC
)
441 *lpNumCodes
= This
->lpLcl
->lpGbl
->dwNumFourCC
;
444 size
= *lpNumCodes
* sizeof(DWORD
);
446 if(!IsBadWritePtr(lpCodes
, size
))
448 memcpy(lpCodes
, This
->lpLcl
->lpGbl
->lpdwFourCC
, size
);
452 *lpNumCodes
= This
->lpLcl
->lpGbl
->dwNumFourCC
;
457 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
462 //LeaveCriticalSection(&ddcs);
468 * We can optain the version of the directdraw object by compare the
469 * vtl table pointer from iface we do not need pass which version
472 * Main_DirectDraw_CreateSurface is dead at moment we do only support
473 * directdraw 7 at moment
476 /* For DirectDraw 1 - 3 */
478 Main_DirectDraw_CreateSurface (LPDDRAWI_DIRECTDRAW_INT This
, LPDDSURFACEDESC pDDSD
,
479 LPDDRAWI_DDRAWSURFACE_INT
*ppSurf
, IUnknown
*pUnkOuter
)
481 HRESULT ret
= DDERR_GENERIC
;
482 DDSURFACEDESC2 dd_desc_v2
;
486 EnterCriticalSection(&ddcs
);
491 if (pDDSD
->dwSize
== sizeof(DDSURFACEDESC
))
493 CopyDDSurfDescToDDSurfDesc2(&dd_desc_v2
, (LPDDSURFACEDESC
)pDDSD
);
494 ret
= Internal_CreateSurface(This
,
501 ret
= DDERR_INVALIDPARAMS
;
504 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
506 ret
= DDERR_INVALIDPARAMS
;
509 LeaveCriticalSection(&ddcs
);
514 /* For DirectDraw 4 - 7 */
516 Main_DirectDraw_CreateSurface4(LPDDRAWI_DIRECTDRAW_INT This
, LPDDSURFACEDESC2 pDDSD
,
517 LPDDRAWI_DDRAWSURFACE_INT
*ppSurf
, IUnknown
*pUnkOuter
)
522 EnterCriticalSection(&ddcs
);
527 ret
= Internal_CreateSurface(This
, pDDSD
, ppSurf
, pUnkOuter
);
529 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
531 ret
= DDERR_INVALIDPARAMS
;
536 Main_DirectDraw_AddRef(This
);
538 LeaveCriticalSection(&ddcs
);
542 /* 5 of 31 DirectDraw7_Vtable api are working simluare to windows */
543 /* 8 of 31 DirectDraw7_Vtable api are under devloping / testing */
545 HRESULT WINAPI
Main_DirectDraw_CreatePalette(LPDDRAWI_DIRECTDRAW_INT This
, DWORD dwFlags
,
546 LPPALETTEENTRY palent
, LPDIRECTDRAWPALETTE
* ppPalette
, LPUNKNOWN pUnkOuter
)
551 EnterCriticalSection(&ddcs
);
556 ret
= Internal_CreatePalette(This
, dwFlags
, palent
, ppPalette
, pUnkOuter
);
558 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
560 ret
= DDERR_INVALIDPARAMS
;
564 //Versions 7 and 4 are addref'ed
565 if((This
->lpVtbl
== &DirectDraw7_Vtable
|| This
->lpVtbl
== &DirectDraw4_Vtable
) && *ppPalette
!= NULL
)
566 Main_DirectDraw_AddRef(This
) ;
568 LeaveCriticalSection(&ddcs
);