00f81cc6e670d995e1688a514a9b97dd95a5038c
[vrtris] / src / game.c
1 #include <assert.h>
2 #include <goatvr.h>
3 #include <cgmath/cgmath.h>
4 #include "opengl.h"
5 #include "game.h"
6 #include "screen.h"
7 #include "osd.h"
8 #include "opt.h"
9
10 static void calc_framerate(void);
11 static void print_framerate(void);
12
13 static float view_matrix[16], proj_matrix[16];
14 static int should_swap;
15 static unsigned long framerate;
16
17 int game_init(int argc, char **argv)
18 {
19         if(init_opengl() == -1) {
20                 return -1;
21         }
22
23         if(init_options(argc, argv, "vrtris.conf") == -1) {
24                 return -1;
25         }
26
27         if(init_screens() == -1) {
28                 return -1;
29         }
30
31         if(opt.flags & OPT_VR) {
32                 if(goatvr_init() == -1) {
33                         return -1;
34                 }
35                 goatvr_set_origin_mode(GOATVR_HEAD);
36
37                 goatvr_startvr();
38                 should_swap = goatvr_should_swap();
39         }
40         return 0;
41 }
42
43 void game_cleanup()
44 {
45         if(opt.flags & OPT_VR) {
46                 goatvr_shutdown();
47         }
48         cleanup_screens();
49 }
50
51 static void update(float dt)
52 {
53         int num_vr_sticks;
54
55         if((num_vr_sticks = goatvr_num_sticks()) > 0) {
56                 float p[2];
57                 goatvr_stick_pos(0, p);
58                 /* TODO */
59         }
60
61         screen->update(dt);
62 }
63
64 void game_display(void)
65 {
66         static long prev_msec;
67         int i;
68         float dt = (float)(time_msec - prev_msec) / 1000.0f;
69         prev_msec = time_msec;
70
71         update(dt);
72
73         if(opt.flags & OPT_VR) {
74                 goatvr_draw_start();
75                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
76
77                 for(i=0; i<2; i++) {
78                         /* for each eye */
79                         goatvr_draw_eye(i);
80
81                         cgm_mcopy(proj_matrix, goatvr_projection_matrix(i, 0.5, 500.0));
82                         glMatrixMode(GL_PROJECTION);
83                         glLoadMatrixf(proj_matrix);
84
85                         cgm_mcopy(view_matrix, goatvr_view_matrix(i));
86                         glMatrixMode(GL_MODELVIEW);
87                         glLoadMatrixf(view_matrix);
88
89                         screen->draw();
90                         print_framerate();
91                         draw_osd();
92                 }
93
94                 goatvr_draw_done();
95
96                 if(should_swap) {
97                         game_swap_buffers();
98                 }
99
100         } else {
101                 /* non-VR mode */
102                 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
103
104                 cgm_mperspective(proj_matrix, cgm_deg_to_rad(50.0), win_aspect, 0.5, 500.0);
105                 glMatrixMode(GL_PROJECTION);
106                 glLoadMatrixf(proj_matrix);
107
108                 cgm_midentity(view_matrix);
109                 glMatrixMode(GL_MODELVIEW);
110                 glLoadMatrixf(view_matrix);
111
112                 screen->draw();
113                 print_framerate();
114                 draw_osd();
115
116                 game_swap_buffers();
117         }
118
119         assert(glGetError() == GL_NO_ERROR);
120
121         calc_framerate();
122 }
123
124 void game_reshape(int x, int y)
125 {
126         glViewport(0, 0, x, y);
127         goatvr_set_fb_size(x, y, 1.0f);
128
129         reshape_screens(x, y);
130 }
131
132 void game_keyboard(int key, int pressed)
133 {
134         unsigned int mod = game_get_modifiers();
135
136         if(pressed) {
137                 switch(key) {
138                 case 27:
139                         game_quit();
140                         return;
141
142                 case '\n':
143                 case '\r':
144                         if(mod & MOD_ALT) {
145                                 game_toggle_fullscreen();
146                                 return;
147                         }
148                         break;
149
150                 default:
151                         break;
152                 }
153         }
154
155         screen->keyboard(key, pressed);
156 }
157
158 void game_mouse_button(int bn, int pressed, int x, int y)
159 {
160         screen->mouse(bn, pressed, x, y);
161 }
162
163 void game_mouse_motion(int x, int y)
164 {
165         screen->motion(x, y);
166 }
167
168 void game_mouse_wheel(int dir)
169 {
170         screen->wheel(dir);
171 }
172
173
174 void game_gamepad_axis(int axis, float val)
175 {
176 }
177
178 void game_gamepad_button(int bn, int pressed)
179 {
180 }
181
182 static void calc_framerate(void)
183 {
184         static unsigned long nframes;
185         static long prev_upd;
186
187         long elapsed = time_msec - prev_upd;
188         if(elapsed >= 1000) {
189                 framerate = nframes * 10000 / elapsed;
190                 nframes = 1;
191                 prev_upd = time_msec;
192         } else {
193                 ++nframes;
194         }
195 }
196
197 static void print_framerate(void)
198 {
199         print_text(9 * win_width / 10, 20, 1, 1, 0, "fps: %d.%d", framerate / 10, framerate % 10);
200 }