vrheights
view src/main.cc @ 1:d06e4e24f922
foo
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Wed, 24 Sep 2014 11:34:07 +0300 |
parents | |
children | b49461618f61 |
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 for(;;) {
21 SDL_Event ev;
22 while(SDL_PollEvent(&ev)) {
23 handle_event(&ev);
24 if(done) break;
25 }
27 game_update(SDL_GetTicks());
28 game_display();
29 }
31 cleanup();
32 return 0;
33 }
35 void exit_game()
36 {
37 done = true;
38 }
40 static bool init()
41 {
42 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) {
43 fprintf(stderr, "failed to initialize SDL\n");
44 return false;
45 }
47 win = SDL_CreateWindow("vrheights", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
48 1280, 800, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
49 if(!win) {
50 fprintf(stderr, "failed to create window\n");
51 return false;
52 }
54 if(!(ctx = SDL_GL_CreateContext(win))) {
55 fprintf(stderr, "failed to create OpenGL context\n");
56 return false;
57 }
59 return game_init();
60 }
62 static void cleanup()
63 {
64 game_cleanup();
65 SDL_Quit();
66 }
68 static void handle_event(SDL_Event *ev)
69 {
70 switch(ev->type) {
71 case SDL_WINDOWEVENT:
72 if(ev->window.event == SDL_WINDOWEVENT_RESIZED) {
73 game_reshape(ev->window.data1, ev->window.data2);
74 }
75 break;
77 case SDL_KEYDOWN:
78 case SDL_KEYUP:
79 game_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED);
80 break;
82 case SDL_MOUSEBUTTONDOWN:
83 case SDL_MOUSEBUTTONUP:
84 game_mouse_button(ev->button.button, ev->button.state == SDL_PRESSED, ev->button.x, ev->button.y);
85 break;
87 case SDL_MOUSEMOTION:
88 game_mouse_motion(ev->motion.x, ev->motion.y);
89 break;
91 default:
92 break;
93 }
94 }