rev |
line source |
nuclear@2
|
1 #include <stdio.h>
|
nuclear@2
|
2 #include <stdlib.h>
|
nuclear@2
|
3 #include <SDL2/SDL.h>
|
nuclear@2
|
4 #include "game.h"
|
nuclear@2
|
5
|
nuclear@2
|
6 static bool init();
|
nuclear@2
|
7 static void cleanup();
|
nuclear@2
|
8 static void handle_event(SDL_Event *ev);
|
nuclear@2
|
9
|
nuclear@2
|
10 static bool done;
|
nuclear@2
|
11 static SDL_Window *win;
|
nuclear@2
|
12 static SDL_GLContext ctx;
|
nuclear@2
|
13
|
nuclear@2
|
14 int main(int argc, char **argv)
|
nuclear@2
|
15 {
|
nuclear@2
|
16 if(!init()) {
|
nuclear@2
|
17 return 1;
|
nuclear@2
|
18 }
|
nuclear@2
|
19
|
nuclear@2
|
20 while(!done) {
|
nuclear@2
|
21 SDL_Event ev;
|
nuclear@2
|
22 while(SDL_PollEvent(&ev)) {
|
nuclear@2
|
23 handle_event(&ev);
|
nuclear@2
|
24 if(done) goto end_main_loop;
|
nuclear@2
|
25 }
|
nuclear@2
|
26
|
nuclear@2
|
27 game_update(SDL_GetTicks());
|
nuclear@2
|
28 game_display();
|
nuclear@2
|
29 }
|
nuclear@2
|
30
|
nuclear@2
|
31 end_main_loop:
|
nuclear@2
|
32 cleanup();
|
nuclear@2
|
33 return 0;
|
nuclear@2
|
34 }
|
nuclear@2
|
35
|
nuclear@2
|
36 void exit_game()
|
nuclear@2
|
37 {
|
nuclear@2
|
38 done = true;
|
nuclear@2
|
39 }
|
nuclear@2
|
40
|
nuclear@2
|
41 void move_window(int x, int y)
|
nuclear@2
|
42 {
|
nuclear@2
|
43 SDL_SetWindowPosition(win, x, y);
|
nuclear@2
|
44 }
|
nuclear@2
|
45
|
nuclear@2
|
46 void get_window_pos(int *x, int *y)
|
nuclear@2
|
47 {
|
nuclear@2
|
48 SDL_GetWindowPosition(win, x, y);
|
nuclear@2
|
49 }
|
nuclear@2
|
50
|
nuclear@2
|
51 void resize_window(int x, int y)
|
nuclear@2
|
52 {
|
nuclear@2
|
53 SDL_SetWindowSize(win, x, y);
|
nuclear@2
|
54 }
|
nuclear@2
|
55
|
nuclear@2
|
56 void enter_fullscreen()
|
nuclear@2
|
57 {
|
nuclear@2
|
58 SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN_DESKTOP);
|
nuclear@2
|
59 }
|
nuclear@2
|
60
|
nuclear@2
|
61 void leave_fullscreen()
|
nuclear@2
|
62 {
|
nuclear@2
|
63 SDL_SetWindowFullscreen(win, 0);
|
nuclear@2
|
64 }
|
nuclear@2
|
65
|
nuclear@2
|
66 static bool init()
|
nuclear@2
|
67 {
|
nuclear@2
|
68 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) {
|
nuclear@2
|
69 fprintf(stderr, "failed to initialize SDL\n");
|
nuclear@2
|
70 return false;
|
nuclear@2
|
71 }
|
nuclear@2
|
72
|
nuclear@2
|
73 win = SDL_CreateWindow("vrheights", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
|
nuclear@2
|
74 1280, 800, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
|
nuclear@2
|
75 if(!win) {
|
nuclear@2
|
76 fprintf(stderr, "failed to create window\n");
|
nuclear@2
|
77 return false;
|
nuclear@2
|
78 }
|
nuclear@2
|
79
|
nuclear@2
|
80 if(!(ctx = SDL_GL_CreateContext(win))) {
|
nuclear@2
|
81 fprintf(stderr, "failed to create OpenGL context\n");
|
nuclear@2
|
82 return false;
|
nuclear@2
|
83 }
|
nuclear@2
|
84
|
nuclear@2
|
85 if(!game_init()) {
|
nuclear@2
|
86 return false;
|
nuclear@2
|
87 }
|
nuclear@2
|
88
|
nuclear@2
|
89 int w, h;
|
nuclear@2
|
90 SDL_GetWindowSize(win, &w, &h);
|
nuclear@2
|
91 game_reshape(w, h);
|
nuclear@2
|
92 return true;
|
nuclear@2
|
93 }
|
nuclear@2
|
94
|
nuclear@2
|
95 static void cleanup()
|
nuclear@2
|
96 {
|
nuclear@2
|
97 game_cleanup();
|
nuclear@2
|
98 SDL_Quit();
|
nuclear@2
|
99 }
|
nuclear@2
|
100
|
nuclear@2
|
101 static void handle_event(SDL_Event *ev)
|
nuclear@2
|
102 {
|
nuclear@2
|
103 switch(ev->type) {
|
nuclear@2
|
104 case SDL_WINDOWEVENT:
|
nuclear@2
|
105 if(ev->window.event == SDL_WINDOWEVENT_RESIZED) {
|
nuclear@2
|
106 game_reshape(ev->window.data1, ev->window.data2);
|
nuclear@2
|
107 }
|
nuclear@2
|
108 break;
|
nuclear@2
|
109
|
nuclear@2
|
110 case SDL_KEYDOWN:
|
nuclear@2
|
111 case SDL_KEYUP:
|
nuclear@2
|
112 game_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED);
|
nuclear@2
|
113 break;
|
nuclear@2
|
114
|
nuclear@2
|
115 case SDL_MOUSEBUTTONDOWN:
|
nuclear@2
|
116 case SDL_MOUSEBUTTONUP:
|
nuclear@2
|
117 game_mouse_button(ev->button.button, ev->button.state == SDL_PRESSED, ev->button.x, ev->button.y);
|
nuclear@2
|
118 break;
|
nuclear@2
|
119
|
nuclear@2
|
120 case SDL_MOUSEMOTION:
|
nuclear@2
|
121 game_mouse_motion(ev->motion.x, ev->motion.y);
|
nuclear@2
|
122 break;
|
nuclear@2
|
123
|
nuclear@2
|
124 default:
|
nuclear@2
|
125 break;
|
nuclear@2
|
126 }
|
nuclear@2
|
127 }
|