-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cc
133 lines (108 loc) · 3.84 KB
/
main.cc
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
#include <GLFW/glfw3.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include "builder/builder.hh"
#include "common/config.hh"
#include "engine/simulate.hh"
#include "renderer/builder.hh"
#include "renderer/events.hh"
#include "renderer/simulate.hh"
void init_view() {
// set up view
// glViewport(0, 0, kWidth, kHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// this creates a canvas to do 2D drawing on
glOrtho(0.0, common::kWidth, 0.0, common::kHeight, 0.0, 1.0);
}
void print_state(EventState state) {
std::cout << "EventHandler::set_state() to '" << event_state_to_string(state) << "'\n";
}
using Clock = std::chrono::high_resolution_clock;
void print_fps() {
// Meh, global variables here just to make it easy
static size_t frame_counter = 0;
static Clock::time_point last_time = Clock::now();
static constexpr size_t kFPSFrames = 100;
if (++frame_counter % kFPSFrames == 0) {
auto now = Clock::now();
const double fps = kFPSFrames / std::chrono::duration<double>(now - last_time).count();
std::cout << "Average fps: " << fps << "\n";
last_time = now;
}
}
int main() {
EventHandler handler;
GLFWwindow *window;
if (!glfwInit()) {
std::cerr << "Failed to initialize GLFW\n";
exit(EXIT_FAILURE);
}
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
window = glfwCreateWindow(common::kWidth, common::kHeight, "Window", NULL, NULL);
if (!window) {
std::cerr << "Unable to create window!\n";
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);
init_view();
Builder builder;
Simulator simulator;
// Set up all the callbacks, note that some of these capture by reference but
// since the EventHandler outlives everything in the main function, this
// should be fine.
handler.add_state_callback(&print_state);
handler.add().key(GLFW_KEY_ESCAPE, [](GLFWwindow *window, int) { glfwSetWindowShouldClose(window, 1); });
handler.add().key(GLFW_KEY_SPACE, [&](GLFWwindow *, int) {
auto state = handler.get_state();
switch (state) {
case EventState::kInit:
case EventState::kSimulate:
handler.set_state(EventState::kBuild);
break;
case EventState::kBuild:
handler.set_state(EventState::kSimulate);
break;
}
});
handler.add_state_callback(EventState::kSimulate,
[&](EventState) { simulator.set_mesh(generate_mesh(builder.building_context())); });
builder.setup_callbacks(handler);
bool step = true;
handler.add().key(GLFW_KEY_P, [&](GLFWwindow *, int) { step = !step; });
glfwSetMouseButtonCallback(window, route_mouse_button_callback);
glfwSetCursorPosCallback(window, route_cursor_position_callback);
glfwSetKeyCallback(window, route_key_callback);
handler.set_state(EventState::kBuild);
// Main loop
while (!glfwWindowShouldClose(window)) {
glClearColor(0.1f, 0.2f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
auto state = handler.get_state();
switch (state) {
case EventState::kBuild: {
draw(builder.building_context());
draw(builder.drawing_context());
break;
}
case EventState::kSimulate: {
if (step) simulator.step(common::kRenderDt);
// step = false;
if (auto ptr = simulator.simulation_context()) draw(*ptr);
break;
}
default:
continue;
}
glfwSwapBuffers(window);
print_fps();
glfwPollEvents();
}
// Terminate GLFW
glfwTerminate();
// Exit program
exit(EXIT_SUCCESS);
}