4
// ****************************************************************************
5
// ********************************** Alea ************************************
6
// ****************************************************************************
7
int Alea (short min, short max)
9
return (unsigned short)rand() * (max - min) / 65536 + min;
13
// ****************************************************************************
14
// ****************************** ImageErreur *********************************
15
// ****************************************************************************
16
void ImageErreur (char *image)
18
// Affiche un message d'erreur et quitte le programme
19
fprintf (stderr, "Impossible de charger l'image %s (%s)\n", image, SDL_GetError ());
24
// ****************************************************************************
25
// ****************************** Case2Coord **********************************
26
// ****************************************************************************
27
int Case2CoordX (int x, GRILLE grille)
29
// Renvoit l'abscisse �cran du coin haut gauche de la case
30
return grille.x1 + x * grille.tc;
33
int Case2CoordY (int y, GRILLE grille)
35
// Renvoit l'abscisse �cran du coin haut gauche de la case
36
return grille.y1 + y * grille.tc;
40
// ****************************************************************************
41
// ****************************** Clic2Case ***********************************
42
// ****************************************************************************
43
int Clic2CaseX (int x, GRILLE grille)
45
// Renvoit l'abscisse de la case cliqu�e ou -1 si en dehors de la grille
46
if (x >= grille.x1 && x < grille.x2)
47
return (x - grille.x1 - 1) / grille.tc;
52
int Clic2CaseY (int y, GRILLE grille)
54
// Renvoit l'ordonn�e de la case cliqu�e ou -1 si en dehors de la grille
55
if (y >= grille.y1 && y <= grille.y2)
56
return (y - grille.y1 - 1) / grille.tc;
62
// ****************************************************************************
63
// ******************************* Attendre_FPS *******************************
64
// ****************************************************************************
65
void Attendre_FPS (void)
67
static int temps = 0; // Moment du dernier appel de cette fonction
69
// Marque une pause pour synchroniser l'affichage sur la vitesse choisie pour que
70
// le temps entre 2 appels de cette fonction soit de: 1000 ms / Frames Par Seconde
71
while (SDL_GetTicks() < temps + 1000 / FPS);
73
temps = SDL_GetTicks (); // Moment o� cette fonction se termine
77
// ****************************************************************************
78
// ********************************* Dans_Rect ********************************
79
// ****************************************************************************
80
BOOL Dans_Rect (int x, int y, SDL_Rect rect)
82
if (x >= rect.x && x <= (rect.x + rect.w) && y >= rect.y && y <= (rect.y + rect.h))
89
// ****************************************************************************
90
// ************************** Afficher_Caractere ******************************
91
// ****************************************************************************
92
void Afficher_Caractere (int x, int y, char c, POLICE police, SDL_Surface *ecran)
96
// Place le rectangle sur la bonne image du caract�re � afficher
97
if (c >= 'A' && c <= 'Z') police.rect.x = (c - 'A') * police.rect.w;
98
else if (c >= 'a' && c <= 'z') police.rect.x = (c - 'a') * police.rect.w;
99
else if (c >= '0' && c <= '9') police.rect.x = (c - '0' + 26) * police.rect.w;
100
else if (c == ':') police.rect.x = 36 * police.rect.w;
103
// Position sur l'�cran
106
dest.w = police.rect.w;
107
dest.h = police.rect.h;
109
// Copie le caract�re et met � jour
110
SDL_BlitSurface (police.img, &police.rect, ecran, &dest);
111
SDL_UpdateRects (ecran, 1, &dest);
115
// ****************************************************************************
116
// **************************** Afficher_Chaine *******************************
117
// ****************************************************************************
118
BOOL Afficher_Chaine (int x, int y, int vitesse, char chaine[], POLICE police, SDL_Surface *ecran, SDL_Surface *back)
120
SDL_Rect dest = {0, 0, police.rect.w, police.rect.h};
122
int xDep, yDep, xDest, yDest;
126
// Parcours la chaine de caract�re et affiche les caract�res un par un en
127
// les faisant glisser de l'un des bord de l'�cran
128
for (c = 0; chaine[c] != '\0'; c ++)
130
// Choisit al�atoirement le bord de l'�cran et sa position dessus
133
case 0: xDep = Alea (0, ecran->w - police.rect.w);
134
yDep = Alea (0, 2) ? 0 : ecran->h - police.rect.h;
136
case 1: xDep = Alea (0, 2) ? 0 : ecran->w - police.rect.w;
137
yDep = Alea (0, ecran->h - police.rect.h);
139
default: xDep = yDep = 0;
142
xDest = x + c * police.rect.w;
145
// Fait glisser le caract�re du bord de l'�cran vers sa position finale
147
for (frame = 0; frame <= vitesse; frame ++)
149
// Quitte sur un appui sur la touche [ESC]
150
if (SDL_PollEvent (&evt) && evt.type == SDL_KEYDOWN && evt.key.keysym.sym == SDLK_ESCAPE) return OUI;
152
// Efface l'ancienne position
153
SDL_BlitSurface (back, &dest, ecran, &dest);
155
// Affiche le caract�re au bon endroit
156
Afficher_Caractere (xDep + ((xDest - xDep) * frame) / vitesse, yDep + ((yDest - yDep) * frame) / vitesse, chaine[c], police, ecran);
157
SDL_UpdateRects (ecran, 1, &dest);
159
// Fait la moyenne pour savoir o� doit �tre affich� le caract�re
160
dest.x = xDep + ((xDest - xDep) * frame) / vitesse;
161
dest.y = yDep + ((yDest - yDep) * frame) / vitesse;
165
// Affiche le caract�re � sa position finale
166
Afficher_Caractere (xDest, yDest, chaine[c], police, ecran);
167
Afficher_Caractere (xDest, yDest, chaine[c], police, back);
174
// ****************************************************************************
175
// ******************************* Questionner ********************************
176
// ****************************************************************************
177
BOOL Questionner (char ch[], POLICE police1, POLICE police2, POLICE police3, SDL_Surface *ecran)
179
SDL_Surface *save, *noire;
181
SDL_Rect box; // Rect de copie de la "boite de dialogue"
182
SDL_Rect oui, non; // Rects de cliquage des boutons OUI et NON
184
// Rect de la boite de dialogue du message
185
box.w = police1.rect.w * (strlen (ch) + 3);
186
box.h = police1.rect.h * 5;
187
box.x = (ecran->w - box.w) / 2;
188
box.y = (ecran->h - box.h) / 2 + police1.rect.h / 3;
190
// Cr�e une surface pour sauvegarder la partie de l'�cran qui sera recouverte par le message
191
save = SDL_CreateRGBSurface (SDL_SWSURFACE, box.w, box.h, 32, 0, 0, 0, 0);
192
SDL_BlitSurface (ecran, &box, save, NULL);
194
// Cr�e une surface noire semi-transparente qui servira � assombrir une partie de l'�cran
195
if (!(noire = SDL_DisplayFormat (save)))
196
{ fprintf (stderr, "Impossible de cr�er un back buffer (%s)\n", SDL_GetError ());
197
SDL_FreeSurface (save);
198
SDL_FreeSurface (noire);
201
SDL_FillRect (noire, NULL, SDL_MapRGB (noire->format, 0, 0, 0));
202
SDL_SetAlpha (noire, SDL_SRCALPHA, 128);
204
// Assombrit l'�cran et met � jour
205
SDL_BlitSurface (noire, NULL, ecran, &box);
206
SDL_UpdateRects (ecran, 1, &box);
207
SDL_FreeSurface (noire);
209
// Rects des boutons OUI et NON
210
oui.w = strlen (TXT_OUI) * police3.rect.w;
211
oui.h = police3.rect.h;
212
oui.x = ecran->w / 2 + police3.rect.w;
213
oui.y = ecran->h / 2 + police3.rect.h;
215
non.w = strlen (TXT_NON) * police2.rect.w;
216
non.h = police2.rect.h;
217
non.x = ecran->w / 2 - non.w - police2.rect.w;
218
non.y = ecran->h / 2 + police2.rect.h;
220
// Affiche le texte de la question et les textes des boutons OUI et NON
221
Afficher_Chaine ((ecran->w - strlen (ch) * police1.rect.w) / 2, ecran->h / 2 - police1.rect.h, 0, ch, police1, ecran, ecran);
222
Afficher_Chaine (oui.x, oui.y, 0, TXT_OUI, police3, ecran, ecran);
223
Afficher_Chaine (non.x, non.y, 0, TXT_NON, police2, ecran, ecran);
226
if (SDL_PollEvent (&evt))
228
// Anulle sur un appui de la touche [ESC], ou un clic sur NON
229
if ((evt.type == SDL_KEYDOWN && evt.key.keysym.sym == SDLK_ESCAPE) || (evt.type == SDL_MOUSEBUTTONDOWN && evt.button.button == 1 && Dans_Rect (evt.button.x, evt.button.y, non)))
230
{ SDL_BlitSurface (save, NULL, ecran, &box);
231
SDL_UpdateRects (ecran, 1, &box);
232
SDL_FreeSurface (save);
236
// Retourne OUI � la fonction ayant pos� cette question
237
if (evt.type == SDL_MOUSEBUTTONDOWN && evt.button.button == 1 && Dans_Rect (evt.button.x, evt.button.y, oui))
238
{ SDL_BlitSurface (save, NULL, ecran, &box);
239
SDL_UpdateRects (ecran, 1, &box);
240
SDL_FreeSurface (save);
247
// ****************************************************************************
248
// ******************************** Informer **********************************
249
// ****************************************************************************
250
BOOL Informer (char ch[], POLICE police, SDL_Surface *ecran)
252
SDL_Rect box; // Rect de copie de la "boite de dialogue"
255
// Rect de la boite de dialogue du message
256
box.w = police.rect.w * (strlen (ch) + 3);
257
box.h = police.rect.h * 3;
258
box.x = (ecran->w - box.w) / 2;
259
box.y = (ecran->h - box.h) / 2;
261
// Cr�e une surface noire semi-transparente qui servira � assombrir une partie de l'�cran
262
if (!(noire = SDL_CreateRGBSurface (SDL_SWSURFACE, box.w, box.h, 32, 0, 0, 0, 0)))
263
{ fprintf (stderr, "Impossible de cr�er une surface (%s)\n", SDL_GetError ());
264
SDL_FreeSurface (noire);
267
SDL_FillRect (noire, NULL, SDL_MapRGB (noire->format, 0, 0, 0));
268
SDL_SetAlpha (noire, SDL_SRCALPHA, 128);
270
// Assombrit l'�cran et met � jour
271
SDL_BlitSurface (noire, NULL, ecran, &box);
272
SDL_UpdateRects (ecran, 1, &box);
273
SDL_FreeSurface (noire);
275
// Affiche le texte de la question et les textes des boutons OUI et NON
276
Afficher_Chaine ((ecran->w - strlen (ch) * police.rect.w) / 2, (ecran->h - police.rect.h) / 2, 0, ch, police, ecran, ecran);
b'\\ No newline at end of file'