-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcontroller.cpp
More file actions
245 lines (197 loc) · 5.45 KB
/
Copy pathcontroller.cpp
File metadata and controls
245 lines (197 loc) · 5.45 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
#include "core.h"
#include "controller.h"
extern Controller* g_controller;
//set callback functions
static void error_callback(int error, const char* description)
{
fputs(description, stderr);
}
static void resize(GLFWwindow *window, int x,int y)
{
::g_controller->Resize(window, x, y);
}
static void keyboard(GLFWwindow * window, int key, int scancode,int action, int mods)
{
::g_controller->Keyboard(window, key, scancode, action, mods);
}
static void mousebutton(GLFWwindow *window,int button,int action,int mods)
{
::g_controller->MouseButton(window, button, action, mods);
}
static void mousemotion(GLFWwindow *window, double x, double y)
{
::g_controller->MouseMotion(window, x, y);
}
static void mousescroll(GLFWwindow *window, double x, double y)
{
::g_controller->MouseScroll(window, x, y);
}
void Controller::RegisterObject(Object* object)
{
object->RegisterParentWindow(_windowHandle);
object->Reset();
_objects[_objectNo++] = object;
_activeObj++;
}
Controller::Controller(int argc,char **argv, const char *windowName)
{
_windowName = windowName;
//for timer
_elapsedTime = 0;
_numOfFrame = 0;
_fps = 0;
_winX = 1280;
_winY = 960;
_activeObj = -1;
_objectNo = 0;
_maxObjectNo = 100;
_objects = new Object*[_maxObjectNo];
_isCtrlPressed = _isLeftKeyPressed = _isMiddleKeyPressed = _isRightKeyPressed = false;
_isLightOn = true;
_prevCursorX = _prevCursorY = 0;
// Initialize components
if (!glfwInit()) {
std::cerr << "glfwInit() failed!" << std::endl;
exit(-1);
}
// Create the window
_windowHandle = glfwCreateWindow(_winX, _winY, windowName, NULL, NULL);
if (!_windowHandle) {
std::cerr << "Create Window failed" << std::endl;
exit(-1);
}
glfwMakeContextCurrent(_windowHandle);
glfwSetWindowPos(_windowHandle, 0, 0);
// Background color
glClearColor( 0., 0., 0., 1. );
// Callbacks
glfwSetErrorCallback(error_callback);
glfwSetMouseButtonCallback(_windowHandle, mousebutton);
glfwSetScrollCallback(_windowHandle, mousescroll);
glfwSetCursorPosCallback(_windowHandle, mousemotion);
glfwSetKeyCallback(_windowHandle, keyboard);
glfwSetWindowSizeCallback(_windowHandle, resize);
if(glewInit() != GLEW_OK) {
std::cerr << "glewInit() failed!" << std::endl;
exit(-1);
}
//InitCamera();
}
void Controller::InitCamera()
{
_camera = new Camera(_windowHandle);
}
void Controller::BeginLoop()
{
while (!glfwWindowShouldClose(_windowHandle))
{
/* Render here */
::g_controller->Render();
/* Swap front and back buffers */
glfwSwapBuffers(_windowHandle);
/* Poll for and process events */
glfwPollEvents();
}
}
//////////////////////////////////////////////////////////////////////////////
Controller::~Controller() {
glFinish();
glfwDestroyWindow(_windowHandle);
glfwTerminate();
}
void Controller::Reset() {
#ifdef DEBUG_LEVEL
std::cout << __FILE__ << " " << __FUNCTION__ << std::endl;
#endif
_camera->Reset();
for(int i = 0 ; i < _objectNo ; i++)
_objects[i]->Reset();
}
void Controller::_ComputeFPS()
{
_numOfFrame++;
_elapsedTime += _timer.StopTimer();
_timer.StartTimer();
if(_elapsedTime > 1) {
_fps = _numOfFrame / _elapsedTime;
//std::cout << _fps << " "<< _numOfFrame << std::endl;
_elapsedTime = 0;
_numOfFrame = 0;
}
//clear stringstream buffer
_titleInfo.str("");
_titleInfo << _windowName;
_titleInfo << " FPS: ";
_titleInfo << std::setprecision(4) << _fps;
_titleInfo.width(2);
}
void Controller::Render() {
//compute fps and display
_ComputeFPS();
glfwSetWindowTitle(_windowHandle, _titleInfo.str().c_str() );
//Begin drawing scene
_camera->Reset();
for(int i = 0 ; i < _objectNo ; i++) {
_objects[i]->SimulateStep();
_objects[i]->Show();
}
//Finish drawing scene
//glFinish();
}
int Controller::GetActiveObject(int mx, int my)
{
//FIXME: do some judgement;
return _activeObj;
}
void Controller::Quit() {
glFinish();
glfwDestroyWindow(_windowHandle);
exit(0);
}
void Controller::Resize(GLFWwindow *window, int x, int y) {
_winX = x;
_winY = y;
for(int i = 0 ; i < _objectNo ; i++)
_objects[i]->Resize(window, x, y);
}
void Controller::Keyboard(GLFWwindow * window, int key, int scancode, int action, int mods)
{
if (action == GLFW_PRESS) {
switch(key) {
case GLFW_KEY_ESCAPE: // Escape
Quit();
break;
case GLFW_KEY_R: //reset
Reset();
break;
case GLFW_KEY_LEFT_CONTROL:
_isCtrlPressed = true;
break;
}
}
else if(action == GLFW_RELEASE)
switch (key) {
case GLFW_KEY_LEFT_CONTROL:
_isCtrlPressed = false;
break;
}
//route message to active object
_objects[_activeObj]->Keyboard(window, key, scancode, action, mods);
}
void Controller::MouseButton(GLFWwindow *window, int button,int action,int mods)
{
//get active object and then transfer the message to the object
if(action == GLFW_PRESS) {
glfwGetCursorPos(window, &_prevCursorX, &_prevCursorY);
}
_activeObj = GetActiveObject(_prevCursorX, _prevCursorY);
_objects[_activeObj]->MouseButton(window, button, action, mods);
}
void Controller::MouseMotion(GLFWwindow *window, double nx, double ny)
{
_objects[_activeObj]->MouseMotion(window, nx, ny);
}
void Controller::MouseScroll(GLFWwindow *window, double nx, double ny)
{
_objects[_activeObj]->MouseScroll(window, nx, ny);
}