-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp.disable
119 lines (87 loc) · 2.92 KB
/
main.cpp.disable
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
#include <iostream>
#include <vector>
#include <thread>
#include <allegro5/allegro.h>
#include <allegro5/allegro_primitives.h>
using namespace std;
void crashOnError(std::string message);
ALLEGRO_KEYBOARD_STATE keyboardState;
ALLEGRO_DISPLAY* displayWindows = nullptr;
int displayWidth = 1800;
int displayHeight =1000;
int totalEntities = 90000;
unsigned int gStep = 0;
int gVx = 1;
int gVy = 1;
int main() {
if(!al_init()) {
crashOnError("failed to initialize allegro!");
}
if(!al_init_primitives_addon()) {
crashOnError("failed to initialize allegro primitives addon!");
}
al_set_new_display_flags(ALLEGRO_OPENGL_3_0);
displayWindows = al_create_display(displayWidth, displayHeight);
if(!displayWindows) {
crashOnError("failed to initialize allegro display!");
}
if(!al_install_keyboard()) {
crashOnError("Failed to initialize allegro keyboard handling!");
}
if(!al_install_mouse()) {
crashOnError("Failed to initialize allegro mouse handling!");
}
// Horrible facon de representer les positions des individus
std::vector<std::vector<double>> entities;
std::vector<std::vector<double>> velocities;
// Initialisation des positions
srand(42);
for (int i = 0; i < totalEntities; ++i) {
int x = rand() % displayWidth;
int y = rand() % displayHeight;
entities.push_back({static_cast<double>(x), static_cast<double>(y)});
double vx = 4*(rand()/RAND_MAX) - 2;
double vy = 4*(rand()/RAND_MAX) - 2;
velocities.push_back({vx, vy});
}
// Boucle principale
while (true){
// Récupération des évenements clavier
al_get_keyboard_state(&keyboardState);
// Sortie si Esc.
if (al_key_down(&keyboardState, ALLEGRO_KEY_ESCAPE))
break;
// Comportement des individus
int i = 0;
for (auto& e: entities){
e[0] += velocities[i][0];
e[1] += velocities[i][1];
++i;
}
// Sometimes, change boids velocity
if (gStep%20 == 0){
// Change boids velocity
for (auto& v: velocities){
v[0] = 4*(rand()/ static_cast<double>(RAND_MAX)) - 2;
v[1] = 4*(rand()/ static_cast<double>(RAND_MAX)) - 2;
}
}
// On efface tout (dans le backbuffer)
al_clear_to_color(al_map_rgb(250,250,250));
// Dessin des individus (dans le backbuffer)
for (const auto& e: entities){
al_draw_filled_ellipse(e[0], e[1], 4, 4, al_map_rgba(10, 20, 100, 150) );
}
// On affiche le backbuffer
al_flip_display();
this_thread::sleep_for(std::chrono::milliseconds(1));
gStep++;
}
al_destroy_display(displayWindows);
al_shutdown_primitives_addon();
return 0;
}
void crashOnError(std::string message) {
cerr << message << endl;
exit(-1);
}