-
Notifications
You must be signed in to change notification settings - Fork 0
/
fonctionsSDL.c
392 lines (309 loc) · 15 KB
/
fonctionsSDL.c
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#include "fonctionsSDL.h"
/// Proc�dure qui initialise la SDL
void initialisationSDL() {
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
fprintf(stderr, "Unable to initialize SDL: %s\n", SDL_GetError());
exit(1);
}
}
/// Fonction qui retourne la cr�ation d'une fen�tre en 640x480 32bits/pixel
// utilisant la RAM de la carte graphique et le double buffering
SDL_Window* creationEcran(int hauteur, int largeur) {
// Création d'une nouvelle fenêtre à l'écran
SDL_Window *ecran = SDL_CreateWindow("Your Window Title", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, hauteur, largeur, SDL_WINDOW_SHOWN);
if (ecran == NULL) { // Si la fenêtre n'existe pas alors :
// On écrit l'erreur dans un fichier
fprintf(stderr, "Impossible de créer la fenêtre SDL : %s\n", SDL_GetError());
exit(EXIT_FAILURE); // On quitte le programme
}
return ecran;
}
/// On effectue une impression de l'�cran en affichant temporairement � l'�cran
// seulement la grille et un fond blanc que l'on enregistre ensuite dans un
// fichier bmp
void impressionEcran(SDL_Renderer* renderer, int grilleSudoku[9][9][10]) {
SDL_Texture *fondBlanc = SDL_CreateTexture(renderer,
SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_TARGET,
800,
600);
// Set render target to fondBlanc and fill it with white color
SDL_SetRenderTarget(renderer, fondBlanc);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderClear(renderer);
// Reset render target to default (the window)
SDL_SetRenderTarget(renderer, NULL);
// Position of the fond
SDL_Rect positionFond = {0, 0 , 0 , 0};
// Load the grille texture
SDL_Surface *grilleSurface = chargementBitmap("C:/Users/user/Desktop/libraries/Assets/img/grilleVierge.bmp");
if (!grilleSurface)
{
fprintf(stderr, "Failed to load image: %s\n", SDL_GetError());
return;
}
SDL_SetColorKey(grilleSurface, SDL_TRUE, SDL_MapRGB(grilleSurface->format, 255, 255, 255));
SDL_Texture *grille = SDL_CreateTextureFromSurface(renderer, grilleSurface);
// Query the grille texture's width and height
int grilleWidth, grilleHeight;
SDL_QueryTexture(grille, NULL, NULL, &grilleWidth, &grilleHeight);
// Position of the grille
SDL_Rect positionGrille = {(800 - grilleWidth) / 2, (600 - grilleHeight) / 2};
// Render the fond and grille textures onto the screen
SDL_RenderCopy(renderer, fondBlanc, NULL, &positionFond);
SDL_RenderCopy(renderer, grille, NULL, &positionGrille);
// Render any additional content here, such as the solution grid
// Update the screen
SDL_RenderPresent(renderer);
// Save the rendered content to a bitmap file
SDL_Surface *surface = SDL_CreateRGBSurfaceWithFormat(0, 800, 600, 32, SDL_PIXELFORMAT_RGBA8888);
SDL_RenderReadPixels(renderer, NULL, SDL_PIXELFORMAT_RGBA8888, surface->pixels, surface->pitch);
SDL_SaveBMP(surface, "C:/Users/user/Desktop/libraries/Assets/img/impression_grille.bmp");
SDL_FreeSurface(surface);
// Free the memory
SDL_DestroyTexture(fondBlanc);
SDL_DestroyTexture(grille);
}
/// Fonction qui retourne une surface contenant une image charg�e au format
// bitmap
SDL_Surface* chargementBitmap( char* filename) {
// Load image from file
SDL_Surface* loadedSurface = SDL_LoadBMP(filename);
if (loadedSurface == NULL) {
fprintf(stderr, "Unable to load image %s! SDL Error: %s\n", filename, SDL_GetError());
return NULL;
}
return loadedSurface;
}
/// Proc�dure qui permet d'afficher au centre de l'�cran une grille de sudoku
// vierge sur l'�cran du r�solveur
void afficherGrilleVierge(SDL_Renderer *renderer)
{
// Chargement du fond d'écran avec transparence
SDL_Surface *fondSurface = chargementBitmap("C:/Users/user/Desktop/libraries/Assets/img/fondGrille.bmp");
if (!fondSurface)
{
fprintf(stderr, "Failed to load image: %s\n", SDL_GetError());
return;
}
SDL_SetColorKey(fondSurface, SDL_TRUE, SDL_MapRGB(fondSurface->format, 255, 255, 255));
SDL_Texture *fondEcran = SDL_CreateTextureFromSurface(renderer, fondSurface);
// Chargement de la grille avec transparence
SDL_Surface *grilleSurface = chargementBitmap("C:/Users/user/Desktop/libraries/Assets/img/grilleVierge.bmp");
if (!grilleSurface)
{
fprintf(stderr, "Failed to load image: %s\n", SDL_GetError());
return;
}
SDL_SetColorKey(grilleSurface, SDL_TRUE, SDL_MapRGB(grilleSurface->format, 255, 255, 255));
SDL_Texture *grille = SDL_CreateTextureFromSurface(renderer, grilleSurface);
// Chargement des autres images
SDL_Surface *retour = chargementBitmap("C:/Users/user/Desktop/libraries/Assets/img/retour.bmp");
SDL_Surface *imprimante = chargementBitmap("C:/Users/user/Desktop/libraries/Assets/img/imprimante.bmp");
SDL_Surface *resoudre = chargementBitmap("C:/Users/user/Desktop/libraries/Assets/img/resoudre.bmp");
SDL_Surface *reinitialiser = chargementBitmap("C:/Users/user/Desktop/libraries/Assets/img/reinitialiser.bmp");
SDL_Texture *retourTexture = SDL_CreateTextureFromSurface(renderer, retour);
SDL_Texture *imprimanteTexture = SDL_CreateTextureFromSurface(renderer, imprimante);
SDL_Texture *resoudreTexture = SDL_CreateTextureFromSurface(renderer, resoudre);
SDL_Texture *reinitialiserTexture = SDL_CreateTextureFromSurface(renderer, reinitialiser);
// Set color key for transparency
SDL_SetTextureBlendMode(retourTexture, SDL_BLENDMODE_BLEND);
SDL_SetTextureBlendMode(imprimanteTexture, SDL_BLENDMODE_BLEND);
SDL_SetTextureBlendMode(resoudreTexture, SDL_BLENDMODE_BLEND);
SDL_SetTextureBlendMode(reinitialiserTexture, SDL_BLENDMODE_BLEND);
// Calcul des positions des images
int windowWidth, windowHeight;
SDL_GetRendererOutputSize(renderer, &windowWidth, &windowHeight);
int imprimanteWidth, imprimanteHeight;
SDL_QueryTexture(imprimanteTexture, NULL, NULL, &imprimanteWidth, &imprimanteHeight);
SDL_Rect positionFond = {0, 0, 0, 0};
SDL_Rect positionRetour = {10, 10, 0, 0};
SDL_Rect positionImprimante = {785 - imprimanteWidth, 12, 0, 0};
SDL_Rect positionGrille = {(windowWidth - 800) / 2, (windowHeight - 600) / 2, 800, 600};
SDL_Rect positionResoudre = {windowWidth - 170, windowHeight - 40, 0, 0};
SDL_Rect positionReinitialiser = {windowWidth - 340, windowHeight - 40, 0, 0};
// Affichage des images
SDL_RenderCopy(renderer, fondEcran, NULL, &positionFond);
SDL_RenderCopy(renderer, grille, NULL, &positionGrille);
SDL_RenderCopy(renderer, retourTexture, NULL, &positionRetour);
SDL_RenderCopy(renderer, imprimanteTexture, NULL, &positionImprimante);
SDL_RenderCopy(renderer, resoudreTexture, NULL, &positionResoudre);
SDL_RenderCopy(renderer, reinitialiserTexture, NULL, &positionReinitialiser);
// Mise à jour de l'écran
SDL_RenderPresent(renderer);
SDL_FreeSurface(fondSurface);
SDL_FreeSurface(grilleSurface);
// Libération de la mémoire
SDL_DestroyTexture(fondEcran);
SDL_DestroyTexture(grille);
SDL_DestroyTexture(retourTexture);
SDL_DestroyTexture(imprimanteTexture);
SDL_DestroyTexture(resoudreTexture);
SDL_DestroyTexture(reinitialiserTexture);
}
/// Proc�dure qui permet d'afficher au centre de l'�cran une grille de sudoku
// vierge sur l'�cran de jeu
void afficherGrilleVierge2(SDL_Renderer *renderer)
{
// Chargement du fond d'éc ran avec transparence
SDL_Surface *fondEcran = chargementBitmap("C:/Users/user/Desktop/libraries/Assets/img/fondGrille.bmp");
if (!fondEcran)
{
fprintf(stderr, "Failed to load image: %s\n", SDL_GetError());
return;
}
SDL_SetColorKey(fondEcran, SDL_TRUE, SDL_MapRGB(fondEcran->format, 255, 255, 255));
SDL_Texture *fondTexture = SDL_CreateTextureFromSurface(renderer, fondEcran);
// Chargement de la grille avec transparence
SDL_Surface *grille = chargementBitmap("C:/Users/user/Desktop/libraries/Assets/img/grilleVierge.bmp");
SDL_Texture *grilleTexture = SDL_CreateTextureFromSurface(renderer, grille);
if (!grille)
{
fprintf(stderr, "Failed to load image: %s\n", SDL_GetError());
return;
}
// SDL_SetColorKey(grille, SDL_TRUE, SDL_MapRGB(grille->format, 255, 255, 255));
// SDL_FreeSurface(grille);
// Chargement des autres images
SDL_Surface *retour = chargementBitmap("C:/Users/user/Desktop/libraries/Assets/img/retour.bmp");
SDL_SetColorKey(retour, SDL_TRUE, SDL_MapRGB(retour->format, 255, 0, 255));
SDL_Texture *retourTexture = SDL_CreateTextureFromSurface(renderer, retour);
SDL_Surface *imprimante = chargementBitmap("C:/Users/user/Desktop/libraries/Assets/img/imprimante.bmp");
SDL_SetColorKey(imprimante, SDL_TRUE, SDL_MapRGB(retour->format, 255, 0, 255));
SDL_Texture *imprimanteTexture = SDL_CreateTextureFromSurface(renderer, imprimante);
SDL_Surface *resoudre = chargementBitmap("C:/Users/user/Desktop/libraries/Assets/img/valider.bmp");
SDL_SetColorKey(resoudre, SDL_TRUE, SDL_MapRGB(resoudre->format, 255, 0, 255));
SDL_Texture *resoudreTexture = SDL_CreateTextureFromSurface(renderer, resoudre);
SDL_Surface *reinitialiser = chargementBitmap("C:/Users/user/Desktop/libraries/Assets/img/reinitialiser.bmp");
SDL_SetColorKey(reinitialiser, SDL_TRUE, SDL_MapRGB(reinitialiser->format, 255, 0, 255));
SDL_Texture *reinitialiserTexture = SDL_CreateTextureFromSurface(renderer, reinitialiser);
int resoudreTextureWidth, resoudreTextureHeight;
SDL_QueryTexture(resoudreTexture, NULL, NULL, &resoudreTextureWidth, &resoudreTextureHeight);
// Calcul des positions des images
int windowWidth, windowHeight;
SDL_GetRendererOutputSize(renderer, &windowWidth, &windowHeight);
int imprimanteWidth, imprimanteHeight;
SDL_QueryTexture(imprimanteTexture, NULL, NULL, &imprimanteWidth, &imprimanteHeight);
SDL_Rect positionRetour = {10, 10, 0, 0};
SDL_GetRendererOutputSize(renderer, &windowWidth, &windowHeight);
positionRetour.w = retour->w;
positionRetour.h = retour->h;
// Position du bouton imprimante
SDL_Rect positionImprimante = {785 - imprimante->w, 12, 0, 0};
SDL_GetRendererOutputSize(renderer, &windowWidth, &windowHeight);
positionImprimante.w = imprimante->w;
positionImprimante.h = imprimante->h;
//grille
SDL_GetRendererOutputSize(renderer, &windowWidth, &windowHeight);
SDL_Rect positionGrille = {(windowWidth - grille->w) / 2, (windowHeight - grille->h) / 2, grille->w, grille->h};
SDL_GetRendererOutputSize(renderer, &windowWidth, &windowHeight);
positionGrille.w = grille->w;
positionGrille.h = grille->w;
//Ressoudre
SDL_Rect positionResoudre = {
(windowWidth -170),
(windowHeight - 40) ,
resoudreTextureWidth,
resoudreTextureHeight
};
// Position du bouton réinitialiser
SDL_Rect positionReinitialiser = {
(windowWidth - 340) ,
(windowHeight - 40) ,
resoudreTextureWidth,
resoudreTextureHeight
};
//fond
SDL_Rect positionFond = {0, 0, 0, 0};
SDL_QueryTexture(fondTexture, NULL, NULL, &positionFond.w, &positionFond.h);
// Affichage des images
SDL_RenderCopy(renderer, fondTexture, NULL, &positionFond);
SDL_RenderCopy(renderer, grilleTexture, NULL, &positionGrille);
SDL_RenderCopy(renderer, retourTexture, NULL, &positionRetour);
SDL_RenderCopy(renderer, resoudreTexture, NULL, &positionResoudre);
SDL_RenderCopy(renderer, reinitialiserTexture, NULL, &positionReinitialiser);
SDL_RenderCopy(renderer, imprimanteTexture, NULL, &positionImprimante);
// Mise à jour de l'écran
SDL_RenderPresent(renderer);
// Libération de la mémoire
SDL_DestroyTexture(fondTexture);
SDL_DestroyTexture(grilleTexture);
SDL_DestroyTexture(retourTexture);
SDL_DestroyTexture(resoudreTexture);
SDL_DestroyTexture(reinitialiserTexture);
SDL_DestroyTexture(imprimanteTexture);
}
/// Proc�dure qui affiche l'ensemble des chiffres d'une grille � l'�cran de jeu
int afficherSolution(int grille[9][9][10], SDL_Renderer *renderer)
{
int ligne = 0, colonne = 0, decalageX = 0, decalageY = 0;
char nomImage[64];
//
//
// for (int i = 0; i < 9; i++) {
// for (int j = 0; j < 9; j++) {
// printf("%d ", *grille[i][j]);
// }
// printf("\n");
// }
for (ligne = 0; ligne < 9; ligne++)
{
if (ligne >= 6)
{
decalageY += 6;
}
else if (ligne >= 3)
{
decalageY += 3;
}
for (colonne = 0; colonne < 9; colonne++)
{
if (grille[ligne][colonne][0] != 0)
{
// printf("the number is %d " , grille[ligne][colonne][0]) ;
// printf("####C:/Users/user/Desktop/libraries/Assets/img/chiffre%d.bmp####", grille[ligne][colonne][0]) ;
// Construct image filename
sprintf(nomImage, "C:/Users/user/Desktop/libraries/Assets/img/chiffre%d.bmp", grille[ligne][colonne][0]);
// Load image surface
SDL_Surface *chiffreSurface = chargementBitmap(nomImage);
if (!chiffreSurface)
{
printf("Error loading image: %s\n", nomImage);
continue; // Skip rendering if surface loading failed
}
// Create texture from surface
SDL_Texture *chiffreTexture = SDL_CreateTextureFromSurface(renderer, chiffreSurface);
SDL_FreeSurface(chiffreSurface); // Free surface after creating texture
if (!chiffreTexture)
{
printf("Error creating texture: %s\n", SDL_GetError());
continue; // Skip rendering if texture creation failed
}
// Get texture size
int textureW, textureH;
SDL_QueryTexture(chiffreTexture, NULL, NULL, &textureW, &textureH);
if (colonne >= 6)
{
decalageX += 6;
}
else if (colonne >= 3)
{
decalageX += 3;
}
// Set rendering position with adjustments
SDL_Rect positionChiffre;
positionChiffre.x = 217 + colonne * 40 + decalageX;
positionChiffre.y = 117 + ligne * 40 + decalageY;
positionChiffre.w = textureW;
positionChiffre.h = textureH;
// Render texture
SDL_RenderCopy(renderer, chiffreTexture, NULL, &positionChiffre);
// Destroy texture
SDL_DestroyTexture(chiffreTexture);
decalageX = 0;
}
}
decalageY = 0;
}
SDL_RenderPresent(renderer);
}