6 static void process_event(SDL_Event *ev);
8 static SDL_Window *win;
9 static SDL_GLContext ctx;
10 static bool redraw_pending = true;
11 static bool fullscreen, mouse_grabbed;
14 static int scale_factor = 1;
16 static SDL_Window *create_window(int width, int height)
19 int x = SDL_WINDOWPOS_UNDEFINED;
20 int y = SDL_WINDOWPOS_UNDEFINED;
21 unsigned int flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI;
22 if(!(win = SDL_CreateWindow("vrfileman", x, y, width, height, flags))) {
28 int main(int argc, char **argv)
30 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) {
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 if(!(win = create_window(def_opt.width, def_opt.height))) {
40 // try again without the SRGB capability
41 SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 0);
42 if(!(win = create_window(def_opt.width, def_opt.height))) {
43 fprintf(stderr, "failed to create window\n");
46 fprintf(stderr, "failed to create sRGB-capable window, defaulting to non-linear color space\n");
50 if(!(ctx = SDL_GL_CreateContext(win))) {
51 fprintf(stderr, "failed to create OpenGL context\n");
54 SDL_GL_GetDrawableSize(win, &win_width, &win_height);
55 win_aspect = (float)win_width / (float)win_height;
57 if(!app_init(argc, argv)) {
61 app_reshape(win_width, win_height);
67 if(!SDL_WaitEvent(0)) {
68 fprintf(stderr, "error while waiting for events\n");
73 time_msec = app_get_msec();
74 time_sec = (double)time_msec / 1000.0;
75 while(SDL_PollEvent(&ev)) {
77 if(quit) goto break_evloop;
81 redraw_pending = false;
92 void app_resize(int x, int y)
94 SDL_SetWindowSize(win, x, y);
97 void app_fullscreen(bool fs)
99 SDL_SetWindowFullscreen(win, fs ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
103 void app_toggle_fullscreen()
105 app_fullscreen(!fullscreen);
108 bool app_is_fullscreen()
113 void app_grab_mouse(bool grab)
115 /*SDL_SetWindowGrab(win, grab ? SDL_TRUE : SDL_FALSE);
116 SDL_ShowCursor(grab ? 1 : 0);
118 SDL_SetRelativeMouseMode(grab ? SDL_TRUE : SDL_FALSE);
119 mouse_grabbed = grab;
122 void app_toggle_grab_mouse()
124 app_grab_mouse(!mouse_grabbed);
127 bool app_is_mouse_grabbed()
129 return mouse_grabbed;
139 redraw_pending = true;
142 void app_swap_buffers()
144 SDL_GL_SwapWindow(win);
149 return SDL_GetTicks();
152 static void process_event(SDL_Event *ev)
161 app_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED);
164 case SDL_MOUSEBUTTONDOWN:
165 case SDL_MOUSEBUTTONUP:
166 app_mouse_button(ev->button.button - SDL_BUTTON_LEFT, ev->button.state == SDL_PRESSED,
167 ev->button.x * scale_factor, ev->button.y * scale_factor);
170 case SDL_MOUSEMOTION:
172 app_mouse_delta(ev->motion.xrel, ev->motion.yrel);
174 app_mouse_motion(ev->motion.x * scale_factor, ev->motion.y * scale_factor);
178 case SDL_WINDOWEVENT:
179 if(ev->window.event == SDL_WINDOWEVENT_RESIZED) {
180 SDL_GL_GetDrawableSize(win, &win_width, &win_height);
181 win_aspect = (float)win_width / (float)win_height;
182 scale_factor = win_width / ev->window.data1;
183 app_reshape(win_width, win_height);