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 int defpos = SDL_WINDOWPOS_UNDEFINED;
37 unsigned int sdlflags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI;
39 if(!(win = SDL_CreateWindow("demo", defpos, defpos, 1024, 768, sdlflags))) {
40 // try again without sRGB capability
41 SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 0);
42 if(!(win = SDL_CreateWindow("demo", defpos, defpos, 1024, 768, sdlflags))) {
43 fprintf(stderr, "failed to create window\n");
47 fprintf(stderr, "failed to get an sRGB framebuffer.\n");
50 SDL_GL_GetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, &val);
51 printf("SDL says we %s an sRGB framebuffer\n", val ? "got" : "didn't get");
54 if(!(ctx = SDL_GL_CreateContext(win))) {
55 fprintf(stderr, "failed to create OpenGL context\n");
59 SDL_GL_GetDrawableSize(win, &win_width, &win_height);
60 win_aspect = (float)win_width / (float)win_height;
62 printf("detected %d joysticks\n", SDL_NumJoysticks());
63 for(int i=0; i<SDL_NumJoysticks(); i++) {
64 if(SDL_IsGameController(i)) {
65 if(!(gamepad = SDL_GameControllerOpen(i))) {
66 fprintf(stderr, "failed to open game controller %i: %s\n", i, SDL_GetError());
69 printf("Using gamepad: %s\n", SDL_GameControllerNameForIndex(i));
73 if(!init(argc, argv)) {
77 app_reshape(win_width, win_height);
82 time_msec = SDL_GetTicks() - start_time;
83 while(SDL_PollEvent(&ev)) {
85 if(quit) goto break_evloop;
97 void app_swap_buffers()
99 SDL_GL_SwapWindow(win);
107 unsigned int app_get_modifiers()
112 void app_resize(int x, int y)
114 SDL_SetWindowSize(win, x, y);
117 void app_fullscreen(bool fs)
119 SDL_SetWindowFullscreen(win, fs ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
123 void app_toggle_fullscreen()
125 app_fullscreen(!fullscreen);
128 bool app_is_fullscreen()
133 void app_grab_mouse(bool grab)
136 SDL_WarpMouseInWindow(win, win_width / 2, win_height / 2);
138 //SDL_SetWindowGrab(win, grab ? SDL_TRUE : SDL_FALSE);
139 //SDL_ShowCursor(grab ? 1 : 0);
140 SDL_SetRelativeMouseMode(grab ? SDL_TRUE : SDL_FALSE);
141 mouse_grabbed = grab;
144 void app_toggle_grab_mouse()
146 app_grab_mouse(!mouse_grabbed);
149 bool app_is_mouse_grabbed()
151 return mouse_grabbed;
155 static bool init(int argc, char **argv)
159 if(!app_init(argc, argv)) {
163 start_time = SDL_GetTicks();
167 static void process_event(SDL_Event *ev)
179 if((key = translate_keysym(ev->key.keysym.sym)) != -1) {
180 app_keyboard(key, ev->key.state == SDL_PRESSED);
184 case SDL_MOUSEBUTTONDOWN:
185 case SDL_MOUSEBUTTONUP:
187 app_mouse_button(ev->button.button - SDL_BUTTON_LEFT, ev->button.state == SDL_PRESSED,
188 ev->button.x * scale_factor, ev->button.y * scale_factor);
191 case SDL_MOUSEMOTION:
193 app_mouse_delta(ev->motion.xrel, ev->motion.yrel);
195 app_mouse_motion(ev->motion.x * scale_factor, ev->motion.y * scale_factor);
200 app_mouse_wheel(ev->wheel.y);
203 case SDL_WINDOWEVENT:
204 if(ev->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
205 SDL_GL_GetDrawableSize(win, &win_width, &win_height);
206 win_aspect = (float)win_width / (float)win_height;
207 scale_factor = win_width / ev->window.data1;
208 app_reshape(win_width, win_height);
212 case SDL_CONTROLLERAXISMOTION:
213 app_gamepad_axis(ev->caxis.axis, ev->caxis.value / 32768.0f);
216 case SDL_CONTROLLERBUTTONDOWN:
217 case SDL_CONTROLLERBUTTONUP:
218 app_gamepad_button(ev->cbutton.button, ev->type == SDL_CONTROLLERBUTTONDOWN);
223 static void proc_modkeys()
226 SDL_Keymod sdlmod = SDL_GetModState();
227 if(sdlmod & KMOD_SHIFT) {
228 modkeys |= MOD_SHIFT;
230 if(sdlmod & KMOD_ALT) {
233 if(sdlmod & KMOD_CTRL) {
238 static int translate_keysym(SDL_Keycode sym)
268 if(sym >= SDLK_F1 && sym <= SDLK_F12) {
269 return KEY_F1 + sym - SDLK_F1;