-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
169 lines (130 loc) · 3.72 KB
/
main.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
#include <ctime>
#include <iostream>
#include <GL/glew.h>
#ifdef _WIN32
#include <GL/glut.h>
#else
#include <GL/freeglut.h>
#endif
#include "Application.h"
//Remove console (only works in Visual Studio)
//#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")
#define TIME_PER_FRAME 1000.f / 60.f // Approx. 60 fps
#define WINDOW_WIDTH 1024
#define WINDOW_HEIGHT 768
static int prevTime;
static Application app; // This object represents our whole app
// If a key is pressed this callback is called
static void keyboardDownCallback(unsigned char key, int x, int y)
{
Application::instance().keyPressed(key);
}
// If a key is released this callback is called
static void keyboardUpCallback(unsigned char key, int x, int y)
{
Application::instance().keyReleased(key);
}
// If a special key is pressed this callback is called
static void specialDownCallback(int key, int x, int y)
{
Application::instance().specialKeyPressed(key);
}
// If a special key is released this callback is called
static void specialUpCallback(int key, int x, int y)
{
Application::instance().specialKeyReleased(key);
}
// Same for changes in mouse cursor position
static void motionCallback(int x, int y)
{
Application::instance().mouseMove(x, y);
}
// Same for mouse button presses or releases
static void mouseCallback(int button, int state, int x, int y)
{
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;
default:
buttonId = 3;
break;
}
if(state == GLUT_DOWN)
Application::instance().mousePress(buttonId);
else if(state == GLUT_UP)
Application::instance().mouseRelease(buttonId);
}
static void mousePassiveCallback(int x, int y) {
Application::instance().mouseMove(x, y);
glutWarpPointer(WINDOW_WIDTH / 2, WINDOW_HEIGHT / 2);
}
// Resizing the window calls this function
static void resizeCallback(int width, int height)
{
Application::instance().resize(width, height);
}
static void drawCallback() {
Application::instance().render();
glutSwapBuffers();
}
static void idleCallback()
{
int currentTime = glutGet(GLUT_ELAPSED_TIME);
int deltaTime = currentTime - prevTime;
if(deltaTime > TIME_PER_FRAME)
{
// Every time we enter here is equivalent to a game loop execution
if(!Application::instance().update(deltaTime))
exit(0);
int deltaTime = currentTime - prevTime;
Application::instance().DisplayFPS(deltaTime);
prevTime = currentTime;
glutPostRedisplay();
}
}
int main(int argc, char **argv)
{
// GLUT initialization
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
#ifdef __linux__
glutInitContextVersion(3, 3);
#endif
glutInitWindowPosition(30, 0);
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
glutCreateWindow(argv[0]);
glutReshapeFunc(resizeCallback);
glutDisplayFunc(drawCallback);
glutIdleFunc(idleCallback);
glutKeyboardFunc(keyboardDownCallback);
glutKeyboardUpFunc(keyboardUpCallback);
glutSpecialFunc(specialDownCallback);
glutSpecialUpFunc(specialUpCallback);
glutMouseFunc(mouseCallback);
glutMotionFunc(motionCallback);
glutPassiveMotionFunc(mousePassiveCallback);
glutSetCursor(GLUT_CURSOR_NONE);
// GLEW will take care of OpenGL extension functions
glewExperimental = GL_TRUE;
glewInit();
// Application instance initialization
Application::instance().init();
/*if (argc > 1)
Application::instance().loadMesh(argv[1]);
else
Application::instance().loadMesh("models_srgge/bunny.ply");
*/
prevTime = glutGet(GLUT_ELAPSED_TIME);
// GLUT gains control of the application
glutMainLoop();
return 0;
}