Repository navigation
Expand file tree
/
Copy pathgui.cpp
More file actions
78 lines (64 loc) · 2.7 KB
/
Copy pathgui.cpp
File metadata and controls
78 lines (64 loc) · 2.7 KB
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
#include <GL/glut.h>
#include "gui.hpp"
#include "simulation.hpp"
#include <chrono>
#include <thread>
#include <iostream>
using namespace std;
namespace gui {
namespace {
const int window_width = 800;
const int window_height = 800;
const int window_position_x = 200;
const int window_position_y = 200;
const char* window_title = "Life Simulator";
const unsigned short frames_per_second = 60;
simulation::simulationConfig *config;
const int colors[4][3] = {
{0, 255, 0},
{0, 255, 255},
{255, 255, 255},
{255, 0, 0}
};
void initGL() {
// Set "clearing" or background color
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black and opaque
}
[[noreturn]] void display2d() {
chrono::milliseconds delay_between_frames = chrono::milliseconds(1000 / frames_per_second);
auto timestamp = chrono::system_clock::now();
while (true) {
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer with current clearing color
glPointSize(2.0);
glBegin(GL_POINTS);
float *coordinates = simulation::get_coordinates();
for (auto pc_index = 0; pc_index < config->num_point_clouds; pc_index++) {
glColor3ub(colors[pc_index][0], colors[pc_index][1], colors[pc_index][2]);
float *pc_coordinates = coordinates + pc_index * config->points_per_cloud * 2;
for (auto p_index = 0; p_index < config->points_per_cloud; p_index++) {
glVertex2fv(pc_coordinates + p_index * 2);
}
}
glEnd();
auto passed_time = chrono::system_clock::now() - timestamp;
if (passed_time < delay_between_frames) {
this_thread::sleep_for(delay_between_frames - passed_time);
}
glFlush(); // Render now
simulation::simulate_next_frame();
}
}
}
void setup_window(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUT
glutInitWindowSize(window_width, window_height); // Set the window's initial width & height
glutInitWindowPosition(window_position_x, window_position_y); // Position the window's initial top-left corner
glutCreateWindow(window_title); // Create a window with the given title
}
void run_animation(simulation::simulationConfig *sim_config) {
config = sim_config;
glutDisplayFunc(display2d);
initGL();
glutMainLoop();
}
}