8 static bool init(int argc, char **argv);
9 static void process_event(SDL_Event *ev);
10 static void proc_modkeys();
11 static int translate_keysym(SDL_Keycode sym);
13 static SDL_Window *win;
14 static SDL_GLContext ctx;
15 static bool fullscreen, mouse_grabbed;
18 static unsigned int start_time;
19 static unsigned int modkeys;
21 SDL_GameController *gamepad;
23 static int scale_factor = 1;
25 int main(int argc, char **argv)
27 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0) {
28 fprintf(stderr, "failed to initialize SDL\n");
32 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
33 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 8);
34 SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 1);
36 SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG);
39 int defpos = SDL_WINDOWPOS_UNDEFINED;
40 unsigned int sdlflags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI;
42 if(!(win = SDL_CreateWindow("demo", defpos, defpos, 1024, 768, sdlflags))) {
43 // try again without sRGB capability
44 SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 0);
45 if(!(win = SDL_CreateWindow("demo", defpos, defpos, 1024, 768, sdlflags))) {
46 fprintf(stderr, "failed to create window\n");
50 fprintf(stderr, "failed to get an sRGB framebuffer.\n");
53 SDL_GL_GetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, &val);
54 printf("SDL says we %s an sRGB framebuffer\n", val ? "got" : "didn't get");
57 if(!(ctx = SDL_GL_CreateContext(win))) {
58 fprintf(stderr, "failed to create OpenGL context\n");
62 SDL_GL_GetDrawableSize(win, &win_width, &win_height);
63 win_aspect = (float)win_width / (float)win_height;
65 printf("detected %d joysticks\n", SDL_NumJoysticks());
66 for(int i=0; i<SDL_NumJoysticks(); i++) {
67 if(SDL_IsGameController(i)) {
68 if(!(gamepad = SDL_GameControllerOpen(i))) {
69 fprintf(stderr, "failed to open game controller %i: %s\n", i, SDL_GetError());
72 printf("Using gamepad: %s\n", SDL_GameControllerNameForIndex(i));
76 if(!init(argc, argv)) {
80 app_reshape(win_width, win_height);
87 time_msec = SDL_GetTicks() - start_time;
88 while(SDL_PollEvent(&ev)) {
90 if(quit) goto break_evloop;
102 void app_swap_buffers()
104 SDL_GL_SwapWindow(win);
112 unsigned int app_get_modifiers()
117 void app_resize(int x, int y)
119 SDL_SetWindowSize(win, x, y);
122 void app_fullscreen(bool fs)
124 SDL_SetWindowFullscreen(win, fs ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
128 void app_toggle_fullscreen()
130 app_fullscreen(!fullscreen);
133 bool app_is_fullscreen()
138 void app_grab_mouse(bool grab)
141 SDL_WarpMouseInWindow(win, win_width / 2, win_height / 2);
143 //SDL_SetWindowGrab(win, grab ? SDL_TRUE : SDL_FALSE);
144 //SDL_ShowCursor(grab ? 1 : 0);
145 SDL_SetRelativeMouseMode(grab ? SDL_TRUE : SDL_FALSE);
146 mouse_grabbed = grab;
149 void app_toggle_grab_mouse()
151 app_grab_mouse(!mouse_grabbed);
154 bool app_is_mouse_grabbed()
156 return mouse_grabbed;
160 static bool init(int argc, char **argv)
162 if(!app_init(argc, argv)) {
166 start_time = SDL_GetTicks();
170 static void process_event(SDL_Event *ev)
182 if((key = translate_keysym(ev->key.keysym.sym)) != -1) {
183 app_keyboard(key, ev->key.state == SDL_PRESSED);
187 case SDL_MOUSEBUTTONDOWN:
188 case SDL_MOUSEBUTTONUP:
190 app_mouse_button(ev->button.button - SDL_BUTTON_LEFT, ev->button.state == SDL_PRESSED,
191 ev->button.x * scale_factor, ev->button.y * scale_factor);
194 case SDL_MOUSEMOTION:
196 app_mouse_delta(ev->motion.xrel, ev->motion.yrel);
198 app_mouse_motion(ev->motion.x * scale_factor, ev->motion.y * scale_factor);
203 app_mouse_wheel(ev->wheel.y);
206 case SDL_WINDOWEVENT:
207 if(ev->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
208 SDL_GL_GetDrawableSize(win, &win_width, &win_height);
209 win_aspect = (float)win_width / (float)win_height;
210 scale_factor = win_width / ev->window.data1;
211 app_reshape(win_width, win_height);
215 case SDL_CONTROLLERAXISMOTION:
216 app_gamepad_axis(ev->caxis.axis, ev->caxis.value / 32768.0f);
219 case SDL_CONTROLLERBUTTONDOWN:
220 case SDL_CONTROLLERBUTTONUP:
221 app_gamepad_button(ev->cbutton.button, ev->type == SDL_CONTROLLERBUTTONDOWN);
226 static void proc_modkeys()
229 SDL_Keymod sdlmod = SDL_GetModState();
230 if(sdlmod & KMOD_SHIFT) {
231 modkeys |= MOD_SHIFT;
233 if(sdlmod & KMOD_ALT) {
236 if(sdlmod & KMOD_CTRL) {
241 static int translate_keysym(SDL_Keycode sym)
271 if(sym >= SDLK_F1 && sym <= SDLK_F12) {
272 return KEY_F1 + sym - SDLK_F1;