Direct draw, sound / hello world (vc++ 5.0)

Description

voila la source d'un petit programme pour ecrire hello world avec des briques, avec un pacman qui passe derriere en rebondissant et en fesant des bruits ...
un hello world classique ...

Source / Exemple :


#include <windows.h>
#include <windowsx.h>
#include <stdio.h>
#include <malloc.h>
#include <time.h>
#include <ddraw.h>
#include <dsound.h>
#include "dsutil.h"
#include "ddutil.h"

char helloworld[375] = {
1,0,0,1,0,1,1,1,1,0,1,0,0,0,0,1,0,0,0,0,1,1,1,1,0,
1,0,0,1,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,1,0,
1,0,0,1,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,1,0,
1,1,1,1,0,1,1,1,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,1,0,
1,0,0,1,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,1,0,
1,0,0,1,0,1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1,0,0,1,0,
1,0,0,1,0,1,1,1,1,0,1,1,1,1,0,1,1,1,1,0,1,1,1,1,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,0,0,0,1,0,1,1,1,1,0,1,1,1,0,0,1,0,0,0,0,1,1,1,0,
1,0,0,0,1,0,1,0,0,1,0,1,0,0,1,0,1,0,0,0,0,1,0,0,1,
1,0,0,0,1,0,1,0,0,1,0,1,0,0,1,0,1,0,0,0,0,1,0,0,1,
1,0,0,0,1,0,1,0,0,1,0,1,1,1,0,0,1,0,0,0,0,1,0,0,1,
1,0,1,0,1,0,1,0,0,1,0,1,1,0,0,0,1,0,0,0,0,1,0,0,1,
1,1,0,1,1,0,1,0,0,1,0,1,0,1,0,0,1,0,0,0,0,1,0,0,1,
1,0,0,0,1,0,1,1,1,1,0,1,0,0,1,0,1,1,1,1,0,1,1,1,0
};

LPDIRECTDRAW	pDD;
LPDIRECTDRAWSURFACE		pDDSPrim,pDDSSec;
LPDIRECTDRAWSURFACE	pDDSFond, pDDSBriques, pDDSPacman;
DDSCAPS	ddscaps;
DDSURFACEDESC	ddsd;
LPDIRECTDRAWCLIPPER	pDDClip;
RGNDATA	*RegionData;

#define NUMEVENTS 2
 
LPDIRECTSOUND	lpds;
HSNDOBJ			HSOBoing = NULL;

DWORD LastGetTick;
unsigned int tempus, tempus2;
int x,y,depx,depy;

HICON hIcon;
HCURSOR hCursor;

int WINAPI WinMain (HINSTANCE, HINSTANCE, LPSTR, int);
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
void Dessine(LPDIRECTDRAWSURFACE,int,int,int,int,int,int,DWORD);
void Update_Frame(void);

int WINAPI WinMain(HINSTANCE hThisInstance,HINSTANCE hPrevInstance,LPSTR lpszArgument,int nFunsterStil){
	HWND hwnd;
	MSG msg;
	WNDCLASSEX wincl;
	RECT *rc;
	int i, i2;

	srand( (unsigned)time( NULL ) );
	wincl.hInstance = hThisInstance;
	wincl.lpszClassName = "HelloWorld";
	wincl.lpfnWndProc = WindowProcedure;	
	wincl.style = CS_HREDRAW|CS_VREDRAW|CS_BYTEALIGNWINDOW;
	wincl.cbSize = sizeof(WNDCLASSEX);

	hIcon = LoadIcon(NULL, IDI_APPLICATION);
	hCursor = LoadCursor(NULL, IDC_ARROW);
	wincl.hIcon = hIcon;
	wincl.hIconSm = hIcon;
	wincl.hCursor = hCursor;
	wincl.lpszMenuName = NULL;
	wincl.cbClsExtra = 0;
	wincl.cbWndExtra = 0;
	wincl.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
	if(!RegisterClassEx(&wincl)) return 0;
	hwnd = CreateWindowEx(0, "HelloWorld", "coucou", WS_POPUP, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN), NULL, NULL, hThisInstance, NULL);

	if (DirectDrawCreate(NULL, &pDD, NULL) != DD_OK) exit(1);
	pDD->SetCooperativeLevel(hwnd, DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN);
	pDD->SetDisplayMode(640, 480, 16);
	memset(&ddsd, 0, sizeof ddsd);
	ddsd.dwSize = sizeof(ddsd);
	ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;
	ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_FLIP | DDSCAPS_COMPLEX;
	ddsd.dwBackBufferCount = 1;
	pDD->CreateSurface(&ddsd, &pDDSPrim, NULL);
	pDD->CreateClipper(NULL,&pDDClip,NULL);
	RegionData=(RGNDATA *) malloc(48);
	RegionData->rdh.dwSize = 32;
	RegionData->rdh.iType = RDH_RECTANGLES;
	RegionData->rdh.nCount = 1;
	RegionData->rdh.nRgnSize = 16;
	RegionData->rdh.rcBound.left = 0;
	RegionData->rdh.rcBound.right = 640;
	RegionData->rdh.rcBound.top = 0;
	RegionData->rdh.rcBound.bottom = 480;
	rc = (RECT *) &RegionData->Buffer[0];
	rc->left = 0;
	rc->right = 640;
	rc->top = 0;
	rc->bottom = 480;
	pDDClip->SetClipList(RegionData,0);
	ddscaps.dwCaps = DDSCAPS_BACKBUFFER;
	pDDSPrim->GetAttachedSurface(&ddscaps, &pDDSSec);
	pDDSSec->SetClipper(pDDClip);
	pDDSFond = DDLoadBitmap(pDD, "fond.bmp", 0, 0);
	pDDSBriques = DDLoadBitmap(pDD, "brique.bmp", 0, 0);
	pDDSPacman = DDLoadBitmap(pDD, "pacman.bmp", 0, 0);
	DDSetColorKey(pDDSPacman, RGB(255, 0, 255));

	for(i=0;i<15;i++)
		for(i2=0;i2<25;i2++)
			if(helloworld[i*25+i2])
				helloworld[i*25+i2] = 1+(rand()%3);

	tempus2 = tempus = 0;
	depx = 5;
	depy;
	x = 320;
	y = 240;

    if (DirectSoundCreate(NULL, &lpds, NULL)!=DS_OK)  exit(1);
    if (lpds->SetCooperativeLevel(hwnd, DSSCL_PRIORITY)!=DS_OK) exit(1);
	HSOBoing = SndObjCreate(lpds, "BOING.WAV", 2);
	
	ShowWindow(hwnd, nFunsterStil);
	UpdateWindow(hwnd);

	LastGetTick = GetTickCount();

	while(true)
		if( PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE ) ){
			if( !GetMessage( &msg, NULL, 0, 0 ) ) return msg.wParam;
			TranslateMessage(&msg); 
			DispatchMessage(&msg);
		}else if((GetTickCount()-LastGetTick)>50){
			LastGetTick = GetTickCount();
			Update_Frame();
		};
}

void Dessine(LPDIRECTDRAWSURFACE Image,int x, int y,int ix, int iy,int iw, int ih,DWORD flag){
	RECT src,drc;
	src.left = ix;
	src.top = iy;
	src.right = ix+iw;
	src.bottom = iy+ih;
	drc.left = x;
	drc.top = y;
	drc.right = x+iw;
	drc.bottom = y+ih;
	pDDSSec->Blt(&drc,Image,&src,flag|DDBLT_WAIT,NULL);
};

void Update_Frame(void){
	int i,i2;
	tempus++;
	if((tempus&0xF)==0) tempus2++;
	Dessine(pDDSFond,0,0,0,0,640,480,NULL);

	if(((x+depx+30)>640)||((x+depx)<0)) depx = -depx;
	x+=depx;

	depy++;
	if((y+depy)>(480-30)){
		depy = -depy;
		SndObjPlay(HSOBoing, NULL);
	}
	if((y+depy)<0) depy = 0;
	y+=depy;

	Dessine(pDDSPacman,x,y,(depx < 0 ? 60 : 0)+(tempus2&0x1)*30,0,30,30,DDBLT_KEYSRC);

	for(i=0;i<15;i++)
		for(i2=0;i2<25;i2++)
			if(helloworld[i*25+i2] != 0)
				Dessine(pDDSBriques,80+i2*20,100+i*20,20*(helloworld[i*25+i2]-1),0,20,20,NULL);

	pDDSPrim->Flip(NULL, 0);
};

LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
	switch(message){
	case WM_SETCURSOR:
		SetCursor(NULL);
	return TRUE;

	case WM_KEYDOWN:
		switch (wParam){
		case VK_ESCAPE:
		case VK_F12:
			PostMessage(hwnd, WM_CLOSE, 0, 0);
		break;
		}
	break;

	case WM_DESTROY:
		pDDSPacman->Release();
		pDDSFond->Release();
		pDDSBriques->Release();
		pDDSSec->Release();
		pDDSPrim->Release();
		pDD->Release();
		free(RegionData);
		SndObjDestroy(HSOBoing);
        HSOBoing = NULL;
		lpds->Release();
		DestroyIcon(hIcon);
		DestroyCursor(hCursor);
		PostQuitMessage(0); 
	break; 

	default: 
		return DefWindowProc(hwnd, message, wParam, lParam);
	}
	return 0;
}

Conclusion :


Bon voila sinon sinon le tutorial est aussi sur :
www.ifrance.com/frenchcodeur/croq/croq.html

Codes Sources

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.