6 static void process_event(SDL_Event *ev);
8 static SDL_Window *win;
9 static SDL_GLContext ctx;
10 static bool redraw_pending = true;
13 static int scale_factor = 1;
15 static SDL_Window *create_window(int width, int height)
18 int x = SDL_WINDOWPOS_UNDEFINED;
19 int y = SDL_WINDOWPOS_UNDEFINED;
20 unsigned int flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI;
21 if(!(win = SDL_CreateWindow("vrfileman", x, y, width, height, flags))) {
27 int main(int argc, char **argv)
29 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) {
30 fprintf(stderr, "failed to initialize SDL\n");
34 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
35 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 8);
36 SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 1);
38 if(!(win = create_window(def_opt.width, def_opt.height))) {
39 // try again without the SRGB capability
40 SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 0);
41 if(!(win = create_window(def_opt.width, def_opt.height))) {
42 fprintf(stderr, "failed to create window\n");
45 fprintf(stderr, "failed to create sRGB-capable window, defaulting to non-linear color space\n");
49 if(!(ctx = SDL_GL_CreateContext(win))) {
50 fprintf(stderr, "failed to create OpenGL context\n");
53 SDL_GL_GetDrawableSize(win, &win_width, &win_height);
54 win_aspect = (float)win_width / (float)win_height;
56 if(!app_init(argc, argv)) {
60 app_reshape(win_width, win_height);
66 if(!SDL_WaitEvent(0)) {
67 fprintf(stderr, "error while waiting for events\n");
72 time_msec = app_get_msec();
73 time_sec = (double)time_msec / 1000.0;
74 while(SDL_PollEvent(&ev)) {
76 if(quit) goto break_evloop;
80 redraw_pending = false;
91 void app_resize(int x, int y)
93 SDL_SetWindowSize(win, x, y);
96 void app_fullscreen(bool fs)
98 SDL_SetWindowFullscreen(win, fs ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
108 redraw_pending = true;
111 void app_swap_buffers()
113 SDL_GL_SwapWindow(win);
118 return SDL_GetTicks();
121 static void process_event(SDL_Event *ev)
130 app_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED);
133 case SDL_MOUSEBUTTONDOWN:
134 case SDL_MOUSEBUTTONUP:
135 app_mouse_button(ev->button.button - SDL_BUTTON_LEFT, ev->button.state == SDL_PRESSED,
136 ev->button.x * scale_factor, ev->button.y * scale_factor);
139 case SDL_MOUSEMOTION:
140 app_mouse_motion(ev->motion.x * scale_factor, ev->motion.y * scale_factor);
143 case SDL_WINDOWEVENT:
144 if(ev->window.event == SDL_WINDOWEVENT_RESIZED) {
145 SDL_GL_GetDrawableSize(win, &win_width, &win_height);
146 win_aspect = (float)win_width / (float)win_height;
147 scale_factor = win_width / ev->window.data1;
148 app_reshape(win_width, win_height);