Code:
BOOL Setup()
{
//Data
DDSCAPS2 SurfaceCaps;
DDSURFACEDESC2 SurfaceDesc;
DDBLTFX BlitFx;
//Create DirectDraw object
if(FAILED(DirectDrawCreateEx(NULL, (LPVOID*)&DirectDraw, IID_IDirectDraw7, NULL)))
{
MessageBox(MainWindow, "Unable to create DirectDraw object!", "Setup failed!", MB_OK | MB_ICONINFORMATION);
return FALSE;
}
//Set the cooperative level
if(FAILED(DirectDraw->SetCooperativeLevel(MainWindow, DDSCL_NORMAL | DDSCL_ALLOWREBOOT)))
{
MessageBox(MainWindow, "Unable to set Cooperative level!", "Setup failed!", MB_OK | MB_ICONINFORMATION);
return FALSE;
}
//Set parameters
ZeroMemory(&SurfaceDesc, sizeof(DDSURFACEDESC2));
SurfaceDesc.dwSize = sizeof(DDSURFACEDESC2);
SurfaceDesc.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT | DDSD_WIDTH | DDSD_HEIGHT;
SurfaceDesc.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX;
SurfaceDesc.dwBackBufferCount = 1;
SurfaceDesc.dwWidth = SCREENWIDTH;
SurfaceDesc.dwHeight = SCREENHEIGHT;
//Create the primary surface
if(FAILED(DirectDraw->CreateSurface(&SurfaceDesc, &PrimarySurface, NULL)))
{
MessageBox(MainWindow, "Unable to create primary surface!", "Setup failed!", MB_OK | MB_ICONINFORMATION);
return FALSE;
}
//Set parameters
ZeroMemory(&SurfaceCaps, sizeof(DDSCAPS2));
SurfaceCaps.dwCaps = DDSCAPS_BACKBUFFER;
//Create the secondary surface (back buffer)
if(FAILED(PrimarySurface->GetAttachedSurface(&SurfaceCaps, &SecondarySurface)))
{
MessageBox(MainWindow, "Unable to create secondary surface!", "Setup failed!", MB_OK | MB_ICONINFORMATION);
return FALSE;
}
//Initiate all buffers with black
/*
ZeroMemory(&BlitFx, sizeof(DDBLTFX));
BlitFx.dwSize = sizeof(DDBLTFX);
BlitFx.dwFillColor = 0x00000000;
PrimarySurface->Blt(NULL, NULL, NULL, DDBLT_COLORFILL, &BlitFx);
SecondarySurface->Blt(NULL, NULL, NULL, DDBLT_COLORFILL, &BlitFx);
*/
//Creates a clipper
if(FAILED(DirectDraw->CreateClipper(0, &Clipper, NULL)))
{
MessageBox(MainWindow, "Unable to create Clipper!", "Setup failed!", MB_OK | MB_ICONINFORMATION);
return FALSE;
}
//Attaches the clipper to the window
if(FAILED(Clipper->SetHWnd(0, MainWindow)))
{
Clipper->Release();
MessageBox(MainWindow, "Unable to attach Clipper to window!", "Setup failed!", MB_OK | MB_ICONINFORMATION);
return FALSE;
}
//Attaches the clipper to the primary surface
if(FAILED(PrimarySurface->SetClipper(Clipper)))
{
Clipper->Release();
MessageBox(MainWindow, "Unable to attach Clipper to primary surface!", "Setup failed!", MB_OK | MB_ICONINFORMATION);
return FALSE;
}
//Load the graphics
if(LoadGraphics() == FALSE)
{
return FALSE;
}
//Return success
return TRUE;
}
MainWindow is a window (a dialog actually).