Accueil > > > DIRECT DRAW, SOUND / HELLO WORLD (VC++ 5.0)
DIRECT DRAW, SOUND / HELLO WORLD (VC++ 5.0)
Information sur la source
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
- #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;
- }
#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
Sources du même auteur
Sources de la même categorie
Commentaires et avis
|
Derniers Blogs
IMAGINE CUP 2012, MAKE A SIGN EN FINALEIMAGINE CUP 2012, MAKE A SIGN EN FINALE par junarnoalg
Voilà qui est fait, la nouvelle est officielle ! L'équipe belge "Make a Sign" va au pays des kangourous défendre son projet dans la catégorie Software Design. http://www.imaginecup.com/CompetitionsContent/Competition/WorldwideFinalists.aspx V...
Cliquez pour lire la suite de l'article par junarnoalg KINECT 1.5 IS OUT !KINECT 1.5 IS OUT ! par Vko
La version 1.5 du Kinect For Microsoft vient tout juste de sortir ! Plein de nouveautés: Tracking de squelette en Near Mode Détection en position assise Détection faciale avec un SDK dédié Documentation et des guideline (enfin) Un out...
Cliquez pour lire la suite de l'article par Vko LES ACTUALITéS DE LA SEMAINE SUR C2I.FR (14 MAI - 20 MAI) LES ACTUALITéS DE LA SEMAINE SUR C2I.FR (14 MAI - 20 MAI) par richardc
Mise à jour des Web API du 14 Mai
Réservez dès maintenant votre journée du 20 juin pour le Windows Azure Dev Camp 2012 à Paris
Mise à jour de Team Foundation Service
MechCommander 2 sur Windows 8
Entity Framework 5 Release Candidate e...
Cliquez pour lire la suite de l'article par richardc REACTIVE EXTENSIONS : CONSOMMER DES SERVICES AVEC RX PARTIE 3, LES PIèGES à éVITERREACTIVE EXTENSIONS : CONSOMMER DES SERVICES AVEC RX PARTIE 3, LES PIèGES à éVITER par Groc
Une mauvaise utilisation de rx lors de l'écriture d'une couche d'accès à des services peut conduire à des cas embarassants avec des erreurs mal gérées, des appels qui ne partent lorsqu'ils le devraient, et même des résultats incorrects . le tout nuis...
Cliquez pour lire la suite de l'article par Groc SHAREPOINT BLOG SITE, PROBLèME D'ARCHIVESSHAREPOINT BLOG SITE, PROBLèME D'ARCHIVES par junarnoalg
Dernièrement, nous avons migré le site
myTIC
vers un nouveau serveur SharePoint 2010. Dans les contenus que nous vouloins récupérer, nous avions un certain nombre de blogs.
Nous avons utilisé les commandes Power...
Cliquez pour lire la suite de l'article par junarnoalg
Forum
MATRICE TEMPLATEMATRICE TEMPLATE par hjr2610
Cliquez pour lire la suite par hjr2610 RE : SAC A DOS RE : SAC A DOS par hadjkaddour
Cliquez pour lire la suite par hadjkaddour
Logiciels
sDEVIS-FACTURES vlPRO (8.1.0.3)SDEVIS-FACTURES VLPRO (8.1.0.3)sDEVIS-FACTURES vlPRO a été mis au point pour les particuliers, créateurs, entrepreneurs, artisa... Cliquez pour télécharger sDEVIS-FACTURES vlPRO 974 Application Server (12.2.4.6)974 APPLICATION SERVER (12.2.4.6)Développez de puissantes applications dans un environnement de 'cloud computing', clusterisé, séc... Cliquez pour télécharger 974 Application Server vPicture (1.4.2.1)VPICTURE (1.4.2.1)Avec vPicture, hébergez vos images facilement et rapidement.
vPicture est un utilitaire simple, ... Cliquez pour télécharger vPicture Easy-Planning (2.2.1.6)EASY-PLANNING (2.2.1.6)Easy-Planning permet de créer des plannings sous la représentation de diagrammes et est adapté au... Cliquez pour télécharger Easy-Planning COM-BACKUP (2.0)COM-BACKUP (2.0)
COM-BACKUP est un logiciel de sauvegarde qui permet de planifier les sauvegardes de vos dossiers ...
Cliquez pour télécharger COM-BACKUP
|