bloboland
view src/main.cc @ 5:2f4406cc341e
meh
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Wed, 19 Dec 2012 02:37:20 +0200 |
parents | 1757973feaed |
children |
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include "opengl.h"
5 #include "game.h"
6 #include "opt.h"
8 static void disp();
9 static void idle();
10 static void reshape(int x, int y);
11 static void keydown(unsigned char key, int x, int y);
12 static void keyup(unsigned char key, int x, int y);
13 static void skeydown(int key, int x, int y);
14 static void skeyup(int key, int x, int y);
15 static void mouse(int bn, int state, int x, int y);
16 static void motion(int x, int y);
17 static void spacemove(int x, int y, int z);
18 static void spacerot(int x, int y, int z);
19 static void spacebut(int bn, int state);
21 static int centerx, centery;
22 static unsigned int prev_msec;
24 static bool stereo_shift_pressed;
26 int main(int argc, char **argv)
27 {
28 glutInit(&argc, argv);
30 if(!parse_opt(argc, argv)) {
31 return 1;
32 }
34 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | (opt.stereo ? GLUT_STEREO : 0));
35 glutInitWindowSize(opt.xsz, opt.ysz);
36 glutCreateWindow("bloboland");
38 glutDisplayFunc(disp);
39 glutIdleFunc(idle);
40 glutReshapeFunc(reshape);
41 glutKeyboardFunc(keydown);
42 glutKeyboardUpFunc(keyup);
43 glutSpecialFunc(skeydown);
44 glutSpecialUpFunc(skeyup);
45 glutMouseFunc(mouse);
46 glutMotionFunc(motion);
47 glutSpaceballMotionFunc(spacemove);
48 glutSpaceballRotateFunc(spacerot);
49 glutSpaceballButtonFunc(spacebut);
51 glewInit();
53 if(!game_init()) {
54 return 1;
55 }
57 glutMainLoop();
58 }
60 static void disp()
61 {
62 unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
63 game_iter((msec - prev_msec) / 1000.0);
64 prev_msec = msec;
66 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
68 game_render();
70 glutSwapBuffers();
71 assert(glGetError() == GL_NO_ERROR);
72 }
74 static void idle()
75 {
76 glutPostRedisplay();
77 }
79 static void reshape(int x, int y)
80 {
81 glViewport(0, 0, x, y);
83 glMatrixMode(GL_PROJECTION);
84 glLoadIdentity();
85 gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
87 win_xsz = x;
88 win_ysz = y;
90 centerx = x / 2;
91 centery = y / 2;
92 }
94 static void keydown(unsigned char key, int x, int y)
95 {
96 skeydown(key, x, y);
97 }
99 static void keyup(unsigned char key, int x, int y)
100 {
101 skeyup(key, x, y);
102 }
104 static void skeydown(int key, int x, int y)
105 {
106 switch(key) {
107 case 27:
108 exit(0);
110 case 'z':
111 case 'Z':
112 stereo_shift_pressed = true;
113 break;
114 }
116 if(key < GAME_MAX_KEYS) {
117 keystate[key] = true;
118 }
119 }
121 static void skeyup(int key, int x, int y)
122 {
123 switch(key) {
124 case 'z':
125 case 'Z':
126 stereo_shift_pressed = false;
127 break;
128 }
130 if(key < GAME_MAX_KEYS) {
131 keystate[key] = false;
132 }
133 }
136 static int prev_x, prev_y;
138 static void mouse(int bn, int state, int x, int y)
139 {
140 int idx = bn - GLUT_LEFT_BUTTON;
142 if(idx < GAME_MAX_BUTTONS) {
143 bnstate[idx] = state == GLUT_DOWN;
145 if(state == GLUT_DOWN) {
146 game_input_shoot(idx);
147 }
148 }
149 prev_x = x;
150 prev_y = y;
151 }
154 static void motion(int x, int y)
155 {
156 /*
157 int dx = x - centerx;
158 int dy = y - centery;
160 if(!dx && !dy) {
161 return;
162 }
163 game_input_rot(dx * 0.5, dy * 0.5);
165 glutWarpPointer(centerx, centery);
166 */
167 int dx = x - prev_x;
168 int dy = y - prev_y;
170 prev_x = x;
171 prev_y = y;
173 if(stereo_shift_pressed) {
174 if(dy != 0) {
175 stereo_focus_dist += dy * 0.01;
176 stereo_eye_sep = stereo_focus_dist / 30.0;
177 printf("foc: %f, sep: %f\n", stereo_focus_dist, stereo_eye_sep);
178 }
179 return;
180 }
182 if(bnstate[0]) {
183 game_input_rot((float)dx / win_xsz, (float)dy / win_ysz);
184 }
185 }
187 static void spacemove(int x, int y, int z)
188 {
189 game_input_move(x * 0.0025, y * 0.0025, -z * 0.0025);
190 }
192 static void spacerot(int x, int y, int z)
193 {
194 game_input_rot(-y * 0.0001, -x * 0.0001);
195 }
197 static void spacebut(int bn, int state)
198 {
199 game_input_shoot(bn);
200 }