-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
307 lines (274 loc) · 7.85 KB
/
main.cpp
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
/* File name: main.cpp
*
* Description:
* Responsible for redirecting user input (argv) and making it usable for the
* reading module (sim.h).
* Error mode tests the validity of input data.
* Force mode prints the mutual force between two particles.
*
* Date: 14.05.2019
* Version: 3.05
*
* Authors - SCIPER:
* Alexis Philip George-Georganopoulos - 228168
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <GL/glut.h>
#include <GL/glui.h>
extern "C"
{
#include "sim.h"
#include "constantes.h"
#include "error.h"
}
namespace
{
enum MODE mode;
GLfloat aspect_ratio = 1.f;
int main_window, nb_g = 0, nb_bh = 0, nb_p = 0;
float width, height;
float gauche= -RMAX, droite = RMAX, bas= -RMAX, haut= RMAX;
GLUI *glui;
GLUI_Panel *file, *simulation, *information;
GLUI_Button *load, *save, *start, *step, *quit;
GLUI_EditText *load_file, *save_file,
*nb_particule, *nb_generateur, *nb_trou_noir;
const char *start_b = "Start";
int running = 0, flag = 0, selected = 0;
double ortho_x, ortho_y;
}
#define BUTTON1_ID 11
#define BUTTON2_ID 12
#define BUTTON3_ID 13
#define BUTTON4_ID 14
//------------------------------------------------------------------------------
enum MODE determine_mode(char * mode)
{
if(strncmp(mode, "Error", sizeof("Error")) == 0)
return ERROR;
else if(strncmp(mode, "Force", sizeof("Force")) == 0)
return FORCE;
else if(strncmp(mode, "Integration", sizeof("Integration")) == 0)
return INTEGRATION;
else if(strncmp(mode, "Graphic", sizeof("Graphic")) == 0)
return GRAPHIC;
else if(strncmp(mode, "Simulation", sizeof("Simulation")) == 0)
return SIMULATION;
else return NOT_VALID;
}
//------------------------------------------------------------------------------
// control call back; a switch for each GLUI callback
void control_cb( int control )
{
switch(control)
{
case BUTTON1_ID :
if(running) break;
lecture(SIMULATION, load_file -> get_text());
get_viewport(&gauche, &droite, &bas, &haut);
float ratio; // allows for non streched layout on screen
ratio =(float) (droite-gauche)/(haut-bas);
if(ratio > 1)
{
bas *= ratio;
haut *= ratio;
}
if(ratio < 1)
{
gauche /= ratio;
droite /= ratio;
}
flag = 1;
glutPostRedisplay();
break;
case BUTTON2_ID :
ecriture(save_file -> get_text());
break;
case BUTTON3_ID :
if(!running)
{
start -> set_name("Stop");
running = 1;
}
else
{
start -> set_name("Start");
running = 0;
}
break;
case BUTTON4_ID :
if(running)
{
running = 0;
start -> set_name("Start");
}
if(mode == GRAPHIC) printf("sim_update\n");
if(mode == SIMULATION)
{
sim_update();
glutPostRedisplay();
}
break;
default :
printf("INVALID COMMAND\nPROGRAM TERMINATION\n");
exit(EXIT_FAILURE);
break;
}
}
//------------------------------------------------------------------------------
// draws everything in window
void affichage(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
if (aspect_ratio <= 1.)
glOrtho(gauche, droite, bas/aspect_ratio, haut/aspect_ratio, -1.0, 1.0);
else
glOrtho(gauche*aspect_ratio, droite*aspect_ratio, bas, haut, -1.0, 1.0);
if(flag)
{
redraw_all();
nb_generateur-> set_int_val(nb_g);
nb_trou_noir -> set_int_val(nb_bh);
nb_particule -> set_int_val(nb_p);
}
glutSwapBuffers();
}
//------------------------------------------------------------------------------
// keeps things in proportion on screen
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
aspect_ratio = (GLfloat) w / (GLfloat) h;
width = (float) w;
height = (float) h;
}
//------------------------------------------------------------------------------
// updates execution while user is not interacting
void idle(void)
{
if(glutGetWindow() != main_window) glutSetWindow(main_window);
get_nb(&nb_g, &nb_bh, &nb_p);
nb_generateur -> set_int_val(nb_g);
nb_trou_noir -> set_int_val(nb_bh);
nb_particule -> set_int_val(nb_p);
if(running && mode == GRAPHIC) printf("sim_update\n");
if(running && mode == SIMULATION) sim_update();
glutPostRedisplay();
}
//------------------------------------------------------------------------------
// mouse call back. selects element
void mouse(int button, int state, int x, int y)
{
selected = 0;
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
selected = 1;
if(aspect_ratio >= 1.f)
{
ortho_x = (x*(droite-gauche)/width + gauche) * aspect_ratio;
ortho_y = haut - y*(haut-bas)/height;
}
if(aspect_ratio < 1.f)
{
ortho_x = x*(droite-gauche)/width + gauche;
ortho_y = (haut - y*(haut-bas)/height) / aspect_ratio;
}
if(mode == GRAPHIC) printf("sim_select\n");
if(mode == SIMULATION) sim_select(ortho_x, ortho_y);
}
else if(button == GLUT_LEFT_BUTTON && state == GLUT_UP)
{
if(mode == GRAPHIC) printf("sim_release\n");
if(mode == SIMULATION) sim_release();
selected = 0;
}
}
//------------------------------------------------------------------------------
// keyboard call back. deletes selected element
void keyboard(unsigned char key, int x, int y)
{
if(selected && key == 'd')
{
if(mode == GRAPHIC) printf("sim_delete\n");
if(mode == SIMULATION) sim_delete(ortho_x, ortho_y);
}
selected = 0;
}
//------------------------------------------------------------------------------
// creates GUI
void create_glui(int argc, char * argv[])
{
glui = GLUI_Master.create_glui("MICROCOSMOS", 0, 467, 0);
file = glui -> add_panel("File", GLUI_PANEL_EMBOSSED);
load_file = glui -> add_edittext_to_panel(file, "Filename");
if(argc == 3)
load_file -> set_text(argv[2]);
load = glui -> add_button_to_panel(file, "Load", BUTTON1_ID, control_cb);
save_file = glui -> add_edittext_to_panel(file, "Filename");
save_file -> set_text("Save.txt");
save = glui -> add_button_to_panel(file, "Save", BUTTON2_ID, control_cb);
simulation = glui -> add_panel("Simulation", GLUI_PANEL_EMBOSSED);
start = glui -> add_button_to_panel(simulation, start_b, BUTTON3_ID,
control_cb);
step = glui -> add_button_to_panel(simulation, "Step", BUTTON4_ID,
control_cb);
information = glui -> add_panel("Information", GLUI_PANEL_EMBOSSED);
nb_particule = glui -> add_edittext_to_panel(information, "Nb Particule");
nb_generateur = glui -> add_edittext_to_panel(information, "Nb Generateur");
nb_trou_noir = glui -> add_edittext_to_panel(information, "Nb Trou Noir");
quit = glui -> add_button("Exit", EXIT_SUCCESS, (GLUI_Update_CB)exit);
glui -> set_main_gfx_window(main_window);
}
//------------------------------------------------------------------------------
// program mainframe (see inside for more details)
int main(int argc, char * argv[])
{
if(argc == 1) mode = SIMULATION;
if((argc != 1) && (argc != 3))
{
error_msg((char *) "Usage: './sim.x mode_test fichier' ou './sim.x'\n");
exit(EXIT_FAILURE);
}
if(argc == 3)
{
mode = determine_mode(argv[1]);
lecture((int) mode, argv[2]);
flag = 1;
}
if(flag)
{
get_viewport(&gauche, &droite, &bas, &haut);
float ratio;
ratio =(float) (droite-gauche)/(haut-bas);
if(ratio > 1)
{
bas *= ratio;
haut *= ratio;
}
if(ratio < 1)
{
gauche /= ratio;
droite /= ratio;
}
get_nb(&nb_g, &nb_bh, &nb_p);
}
// responsible for GLUT window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowPosition(0, 0);
glutInitWindowSize(400, 600);
main_window = glutCreateWindow("MICROCOSMOS");
glClearColor(1.0, 1.0, 1.0, 0.0);
glutDisplayFunc(affichage);
glutReshapeFunc(reshape);
glutIdleFunc(idle);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
// responsible for GUI
create_glui(argc, argv);
glutMainLoop();
return EXIT_SUCCESS;
}