Revision 363131343634 () - Diff

Link to this snippet: https://friendpaste.com/1Mp0rKqgCePj2hXLYDysoh
Embed:
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#include <stdio.h>
#include <stdlib.h>
#include <SDL/SDL.h>
#include <string.h>
#define TAILLEMAX 2000
enum {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;
}