main.c 7.36 KB
Newer Older
Valentin MEUNIER's avatar
Valentin MEUNIER committed
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
/*main.c*/
/*15 juin 2021*/
/*make*/

#include "main.h"

void end_sdl(char ok,char const* msg,SDL_Window* window, SDL_Renderer* renderer)
{
	char msg_formated[255];
	int l;

	if (!ok) 
	{	                             
		strncpy(msg_formated, msg, 250);
		l = strlen(msg_formated);           
		strcpy(msg_formated + l, " : %s\n");
		SDL_Log(msg_formated, SDL_GetError());
	}

	if (renderer != NULL) SDL_DestroyRenderer(renderer);
	if (window != NULL)   SDL_DestroyWindow(window);

	SDL_Quit();

	if (!ok) exit(EXIT_FAILURE);
}
void afficherEcranIntelligemment(SDL_Renderer *renderer,int **tab,int taille_cell)
{
Valentin MEUNIER's avatar
enfin    
Valentin MEUNIER committed
29
	int x=0;
Valentin MEUNIER's avatar
Valentin MEUNIER committed
30
31
	SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0);
	SDL_RenderClear(renderer);
32
33
34
35
36
	SDL_SetRenderDrawColor(renderer,0,0,0,0);
 	SDL_DisplayMode screen;
	SDL_GetCurrentDisplayMode(0,&screen);
	int decalage_horizontale=(screen.w/taille_cell-P)*taille_cell/3;
	int decalage_vertical=(screen.h/taille_cell-N)*taille_cell/4;
Valentin MEUNIER's avatar
Valentin MEUNIER committed
37

Valentin MEUNIER's avatar
enfin    
Valentin MEUNIER committed
38
	SDL_SetRenderDrawColor(renderer,0,0,0,0);
Valentin MEUNIER's avatar
disj    
Valentin MEUNIER committed
39
	int i1,j1,noeud=0;
Valentin MEUNIER's avatar
Valentin MEUNIER committed
40
	
Valentin MEUNIER's avatar
disj    
Valentin MEUNIER committed
41
42
43
	for (int i=0;i<N;i++)
	{
		for (int j=0;j<P;j++)
Valentin MEUNIER's avatar
Valentin MEUNIER committed
44
		{
Valentin MEUNIER's avatar
disj    
Valentin MEUNIER committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
			x=tab[j][i];
			i1=(noeud%P);
			j1=((int)noeud/P);

			if (!(x & FLAG_N))
				SDL_RenderDrawLine(renderer,i1*taille_cell+decalage_horizontale,j1*taille_cell+decalage_vertical,(i1+1)*taille_cell+decalage_horizontale,j1*taille_cell+decalage_vertical); //mur au nord
			if ((x & FLAG_S)!=2)
				SDL_RenderDrawLine(renderer,i1*taille_cell+decalage_horizontale,(j1+1)*taille_cell+decalage_vertical,(i1+1)*taille_cell+decalage_horizontale,(j1+1)*taille_cell+decalage_vertical); //mur au sud
			if ((x & FLAG_E)!=4)
				SDL_RenderDrawLine(renderer,(i1+1)*taille_cell+decalage_horizontale,j1*taille_cell+decalage_vertical,(i1+1)*taille_cell+decalage_horizontale,(j1+1)*taille_cell+decalage_vertical); //mur à l'est
			if ((x & FLAG_O)!=8)
				SDL_RenderDrawLine(renderer,i1*taille_cell+decalage_horizontale,j1*taille_cell+decalage_vertical,i1*taille_cell+decalage_horizontale,(j1+1)*taille_cell+decalage_vertical); //mur à l'ouest

			noeud+=1;
Valentin MEUNIER's avatar
enfin    
Valentin MEUNIER committed
59

Valentin MEUNIER's avatar
Valentin MEUNIER committed
60
61
62
63
		}
	}
}

64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
void peindreDFS(SDL_Renderer * renderer, int noeuds, int taille_cell)
{
 		SDL_DisplayMode screen;
		SDL_GetCurrentDisplayMode(0,&screen);
		int decalage_horizontale=(screen.w/taille_cell-P)*taille_cell/3;
		int decalage_vertical=(screen.h/taille_cell-N)*taille_cell/4;
		SDL_Rect rectangle;
		
		int x=noeuds%P;
		int y=noeuds/P;
		SDL_SetRenderDrawColor(renderer, 0,255,0,0);
		
		rectangle.x=taille_cell*x+ decalage_horizontale;
		rectangle.y=taille_cell*y+ decalage_vertical;
		rectangle.h=rectangle.w=taille_cell;	
		
		SDL_RenderFillRect(renderer, &rectangle);
}

Valentin MEUNIER's avatar
Valentin MEUNIER committed
83
84
85
86
void peindreDFSGraphique(SDL_Texture * texture, SDL_Window* window ,SDL_Renderer *renderer, int noeuds, int taille_cell, int ** laby)
{
	affichage_texture(texture,window, renderer, laby[noeuds%P][noeuds/P], noeuds%P, noeuds/P, taille_cell);
}
87

Valentin MEUNIER's avatar
Valentin MEUNIER committed
88
89
90
91
92
93
94
void chemin(SDL_Renderer * renderer, int depart, int arrivee, int taille_cell,int *parent)
{
 		SDL_DisplayMode screen;
		SDL_GetCurrentDisplayMode(0,&screen);
		int decalage_horizontale=(screen.w/taille_cell-P)*taille_cell/3;
		int decalage_vertical=(screen.h/taille_cell-N)*taille_cell/4;
		SDL_Rect rectangle;
95

Valentin MEUNIER's avatar
Valentin MEUNIER committed
96
97
98
99
100
101
		int cours=depart;
		SDL_SetRenderDrawColor(renderer, 0,255,0,0);
		rectangle.x=taille_cell*(cours%P)+decalage_horizontale;
		rectangle.y=taille_cell*(cours/P)+decalage_vertical;
		rectangle.w=rectangle.h=taille_cell;
		SDL_RenderFillRect(renderer, &rectangle);
102
103
104
105
106
107
108
109
110

		cours=arrivee;
		SDL_SetRenderDrawColor(renderer, 0,0,255,0);
		rectangle.x=taille_cell*(cours%P)+decalage_horizontale;
		rectangle.y=taille_cell*(cours/P)+decalage_vertical;
		rectangle.w=rectangle.h=taille_cell;
		SDL_RenderFillRect(renderer, &rectangle);

		cours=parent[depart];
Valentin MEUNIER's avatar
Valentin MEUNIER committed
111
112
113
114
115
116
117
118
		SDL_SetRenderDrawColor(renderer, 255,0,0,0);
		while(cours!=arrivee)
		{
			rectangle.x=taille_cell*(cours%P)+decalage_horizontale;
			rectangle.y=taille_cell*(cours/P)+decalage_vertical;
			rectangle.w=rectangle.h=taille_cell;
			SDL_RenderFillRect(renderer, &rectangle);
			cours=parent[cours];
119
120
			SDL_RenderPresent(renderer);
			SDL_Delay(20);
Valentin MEUNIER's avatar
Valentin MEUNIER committed
121
122
123
124
125
126
127
128
129
		}
		cours=arrivee;
		SDL_SetRenderDrawColor(renderer, 0,0,255,0);
		rectangle.x=taille_cell*(cours%P)+decalage_horizontale;
		rectangle.y=taille_cell*(cours/P)+decalage_vertical;
		rectangle.w=rectangle.h=taille_cell;
		SDL_RenderFillRect(renderer, &rectangle);
}

Valentin MEUNIER's avatar
enfin    
Valentin MEUNIER committed
130
131
132
133
134
135
136
int min(int a, int b)
{
	if (a>b)
		return b;
	else
		return a;
}	
Valentin MEUNIER's avatar
Valentin MEUNIER committed
137
138


Valentin MEUNIER's avatar
Valentin MEUNIER committed
139
int main (int argc, char** argv)
Valentin MEUNIER's avatar
Valentin MEUNIER committed
140
{
Valentin MEUNIER's avatar
Valentin MEUNIER committed
141
142
143
	double p;
	if (argc==2)  p=atof(argv[1]);
	else p=1; 
Valentin MEUNIER's avatar
Valentin MEUNIER committed
144
145
146
147
148
149
	if (SDL_Init(SDL_INIT_VIDEO) == -1)
	{
		fprintf(stderr, "Erreur d'initialisation de la SDL : %s\n", SDL_GetError());
		return EXIT_FAILURE;
	}

Valentin MEUNIER's avatar
enfin    
Valentin MEUNIER committed
150
151
 	SDL_DisplayMode screen;
	SDL_GetCurrentDisplayMode(0,&screen);
Valentin MEUNIER's avatar
Valentin MEUNIER committed
152
	SDL_Window *window;
Valentin MEUNIER's avatar
enfin    
Valentin MEUNIER committed
153
	window = SDL_CreateWindow("SDL2 Programme 0.1", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,screen.w,screen.h, SDL_WINDOW_RESIZABLE);
Valentin MEUNIER's avatar
Valentin MEUNIER committed
154
155
156
157
158
	if (window == 0) fprintf(stderr, "Erreur d'initialisation de la SDL : %s\n", SDL_GetError());
	SDL_SetWindowTitle(window, "Mon_chef d'oeuvre");

	SDL_Renderer *renderer;
	renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); /*  SDL_RENDERER_SOFTWARE */
Valentin MEUNIER's avatar
Valentin MEUNIER committed
159

Valentin MEUNIER's avatar
Valentin MEUNIER committed
160
	if (renderer == 0) fprintf(stderr, "Erreur d'initialisation de la SDL : %s\n", SDL_GetError());
Valentin MEUNIER's avatar
Valentin MEUNIER committed
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
	SDL_Texture * texture,*texture_fin,* texture_elve,* texture_elve_reverse;

	texture_elve = load_texture_from_image("Elve_lign.png",renderer);
	if (texture_elve == NULL)
	{
		printf("erreur texture Elve_lign.png\n");
		exit(EXIT_FAILURE);
	}	
	texture_elve_reverse = load_texture_from_image("Elve_ligne_reverse.png",renderer);
	if (texture_elve_reverse == NULL) 
	{
		printf("erreur texture Elve_lign_reverse.png\n");
		exit(EXIT_FAILURE);
	}	
	
	texture = load_texture_from_image("road.png",renderer);
	if (texture==NULL) 
	{
		printf("erreur texture road.png\n");
		exit(EXIT_FAILURE);
	}	

	texture_fin = load_texture_from_image("roguelikeChar_transparent.png",renderer);
	if (texture_fin==NULL) 
	{
		printf("erreur texture roguelikechar.png\n");
		exit(EXIT_FAILURE);
	}	

Valentin MEUNIER's avatar
Valentin MEUNIER committed
190
	int running=1;
Valentin MEUNIER's avatar
Valentin MEUNIER committed
191
	srand(0);
Valentin MEUNIER's avatar
Valentin MEUNIER committed
192
	int noeuds=N*P;
Valentin MEUNIER's avatar
enfin    
Valentin MEUNIER committed
193
	int nb_aretes=2*N*P-N-P;
Valentin MEUNIER's avatar
Valentin MEUNIER committed
194
	int **tab;
Valentin MEUNIER's avatar
Valentin MEUNIER committed
195
	int *parent;
Valentin MEUNIER's avatar
enfin    
Valentin MEUNIER committed
196
	int cours=0;
Valentin MEUNIER's avatar
Valentin MEUNIER committed
197
198
	int depart=0;
	int arrivee=12;
199
	int * explo;
Valentin MEUNIER's avatar
enfin    
Valentin MEUNIER committed
200
	int taille_cell=min(screen.w/(P+2),screen.h/(N+2));
Valentin MEUNIER's avatar
Valentin MEUNIER committed
201
	//printf("%d \n", taille_cell);
Valentin MEUNIER's avatar
enfin    
Valentin MEUNIER committed
202
203
	graph_t * graph=creer_graph(noeuds,nb_aretes);
	generation(graph);
Valentin MEUNIER's avatar
Valentin MEUNIER committed
204
	graph=Fisher(graph,nb_aretes);
Valentin MEUNIER's avatar
Valentin MEUNIER committed
205
	graph=kruskal(graph,noeuds,nb_aretes,&cours,p);
Valentin MEUNIER's avatar
enfin    
Valentin MEUNIER committed
206
	tab=tableau_ligne(graph,cours);
207
208

	explo=DFS(tab,noeuds);
Valentin MEUNIER's avatar
disj    
Valentin MEUNIER committed
209
	int temps=600;
210
	int i=1;
Valentin MEUNIER's avatar
Valentin MEUNIER committed
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
	SDL_Event event;
	while (running)
	{
		while (SDL_PollEvent(&event))
		{
			switch (event.type)
			{
				case SDL_QUIT:
					running = 0;
					break;
				case SDL_WINDOWEVENT:
					switch (event.window.event)
					{
					case SDL_WINDOWEVENT_CLOSE:
						running = 0;
						break;
					}
					break;
			break;
			}
231
232
233
		}
/*
			if (temps>100)
Valentin MEUNIER's avatar
disj    
Valentin MEUNIER committed
234
			{
235
			
Valentin MEUNIER's avatar
disj    
Valentin MEUNIER committed
236
237
238
239
240
241
242
243
244
245
				temps=0;
				SDL_RenderClear(renderer);
				afficherEcranIntelligemment(renderer,tab,taille_cell);
				depart=arrivee;
				arrivee=rand()%noeuds;
				parent=dijsktra(tab,noeuds,arrivee);
				chemin(renderer,depart, arrivee, taille_cell, parent);
			}
			else
				temps+=10;
246
247
*/
			if (i==1)
Valentin MEUNIER's avatar
Valentin MEUNIER committed
248
249
250
			{
//				afficherEcranIntelligemment(renderer,tab,taille_cell);
			}
251
252
			if  (i<=noeuds)
			{
Valentin MEUNIER's avatar
Valentin MEUNIER committed
253
254
//				peindreDFS(renderer,explo[i],taille_cell);
				peindreDFSGraphique(texture,window,renderer,explo[i],taille_cell,tab);
255
256
257
258
259
260
261
				i++;
			}
			else
			{
				SDL_Delay(8000);
				running=0;
			}
Valentin MEUNIER's avatar
Valentin MEUNIER committed
262
			SDL_RenderPresent(renderer);
263
			SDL_Delay(20);
Valentin MEUNIER's avatar
Valentin MEUNIER committed
264
265
266
267
268
269
	}
	end_sdl(1, "Normal ending", window, renderer);
	return 1;
}