/
glut.cpp
218 lines (196 loc) · 5.65 KB
/
glut.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
#include <stdlib.h> // For EXIT_SUCCESS
#include <SDL.h>
#include <SDL_mixer.h>
#include "state_manager.h"
#ifdef __APPLE__
#define glutLeaveMainLoop() exit(EXIT_SUCCESS)
#include <GLUT/glut.h>
#else
#ifdef _WIN32
#include <windows.h>
#endif
#include <GL/glut.h>
#include <GL/freeglut_std.h>
#include <GL/freeglut_ext.h>
#endif
// Glut callbacks cannot be member functions. Need to pre-declare game object
StateManager *game;
bool ALLOW_BLUR = false;
double blur_factor = 0.15;
SDL_GameController *controller = NULL;
bool ENABLE_AUDIO = true;
int last_render_time;
void draw() {
int current_time = glutGet(GLUT_ELAPSED_TIME);
//cout << "fps: " << 1000.0 / (current_time - last_render_time) << endl;
last_render_time = current_time;
game->draw();
if(ALLOW_BLUR) {
glAccum(GL_MULT, blur_factor);
glAccum(GL_ACCUM, 1.0-blur_factor);
glAccum(GL_RETURN, 1.0);
}
glutSwapBuffers();
//glutPostRedisplay();
//glFlush();
}
int old_x = 50;
int old_y = 50;
int old_width = 320;
int old_height = 320;
void keyboard(unsigned char key, int x, int y) {
switch (key) {
case 'B':
blur_factor = (1+blur_factor) / 2.0;
cout << blur_factor << endl;
break;
case 'b':
blur_factor = blur_factor / 2.0;
cout << blur_factor << endl;
break;
case '\r':
if(glutGetModifiers() != GLUT_ACTIVE_ALT) {
break;
}
case 'f':
// http://www.xmission.com/~nate/sgi/sgi-macosx.zip
if (glutGet(GLUT_WINDOW_WIDTH) < glutGet(GLUT_SCREEN_WIDTH)) {
old_x = glutGet(GLUT_WINDOW_X);
old_y = glutGet(GLUT_WINDOW_Y);
old_width = glutGet(GLUT_WINDOW_WIDTH);
old_height = glutGet(GLUT_WINDOW_HEIGHT);
glutFullScreen();
} else {
glutPositionWindow(old_x, old_y);
glutReshapeWindow(old_width, old_height);
}
break;
}
game->keyboard(key, x, y);
}
void special(int key, int x, int y) {
switch (key) {
case GLUT_KEY_F4:
if(glutGetModifiers() == GLUT_ACTIVE_ALT) {
glutLeaveMainLoop();
}
break;
}
keyboard(key+128, x, y);
}
void keyboard_up(unsigned char key, int x, int y) {
if(!(key == '\r' && glutGetModifiers() == GLUT_ACTIVE_ALT))
game->keyboard_up(key, x, y);
}
void special_up(int key, int x, int y) {
keyboard_up(key+128, x, y);
}
void resize(int width, int height) {
Typer::resize(width, height);
game->resize(width, height);
}
void mouse_move(int x, int y) {
game->mouse_move(x, y);
glutWarpPointer(glutGet(GLUT_WINDOW_WIDTH)/2.0f, glutGet(GLUT_WINDOW_HEIGHT)/2.0f);
}
void check_controller() {
SDL_Event e;
while(SDL_PollEvent(&e)) {
game->controller(e);
}
}
int last_tick_time;
void tick() {
int current_time = glutGet(GLUT_ELAPSED_TIME);
//cout << "tps: " << 1000.0 / (current_time - last_tick_time) << endl;
game->tick(current_time - last_tick_time);
last_tick_time = current_time;
check_controller();
glutPostRedisplay();
}
void isVisible(int state) {
if(state == GLUT_VISIBLE) {
last_render_time = last_tick_time = glutGet(GLUT_ELAPSED_TIME);
glutVisibilityFunc(NULL);
glutIdleFunc(tick);
}
}
void init_controllers_and_audio() {
SDL_SetHint(SDL_HINT_JOYSTICK_ALLOW_BACKGROUND_EVENTS, "1");
SDL_SetHint(SDL_HINT_GAMECONTROLLERCONFIG, "1");
Uint32 SDL_INIT_FLAGS = SDL_INIT_GAMECONTROLLER;
// custom mappings SDL_HINT_GAMECONTROLLERCONFIG;
if(ENABLE_AUDIO) {
SDL_INIT_FLAGS |= SDL_INIT_AUDIO; // this does not seem to work :S
}
if(SDL_Init(SDL_INIT_FLAGS) == 0) {
if( ENABLE_AUDIO && Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 1024 ) < 0) {
std::cout << "Unable to open audio device" << std::endl;
std::cout << Mix_GetError() << std::endl;
}
//TODO: SDL_JOYDEVICEADDED or SDL_JOYDEVICEREMOVED
//SDL_GameControllerEventState(int state);
SDL_JoystickEventState(SDL_ENABLE);
if(SDL_NumJoysticks() == 0) {
std::cout << "No joysticks" << std::endl;
} else {
for (int i = 0; i < SDL_NumJoysticks(); ++i) {
if (SDL_IsGameController(i)) {
controller = SDL_GameControllerOpen(i);
if (controller) {
std::cout << "Controller: " << SDL_GameControllerName(controller) << std::endl;
break;
} else {
std::cout << "Could not open gamecontroller " << i << ":" << SDL_GetError() << std::endl;
}
} else {
std::cout << "Not controller" << std::endl;
}
}
}
} else {
std::cout << "SDL Failed to initialize" << std::endl;
std::cout << SDL_GetError() << std::endl;
}
}
void init(int &argc, char* argv[], float width, float height);
int main(int argc, char* argv[]) {
init(argc, argv, 800, 600);
init_controllers_and_audio();
game = new StateManager();
glutMainLoop();
if (SDL_GameControllerGetAttached(controller)) {
SDL_GameControllerClose(controller);
}
delete game;
return EXIT_SUCCESS;
}
void init(int &argc, char* argv[], float width, float height) {
glutInit(&argc, argv);
int DISPLAY_TYPE = GLUT_RGBA | GLUT_DOUBLE;
if(ALLOW_BLUR) {
DISPLAY_TYPE = DISPLAY_TYPE | GLUT_ACCUM;
}
glutInitDisplayMode(DISPLAY_TYPE);
glutInitWindowSize(width, height);
glutCreateWindow("Newtonia");
//glEnable(GL_DEPTH_TEST);
//glDepthFunc(GL_LESS);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
glEnable(GL_LINE_SMOOTH);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
glEnable(GL_POINT_SMOOTH);
if(ALLOW_BLUR) {
glEnable(GL_ACCUM);
glClear(GL_ACCUM_BUFFER_BIT);
}
glutDisplayFunc(draw);
glutKeyboardFunc(keyboard);
glutKeyboardUpFunc(keyboard_up);
glutSpecialFunc(special);
glutSpecialUpFunc(special_up);
glutReshapeFunc(resize);
glutVisibilityFunc(isVisible);
}