-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
358 lines (269 loc) · 8.06 KB
/
main.cpp
File metadata and controls
358 lines (269 loc) · 8.06 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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
//PhysX Initialization code adapted from the "Boxes Sample" program contained within the AGEIA PhysX SDK
#include "imgui.h"
#include "imgui_impl_glfw.h"
#include "imgui_impl_opengl3.h"
#include <stdio.h>
#include <iostream>
#include <Windows.h>
#include <glad/gl.h>
#include <GLFW/glfw3.h>
#include <string>
#include "BlockDriver.h"
#include "BlockStructure.h"
#include "Matrix44.h"
#include "Controller.h"
// Rendering
static int gMouseX = 0;
static int gMouseY = 0;
//My variables
static bool fullscreenEnabled = false;
static double PI = 3.141592654;
static double yRotation = PI / 4.0;
static double xzRotation = 3.0 * PI / 4.0;
static float cameraRadius = 500.0;
enum {X, Y, Z, W};
static GLfloat originalWindowWidth = 640, originalWindowHeight = 360;
static GLfloat windowWidth = originalWindowWidth, windowHeight = originalWindowHeight;
static GLfloat eye[3] = {(cameraRadius*(cos(yRotation)))*cos(xzRotation), cameraRadius*sin(yRotation), (cameraRadius*(cos(yRotation)))*sin(xzRotation)};
static GLint mouse[2] = {0, 0};
Controller* controller;
static bool init()
{
cout << "Blocks is a game written by Scott Duffey. For best results, play in full screen mode\n\n";
cout << "The object of the game is to light up all of the colored blocks without crossing your own path or running into solid blocks.\n\n";
cout << "Controls:\n\n";
cout << "F1:\t\t\tToggle full screen\n";
cout << "F2:\t\t\tToggle debug mode\n";
cout << "Arrows Keys:\t\tRotate the laser's direction (with respect to itself)\n";
cout << "w:\t\t\tZoom camera in\n";
cout << "s:\t\t\tZoom camera out\n";
cout << "Right-click + Drag:\tRotate camera\n";
cout << "q:\t\t\tReturn to main menu\n";
cout << "ESC:\t\t\tQuit\n";
controller = new Controller(windowWidth, windowHeight);
return true;
}
static void exit()
{
delete controller;
}
static void rotateXZ(double radian)
{
xzRotation += radian;
if(xzRotation > 2*PI)
xzRotation = 0;
if(xzRotation < 0)
xzRotation = 2*PI;
eye[X] = (cameraRadius*(cos(yRotation)))*cos(xzRotation);
eye[Z] = (cameraRadius*(cos(yRotation)))*sin(xzRotation);
}
static void rotateY(double radian)
{
rotateXZ(0);
yRotation += radian;
if(yRotation > 2*PI)
yRotation = 0;
if(yRotation < 0)
yRotation = 2*PI;
eye[Y] = cameraRadius*sin(yRotation);
rotateXZ(0);
}
void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
(window, GLFW_TRUE);
if (action == GLFW_PRESS)
controller->sendKeyPress(key);
}
int left_action = GLFW_RELEASE;
int right_action = GLFW_RELEASE;
static void cursor_position_callback(GLFWwindow* window, double xpos, double ypos)
{
int origmousex = mouse[X];
int origmousey = mouse[Y];
mouse[X] = (int) xpos;
mouse[Y] = (int) ypos;
if (controller == NULL) return;
if (controller->isMainMenuEnabled()) {
controller->sendMouseUp(mouse[X], windowHeight - mouse[Y]);
}
else {
if (left_action == GLFW_PRESS) {
controller->sendMouseDown(mouse[X], windowHeight - mouse[Y]);
}
if (right_action == GLFW_PRESS) {
controller->sendMouseDown(mouse[X], windowHeight - mouse[Y]);
rotateXZ(-1.0 * (origmousex - mouse[X]) / 100.0);
rotateY(-1.0 * (origmousey - mouse[Y]) / 100.0);
}
else {
controller->sendMouseUp(mouse[X], windowHeight - mouse[Y]);
}
}
}
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
{
if (button == GLFW_MOUSE_BUTTON_RIGHT)
right_action = action;
else if (button == GLFW_MOUSE_BUTTON_LEFT)
left_action = action;
if (left_action == GLFW_PRESS)
controller->sendMouseDown(mouse[X], windowHeight - mouse[Y]);
else
controller->sendMouseUp(mouse[X], windowHeight - mouse[Y]);
}
void perspectiveGL(GLdouble fovY, GLdouble aspect, GLdouble zNear, GLdouble zFar)
{
const GLdouble pi = 3.1415926535897932384626433832795;
GLdouble fW, fH;
//fH = tan( (fovY / 2) / 180 * pi ) * zNear;
fH = tan(fovY / 360 * pi) * zNear;
fW = fH * aspect;
glFrustum(-fW, fW, -fH, fH, zNear, zFar);
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
windowWidth = width;
windowHeight = height;
controller->setWindowWidth(width);
controller->setWindowHeight(height);
glViewport(0, 0, windowWidth, windowHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
perspectiveGL(45.0, (double)(windowWidth / windowHeight), 0.1, 2000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
cout << "resize ran" << endl;
}
void error_callback(int error, const char* description)
{
fprintf(stderr, "Error: %s\n", description);
}
void normalize(float v[3]) {
float lensq = v[0] * v[0]
+ v[1] * v[1]
+ v[2] * v[2];
float len = sqrt(lensq);
v[0] /= len;
v[1] /= len;
v[2] /= len;
}
void cross(float side[3], float forward[3], float up[3]) {
up[0] = side[1] * forward[2] - side[2] * forward[1];
up[1] = -(side[0] * forward[2] - side[2] * forward[0]);
up[2] = side[0] * forward[1] - side[1] * forward[0];
}
void lookAt(GLdouble eyex, GLdouble eyey, GLdouble eyez, GLdouble centerx,
GLdouble centery, GLdouble centerz, GLdouble upx, GLdouble upy,
GLdouble upz)
{
int i;
float forward[3], side[3], up[3];
GLfloat m[4][4];
forward[0] = centerx - eyex;
forward[1] = centery - eyey;
forward[2] = centerz - eyez;
up[0] = upx;
up[1] = upy;
up[2] = upz;
normalize(forward);
/* Side = forward x up */
cross(forward, up, side);
normalize(side);
/* Recompute up as: up = side x forward */
cross(side, forward, up);
for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
m[i][j] = i == j ? 1.0 : 0.0;
m[0][0] = side[0];
m[1][0] = side[1];
m[2][0] = side[2];
m[0][1] = up[0];
m[1][1] = up[1];
m[2][1] = up[2];
m[0][2] = -forward[0];
m[1][2] = -forward[1];
m[2][2] = -forward[2];
glMultMatrixf(&m[0][0]);
glTranslated(-eyex, -eyey, -eyez);
}
int main(int argc, char** argv)
{
GLFWwindow* window;
// Initialize glut
glfwSetErrorCallback(error_callback);
if (!glfwInit())
exit(EXIT_FAILURE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
window = glfwCreateWindow(640, 480, "Blocks", NULL, NULL);
if (!window)
{
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwSetKeyCallback(window, key_callback);
glfwSetCursorPosCallback(window, cursor_position_callback);
glfwSetMouseButtonCallback(window, mouse_button_callback);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwMakeContextCurrent(window);
gladLoadGL(glfwGetProcAddress);
glfwSwapInterval(1);
ImGui::CreateContext();
ImGuiIO& io = ImGui::GetIO(); (void)io;
ImGui::StyleColorsDark();
ImGui_ImplGlfw_InitForOpenGL(window, true);
ImGui_ImplOpenGL3_Init("#version 130");
bool show_demo_window = true;
bool show_another_window = false;
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
init();
framebuffer_size_callback(NULL, 640, 480);
while (!glfwWindowShouldClose(window))
{
float ratio;
int width, height;
glfwGetFramebufferSize(window, &width, &height);
ratio = width / (float)height;
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT);
// Draw stuff
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
lookAt(eye[X], eye[Y], eye[Z], 0.0, 45.0, 0.0, 0, 1, 0);
//gluLookAt(0.0, 0, -1.0, 0.0, 0.0, 0.0, 0, 1, 0);
controller->display();
//glutSolidTeapot(1.0);
glFinish();
controller->update();
glFinish();
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_SUCCESS);
/*
glutInit(&argc, argv);
glutInitWindowPosition(100, 100);
glutInitWindowSize(originalWindowWidth, originalWindowHeight);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH | GLUT_STENCIL);
int mainHandle = glutCreateWindow("Blocks");
glutSetWindow(mainHandle);
glutDisplayFunc(display);
glutReshapeFunc(resize);
glutIdleFunc(idle);
glutKeyboardFunc(keyPress);
glutSpecialFunc(arrowKeyPress);
glutMouseFunc(mousePress);
glutMotionFunc(mouseDrag);
glutPassiveMotionFunc(mouseMove);
mouseDrag(0,0);
atexit(exit);
// Initialize physics scene and start the application main loop if scene was created
if (init())
glutMainLoop();
glfwTerminate();
*/
return 0;
}