ok now it works nicely in VR
[vrtris] / src / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <SDL2/SDL.h>
5 #include "game.h"
6
7 static void process_event(SDL_Event *ev);
8 static void proc_modkeys();
9 static int translate_keysym(SDL_Keycode sym);
10
11 static SDL_Window *win;
12 static SDL_GLContext ctx;
13 static int fullscreen;
14 static int quit;
15
16 static unsigned int start_time;
17 static unsigned int modkeys;
18
19 SDL_GameController *gamepad;
20
21 static int scale_factor = 1;
22
23 #define DEFPOS  SDL_WINDOWPOS_UNDEFINED
24
25 int main(int argc, char **argv)
26 {
27         int i;
28         unsigned int sdlflags;
29
30         if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0) {
31                 fprintf(stderr, "failed to initialize SDL\n");
32                 return 1;
33         }
34
35         SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
36         SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 8);
37         SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 1);
38 #ifndef NDEBUG
39         SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG);
40 #endif
41
42         sdlflags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI;
43
44         if(!(win = SDL_CreateWindow("vrtris", DEFPOS, DEFPOS, 1024, 768, sdlflags))) {
45                 /* try again without sRGB capability */
46                 SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 0);
47                 if(!(win = SDL_CreateWindow("vrtris", DEFPOS, DEFPOS, 1024, 768, sdlflags))) {
48                         fprintf(stderr, "failed to create window\n");
49                         SDL_Quit();
50                         return 1;
51                 }
52                 fprintf(stderr, "failed to get an sRGB framebuffer\n");
53         }
54         SDL_GL_GetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, &fb_srgb);
55
56         if(!(ctx = SDL_GL_CreateContext(win))) {
57                 fprintf(stderr, "failed to create OpenGL context\n");
58                 SDL_Quit();
59                 return 1;
60         }
61         SDL_GL_GetDrawableSize(win, &win_width, &win_height);
62         win_aspect = (float)win_width / (float)win_height;
63
64         printf("detected %d joysticks\n", SDL_NumJoysticks());
65         for(i=0; i<SDL_NumJoysticks(); i++) {
66                 if(SDL_IsGameController(i)) {
67                         if(!(gamepad = SDL_GameControllerOpen(i))) {
68                                 fprintf(stderr, "failed to open game controller %i: %s\n", i, SDL_GetError());
69                                 continue;
70                         }
71                         printf("Using gamepad: %s\n", SDL_GameControllerNameForIndex(i));
72                 }
73         }
74
75         if(game_init(argc, argv) == -1) {
76                 SDL_Quit();
77                 return 1;
78         }
79
80         game_reshape(win_width, win_height);
81         SDL_RaiseWindow(win);
82
83         start_time = SDL_GetTicks();
84
85         while(!quit) {
86                 SDL_Event ev;
87
88                 time_msec = SDL_GetTicks() - start_time;
89                 while(SDL_PollEvent(&ev)) {
90                         process_event(&ev);
91                         if(quit) goto break_evloop;
92                 }
93
94                 game_display();
95         }
96 break_evloop:
97
98         game_cleanup();
99         SDL_Quit();
100         return 0;
101 }
102
103 void game_swap_buffers()
104 {
105         SDL_GL_SwapWindow(win);
106 }
107
108 void game_quit()
109 {
110         quit = 1;
111 }
112
113 unsigned int game_get_modifiers()
114 {
115         return modkeys;
116 }
117
118 void game_resize(int x, int y)
119 {
120         SDL_SetWindowSize(win, x, y);
121 }
122
123 void game_fullscreen(int fs)
124 {
125         SDL_SetWindowFullscreen(win, fs ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
126         fullscreen = fs;
127 }
128
129 void game_toggle_fullscreen()
130 {
131         game_fullscreen(!fullscreen);
132 }
133
134 int game_is_fullscreen()
135 {
136         return fullscreen;
137 }
138
139 static void process_event(SDL_Event *ev)
140 {
141         int key;
142
143         switch(ev->type) {
144         case SDL_QUIT:
145                 quit = 1;
146                 break;
147
148         case SDL_KEYDOWN:
149         case SDL_KEYUP:
150                 proc_modkeys();
151                 if((key = translate_keysym(ev->key.keysym.sym)) != -1) {
152                         game_keyboard(key, ev->key.state == SDL_PRESSED);
153                 }
154                 break;
155
156         case SDL_MOUSEBUTTONDOWN:
157         case SDL_MOUSEBUTTONUP:
158                 proc_modkeys();
159                 game_mouse_button(ev->button.button - SDL_BUTTON_LEFT, ev->button.state == SDL_PRESSED,
160                                 ev->button.x * scale_factor, ev->button.y * scale_factor);
161                 break;
162
163         case SDL_MOUSEMOTION:
164                 game_mouse_motion(ev->motion.x * scale_factor, ev->motion.y * scale_factor);
165                 break;
166
167         case SDL_MOUSEWHEEL:
168                 game_mouse_wheel(ev->wheel.y);
169                 break;
170
171         case SDL_WINDOWEVENT:
172                 if(ev->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
173                         SDL_GL_GetDrawableSize(win, &win_width, &win_height);
174                         win_aspect = (float)win_width / (float)win_height;
175                         scale_factor = win_width / ev->window.data1;
176                         game_reshape(win_width, win_height);
177                 }
178                 break;
179
180         case SDL_CONTROLLERAXISMOTION:
181                 game_gamepad_axis(ev->caxis.axis, ev->caxis.value / 32768.0f);
182                 break;
183
184         case SDL_CONTROLLERBUTTONDOWN:
185         case SDL_CONTROLLERBUTTONUP:
186                 game_gamepad_button(ev->cbutton.button, ev->type == SDL_CONTROLLERBUTTONDOWN);
187                 break;
188         }
189 }
190
191 static void proc_modkeys()
192 {
193         modkeys = 0;
194         SDL_Keymod sdlmod = SDL_GetModState();
195         if(sdlmod & KMOD_SHIFT) {
196                 modkeys |= MOD_SHIFT;
197         }
198         if(sdlmod & KMOD_ALT) {
199                 modkeys |= MOD_ALT;
200         }
201         if(sdlmod & KMOD_CTRL) {
202                 modkeys |= MOD_CTRL;
203         }
204 }
205
206 static int translate_keysym(SDL_Keycode sym)
207 {
208         switch(sym) {
209         case SDLK_RETURN:
210                 return '\n';
211         case SDLK_DELETE:
212                 return KEY_DEL;
213         case SDLK_LEFT:
214                 return KEY_LEFT;
215         case SDLK_RIGHT:
216                 return KEY_RIGHT;
217         case SDLK_UP:
218                 return KEY_UP;
219         case SDLK_DOWN:
220                 return KEY_DOWN;
221         case SDLK_PAGEUP:
222                 return KEY_PGUP;
223         case SDLK_PAGEDOWN:
224                 return KEY_PGDOWN;
225         case SDLK_HOME:
226                 return KEY_HOME;
227         case SDLK_END:
228                 return KEY_END;
229         default:
230                 break;
231         }
232
233         if(sym < 127) {
234                 return sym;
235         }
236         if(sym >= SDLK_F1 && sym <= SDLK_F12) {
237                 return KEY_F1 + sym - SDLK_F1;
238         }
239         return -1;
240 }