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);
85 time_msec = SDL_GetTicks() - start_time;
86 while(SDL_PollEvent(&ev)) {
88 if(quit) goto break_evloop;
100 void app_swap_buffers()
102 SDL_GL_SwapWindow(win);
110 unsigned int app_get_modifiers()
115 void app_resize(int x, int y)
117 SDL_SetWindowSize(win, x, y);
120 void app_fullscreen(bool fs)
122 SDL_SetWindowFullscreen(win, fs ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
126 void app_toggle_fullscreen()
128 app_fullscreen(!fullscreen);
131 bool app_is_fullscreen()
136 void app_grab_mouse(bool grab)
139 SDL_WarpMouseInWindow(win, win_width / 2, win_height / 2);
141 //SDL_SetWindowGrab(win, grab ? SDL_TRUE : SDL_FALSE);
142 //SDL_ShowCursor(grab ? 1 : 0);
143 SDL_SetRelativeMouseMode(grab ? SDL_TRUE : SDL_FALSE);
144 mouse_grabbed = grab;
147 void app_toggle_grab_mouse()
149 app_grab_mouse(!mouse_grabbed);
152 bool app_is_mouse_grabbed()
154 return mouse_grabbed;
158 static bool init(int argc, char **argv)
160 if(!app_init(argc, argv)) {
164 start_time = SDL_GetTicks();
168 static void process_event(SDL_Event *ev)
180 if((key = translate_keysym(ev->key.keysym.sym)) != -1) {
181 app_keyboard(key, ev->key.state == SDL_PRESSED);
185 case SDL_MOUSEBUTTONDOWN:
186 case SDL_MOUSEBUTTONUP:
188 app_mouse_button(ev->button.button - SDL_BUTTON_LEFT, ev->button.state == SDL_PRESSED,
189 ev->button.x * scale_factor, ev->button.y * scale_factor);
192 case SDL_MOUSEMOTION:
194 app_mouse_delta(ev->motion.xrel, ev->motion.yrel);
196 app_mouse_motion(ev->motion.x * scale_factor, ev->motion.y * scale_factor);
201 app_mouse_wheel(ev->wheel.y);
204 case SDL_WINDOWEVENT:
205 if(ev->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
206 SDL_GL_GetDrawableSize(win, &win_width, &win_height);
207 win_aspect = (float)win_width / (float)win_height;
208 scale_factor = win_width / ev->window.data1;
209 app_reshape(win_width, win_height);
213 case SDL_CONTROLLERAXISMOTION:
214 app_gamepad_axis(ev->caxis.axis, ev->caxis.value / 32768.0f);
217 case SDL_CONTROLLERBUTTONDOWN:
218 case SDL_CONTROLLERBUTTONUP:
219 app_gamepad_button(ev->cbutton.button, ev->type == SDL_CONTROLLERBUTTONDOWN);
224 static void proc_modkeys()
227 SDL_Keymod sdlmod = SDL_GetModState();
228 if(sdlmod & KMOD_SHIFT) {
229 modkeys |= MOD_SHIFT;
231 if(sdlmod & KMOD_ALT) {
234 if(sdlmod & KMOD_CTRL) {
239 static int translate_keysym(SDL_Keycode sym)
269 if(sym >= SDLK_F1 && sym <= SDLK_F12) {
270 return KEY_F1 + sym - SDLK_F1;