main.c 5.15 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
		}
	}
}

Valentin MEUNIER's avatar
Valentin MEUNIER committed
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
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;
		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);
		cours=parent[cours];
		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];
		}
		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
95
96
97
98
99
100
101
int min(int a, int b)
{
	if (a>b)
		return b;
	else
		return a;
}	
Valentin MEUNIER's avatar
Valentin MEUNIER committed
102
103


Valentin MEUNIER's avatar
Valentin MEUNIER committed
104
int main (int argc, char** argv)
Valentin MEUNIER's avatar
Valentin MEUNIER committed
105
{
Valentin MEUNIER's avatar
Valentin MEUNIER committed
106
107
108
	double p;
	if (argc==2)  p=atof(argv[1]);
	else p=1; 
Valentin MEUNIER's avatar
Valentin MEUNIER committed
109
110
111
112
113
114
	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
115
116
 	SDL_DisplayMode screen;
	SDL_GetCurrentDisplayMode(0,&screen);
Valentin MEUNIER's avatar
Valentin MEUNIER committed
117
	SDL_Window *window;
Valentin MEUNIER's avatar
enfin    
Valentin MEUNIER committed
118
	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
119
120
121
122
123
124
125
	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 */
	if (renderer == 0) fprintf(stderr, "Erreur d'initialisation de la SDL : %s\n", SDL_GetError());
	int running=1;
Valentin MEUNIER's avatar
Valentin MEUNIER committed
126
	srand(0);
Valentin MEUNIER's avatar
Valentin MEUNIER committed
127
	int noeuds=N*P;
Valentin MEUNIER's avatar
enfin    
Valentin MEUNIER committed
128
	int nb_aretes=2*N*P-N-P;
Valentin MEUNIER's avatar
Valentin MEUNIER committed
129
	int **tab;
Valentin MEUNIER's avatar
Valentin MEUNIER committed
130
	int *parent;
Valentin MEUNIER's avatar
enfin    
Valentin MEUNIER committed
131
	int cours=0;
Valentin MEUNIER's avatar
Valentin MEUNIER committed
132
133
	int depart=0;
	int arrivee=12;
Valentin MEUNIER's avatar
enfin    
Valentin MEUNIER committed
134
	int taille_cell=min(screen.w/(P+2),screen.h/(N+2));
Valentin MEUNIER's avatar
Valentin MEUNIER committed
135
	//printf("%d \n", taille_cell);
Valentin MEUNIER's avatar
enfin    
Valentin MEUNIER committed
136
137
	graph_t * graph=creer_graph(noeuds,nb_aretes);
	generation(graph);
Valentin MEUNIER's avatar
Valentin MEUNIER committed
138
	graph=Fisher(graph,nb_aretes);
Valentin MEUNIER's avatar
Valentin MEUNIER committed
139
	graph=kruskal(graph,noeuds,nb_aretes,&cours,p);
Valentin MEUNIER's avatar
enfin    
Valentin MEUNIER committed
140
	tab=tableau_ligne(graph,cours);
Valentin MEUNIER's avatar
disj    
Valentin MEUNIER committed
141
	int temps=600;
Valentin MEUNIER's avatar
Valentin MEUNIER committed
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
	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;
			}
Valentin MEUNIER's avatar
disj    
Valentin MEUNIER committed
162
163
164
165
166
167
168
169
170
171
172
173
			if (temps>600)
			{
				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;
Valentin MEUNIER's avatar
Valentin MEUNIER committed
174
			SDL_RenderPresent(renderer);
Valentin MEUNIER's avatar
disj    
Valentin MEUNIER committed
175
			SDL_Delay(10);
Valentin MEUNIER's avatar
Valentin MEUNIER committed
176
177
178
179
180
181
182
		}
	}
	end_sdl(1, "Normal ending", window, renderer);
	return 1;
}