fullscreen, mouse grab, stuff
[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();
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 static int scale_factor = 1;
21
22 int main(int argc, char **argv)
23 {
24         if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) {
25                 fprintf(stderr, "failed to initialize SDL\n");
26                 return 1;
27         }
28
29         SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
30         SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 8);
31         SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 1);
32
33         int defpos = SDL_WINDOWPOS_UNDEFINED;
34         unsigned int sdlflags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI;
35
36         if(!(win = SDL_CreateWindow("demo", defpos, defpos, 1024, 768, sdlflags))) {
37                 // try again without sRGB capability
38                 SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 0);
39                 if(!(win = SDL_CreateWindow("demo", defpos, defpos, 1024, 768, sdlflags))) {
40                         fprintf(stderr, "failed to create window\n");
41                         SDL_Quit();
42                         return 1;
43                 }
44         }
45
46         if(!(ctx = SDL_GL_CreateContext(win))) {
47                 fprintf(stderr, "failed to create OpenGL context\n");
48                 SDL_Quit();
49                 return 1;
50         }
51         SDL_GL_GetDrawableSize(win, &win_width, &win_height);
52         win_aspect = (float)win_width / (float)win_height;
53
54         if(!init()) {
55                 SDL_Quit();
56                 return 1;
57         }
58         app_reshape(win_width, win_height);
59
60         while(!quit) {
61                 SDL_Event ev;
62
63                 time_msec = SDL_GetTicks() - start_time;
64                 while(SDL_PollEvent(&ev)) {
65                         process_event(&ev);
66                         if(quit) goto break_evloop;
67                 }
68
69                 app_display();
70         }
71 break_evloop:
72
73         app_cleanup();
74         SDL_Quit();
75         return 0;
76 }
77
78 void app_swap_buffers()
79 {
80         SDL_GL_SwapWindow(win);
81 }
82
83 void app_quit()
84 {
85         quit = true;
86 }
87
88 unsigned int app_get_modifiers()
89 {
90         return modkeys;
91 }
92
93 void app_resize(int x, int y)
94 {
95         SDL_SetWindowSize(win, x, y);
96 }
97
98 void app_fullscreen(bool fs)
99 {
100         SDL_SetWindowFullscreen(win, fs ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
101         fullscreen = fs;
102 }
103
104 void app_toggle_fullscreen()
105 {
106         app_fullscreen(!fullscreen);
107 }
108
109 bool app_is_fullscreen()
110 {
111         return fullscreen;
112 }
113
114 void app_grab_mouse(bool grab)
115 {
116         /*SDL_SetWindowGrab(win, grab ? SDL_TRUE : SDL_FALSE);
117         SDL_ShowCursor(grab ? 1 : 0);
118         */
119         SDL_SetRelativeMouseMode(grab ? SDL_TRUE : SDL_FALSE);
120         mouse_grabbed = grab;
121 }
122
123 void app_toggle_grab_mouse()
124 {
125         app_grab_mouse(!mouse_grabbed);
126 }
127
128 bool app_is_mouse_grabbed()
129 {
130         return mouse_grabbed;
131 }
132
133
134 static bool init()
135 {
136         glewInit();
137
138         if(!app_init()) {
139                 return false;
140         }
141
142         start_time = SDL_GetTicks();
143         return true;
144 }
145
146 static void process_event(SDL_Event *ev)
147 {
148         switch(ev->type) {
149         case SDL_QUIT:
150                 quit = true;
151                 break;
152
153         case SDL_KEYDOWN:
154         case SDL_KEYUP:
155                 proc_modkeys();
156                 app_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED);
157                 break;
158
159         case SDL_MOUSEBUTTONDOWN:
160         case SDL_MOUSEBUTTONUP:
161                 proc_modkeys();
162                 app_mouse_button(ev->button.button - SDL_BUTTON_LEFT, ev->button.state == SDL_PRESSED,
163                                 ev->button.x * scale_factor, ev->button.y * scale_factor);
164                 break;
165
166         case SDL_MOUSEMOTION:
167                 if(mouse_grabbed) {
168                         app_mouse_delta(ev->motion.xrel, ev->motion.yrel);
169                 } else {
170                         app_mouse_motion(ev->motion.x * scale_factor, ev->motion.y * scale_factor);
171                 }
172                 break;
173
174         case SDL_WINDOWEVENT:
175                 if(ev->window.event == SDL_WINDOWEVENT_RESIZED) {
176                         SDL_GL_GetDrawableSize(win, &win_width, &win_height);
177                         win_aspect = (float)win_width / (float)win_height;
178                         scale_factor = win_width / ev->window.data1;
179                         app_reshape(win_width, win_height);
180                 }
181                 break;
182         }
183 }
184
185 static void proc_modkeys()
186 {
187         modkeys = 0;
188         SDL_Keymod sdlmod = SDL_GetModState();
189         if(sdlmod & KMOD_SHIFT) {
190                 modkeys |= MOD_SHIFT;
191         }
192         if(sdlmod & KMOD_ALT) {
193                 modkeys |= MOD_ALT;
194         }
195         if(sdlmod & KMOD_CTRL) {
196                 modkeys |= MOD_CTRL;
197         }
198 }