-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
206 lines (168 loc) · 5.47 KB
/
Copy pathmain.cpp
File metadata and controls
206 lines (168 loc) · 5.47 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
#include "Application.h"
#include "imgui.h"
#include "imgui_impl_glut.h"
#include "imgui_impl_opengl3.h"
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <iostream>
//Remove console (only works in Visual Studio)
#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")
#define GLUT_SCROLL_UP 0x0003
#define GLUT_SCROLL_DOWN 0x0004
static int prevTime;
static bool capturingMouse;
void beginCapturingMouse() {
capturingMouse = true;
int width = glutGet(GLUT_WINDOW_WIDTH);
int height = glutGet(GLUT_WINDOW_HEIGHT);
glutWarpPointer(width/2, height/2);
glutSetCursor(GLUT_CURSOR_NONE);
}
void endCapturingMouse() {
capturingMouse = false;
glutSetCursor(GLUT_CURSOR_INHERIT);
}
// If a key is pressed this callback is called
static void keyboardDownCallback(unsigned char key, int x, int y)
{
ImGui_ImplGLUT_KeyboardFunc(key, x, y);
if (ImGui::GetIO().WantCaptureKeyboard) return;
if (key == 'i') {
if (capturingMouse) endCapturingMouse();
else beginCapturingMouse();
}
Application::instance().keyPressed(key);
}
// If a key is released this callback is called
static void keyboardUpCallback(unsigned char key, int x, int y)
{
ImGui_ImplGLUT_KeyboardUpFunc(key, x, y);
if (ImGui::GetIO().WantCaptureKeyboard) return;
Application::instance().keyReleased(key);
}
// If a special key is pressed this callback is called
static void specialDownCallback(int key, int x, int y)
{
ImGui_ImplGLUT_SpecialFunc(key, x, y);
if (ImGui::GetIO().WantCaptureKeyboard) return;
Application::instance().specialKeyPressed(key);
}
// If a special key is released this callback is called
static void specialUpCallback(int key, int x, int y)
{
ImGui_ImplGLUT_SpecialFunc(key, x, y);
if (ImGui::GetIO().WantCaptureKeyboard) return;
Application::instance().specialKeyReleased(key);
}
// Same for changes in mouse cursor position
static void motionCallback(int x, int y)
{
if (capturingMouse) return;
ImGui_ImplGLUT_MotionFunc(x, y);
}
static void passiveMotionCallback(int x, int y)
{
if (!capturingMouse) return;
int width = glutGet(GLUT_WINDOW_WIDTH);
int height = glutGet(GLUT_WINDOW_HEIGHT);
Application::instance().mouseMove(x - width/2, y - height/2);
if (x != width/2 || y != height/2) glutWarpPointer(width/2, height/2);
}
// Same for mouse button presses or releases
static void mouseCallback(int button, int state, int x, int y)
{
if (button == GLUT_SCROLL_UP) ImGui_ImplGLUT_MouseWheelFunc(button, 1, x, y);
else if (button == GLUT_SCROLL_DOWN) ImGui_ImplGLUT_MouseWheelFunc(button, -1, x, y);
else ImGui_ImplGLUT_MouseFunc(button, state, x, y);
if (ImGui::GetIO().WantCaptureMouse) return;
int buttonId;
switch (button)
{
case GLUT_LEFT_BUTTON:
buttonId = 0;
break;
case GLUT_RIGHT_BUTTON:
buttonId = 1;
break;
case GLUT_MIDDLE_BUTTON:
buttonId = 2;
break;
case GLUT_SCROLL_UP:
buttonId = 3;
break;
case GLUT_SCROLL_DOWN:
buttonId = 4;
break;
}
if (state == GLUT_DOWN)
Application::instance().mousePress(buttonId);
else if (state == GLUT_UP)
Application::instance().mouseRelease(buttonId);
}
// Resizing the window calls this function
static void resizeCallback(int width, int height)
{
ImGui_ImplGLUT_ReshapeFunc(width, height);
Application::instance().resize(width, height);
}
static void drawCallback()
{
// Start the Dear ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplGLUT_NewFrame();
// Render the application frame (including calls to Dear ImGui)
Application::instance().render();
// Render the Dear ImGui frame (with the calls to Dear ImGui that the applcation has made)
ImGui::Render();
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
glutSwapBuffers();
}
static void idleCallback()
{
int currentTime = glutGet(GLUT_ELAPSED_TIME);
int deltaTime = currentTime - prevTime;
//if (deltaTime >= TARGET_FRAME_TIME) {
// Every time we enter here is equivalent to a game loop execution
if (!Application::instance().update(deltaTime)) glutLeaveMainLoop();
prevTime = currentTime;
glutPostRedisplay();
//}
}
int main(int argc, char **argv)
{
// GLUT initialization
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowPosition(100, 100);
glutInitWindowSize(640, 480);
glutCreateWindow(argv[0]);
glutReshapeFunc(resizeCallback);
glutDisplayFunc(drawCallback);
glutIdleFunc(idleCallback);
glutKeyboardFunc(keyboardDownCallback);
glutKeyboardUpFunc(keyboardUpCallback);
glutSpecialFunc(specialDownCallback);
glutSpecialUpFunc(specialUpCallback);
glutMouseFunc(mouseCallback);
glutMotionFunc(motionCallback);
glutPassiveMotionFunc(passiveMotionCallback);
beginCapturingMouse();
IMGUI_CHECKVERSION();
ImGui::CreateContext();
// Setup Platform/Renderer backends
ImGui_ImplGLUT_Init();
ImGui_ImplOpenGL3_Init();
// GLEW will take care of OpenGL extension functions
glewExperimental = GL_TRUE;
glewInit();
// Application instance initialization
Application::instance().init();
prevTime = glutGet(GLUT_ELAPSED_TIME);
// GLUT gains control of the application
glutMainLoop();
// Dear ImGui cleanup
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGLUT_Shutdown();
ImGui::DestroyContext();
return 0;
}