RECAPITULATIF DIRECTDRAW


Voici la routine presque entiere d'initialisation de Boulder Dash X. Les surfaces contenant les graphismes sont creees par la fonction BitmapSurface("nom du fichier bmp").
Cette fonction est traitee sur une autre page
Ces surfaces sont affichees en jaune dans la fonction.


Les surfaces en jaunes de Boulder Dash X sont declarees en global de cette facon:

LPDIRECTDRAW7 lpDD =NULL; //objet DirectDraw
LPDIRECTDRAWSURFACE7 FondEcran =NULL; //surface primaire
LPDIRECTDRAWSURFACE7 Texte =NULL; //bitmap
LPDIRECTDRAWSURFACE7 Layer[2];
LPDIRECTDRAWSURFACE7 BackBuffer =NULL; //BackBuffer
LPDIRECTDRAWSURFACE7 Sprite =NULL; //Image des sprites
LPDIRECTDRAWSURFACE7 Edition =NULL; //Image des sprites
LPDIRECTDRAWCLIPPER lpClip


 

static BOOL Init(HINSTANCE hInstance,int nCmdShow)
{

//Variables locales

WNDCLASS wc;
HRESULT hRet,hr;
DDSURFACEDESC2 ddsd;
DDPIXELFORMAT ddpf;
DDCOLORKEY Cle;

//definit et inscrit la class de fenetre

wc.style= CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc= (WNDPROC) WindowProc;
wc.cbClsExtra= 0;
wc.cbWndExtra= sizeof(DWORD);
wc.hInstance= hInstance;
wc.hIcon= NULL;
wc.hCursor= LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground= (HBRUSH) GetStockObject(BLACK_BRUSH);
wc.lpszMenuName= NULL; //MAKEINTRESOURCE(IDR_MENU);
wc.lpszClassName= szClass;

//Teste si la fenetre en initialisation est correcte

if (!RegisterClass(&wc))
{
MessageBox(NULL,"Erreur de registration fenêtre","Totalwar",MB_OK);
return FALSE;
}

//Obtient les dimensions de l'ecran

int ScreenWidth=GetSystemMetrics(SM_CXSCREEN);
int ScreenHeight=GetSystemMetrics(SM_CYSCREEN);

//cree une fenetre et genere l'affichage
HWND hWnd;

hWnd=CreateWindow(szClass,"Boulder",WS_POPUP, 0, 0, ScreenWidth, ScreenHeight, NULL, NULL, hInstance, NULL);

if (!hWnd)
{
MessageBox(NULL,"Erreur création de la fenêtre","Totalwar",MB_OK);
return FALSE;
}

ShowWindow(hWnd,nCmdShow);
ShowCursor(FALSE); // Interdit l'affichage de la souris

UpdateWindow(hWnd);

// Creation de l'Objet Directdraw

hRet = DirectDrawCreateEx(NULL,(LPVOID*)&lpDD,IID_IDirectDraw7 , NULL);
if (hRet != DD_OK)
{
MessageBox(NULL,"Erreur Création objet DDraw","Boulder",MB_OK);
return FALSE;
}

// Fixe le niveau de cooperation (Fullscreen)

hRet = lpDD->SetCooperativeLevel(hWnd,DDSCL_FULLSCREEN|DDSCL_EXCLUSIVE |DDSCL_ALLOWREBOOT);
if (hRet != DD_OK)
{
MessageBox(NULL,"Erreur création coopération FULLSCREEN EXCLUSIVE","Boulder",MB_OK);
return FALSE;
}

// Fixe la resolution ecran (640*480*16)

hRet = lpDD->SetDisplayMode(640,480,16,0,0);
if (hRet != DD_OK)
{
MessageBox(NULL,"Erreur en tentant le 640x480x16b !","Boulder",MB_OK);
return FALSE;
}



// Cree un clipper

hRet = lpDD->CreateClipper(NULL,&lpClip,NULL);
if (hRet != DD_OK)
{
MessageBox(NULL,"Erreur clipper","Boulder",MB_OK);
return FALSE;
}

lpClip->SetHWnd(0,hWnd);

// Creation de la surface primaire + backbuffer

ZeroMemory(&ddsd,sizeof(ddsd));
ddsd.dwSize = sizeof( ddsd );
ddsd.dwFlags = DDSD_CAPS|DDSD_BACKBUFFERCOUNT;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE|DDSCAPS_FLIP|DDSCAPS_COMPLEX;
ddsd.dwBackBufferCount = 1;
hRet = lpDD->CreateSurface( &ddsd, &FondEcran, NULL );
if (hRet!=DD_OK)
{
MessageBox(NULL,"Erreur création surface primaire","Boulder",MB_OK);
return FALSE;
}

// Obtenir l'interface du BackBuffer

DDSCAPS2 ddscaps;
ZeroMemory(&ddscaps, sizeof(ddscaps));
ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
ddscaps.dwCaps2 = 0;
ddscaps.dwCaps3 = 0;
ddscaps.dwCaps4 = 0;
hRet = FondEcran->GetAttachedSurface(&ddscaps,&BackBuffer);
if (hRet != DD_OK)
{
MessageBox(NULL,"Erreur création BackBuffer","Boulder",MB_OK);
return FALSE;
}


FondEcran->SetClipper(lpClip);

// Initialistion de la cle de couleur (0,255,0)
// pour les images sources
// La cle couleur transparente sera le vert sature (0,255,0)


ddpf.dwSize=sizeof(ddpf);
BackBuffer->GetPixelFormat(&ddpf);

KeyColor=ddpf.dwGBitMask;

Cle.dwColorSpaceHighValue=KeyColor;
Cle.dwColorSpaceLowValue=KeyColor;

FondEcran->SetColorKey(DDCKEY_SRCBLT, &Cle);
Layer[0]->SetColorKey(DDCKEY_SRCBLT, &Cle);
Layer[1]->SetColorKey(DDCKEY_SRCBLT, &Cle);
Texte->SetColorKey(DDCKEY_SRCBLT, &Cle);
Sprite->SetColorKey(DDCKEY_SRCBLT, &Cle);
Edition->SetColorKey(DDCKEY_SRCBLT, &Cle);

BackBuffer->SetColorKey(DDCKEY_SRCBLT, &Cle);

BInit=TRUE;

return TRUE;
}


La fonction d'initialisation est appelee par la fonction winmain de cette facon:

if (!Init(hInstance,nCmdShow))
{
Cleanup();
MessageBox(NULL,"Erreur initialisation de l'instance","Totalwar",MB_OK);
return FALSE;
}