1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| SDL_Surface *ecran = NULL, *mario = NULL, *marioBas = NULL, *marioHaut = NULL, *marioGauche = NULL, *marioDroite = NULL;
SDL_Rect positionMario;
int continuer = 1, render = 0, FPS = 30;
Input in;
SDL_Init(SDL_INIT_VIDEO);
ecran = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE);
SDL_WM_SetCaption("Essais Mario", NULL);
SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran -> format, 255, 255, 255));
// Chargement de Mario
mario = IMG_Load("images/mario_bas.gif");
marioBas = IMG_Load("images/mario_bas.gif");
marioHaut = IMG_Load("images/mario_haut.gif");
marioGauche = IMG_Load("images/mario_gauche.gif");
marioDroite = IMG_Load("images/mario_droite.gif");
// Position de mario
positionMario.x = (ecran -> w / 2) - (mario -> w / 2);
positionMario.y = (ecran -> h / 2) - (mario -> h / 2);
memset(&in, 0, sizeof(in)); // Toute la structure est à 0
// Boucle principale
do {
UpdateEvent(&in);
// On gère les directions
if (in.key[SDLK_UP]){
mario = marioHaut;
positionMario.y -= 5;
}
if (in.key[SDLK_DOWN]){
mario = marioBas;
positionMario.y += 5;
}
if (in.key[SDLK_RIGHT]){
mario = marioDroite;
positionMario.x += 5;
}
if (in.key[SDLK_LEFT]){
mario = marioGauche;
positionMario.x -= 5;
}
// On efface l'écran
SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran -> format, 255, 255, 255));
render = SDL_GetTicks();
if (render > 1000 / FPS){
SDL_Delay(1000 / FPS);
}
// On affiche la nouvelle position
SDL_BlitSurface(mario, NULL, ecran, &positionMario);
SDL_Flip(ecran);
} while (!in.key[SDLK_ESCAPE]);
SDL_FreeSurface(mario);
SDL_Quit();
return EXIT_SUCCESS; |
Partager