-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cpp
277 lines (221 loc) · 8.38 KB
/
Game.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
#define GLEW_STATIC
#include <GL/glew.h>
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
#include "glm/glm.hpp"
#include "glm/gtc/type_ptr.hpp"
#include <stdlib.h>
#include <stdio.h>
#include <chrono>
#include <thread>
#include "Scene.h"
#include "Game.h"
#include <random>
#include <thread>
namespace PhysicalSimulationGame {
Scene *scene;
GLFWwindow *window;
glm::vec2 press_mouse_position;
glm::vec2 current_mouse_position;
bool is_mouse_press;
bool create_box;
bool create_circle;
float changeRotation;
glm::vec2 viewport2camera(glm::vec2 point);
static void error_callback(int error, const char *description) {
fprintf(stderr, "Error: %s\n", description);
}
static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) {
if (mods == GLFW_MOD_CONTROL) {
switch (key) {
case GLFW_KEY_UP:
scene->Move(0, .1, true);
return;
case GLFW_KEY_DOWN:
scene->Move(0, -.1, true);
return;
case GLFW_KEY_LEFT:
scene->Move(-.1, 0, true);
return;
case GLFW_KEY_RIGHT:
scene->Move(.1, 0, true);
return;
}
}
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GLFW_TRUE);
else if (key >= GLFW_KEY_1 && key <= GLFW_KEY_9 && action == GLFW_PRESS) {
scene->Select(key - GLFW_KEY_1);
} else if (key == GLFW_KEY_UP && action == GLFW_PRESS) {
scene->ChangeMass(1);
} else if (key == GLFW_KEY_DOWN && action == GLFW_PRESS) {
scene->ChangeMass(-1);
} else if (key == GLFW_KEY_LEFT && action == GLFW_PRESS) {
scene->Select(scene->GetSelectedRigidBody() - 1);
} else if (key == GLFW_KEY_RIGHT && action == GLFW_PRESS) {
scene->Select(scene->GetSelectedRigidBody() + 1);
} else if (key == GLFW_KEY_R) {
changeRotation = 1;
} else if (key == GLFW_KEY_C && action == GLFW_PRESS) {
create_circle = true;
} else if (key == GLFW_KEY_B && action == GLFW_PRESS) {
create_box = true;
}
}
static void mouse_callback(GLFWwindow *window, int button, int action, int mods) {
if (action == GLFW_RELEASE) {
press_mouse_position = current_mouse_position;
}
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
double x, y;
glfwGetCursorPos(window, &x, &y);
press_mouse_position = glm::vec2(x, y);
is_mouse_press = true;
static auto before = std::chrono::system_clock::now();
auto now = std::chrono::system_clock::now();
double diff_ms = std::chrono::duration<double, std::milli>(now - before).count();
before = now;
if (diff_ms > 10 && diff_ms < 250) {
create_box = true;
} else {
auto pos = viewport2camera(glm::vec2(x, y));
scene->Select(pos.x, pos.y);
}
} else
is_mouse_press = false;
}
static void cursor_position_callback(GLFWwindow *window, double xpos, double ypos) {
current_mouse_position = glm::vec2(xpos, ypos);
}
static void scroll_callback(GLFWwindow *window, double xoffset, double yoffset) {
changeRotation = yoffset;
}
static void resize_callback(GLFWwindow *window, int width, int height) {
double ratio = width / (float) height;
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-ratio * 75.f, ratio * 75.f, -75, 75.f);
}
glm::vec2 viewport2camera(glm::vec2 point) {
float ratio;
int width, height;
glfwGetFramebufferSize(window, &width, &height);
ratio = width / (float) height;
return glm::vec2(point.x * 150 * ratio / width - 75 * ratio, -point.y * 150 / height + 75);
}
void init() {
glClearColor(0, 0, 0, 1);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
scene = new Scene();
scene->init();
}
void Update(float dt) {
scene->update(dt);
}
void Render() {
glClear(GL_COLOR_BUFFER_BIT);
auto pos = viewport2camera(is_mouse_press ? current_mouse_position : press_mouse_position);
glColor3f(1, 0, 0);
glBegin(GL_LINES);
glVertex2f(pos.x - 5, pos.y + 0);
glVertex2f(pos.x + 5, pos.y + 0);
glVertex2f(pos.x + 0, pos.y - 5);
glVertex2f(pos.x + 0, pos.y + 5);
glEnd();
scene->render();
glfwSwapBuffers(window);
}
void processInput() {
if (create_box) {
create_box = false;
int h = 10 + (rand() / (float) RAND_MAX) * 20;
int w = 10 + (rand() / (float) RAND_MAX) * 20;
int m = 1 + (rand() / (float) RAND_MAX) * 3;
scene->Select(scene->AddBox(h, w, m));
auto pos = viewport2camera(press_mouse_position);
scene->Move(pos.x, pos.y);
} else if (create_circle) {
create_circle = false;
int radius = 8 + (rand() / (float) RAND_MAX) * 5;
int mass = 10 + (rand() / (float) RAND_MAX) * 5;
scene->Select(scene->AddCircle(radius, mass));
auto pos = viewport2camera(press_mouse_position);
scene->Move(pos.x, pos.y);
} else if (is_mouse_press && current_mouse_position != press_mouse_position) {
auto pos = viewport2camera(current_mouse_position);
scene->Move(pos.x, pos.y);
} else if (changeRotation != 0) {
scene->Rotate(changeRotation / 30.0, true);
changeRotation = 0;
}
glfwPollEvents();
}
}
using namespace PhysicalSimulationGame;
int main() {
glfwSetErrorCallback(error_callback);
if (!glfwInit()) exit(EXIT_FAILURE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
glfwWindowHint(GLFW_SAMPLES, 8);
window = glfwCreateWindow(640, 480, "Rigid Body Simulation", NULL, NULL);
if (!window) {
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwSetKeyCallback(window, key_callback);
glfwSetMouseButtonCallback(window, mouse_callback);
glfwSetCursorPosCallback(window, cursor_position_callback);
glfwSetScrollCallback(window, scroll_callback);
glfwSetWindowSizeCallback(window, resize_callback);
glfwMakeContextCurrent(window);
glewInit();
init();
glfwSwapInterval(0);
//double previous_time = glfwGetTime();
//while (!glfwWindowShouldClose(window))
//{
// double current_time = glfwGetTime();
// double delta = current_time - previous_time;
// if (delta >= 0.1)
// {
// processInput();
// Update(0.1);
// Render();
// prevTime = current_time;
// }
// else
// {
// std::this_thread::sleep_for(std::chrono::milliseconds(10));
// }
//}
static const float MS_PER_RENDER = 0.1;
static const float MS_PER_UPDATE = 0.001;
double previous_time = glfwGetTime();
double previous_render_time = previous_time;
double lag = 0.0;
while (!glfwWindowShouldClose(window)) {
double current_time = glfwGetTime();
double elapsed = current_time - previous_time;
double delta = current_time - previous_render_time;
previous_time = current_time;
lag += elapsed;
processInput();
while (lag >= MS_PER_UPDATE) {
Update(MS_PER_UPDATE);
lag -= MS_PER_UPDATE;
}
if (delta >= MS_PER_RENDER) {
Render(/*lag / MS_PER_UPDATE*/);
previous_render_time = current_time;
} else {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_SUCCESS);
}