7 static void process_event(SDL_Event *ev);
8 static void proc_modkeys();
9 static int translate_keysym(SDL_Keycode sym);
11 static SDL_Window *win;
12 static SDL_GLContext ctx;
13 static int fullscreen;
16 static unsigned int start_time;
17 static unsigned int modkeys;
19 SDL_GameController *gamepad;
21 static int scale_factor = 1;
23 #define DEFPOS SDL_WINDOWPOS_UNDEFINED
25 int main(int argc, char **argv)
28 unsigned int sdlflags;
30 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) != 0) {
31 fprintf(stderr, "failed to initialize SDL\n");
35 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
36 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 8);
37 SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 1);
39 SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG);
42 sdlflags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI;
44 if(!(win = SDL_CreateWindow("vrtris", DEFPOS, DEFPOS, 1024, 768, sdlflags))) {
45 /* try again without sRGB capability */
46 SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 0);
47 if(!(win = SDL_CreateWindow("vrtris", DEFPOS, DEFPOS, 1024, 768, sdlflags))) {
48 fprintf(stderr, "failed to create window\n");
52 fprintf(stderr, "failed to get an sRGB framebuffer\n");
54 SDL_GL_GetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, &fb_srgb);
56 if(!(ctx = SDL_GL_CreateContext(win))) {
57 fprintf(stderr, "failed to create OpenGL context\n");
61 SDL_GL_GetDrawableSize(win, &win_width, &win_height);
62 win_aspect = (float)win_width / (float)win_height;
64 printf("detected %d joysticks\n", SDL_NumJoysticks());
65 for(i=0; i<SDL_NumJoysticks(); i++) {
66 if(SDL_IsGameController(i)) {
67 if(!(gamepad = SDL_GameControllerOpen(i))) {
68 fprintf(stderr, "failed to open game controller %i: %s\n", i, SDL_GetError());
71 printf("Using gamepad: %s\n", SDL_GameControllerNameForIndex(i));
75 if(game_init(argc, argv) == -1) {
80 game_reshape(win_width, win_height);
83 start_time = SDL_GetTicks();
88 time_msec = SDL_GetTicks() - start_time;
89 while(SDL_PollEvent(&ev)) {
91 if(quit) goto break_evloop;
103 void game_swap_buffers()
105 SDL_GL_SwapWindow(win);
113 unsigned int game_get_modifiers()
118 void game_resize(int x, int y)
120 SDL_SetWindowSize(win, x, y);
123 void game_fullscreen(int fs)
125 SDL_SetWindowFullscreen(win, fs ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
129 void game_toggle_fullscreen()
131 game_fullscreen(!fullscreen);
134 int game_is_fullscreen()
139 static void process_event(SDL_Event *ev)
151 if((key = translate_keysym(ev->key.keysym.sym)) != -1) {
152 game_keyboard(key, ev->key.state == SDL_PRESSED);
156 case SDL_MOUSEBUTTONDOWN:
157 case SDL_MOUSEBUTTONUP:
159 game_mouse_button(ev->button.button - SDL_BUTTON_LEFT, ev->button.state == SDL_PRESSED,
160 ev->button.x * scale_factor, ev->button.y * scale_factor);
163 case SDL_MOUSEMOTION:
164 game_mouse_motion(ev->motion.x * scale_factor, ev->motion.y * scale_factor);
168 game_mouse_wheel(ev->wheel.y);
171 case SDL_WINDOWEVENT:
172 if(ev->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
173 SDL_GL_GetDrawableSize(win, &win_width, &win_height);
174 win_aspect = (float)win_width / (float)win_height;
175 scale_factor = win_width / ev->window.data1;
176 game_reshape(win_width, win_height);
180 case SDL_CONTROLLERAXISMOTION:
181 game_gamepad_axis(ev->caxis.axis, ev->caxis.value / 32768.0f);
184 case SDL_CONTROLLERBUTTONDOWN:
185 case SDL_CONTROLLERBUTTONUP:
186 game_gamepad_button(ev->cbutton.button, ev->type == SDL_CONTROLLERBUTTONDOWN);
191 static void proc_modkeys()
194 SDL_Keymod sdlmod = SDL_GetModState();
195 if(sdlmod & KMOD_SHIFT) {
196 modkeys |= MOD_SHIFT;
198 if(sdlmod & KMOD_ALT) {
201 if(sdlmod & KMOD_CTRL) {
206 static int translate_keysym(SDL_Keycode sym)
236 if(sym >= SDLK_F1 && sym <= SDLK_F12) {
237 return KEY_F1 + sym - SDLK_F1;