-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
196 lines (175 loc) · 6.83 KB
/
main.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
/*********************************************************************
* Final Project for the Programming Subject *
* Integrated Master in Electrical and Computer Engineering - IST *
* Authors: João Maria Janeiro - ist190105 *
* Guilherme Viegas - ist190090 *
*********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "structs.h"
#include "menus.h"
#include "lists.h"
#include "statistics.h"
#include "graphics.h"
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_image.h>
#include <time.h>
#define WINDOW_POSX 200 // initial position of the window: x
#define WINDOW_POSY 200 // initial position of the window: y
#define WORLD_WIDTH 1400 //World map sizes
#define WORLD_HEIGHT 601
#define MAP_CENTER_WIDTH 600
#define MAP_CENTER_HEIGHT 300.5
#define M_PI 3.14158265
int GetUserMainParams(int, char*[], char[], char[], int *);
int main(int argc, char* argv[]){
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
TTF_Font *serif = NULL;
SDL_Surface *imgs[3];
SDL_Event event;
int width = WORLD_WIDTH; //map image width
int height = WORLD_HEIGHT; //map image height
int delay = 100;
int quit = 0;
int colors[3][3] = {{0, 255, 255}, {0, 255, 0}, {255, 0, 0}}; //Azul, Amarelo, Vermelho
int anoAtual = 1743;
int primeiroAno = 2500;
char strAnoAtual[10] = {0};
int pt_x = 0, pt_y = 0;
SDL_Color white = {255, 255, 255};
int isGraphic = -1; //If is graphic->1, if is textual->0
char fileNameCountry[40] = {0};
char fileNameCity[40] = {0};
node_t_country *headCountry = NULL;
node_t_country *tailCountry = NULL;
node_t_city *headCity = NULL;
node_t_city *tailCity = NULL;
node_t_city *aux = NULL;
node_t_country *auxCountry = NULL;
int doExit = 0;
//If user params weren't well inserted
if(!GetUserMainParams(argc, argv, fileNameCountry, fileNameCity, &isGraphic)) {
exit(EXIT_FAILURE);
}
printf("A Carregar os Dados - Espere 1 Minuto\n");
loadCountryFromFile(fileNameCountry, &headCountry, &tailCountry);
printf("Loaded Country File - Loading City File\n");
loadCityFromFile(fileNameCity, &headCity, &tailCity);
aux = headCity;
if(isGraphic == 1) {
//Modo Gráfico:
while(aux != NULL) { //Para saber qual o ano a começar a mostrar na gráfica (permite um código independente do ficheiro, senão seria preciso inserir diretamente 1743)
if(aux->payload.dt.ano < primeiroAno)
primeiroAno = aux->payload.dt.ano;
aux=aux->next;
}
anoAtual = primeiroAno;
InitEverything(width, height, WINDOW_POSX, WINDOW_POSY, &serif, imgs, &window, &renderer);
while( quit == 0) {
while( SDL_PollEvent( &event) ) {
if(event.type == SDL_QUIT) {
quit = 1;
//Frees both city and country lists in case of exit
auxCountry = headCountry;
while(auxCountry != tailCountry) {
auxCountry = auxCountry->next;
free(auxCountry->prev);
}
free(tailCountry);
aux = headCity;
while(aux != tailCity) {
aux = aux->next;
free(aux->prev);
}
free(tailCity);
}
if(event.type == SDL_MOUSEMOTION) {
//Saves the mouse positions
pt_x = event.button.x;
pt_y = event.button.y;
}
}
RenderTexture(imgs, serif, renderer);
if(anoAtual > tailCity->payload.dt.ano) {
anoAtual = primeiroAno;
}
sprintf(strAnoAtual, "%d", anoAtual); //Converts an int to a string
RenderText(10, 10, strAnoAtual, serif, &white, renderer);
ReadFromCityList(renderer, serif, &anoAtual, &headCity, &tailCity, colors, MAP_CENTER_HEIGHT, MAP_CENTER_WIDTH, pt_x, pt_y);
// render in the screen all changes above
SDL_RenderPresent(renderer);
// add a delay
SDL_Delay( delay );
}
TTF_CloseFont(serif);
SDL_FreeSurface(imgs[0]);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}else if(!isGraphic) {
//Modo Textual:
while(doExit != 1) {
switch(MainMenu()) {
case 1:
FiltragemDeDados(&headCountry, &tailCountry, &headCity, &tailCity, fileNameCountry, fileNameCity);
break;
case 2:
HistoricoTemperaturas(&headCountry, &tailCountry, &headCity, &tailCity);
getchar();
break;
case 3:
AnaliseTemperaturaPorAno(&headCountry, &headCity);
getchar();
break;
case 4:
AnaliseTemperaturaGlobal(&headCountry, &tailCountry, &headCity, &tailCity);
getchar();
break;
case 5:
doExit = 1;
//Frees both country and city lists
auxCountry = headCountry;
while(auxCountry != tailCountry) {
auxCountry = auxCountry->next;
free(auxCountry->prev);
}
free(tailCountry);
aux = headCity;
while(aux != tailCity) {
aux = aux->next;
free(aux->prev);
}
free(tailCity);
break;
default:
printf("Erro na introdução de dados!\n");
exit(EXIT_FAILURE);
}
}
} else {
printf("Need to choose Textual or Graphical Mode!\n");
exit(EXIT_FAILURE);
}
return EXIT_SUCCESS;
}
/* Gets and Verifies User Params from terminal
*/
int GetUserMainParams(int _argc, char* _argv[], char _fileNameCountry[], char _fileNameCity[], int *_isGraphic) {
if(_argc != 6)
return 0;
for(int i=1; i<_argc; i++) {
if(!strcmp(_argv[i], "-g")) {
*_isGraphic = 1;
} else if(!strcmp(_argv[i], "-t")) {
*_isGraphic = 0;
} else if(i != _argc-1 && !strcmp(_argv[i], "-f1")) {
strcpy(_fileNameCountry, _argv[i+1]);
} else if(i != _argc-1 && !strcmp(_argv[i], "-f2")) {
strcpy(_fileNameCity, _argv[i+1]);
}
}
return 1;
}