8 static bool init(int argc, char **argv);
9 static void process_event(SDL_Event *ev);
10 static void proc_modkeys();
12 static SDL_Window *win;
13 static SDL_GLContext ctx;
14 static bool fullscreen, mouse_grabbed;
17 static unsigned int start_time;
18 static unsigned int modkeys;
20 static int scale_factor = 1;
22 int main(int argc, char **argv)
24 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) {
25 fprintf(stderr, "failed to initialize SDL\n");
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);
33 int defpos = SDL_WINDOWPOS_UNDEFINED;
34 unsigned int sdlflags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI;
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");
46 if(!(ctx = SDL_GL_CreateContext(win))) {
47 fprintf(stderr, "failed to create OpenGL context\n");
51 SDL_GL_GetDrawableSize(win, &win_width, &win_height);
52 win_aspect = (float)win_width / (float)win_height;
54 if(!init(argc, argv)) {
58 app_reshape(win_width, win_height);
63 time_msec = SDL_GetTicks() - start_time;
64 while(SDL_PollEvent(&ev)) {
66 if(quit) goto break_evloop;
78 void app_swap_buffers()
80 SDL_GL_SwapWindow(win);
88 unsigned int app_get_modifiers()
93 void app_resize(int x, int y)
95 SDL_SetWindowSize(win, x, y);
98 void app_fullscreen(bool fs)
100 SDL_SetWindowFullscreen(win, fs ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
104 void app_toggle_fullscreen()
106 app_fullscreen(!fullscreen);
109 bool app_is_fullscreen()
114 void app_grab_mouse(bool grab)
116 SDL_SetWindowGrab(win, grab ? SDL_TRUE : SDL_FALSE);
117 SDL_ShowCursor(grab ? 1 : 0);
118 SDL_SetRelativeMouseMode(grab ? SDL_TRUE : SDL_FALSE);
119 mouse_grabbed = grab;
122 void app_toggle_grab_mouse()
124 app_grab_mouse(!mouse_grabbed);
127 bool app_is_mouse_grabbed()
129 return mouse_grabbed;
133 static bool init(int argc, char **argv)
137 if(!app_init(argc, argv)) {
141 start_time = SDL_GetTicks();
145 static void process_event(SDL_Event *ev)
155 app_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED);
158 case SDL_MOUSEBUTTONDOWN:
159 case SDL_MOUSEBUTTONUP:
161 app_mouse_button(ev->button.button - SDL_BUTTON_LEFT, ev->button.state == SDL_PRESSED,
162 ev->button.x * scale_factor, ev->button.y * scale_factor);
165 case SDL_MOUSEMOTION:
167 app_mouse_delta(ev->motion.xrel, ev->motion.yrel);
169 app_mouse_motion(ev->motion.x * scale_factor, ev->motion.y * scale_factor);
173 case SDL_WINDOWEVENT:
174 if(ev->window.event == SDL_WINDOWEVENT_RESIZED) {
175 SDL_GL_GetDrawableSize(win, &win_width, &win_height);
176 win_aspect = (float)win_width / (float)win_height;
177 scale_factor = win_width / ev->window.data1;
178 app_reshape(win_width, win_height);
184 static void proc_modkeys()
187 SDL_Keymod sdlmod = SDL_GetModState();
188 if(sdlmod & KMOD_SHIFT) {
189 modkeys |= MOD_SHIFT;
191 if(sdlmod & KMOD_ALT) {
194 if(sdlmod & KMOD_CTRL) {