pulsing grid
[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 int main(int argc, char **argv)
14 {
15         if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) {
16                 fprintf(stderr, "failed to initialize SDL\n");
17                 return 1;
18         }
19
20         SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
21         SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);
22         SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 1);
23
24         int x = SDL_WINDOWPOS_UNDEFINED;
25         int y = SDL_WINDOWPOS_UNDEFINED;
26         unsigned int flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI;
27         win_width = 1280;
28         win_height = 800;
29         if(!(win = SDL_CreateWindow("vrfileman", x, y, win_width, win_height, flags))) {
30                 fprintf(stderr, "failed to create window\n");
31                 return 1;
32         }
33
34         if(!(ctx = SDL_GL_CreateContext(win))) {
35                 fprintf(stderr, "failed to create OpenGL context\n");
36                 return 1;
37         }
38         SDL_GL_GetDrawableSize(win, &win_width, &win_height);
39         win_aspect = (float)win_width / (float)win_height;
40
41         if(!app_init(argc, argv)) {
42                 SDL_Quit();
43                 return 1;
44         }
45         app_reshape(win_width, win_height);
46
47         while(!quit) {
48                 SDL_Event ev;
49
50                 if(!redraw_pending) {
51                         if(!SDL_WaitEvent(0)) {
52                                 fprintf(stderr, "error while waiting for events\n");
53                                 break;
54                         }
55                 }
56
57                 time_msec = app_get_msec();
58                 time_sec = (double)time_msec / 1000.0;
59                 while(SDL_PollEvent(&ev)) {
60                         process_event(&ev);
61                         if(quit) goto break_evloop;
62                 }
63
64                 if(redraw_pending) {
65                         redraw_pending = false;
66                         app_draw();
67                 }
68         }
69 break_evloop:
70
71         app_cleanup();
72         SDL_Quit();
73         return 0;
74 }
75
76 void app_resize(int x, int y)
77 {
78         SDL_SetWindowSize(win, x, y);
79 }
80
81 void app_fullscreen(int fs)
82 {
83         SDL_SetWindowFullscreen(win, fs ? SDL_WINDOW_FULLSCREEN_DESKTOP : 0);
84 }
85
86 void app_quit()
87 {
88         quit = true;
89 }
90
91 void app_redraw()
92 {
93         redraw_pending = true;
94 }
95
96 void app_swap_buffers()
97 {
98         SDL_GL_SwapWindow(win);
99 }
100
101 long app_get_msec()
102 {
103         return SDL_GetTicks();
104 }
105
106 static void process_event(SDL_Event *ev)
107 {
108         switch(ev->type) {
109         case SDL_QUIT:
110                 quit = true;
111                 break;
112
113         case SDL_KEYDOWN:
114         case SDL_KEYUP:
115                 app_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED);
116                 break;
117
118         case SDL_MOUSEBUTTONDOWN:
119         case SDL_MOUSEBUTTONUP:
120                 app_mouse_button(ev->button.button - SDL_BUTTON_LEFT, ev->button.state == SDL_PRESSED,
121                                 ev->button.x, ev->button.y);
122                 break;
123
124         case SDL_MOUSEMOTION:
125                 app_mouse_motion(ev->motion.x, ev->motion.y);
126                 break;
127
128         case SDL_WINDOWEVENT:
129                 if(ev->window.event == SDL_WINDOWEVENT_RESIZED) {
130                         win_width = ev->window.data1;
131                         win_height = ev->window.data2;
132                         win_aspect = (float)win_width / (float)win_height;
133                         app_reshape(win_width, win_height);
134                 }
135                 break;
136         }
137 }