vrheights

view src/main.cc @ 4:690ef7fa791f

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 26 Sep 2014 13:19:22 +0300
parents b49461618f61
children 053a52f0cb64
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 win = SDL_CreateWindow("vrheights", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
74 1280, 800, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
75 if(!win) {
76 fprintf(stderr, "failed to create window\n");
77 return false;
78 }
80 if(!(ctx = SDL_GL_CreateContext(win))) {
81 fprintf(stderr, "failed to create OpenGL context\n");
82 return false;
83 }
85 if(!game_init()) {
86 return false;
87 }
89 int w, h;
90 SDL_GetWindowSize(win, &w, &h);
91 game_reshape(w, h);
92 return true;
93 }
95 static void cleanup()
96 {
97 game_cleanup();
98 SDL_Quit();
99 }
101 static void handle_event(SDL_Event *ev)
102 {
103 switch(ev->type) {
104 case SDL_WINDOWEVENT:
105 if(ev->window.event == SDL_WINDOWEVENT_RESIZED) {
106 game_reshape(ev->window.data1, ev->window.data2);
107 }
108 break;
110 case SDL_KEYDOWN:
111 case SDL_KEYUP:
112 game_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED);
113 break;
115 case SDL_MOUSEBUTTONDOWN:
116 case SDL_MOUSEBUTTONUP:
117 {
118 int bn = ev->button.button - SDL_BUTTON_LEFT;
119 game_mouse_button(bn, ev->button.state == SDL_PRESSED, ev->button.x, ev->button.y);
120 }
121 break;
123 case SDL_MOUSEMOTION:
124 game_mouse_motion(ev->motion.x, ev->motion.y);
125 break;
127 default:
128 break;
129 }
130 }