fixed hidpi on mac
[vrfileman] / src / main.cc
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <SDL2/SDL.h>
4 #include "app.h"
5
6 static void process_event(SDL_Event *ev);
7
8 static SDL_Window *win;
9 static SDL_GLContext ctx;
10 static bool redraw_pending = true;
11 static bool quit;
12
13 static int scale_factor = 1;
14
15 static SDL_Window *create_window(int width, int height)
16 {
17         SDL_Window *win;
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))) {
22                 return 0;
23         }
24         return win;
25 }
26
27 int main(int argc, char **argv)
28 {
29         if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) {
30                 fprintf(stderr, "failed to initialize SDL\n");
31                 return 1;
32         }
33
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);
37
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");
43                         return 1;
44                 }
45                 fprintf(stderr, "failed to create sRGB-capable window, defaulting to non-linear color space\n");
46                 def_opt.srgb = false;
47         }
48
49         if(!(ctx = SDL_GL_CreateContext(win))) {
50                 fprintf(stderr, "failed to create OpenGL context\n");
51                 return 1;
52         }
53         SDL_GL_GetDrawableSize(win, &win_width, &win_height);
54         win_aspect = (float)win_width / (float)win_height;
55
56         if(!app_init(argc, argv)) {
57                 SDL_Quit();
58                 return 1;
59         }
60         app_reshape(win_width, win_height);
61
62         while(!quit) {
63                 SDL_Event ev;
64
65                 if(!redraw_pending) {
66                         if(!SDL_WaitEvent(0)) {
67                                 fprintf(stderr, "error while waiting for events\n");
68                                 break;
69                         }
70                 }
71
72                 time_msec = app_get_msec();
73                 time_sec = (double)time_msec / 1000.0;
74                 while(SDL_PollEvent(&ev)) {
75                         process_event(&ev);
76                         if(quit) goto break_evloop;
77                 }
78
79                 if(redraw_pending) {
80                         redraw_pending = false;
81                         app_draw();
82                 }
83         }
84 break_evloop:
85
86         app_cleanup();
87         SDL_Quit();
88         return 0;
89 }
90
91 void app_resize(int x, int y)
92 {
93         SDL_SetWindowSize(win, x, y);
94 }
95
96 void app_fullscreen(bool fs)
97 {
98         SDL_SetWindowFullscreen(win, fs ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
99 }
100
101 void app_quit()
102 {
103         quit = true;
104 }
105
106 void app_redraw()
107 {
108         redraw_pending = true;
109 }
110
111 void app_swap_buffers()
112 {
113         SDL_GL_SwapWindow(win);
114 }
115
116 long app_get_msec()
117 {
118         return SDL_GetTicks();
119 }
120
121 static void process_event(SDL_Event *ev)
122 {
123         switch(ev->type) {
124         case SDL_QUIT:
125                 quit = true;
126                 break;
127
128         case SDL_KEYDOWN:
129         case SDL_KEYUP:
130                 app_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED);
131                 break;
132
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);
137                 break;
138
139         case SDL_MOUSEMOTION:
140                 app_mouse_motion(ev->motion.x * scale_factor, ev->motion.y * scale_factor);
141                 break;
142
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);
149                 }
150                 break;
151         }
152 }