mouse speed
[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 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                 fprintf(stderr, "failed to get an sRGB framebuffer.\n");
45         }
46         int val;
47         SDL_GL_GetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, &val);
48         printf("SDL says we %s an sRGB framebuffer\n", val ? "got" : "didn't get");
49
50         if(!(ctx = SDL_GL_CreateContext(win))) {
51                 fprintf(stderr, "failed to create OpenGL context\n");
52                 SDL_Quit();
53                 return 1;
54         }
55         SDL_GL_GetDrawableSize(win, &win_width, &win_height);
56         win_aspect = (float)win_width / (float)win_height;
57
58         if(!init(argc, argv)) {
59                 SDL_Quit();
60                 return 1;
61         }
62         app_reshape(win_width, win_height);
63
64         while(!quit) {
65                 SDL_Event ev;
66
67                 time_msec = SDL_GetTicks() - start_time;
68                 while(SDL_PollEvent(&ev)) {
69                         process_event(&ev);
70                         if(quit) goto break_evloop;
71                 }
72
73                 app_display();
74         }
75 break_evloop:
76
77         app_cleanup();
78         SDL_Quit();
79         return 0;
80 }
81
82 void app_swap_buffers()
83 {
84         SDL_GL_SwapWindow(win);
85 }
86
87 void app_quit()
88 {
89         quit = true;
90 }
91
92 unsigned int app_get_modifiers()
93 {
94         return modkeys;
95 }
96
97 void app_resize(int x, int y)
98 {
99         SDL_SetWindowSize(win, x, y);
100 }
101
102 void app_fullscreen(bool fs)
103 {
104         SDL_SetWindowFullscreen(win, fs ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
105         fullscreen = fs;
106 }
107
108 void app_toggle_fullscreen()
109 {
110         app_fullscreen(!fullscreen);
111 }
112
113 bool app_is_fullscreen()
114 {
115         return fullscreen;
116 }
117
118 void app_grab_mouse(bool grab)
119 {
120         if(grab) {
121                 SDL_WarpMouseInWindow(win, win_width / 2, win_height / 2);
122         }
123         //SDL_SetWindowGrab(win, grab ? SDL_TRUE : SDL_FALSE);
124         //SDL_ShowCursor(grab ? 1 : 0);
125         SDL_SetRelativeMouseMode(grab ? SDL_TRUE : SDL_FALSE);
126         mouse_grabbed = grab;
127 }
128
129 void app_toggle_grab_mouse()
130 {
131         app_grab_mouse(!mouse_grabbed);
132 }
133
134 bool app_is_mouse_grabbed()
135 {
136         return mouse_grabbed;
137 }
138
139
140 static bool init(int argc, char **argv)
141 {
142         glewInit();
143
144         if(!app_init(argc, argv)) {
145                 return false;
146         }
147
148         start_time = SDL_GetTicks();
149         return true;
150 }
151
152 static void process_event(SDL_Event *ev)
153 {
154         switch(ev->type) {
155         case SDL_QUIT:
156                 quit = true;
157                 break;
158
159         case SDL_KEYDOWN:
160         case SDL_KEYUP:
161                 proc_modkeys();
162                 app_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED);
163                 break;
164
165         case SDL_MOUSEBUTTONDOWN:
166         case SDL_MOUSEBUTTONUP:
167                 proc_modkeys();
168                 app_mouse_button(ev->button.button - SDL_BUTTON_LEFT, ev->button.state == SDL_PRESSED,
169                                 ev->button.x * scale_factor, ev->button.y * scale_factor);
170                 break;
171
172         case SDL_MOUSEMOTION:
173                 if(mouse_grabbed) {
174                         // XXX xrel/yrel seems faster by default
175                         app_mouse_delta(ev->motion.xrel * 0.75, ev->motion.yrel * 0.75);
176                 } else {
177                         app_mouse_motion(ev->motion.x * scale_factor, ev->motion.y * scale_factor);
178                 }
179                 break;
180
181         case SDL_WINDOWEVENT:
182                 if(ev->window.event == SDL_WINDOWEVENT_RESIZED) {
183                         SDL_GL_GetDrawableSize(win, &win_width, &win_height);
184                         win_aspect = (float)win_width / (float)win_height;
185                         scale_factor = win_width / ev->window.data1;
186                         app_reshape(win_width, win_height);
187                 }
188                 break;
189         }
190 }
191
192 static void proc_modkeys()
193 {
194         modkeys = 0;
195         SDL_Keymod sdlmod = SDL_GetModState();
196         if(sdlmod & KMOD_SHIFT) {
197                 modkeys |= MOD_SHIFT;
198         }
199         if(sdlmod & KMOD_ALT) {
200                 modkeys |= MOD_ALT;
201         }
202         if(sdlmod & KMOD_CTRL) {
203                 modkeys |= MOD_CTRL;
204         }
205 }