bloboland

view src/main.cc @ 2:1757973feaed

added stereoscopic rendering for no apparent reason
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 16 Dec 2012 00:37:35 +0200
parents cfe68befb7cc
children 9021a906c5d3
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;
67 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
69 game_render();
71 glutSwapBuffers();
72 assert(glGetError() == GL_NO_ERROR);
73 }
75 static void idle()
76 {
77 glutPostRedisplay();
78 }
80 static void reshape(int x, int y)
81 {
82 glViewport(0, 0, x, y);
84 glMatrixMode(GL_PROJECTION);
85 glLoadIdentity();
86 gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
88 win_xsz = x;
89 win_ysz = y;
91 centerx = x / 2;
92 centery = y / 2;
93 }
95 static void keydown(unsigned char key, int x, int y)
96 {
97 skeydown(key, x, y);
98 }
100 static void keyup(unsigned char key, int x, int y)
101 {
102 skeyup(key, x, y);
103 }
105 static void skeydown(int key, int x, int y)
106 {
107 switch(key) {
108 case 27:
109 exit(0);
111 case 'z':
112 case 'Z':
113 stereo_shift_pressed = true;
114 break;
115 }
117 if(key < GAME_MAX_KEYS) {
118 keystate[key] = true;
119 }
120 }
122 static void skeyup(int key, int x, int y)
123 {
124 switch(key) {
125 case 'z':
126 case 'Z':
127 stereo_shift_pressed = false;
128 break;
129 }
131 if(key < GAME_MAX_KEYS) {
132 keystate[key] = false;
133 }
134 }
137 static int prev_x, prev_y;
139 static void mouse(int bn, int state, int x, int y)
140 {
141 int idx = bn - GLUT_LEFT_BUTTON;
143 if(idx < GAME_MAX_BUTTONS) {
144 bnstate[idx] = state == GLUT_DOWN;
146 if(state == GLUT_DOWN) {
147 game_input_shoot(idx);
148 }
149 }
150 prev_x = x;
151 prev_y = y;
152 }
155 static void motion(int x, int y)
156 {
157 /*
158 int dx = x - centerx;
159 int dy = y - centery;
161 if(!dx && !dy) {
162 return;
163 }
164 game_input_rot(dx * 0.5, dy * 0.5);
166 glutWarpPointer(centerx, centery);
167 */
168 int dx = x - prev_x;
169 int dy = y - prev_y;
171 prev_x = x;
172 prev_y = y;
174 if(stereo_shift_pressed) {
175 if(dy != 0) {
176 stereo_focus_dist += dy * 0.01;
177 stereo_eye_sep = stereo_focus_dist / 30.0;
178 printf("foc: %f, sep: %f\n", stereo_focus_dist, stereo_eye_sep);
179 }
180 return;
181 }
183 if(bnstate[0]) {
184 game_input_rot((float)dx / win_xsz, (float)dy / win_ysz);
185 }
186 }
188 static void spacemove(int x, int y, int z)
189 {
190 game_input_move(x * 0.0025, y * 0.0025, -z * 0.0025);
191 }
193 static void spacerot(int x, int y, int z)
194 {
195 game_input_rot(-y * 0.0001, -x * 0.0001);
196 }
198 static void spacebut(int bn, int state)
199 {
200 game_input_shoot(bn);
201 }