5 #include <cgmath/cgmath.h>
14 static void calc_framerate(void);
15 static void print_framerate(void);
18 static int should_swap;
20 static unsigned long framerate;
23 int game_init(int argc, char **argv)
25 if(init_opengl() == -1) {
29 if(init_options(argc, argv, "vrtris.conf") == -1) {
33 if(init_screens() == -1) {
38 if(opt.flags & OPT_VR) {
39 if(goatvr_init() == -1) {
42 goatvr_set_origin_mode(GOATVR_HEAD);
43 goatvr_set_units_scale(10.0f);
46 should_swap = goatvr_should_swap();
50 glEnable(GL_DEPTH_TEST);
51 glEnable(GL_CULL_FACE);
52 glEnable(GL_LIGHTING);
61 if(opt.flags & OPT_VR) {
68 static void update(float dt)
73 void game_display(void)
75 static long prev_msec;
76 float dt = (float)(time_msec - prev_msec) / 1000.0f;
77 prev_msec = time_msec;
82 if(opt.flags & OPT_VR) {
85 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
91 cgm_mcopy(proj_matrix, goatvr_projection_matrix(i, 0.5, 500.0));
92 glMatrixMode(GL_PROJECTION);
93 glLoadMatrixf(proj_matrix);
95 cgm_mcopy(view_matrix, goatvr_view_matrix(i));
96 glMatrixMode(GL_MODELVIEW);
97 glLoadMatrixf(view_matrix);
111 #endif /* BUILD_VR */
114 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
116 cgm_mperspective(proj_matrix, cgm_deg_to_rad(40.0), win_aspect, 0.5, 500.0);
117 glMatrixMode(GL_PROJECTION);
118 glLoadMatrixf(proj_matrix);
120 cgm_midentity(view_matrix);
121 glMatrixMode(GL_MODELVIEW);
122 glLoadMatrixf(view_matrix);
131 assert(glGetError() == GL_NO_ERROR);
136 void game_reshape(int x, int y)
138 glViewport(0, 0, x, y);
140 goatvr_set_fb_size(x, y, 1.0f);
143 reshape_screens(x, y);
146 void game_keyboard(int key, int pressed)
148 unsigned int mod = game_get_modifiers();
159 game_toggle_fullscreen();
166 if(opt.flags & OPT_VR) {
177 screen->keyboard(key, pressed);
180 void game_mouse_button(int bn, int pressed, int x, int y)
182 screen->mouse(bn, pressed, x, y);
185 void game_mouse_motion(int x, int y)
187 screen->motion(x, y);
190 void game_mouse_wheel(int dir)
196 void game_gamepad_axis(int axis, float val)
198 joy_axis[axis] = val;
201 void game_gamepad_button(int bn, int pressed)
204 joy_bnstate |= (1 << bn);
206 joy_bnstate &= ~(1 << bn);
210 static void calc_framerate(void)
212 static unsigned long nframes;
213 static long prev_upd;
215 long elapsed = time_msec - prev_upd;
216 if(elapsed >= 1000) {
217 framerate = nframes * 10000 / elapsed;
219 prev_upd = time_msec;
225 static void print_framerate(void)
227 print_text(9 * win_width / 10, 20, 1, 1, 0, "fps: %d.%d", framerate / 10, framerate % 10);