No title Revision 363131343634 (Thu Jun 25 2009 at 18:50) - Diff Link to this snippet: https://friendpaste.com/1Mp0rKqgCePj2hXLYDysoh Embed: manni perldoc borland colorful default murphy trac fruity autumn bw emacs pastie friendly Show line numbers Wrap lines 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258#include <stdio.h>#include <stdlib.h>#include <SDL/SDL.h>#include <string.h>#define TAILLEMAX 2000enum {BAS, DROITE, HAUT, GAUCHE};void initialiseSerpent(SDL_Rect positionSerpent[], SDL_Surface *serpent, SDL_Surface *ecran, int taille);void blitteSerpent(SDL_Surface *serpent, SDL_Surface *ecran, SDL_Rect positionSerpent[], int taille, int direction, SDL_Surface *tete[]);void decalePositions(SDL_Rect positionSerpent[], int taille);void avanceSerpent(SDL_Surface *serpent, SDL_Rect positionSerpent[], int direction);void perdu(SDL_Surface *boum, SDL_Surface *ecran, SDL_Rect positionSerpent[]);int cogne(SDL_Rect positionSerpent[], SDL_Surface *ecran, int taille);int surSerpent(SDL_Rect positionSerpent[], int taille);void demarreSdl(SDL_Surface **ecran, char titre[]){ if(SDL_Init(SDL_INIT_VIDEO) == -1) { fprintf(stderr, "Erreur d'initialisation de la SDL : %s\n", SDL_GetError()); // Ecriture de l'erreur exit(EXIT_FAILURE); // On quitte le programme } SDL_WM_SetIcon(SDL_LoadBMP("ico.bmp"), NULL); *ecran = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_RESIZABLE | SDL_DOUBLEBUF); if(*ecran == NULL) { fprintf(stderr, "Impossible de charger le mode vidéo: %s\n", SDL_GetError()); } SDL_WM_SetCaption(titre, NULL); SDL_EnableKeyRepeat(1, 1);}void fermeSdl(SDL_Surface *serpent, SDL_Surface *boum){ SDL_FreeSurface(serpent); SDL_FreeSurface(boum); SDL_Quit();}void evenements(SDL_Surface *ecran, SDL_Surface *serpent, SDL_Surface *boum, SDL_Surface *tete[]){ int continuer =1, direction=BAS, taille=5, i=0; SDL_Event evenement; SDL_Rect positionSerpent[TAILLEMAX]; initialiseSerpent(positionSerpent, serpent, ecran, taille); i=100; while (continuer) { SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 2, 100, 40)); blitteSerpent(serpent, ecran, positionSerpent, taille, direction, tete); SDL_Flip(ecran); if(i==100) { decalePositions(positionSerpent, taille); avanceSerpent(serpent, positionSerpent, direction); i=0; } else { i++; } if(SDL_PollEvent(&evenement)) { switch(evenement.type) { case SDL_KEYDOWN: switch(evenement.key.keysym.sym) { case SDLK_RIGHT: if(direction != GAUCHE) direction = DROITE; break; case SDLK_LEFT: if(direction != DROITE) direction = GAUCHE; break; case SDLK_UP: if(direction != BAS) direction = HAUT; break; case SDLK_DOWN: if(direction != HAUT) direction = BAS; break; case SDLK_PLUS: case SDLK_p: if(taille<TAILLEMAX) taille++; break; case SDLK_MINUS: case SDLK_m: if(taille>0) taille--; break; case SDLK_ESCAPE: continuer = 0; break; default: break; } break; case SDL_QUIT: continuer =0; break; default: break; } } if(cogne(positionSerpent, ecran, taille)) { perdu(boum, ecran, positionSerpent); continuer =0; } }}void initialise(SDL_Surface *ecran, SDL_Surface **serpent, SDL_Surface **boum, SDL_Surface *tete[]){ int i=0; char nom[10]; SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 2, 100, 40)); *serpent = SDL_LoadBMP("serpent.bmp"); *boum = SDL_LoadBMP("boum.bmp"); for(i=0; i<4; i++) { sprintf(nom, "tete%d.bmp", i); tete[i]= SDL_LoadBMP(nom); }}void initialiseSerpent(SDL_Rect positionSerpent[], SDL_Surface *serpent, SDL_Surface *ecran, int taille){ puts("foo"); int i=0; for(i=0; i<TAILLEMAX; i++) { positionSerpent[i].x = ((ecran->w)/2)-((serpent->w)/2); positionSerpent[i].y = ((ecran->h/2)-(serpent->h/2)); } puts("bar");}void blitteSerpent(SDL_Surface *serpent, SDL_Surface *ecran, SDL_Rect positionSerpent[], int taille, int direction, SDL_Surface *tete[]){ int i; SDL_Rect positionTete; positionTete.x = positionSerpent[0].x; positionTete.y = positionSerpent[0].y; switch(direction) { case BAS: positionTete.x = positionSerpent[0].x+tete[0]->w-serpent->w; SDL_BlitSurface(tete[0], NULL, ecran, &positionTete); break; case DROITE: positionTete.y = positionSerpent[0].y+tete[1]->h-serpent->h; SDL_BlitSurface(tete[1], NULL, ecran, &positionTete); break; case HAUT: positionTete.y = positionSerpent[0].y-tete[2]->h+serpent->h; SDL_BlitSurface(tete[2], NULL, ecran, &positionTete); break; case GAUCHE: positionTete.x = positionSerpent[0].x-tete[3]->w+serpent->w; SDL_BlitSurface(tete[3], NULL, ecran, &positionTete); break; } for(i=1; i<taille; i++) { SDL_BlitSurface(serpent, NULL, ecran, &positionSerpent[i]); }}void decalePositions(SDL_Rect positionSerpent[], int taille){ int i; for(i=1; i<TAILLEMAX; i++) { positionSerpent[TAILLEMAX-i].x = positionSerpent[TAILLEMAX-i-1].x; positionSerpent[TAILLEMAX-i].y = positionSerpent[TAILLEMAX-i-1].y; }}void avanceSerpent(SDL_Surface *serpent, SDL_Rect positionSerpent[], int direction){ switch(direction) { case BAS: positionSerpent[0].y+=serpent->h+1; break; case DROITE: positionSerpent[0].x+=serpent->w+1; break; case HAUT: positionSerpent[0].y-=serpent->h+1; break; case GAUCHE: positionSerpent[0].x-=serpent->w+1; break; }}int cogne(SDL_Rect positionSerpent[], SDL_Surface *ecran, int taille){ if(positionSerpent[0].x <0 || positionSerpent[0].x >= ecran->w || positionSerpent[0].y<0 || positionSerpent[0].y >= ecran->h) { return 1; } else if(surSerpent(positionSerpent, taille)) { return 1; } else { return 0; }}int surSerpent(SDL_Rect positionSerpent[], int taille){ int resultat=0, i; for(i=1; i<taille; i++) { if(positionSerpent[0].x == positionSerpent[i].x && positionSerpent[0].y == positionSerpent[i].y) { resultat = 1; } } return resultat;}void perdu(SDL_Surface *boum, SDL_Surface *ecran, SDL_Rect positionSerpent[]){ SDL_Rect position; printf("Out !!%d\n", positionSerpent[0].x); position.x=ecran->w/2-boum->w/2; position.y=ecran->h/2-boum->h/2; printf("%d, %d:%d", SDL_BlitSurface(boum, NULL, ecran, &position), position.x, position.y);}int main(int argc, char* argv[]){ SDL_Surface *ecran, *serpent, *boum, *tete[4]; char titre[20] = "Snake"; demarreSdl(&ecran, titre); initialise(ecran, &serpent, &boum, tete); evenements(ecran, serpent, boum, tete); fermeSdl(serpent, boum); return 0;}