d24428cc65e9362b8820a5f525a60ad5da0f0ed0
[laserbrain_demo] / src / main.cc
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <GL/glew.h>
5 #include <SDL2/SDL.h>
6 #include "app.h"
7
8 static bool init(int argc, char **argv);
9 static void process_event(SDL_Event *ev);
10 static void proc_modkeys();
11
12 static SDL_Window *win;
13 static SDL_GLContext ctx;
14 static bool fullscreen, mouse_grabbed;
15 static bool quit;
16
17 static unsigned int start_time;
18 static unsigned int modkeys;
19
20 SDL_GameController *gamepad;
21
22 static int scale_factor = 1;
23
24 int main(int argc, char **argv)
25 {
26         if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0) {
27                 fprintf(stderr, "failed to initialize SDL\n");
28                 return 1;
29         }
30
31         SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
32         SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 8);
33         SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 1);
34
35         int defpos = SDL_WINDOWPOS_UNDEFINED;
36         unsigned int sdlflags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI;
37
38         if(!(win = SDL_CreateWindow("demo", defpos, defpos, 1024, 768, sdlflags))) {
39                 // try again without sRGB capability
40                 SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 0);
41                 if(!(win = SDL_CreateWindow("demo", defpos, defpos, 1024, 768, sdlflags))) {
42                         fprintf(stderr, "failed to create window\n");
43                         SDL_Quit();
44                         return 1;
45                 }
46                 fprintf(stderr, "failed to get an sRGB framebuffer.\n");
47         }
48         int val;
49         SDL_GL_GetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, &val);
50         printf("SDL says we %s an sRGB framebuffer\n", val ? "got" : "didn't get");
51         fb_srgb = val;
52
53         if(!(ctx = SDL_GL_CreateContext(win))) {
54                 fprintf(stderr, "failed to create OpenGL context\n");
55                 SDL_Quit();
56                 return 1;
57         }
58         SDL_GL_GetDrawableSize(win, &win_width, &win_height);
59         win_aspect = (float)win_width / (float)win_height;
60
61         printf("detected %d joysticks\n", SDL_NumJoysticks());
62         for(int i=0; i<SDL_NumJoysticks(); i++) {
63                 if(SDL_IsGameController(i)) {
64                         if(!(gamepad = SDL_GameControllerOpen(i))) {
65                                 fprintf(stderr, "failed to open game controller %i: %s\n", i, SDL_GetError());
66                                 continue;
67                         }
68                         printf("Using gamepad: %s\n", SDL_GameControllerNameForIndex(i));
69                 }
70         }
71
72         if(!init(argc, argv)) {
73                 SDL_Quit();
74                 return 1;
75         }
76         app_reshape(win_width, win_height);
77
78         while(!quit) {
79                 SDL_Event ev;
80
81                 time_msec = SDL_GetTicks() - start_time;
82                 while(SDL_PollEvent(&ev)) {
83                         process_event(&ev);
84                         if(quit) goto break_evloop;
85                 }
86
87                 app_display();
88         }
89 break_evloop:
90
91         app_cleanup();
92         SDL_Quit();
93         return 0;
94 }
95
96 void app_swap_buffers()
97 {
98         SDL_GL_SwapWindow(win);
99 }
100
101 void app_quit()
102 {
103         quit = true;
104 }
105
106 unsigned int app_get_modifiers()
107 {
108         return modkeys;
109 }
110
111 void app_resize(int x, int y)
112 {
113         SDL_SetWindowSize(win, x, y);
114 }
115
116 void app_fullscreen(bool fs)
117 {
118         SDL_SetWindowFullscreen(win, fs ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
119         fullscreen = fs;
120 }
121
122 void app_toggle_fullscreen()
123 {
124         app_fullscreen(!fullscreen);
125 }
126
127 bool app_is_fullscreen()
128 {
129         return fullscreen;
130 }
131
132 void app_grab_mouse(bool grab)
133 {
134         if(grab) {
135                 SDL_WarpMouseInWindow(win, win_width / 2, win_height / 2);
136         }
137         //SDL_SetWindowGrab(win, grab ? SDL_TRUE : SDL_FALSE);
138         //SDL_ShowCursor(grab ? 1 : 0);
139         SDL_SetRelativeMouseMode(grab ? SDL_TRUE : SDL_FALSE);
140         mouse_grabbed = grab;
141 }
142
143 void app_toggle_grab_mouse()
144 {
145         app_grab_mouse(!mouse_grabbed);
146 }
147
148 bool app_is_mouse_grabbed()
149 {
150         return mouse_grabbed;
151 }
152
153
154 static bool init(int argc, char **argv)
155 {
156         glewInit();
157
158         if(!app_init(argc, argv)) {
159                 return false;
160         }
161
162         start_time = SDL_GetTicks();
163         return true;
164 }
165
166 static void process_event(SDL_Event *ev)
167 {
168         switch(ev->type) {
169         case SDL_QUIT:
170                 quit = true;
171                 break;
172
173         case SDL_KEYDOWN:
174         case SDL_KEYUP:
175                 proc_modkeys();
176                 app_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED);
177                 break;
178
179         case SDL_MOUSEBUTTONDOWN:
180         case SDL_MOUSEBUTTONUP:
181                 proc_modkeys();
182                 app_mouse_button(ev->button.button - SDL_BUTTON_LEFT, ev->button.state == SDL_PRESSED,
183                                 ev->button.x * scale_factor, ev->button.y * scale_factor);
184                 break;
185
186         case SDL_MOUSEMOTION:
187                 if(mouse_grabbed) {
188                         app_mouse_delta(ev->motion.xrel, ev->motion.yrel);
189                 } else {
190                         app_mouse_motion(ev->motion.x * scale_factor, ev->motion.y * scale_factor);
191                 }
192                 break;
193
194         case SDL_WINDOWEVENT:
195                 if(ev->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
196                         SDL_GL_GetDrawableSize(win, &win_width, &win_height);
197                         win_aspect = (float)win_width / (float)win_height;
198                         scale_factor = win_width / ev->window.data1;
199                         app_reshape(win_width, win_height);
200                 }
201                 break;
202
203         case SDL_CONTROLLERAXISMOTION:
204                 app_gamepad_axis(ev->caxis.axis, ev->caxis.value / 32768.0f);
205                 break;
206
207         case SDL_CONTROLLERBUTTONDOWN:
208         case SDL_CONTROLLERBUTTONUP:
209                 app_gamepad_button(ev->cbutton.button, ev->type == SDL_CONTROLLERBUTTONDOWN);
210                 break;
211         }
212 }
213
214 static void proc_modkeys()
215 {
216         modkeys = 0;
217         SDL_Keymod sdlmod = SDL_GetModState();
218         if(sdlmod & KMOD_SHIFT) {
219                 modkeys |= MOD_SHIFT;
220         }
221         if(sdlmod & KMOD_ALT) {
222                 modkeys |= MOD_ALT;
223         }
224         if(sdlmod & KMOD_CTRL) {
225                 modkeys |= MOD_CTRL;
226         }
227 }