#include "CObjBufferDirectX.h"
#include "lib2Dconf.h"

#ifdef USE_DIRECTX

#define DD_ERROR(i) (i!=DD_OK) 


CObjBufferDirectX::CObjBufferDirectX(HWND hwnd):CObjBuffer()
{

	DDSURFACEDESC2              ddsd;
    HRESULT                     hRet;
    LPDIRECTDRAW                pDD;

	// creation de l'objet DirectDraw
	hRet = DirectDrawCreate(NULL, &pDD, NULL);
    if (DD_ERROR(hRet)) 
		MMechostr(MSKTRACE,"------------>DirectDrawCreate FAILED\n");

    // Fetch DirectDraw4 interface
    hRet = pDD->QueryInterface(IID_IDirectDraw4, (LPVOID *) & lpDD);
	if (DD_ERROR(hRet)) 
		MMechostr(MSKTRACE,"------------>QueryInterface FAILED\n");

    // Get normal mode
    hRet = lpDD->SetCooperativeLevel(hwnd, DDSCL_NORMAL);
	if (DD_ERROR(hRet)) 
		MMechostr(MSKTRACE,"------------>SetCooperativeLevel FAILED\n");

    // Create the primary surface
    ZeroMemory(&ddsd, sizeof(ddsd));
    ddsd.dwSize = sizeof(ddsd);
    ddsd.dwFlags = DDSD_CAPS;
    ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
    hRet = lpDD->CreateSurface(&ddsd, &lpDDSPrimary, NULL);
    if (DD_ERROR(hRet)) 
		MMechostr(MSKTRACE,"------------>CreateSurface FAILED\n");

    // Create a clipper for the primary surface
    hRet = lpDD->CreateClipper(0, &lpClipper, NULL);
	if (DD_ERROR(hRet)) 
		MMechostr(MSKTRACE,"------------>CreateClipper FAILED\n");
	hRet = lpClipper->SetHWnd(0, hwnd);
	if (DD_ERROR(hRet)) 
		MMechostr(MSKTRACE,"------------>CreateClipper FAILED\n");
	hRet = lpDDSPrimary->SetClipper(lpClipper);
}

CObjBufferDirectX::~CObjBufferDirectX()
{
	if (lpDD != NULL)
    {
        MMechostr(MSKTRACE,"----------->Release clipper...\n");
		if (lpClipper != NULL)
        {
            lpClipper->Release();
            lpClipper = NULL;
        }
        MMechostr(MSKTRACE,"----------->Release primary surface...\n");
		if (lpDDSPrimary != NULL)
        {
            lpDDSPrimary->Release();
            lpDDSPrimary = NULL;
        }
        MMechostr(1,"----------->Release IDirectDraw4...\n");
		lpDD->Release();
        lpDD = NULL;
    };
}

int  CObjBufferDirectX::GetId()
{
	return 0;
}

void CObjBufferDirectX::Blit(int dx,int dy,Layer *buffer)
{
	0;
}

void CObjBufferDirectX::Clear(int dx,int dy,int dw,int dh,int bckcolor)
{
	0;
}

int  CObjBufferDirectX::CreateScolBuffer(mmachine m,int ndxchannel,int idcont)
{
	return 0;
}

#endif