initial commit
[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         int x = SDL_WINDOWPOS_UNDEFINED;
21         int y = SDL_WINDOWPOS_UNDEFINED;
22         unsigned int flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI;
23         win_width = 1280;
24         win_height = 800;
25         if(!(win = SDL_CreateWindow("vrfileman", x, y, win_width, win_height, flags))) {
26                 fprintf(stderr, "failed to create window\n");
27                 return 1;
28         }
29
30         SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
31         SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
32         SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
33         SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
34         SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, 1);
35
36         if(!(ctx = SDL_GL_CreateContext(win))) {
37                 fprintf(stderr, "failed to create OpenGL context\n");
38                 return 1;
39         }
40         SDL_GL_GetDrawableSize(win, &win_width, &win_height);
41         app_reshape(win_width, win_height);
42
43         while(!quit) {
44                 SDL_Event ev;
45
46                 if(!redraw_pending) {
47                         if(!SDL_WaitEvent(0)) {
48                                 fprintf(stderr, "error while waiting for events\n");
49                                 break;
50                         }
51                 }
52
53                 time_msec = app_get_msec();
54                 while(SDL_PollEvent(&ev)) {
55                         process_event(&ev);
56                         if(quit) goto break_evloop;
57                 }
58
59                 if(redraw_pending) {
60                         redraw_pending = false;
61                         app_draw();
62                 }
63         }
64 break_evloop:
65
66         app_cleanup();
67         SDL_Quit();
68         return 0;
69 }
70
71 void app_redraw()
72 {
73         redraw_pending = true;
74 }
75
76 void app_swap_buffers()
77 {
78         SDL_GL_SwapWindow(win);
79 }
80
81 long app_get_msec()
82 {
83         return SDL_GetTicks();
84 }