Bonjour je suis entrain de faire l'exercice du cours, pour le moment mon objectif est d'afficher un level charger à partir d'un fichier .txt seulement les textures ne s'affichent pas et mon code ne laisse pas d"erreurs.
Voila merci pour votre aide:
FICHIER fichier.c
FICHIER main.c
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 #include <stdlib.h> #include <stdio.h> #include <SDL.h> #include <SDL_image.h> #include "constantes.h" #include "fichiers.h" #include "errorController.h" int chargerMap(SDL_Window* window, SDL_Renderer* renderer, int carte[][NB_BLOCS_HAUTEUR]) { int i = 0, j = 0; FILE* map = NULL; int ok = 0; map = fopen("map.txt", "r"); if (map == NULL) { DestroyAndExitWithError("fopen map.lvl", renderer, window); } else { for (i = 0; i < NB_BLOCS_LARGEUR; i++) { for (j = 0; j < NB_BLOCS_HAUTEUR; j++) { ok = fgetc(map); switch (ok) { case '0': carte[i][j] = VIDE; break; case '1': carte[i][j] = MUR; break; case '2': //printf("2"); carte[i][j] = CAISSE; break; case '3': carte[i][j] = OBJECTIF; break; case '4': //printf("4"); carte[i][j] = MARIO; break; } } } fclose(map); return 0; } } void afficherTexture(SDL_Renderer* renderer, const char* chemin, SDL_Rect* position, SDL_Window *window) { SDL_Surface* actualSurface = IMG_Load(chemin); if (actualSurface == NULL) { DestroyAndExitWithError("IMG LOAD", renderer, window); } SDL_Texture* actualTexture = SDL_CreateTextureFromSurface(renderer, actualSurface); if (actualTexture == NULL) { DestroyAndExitWithError("SDL_CreateTextureFromSurface", renderer, window); } SDL_FreeSurface(actualSurface); if (SDL_RenderCopy(renderer, actualTexture, NULL, position) != 0) { DestroyAndExitWithError("SDL_RenderCopy", renderer, window); } printf("oui"); }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 #include <stdlib.h> #include <stdio.h> #include <SDL.h> #include <SDL_image.h> #include <Windows.h> #include "constantes.h" #include "errorController.h" #include "fichiers.h" int main(int argc, char argv[]) { SDL_Window* window; SDL_Renderer* renderer; SDL_Surface* menu; SDL_Texture* menuTexture; if (SDL_Init(SDL_INIT_VIDEO) != 0) { SDL_Log("Unable to initialize SDL: %s", SDL_GetError()); return EXIT_FAILURE; } window = SDL_CreateWindow("Mario Sakoban", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, LARGEUR_FENETRE, HAUTEUR_FENETRE, 0); if (window == NULL) { ExitWithError("SDL_CreateWindow"); } renderer = SDL_CreateRenderer(window, -1, 0); if (renderer == NULL) { SDL_DestroyWindow(window); ExitWithError("SDL_CreateRenderer"); } menu = IMG_Load("../../../../Desktop/ListeOfImg/menu.jpg"); if (menu == NULL) { DestroyAndExitWithError("IMG_Load", renderer, window); } menuTexture = SDL_CreateTextureFromSurface(renderer, menu); if (menuTexture == NULL) { DestroyAndExitWithError("SDL_CreateTextureFromSurface", renderer, window); } SDL_FreeSurface(menu); if (SDL_RenderCopy(renderer, menuTexture, NULL, NULL) != 0) { DestroyAndExitWithError("SDL_RenderCopy", renderer, window); } SDL_RenderPresent(renderer); SDL_bool continuer = SDL_TRUE; SDL_Event event; int carte[NB_BLOCS_LARGEUR][NB_BLOCS_HAUTEUR] = { {0},{0} }; SDL_Rect position, positionJoueur; int i = 0, j = 0; while (continuer) { SDL_WaitEvent(&event); switch (event.type) { case SDL_QUIT: continuer = SDL_FALSE; case SDL_KEYDOWN: switch (event.key.keysym.sym) { case SDLK_1: //CODE JOUER case SDLK_2: if (chargerMap(window, renderer, carte) != 0) { DestroyAndExitWithError("chargerMap", renderer, window); } SDL_RenderClear(renderer); for (i = 0; i < NB_BLOCS_LARGEUR; i++) { for (j = 0; j < NB_BLOCS_HAUTEUR; j++) { position.x = i * TAILLE_BLOC; position.y = j * TAILLE_BLOC; switch (carte[i][j]) { case VIDE: printf("%d %d", i, j); break; case MUR: afficherTexture(renderer, "../../../../Desktop/ListeOfImg/mur.jpg", &position, window); break; case CAISSE: afficherTexture(renderer, "../../../../Desktop/ListeOfImg/caisse.jpg", &position, window); break; case CAISSE_OK: afficherTexture(renderer, "../../../../Desktop/ListeOfImg/caisse_ok.jpg", &position, window); break; case OBJECTIF: afficherTexture(renderer, "../../../../Desktop/ListeOfImg/objectif.png", &position, window); break; case MARIO: afficherTexture(renderer, "../../../../Desktop/ListeOfImg/mario_bas.gif", &position, window); break; } } } } } SDL_RenderPresent(renderer); } Sleep(5000); SDL_Quit(); return EXIT_SUCCESS; }
Partager