vrheights

view src/main.cc @ 16:7f6d68d95c22

updated to new version of goatvr
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 30 Oct 2015 06:34:31 +0200
parents 053a52f0cb64
children
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <SDL2/SDL.h>
4 #include "game.h"
6 static bool init();
7 static void cleanup();
8 static void handle_event(SDL_Event *ev);
10 static bool done;
11 static SDL_Window *win;
12 static SDL_GLContext ctx;
14 int main(int argc, char **argv)
15 {
16 if(!init()) {
17 return 1;
18 }
20 while(!done) {
21 SDL_Event ev;
22 while(SDL_PollEvent(&ev)) {
23 handle_event(&ev);
24 if(done) goto end_main_loop;
25 }
27 game_update(SDL_GetTicks());
28 game_display();
29 }
31 end_main_loop:
32 cleanup();
33 return 0;
34 }
36 void exit_game()
37 {
38 done = true;
39 }
41 void move_window(int x, int y)
42 {
43 SDL_SetWindowPosition(win, x, y);
44 }
46 void get_window_pos(int *x, int *y)
47 {
48 SDL_GetWindowPosition(win, x, y);
49 }
51 void resize_window(int x, int y)
52 {
53 SDL_SetWindowSize(win, x, y);
54 }
56 void enter_fullscreen()
57 {
58 SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN_DESKTOP);
59 }
61 void leave_fullscreen()
62 {
63 SDL_SetWindowFullscreen(win, 0);
64 }
66 static bool init()
67 {
68 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) {
69 fprintf(stderr, "failed to initialize SDL\n");
70 return false;
71 }
73 char *env = getenv("VR_NULL_STEREO_GL");
74 if(env && atoi(env)) {
75 SDL_GL_SetAttribute(SDL_GL_STEREO, 1);
76 }
78 win = SDL_CreateWindow("vrheights", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
79 1280, 800, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
80 if(!win) {
81 fprintf(stderr, "failed to create window\n");
82 return false;
83 }
85 if(!(ctx = SDL_GL_CreateContext(win))) {
86 fprintf(stderr, "failed to create OpenGL context\n");
87 return false;
88 }
90 if(!game_init()) {
91 return false;
92 }
94 int w, h;
95 SDL_GetWindowSize(win, &w, &h);
96 game_reshape(w, h);
97 return true;
98 }
100 static void cleanup()
101 {
102 game_cleanup();
103 SDL_Quit();
104 }
106 static void handle_event(SDL_Event *ev)
107 {
108 switch(ev->type) {
109 case SDL_WINDOWEVENT:
110 if(ev->window.event == SDL_WINDOWEVENT_RESIZED) {
111 game_reshape(ev->window.data1, ev->window.data2);
112 }
113 break;
115 case SDL_KEYDOWN:
116 case SDL_KEYUP:
117 game_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED);
118 break;
120 case SDL_MOUSEBUTTONDOWN:
121 case SDL_MOUSEBUTTONUP:
122 {
123 int bn = ev->button.button - SDL_BUTTON_LEFT;
124 game_mouse_button(bn, ev->button.state == SDL_PRESSED, ev->button.x, ev->button.y);
125 }
126 break;
128 case SDL_MOUSEMOTION:
129 game_mouse_motion(ev->motion.x, ev->motion.y);
130 break;
132 case SDL_QUIT:
133 exit_game();
134 break;
136 default:
137 break;
138 }
139 }