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 SDL_GameController *gamepad;
22 static int scale_factor = 1;
24 int main(int argc, char **argv)
26 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0) {
27 fprintf(stderr, "failed to initialize SDL\n");
31 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
32 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 8);
33 SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 1);
35 int defpos = SDL_WINDOWPOS_UNDEFINED;
36 unsigned int sdlflags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI;
38 if(!(win = SDL_CreateWindow("demo", defpos, defpos, 1024, 768, sdlflags))) {
39 // try again without sRGB capability
40 SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 0);
41 if(!(win = SDL_CreateWindow("demo", defpos, defpos, 1024, 768, sdlflags))) {
42 fprintf(stderr, "failed to create window\n");
46 fprintf(stderr, "failed to get an sRGB framebuffer.\n");
49 SDL_GL_GetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, &val);
50 printf("SDL says we %s an sRGB framebuffer\n", val ? "got" : "didn't get");
52 if(!(ctx = SDL_GL_CreateContext(win))) {
53 fprintf(stderr, "failed to create OpenGL context\n");
57 SDL_GL_GetDrawableSize(win, &win_width, &win_height);
58 win_aspect = (float)win_width / (float)win_height;
60 printf("detected %d joysticks\n", SDL_NumJoysticks());
61 for(int i=0; i<SDL_NumJoysticks(); i++) {
62 if(SDL_IsGameController(i)) {
63 if(!(gamepad = SDL_GameControllerOpen(i))) {
64 fprintf(stderr, "failed to open game controller %i: %s\n", i, SDL_GetError());
67 printf("Using gamepad: %s\n", SDL_GameControllerNameForIndex(i));
71 if(!init(argc, argv)) {
75 app_reshape(win_width, win_height);
80 time_msec = SDL_GetTicks() - start_time;
81 while(SDL_PollEvent(&ev)) {
83 if(quit) goto break_evloop;
95 void app_swap_buffers()
97 SDL_GL_SwapWindow(win);
105 unsigned int app_get_modifiers()
110 void app_resize(int x, int y)
112 SDL_SetWindowSize(win, x, y);
115 void app_fullscreen(bool fs)
117 SDL_SetWindowFullscreen(win, fs ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
121 void app_toggle_fullscreen()
123 app_fullscreen(!fullscreen);
126 bool app_is_fullscreen()
131 void app_grab_mouse(bool grab)
134 SDL_WarpMouseInWindow(win, win_width / 2, win_height / 2);
136 //SDL_SetWindowGrab(win, grab ? SDL_TRUE : SDL_FALSE);
137 //SDL_ShowCursor(grab ? 1 : 0);
138 SDL_SetRelativeMouseMode(grab ? SDL_TRUE : SDL_FALSE);
139 mouse_grabbed = grab;
142 void app_toggle_grab_mouse()
144 app_grab_mouse(!mouse_grabbed);
147 bool app_is_mouse_grabbed()
149 return mouse_grabbed;
153 static bool init(int argc, char **argv)
157 if(!app_init(argc, argv)) {
161 start_time = SDL_GetTicks();
165 static void process_event(SDL_Event *ev)
175 app_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED);
178 case SDL_MOUSEBUTTONDOWN:
179 case SDL_MOUSEBUTTONUP:
181 app_mouse_button(ev->button.button - SDL_BUTTON_LEFT, ev->button.state == SDL_PRESSED,
182 ev->button.x * scale_factor, ev->button.y * scale_factor);
185 case SDL_MOUSEMOTION:
187 // XXX xrel/yrel seems faster by default
188 app_mouse_delta(ev->motion.xrel * 0.75, ev->motion.yrel * 0.75);
190 app_mouse_motion(ev->motion.x * scale_factor, ev->motion.y * scale_factor);
194 case SDL_WINDOWEVENT:
195 if(ev->window.event == SDL_WINDOWEVENT_RESIZED) {
196 SDL_GL_GetDrawableSize(win, &win_width, &win_height);
197 win_aspect = (float)win_width / (float)win_height;
198 scale_factor = win_width / ev->window.data1;
199 app_reshape(win_width, win_height);
203 case SDL_CONTROLLERAXISMOTION:
204 app_gamepad_axis(ev->caxis.axis, ev->caxis.value / 65535.0f);
209 static void proc_modkeys()
212 SDL_Keymod sdlmod = SDL_GetModState();
213 if(sdlmod & KMOD_SHIFT) {
214 modkeys |= MOD_SHIFT;
216 if(sdlmod & KMOD_ALT) {
219 if(sdlmod & KMOD_CTRL) {