vrheights
diff src/main.cc @ 2:b49461618f61
starting point
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 25 Sep 2014 11:44:45 +0300 |
parents | ccbd444939a1 |
children | 690ef7fa791f |
line diff
1.1 --- a/src/main.cc Wed Sep 24 11:34:07 2014 +0300 1.2 +++ b/src/main.cc Thu Sep 25 11:44:45 2014 +0300 1.3 @@ -1,94 +1,127 @@ 1.4 -#include <stdio.h> 1.5 -#include <stdlib.h> 1.6 -#include <SDL2/SDL.h> 1.7 -#include "game.h" 1.8 - 1.9 -static bool init(); 1.10 -static void cleanup(); 1.11 -static void handle_event(SDL_Event *ev); 1.12 - 1.13 -static bool done; 1.14 -static SDL_Window *win; 1.15 -static SDL_GLContext ctx; 1.16 - 1.17 -int main(int argc, char **argv) 1.18 -{ 1.19 - if(!init()) { 1.20 - return 1; 1.21 - } 1.22 - 1.23 - for(;;) { 1.24 - SDL_Event ev; 1.25 - while(SDL_PollEvent(&ev)) { 1.26 - handle_event(&ev); 1.27 - if(done) break; 1.28 - } 1.29 - 1.30 - game_update(SDL_GetTicks()); 1.31 - game_display(); 1.32 - } 1.33 - 1.34 - cleanup(); 1.35 - return 0; 1.36 -} 1.37 - 1.38 -void exit_game() 1.39 -{ 1.40 - done = true; 1.41 -} 1.42 - 1.43 -static bool init() 1.44 -{ 1.45 - if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) { 1.46 - fprintf(stderr, "failed to initialize SDL\n"); 1.47 - return false; 1.48 - } 1.49 - 1.50 - win = SDL_CreateWindow("vrheights", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1.51 - 1280, 800, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE); 1.52 - if(!win) { 1.53 - fprintf(stderr, "failed to create window\n"); 1.54 - return false; 1.55 - } 1.56 - 1.57 - if(!(ctx = SDL_GL_CreateContext(win))) { 1.58 - fprintf(stderr, "failed to create OpenGL context\n"); 1.59 - return false; 1.60 - } 1.61 - 1.62 - return game_init(); 1.63 -} 1.64 - 1.65 -static void cleanup() 1.66 -{ 1.67 - game_cleanup(); 1.68 - SDL_Quit(); 1.69 -} 1.70 - 1.71 -static void handle_event(SDL_Event *ev) 1.72 -{ 1.73 - switch(ev->type) { 1.74 - case SDL_WINDOWEVENT: 1.75 - if(ev->window.event == SDL_WINDOWEVENT_RESIZED) { 1.76 - game_reshape(ev->window.data1, ev->window.data2); 1.77 - } 1.78 - break; 1.79 - 1.80 - case SDL_KEYDOWN: 1.81 - case SDL_KEYUP: 1.82 - game_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED); 1.83 - break; 1.84 - 1.85 - case SDL_MOUSEBUTTONDOWN: 1.86 - case SDL_MOUSEBUTTONUP: 1.87 - game_mouse_button(ev->button.button, ev->button.state == SDL_PRESSED, ev->button.x, ev->button.y); 1.88 - break; 1.89 - 1.90 - case SDL_MOUSEMOTION: 1.91 - game_mouse_motion(ev->motion.x, ev->motion.y); 1.92 - break; 1.93 - 1.94 - default: 1.95 - break; 1.96 - } 1.97 -} 1.98 \ No newline at end of file 1.99 +#include <stdio.h> 1.100 +#include <stdlib.h> 1.101 +#include <SDL2/SDL.h> 1.102 +#include "game.h" 1.103 + 1.104 +static bool init(); 1.105 +static void cleanup(); 1.106 +static void handle_event(SDL_Event *ev); 1.107 + 1.108 +static bool done; 1.109 +static SDL_Window *win; 1.110 +static SDL_GLContext ctx; 1.111 + 1.112 +int main(int argc, char **argv) 1.113 +{ 1.114 + if(!init()) { 1.115 + return 1; 1.116 + } 1.117 + 1.118 + while(!done) { 1.119 + SDL_Event ev; 1.120 + while(SDL_PollEvent(&ev)) { 1.121 + handle_event(&ev); 1.122 + if(done) goto end_main_loop; 1.123 + } 1.124 + 1.125 + game_update(SDL_GetTicks()); 1.126 + game_display(); 1.127 + } 1.128 + 1.129 +end_main_loop: 1.130 + cleanup(); 1.131 + return 0; 1.132 +} 1.133 + 1.134 +void exit_game() 1.135 +{ 1.136 + done = true; 1.137 +} 1.138 + 1.139 +void move_window(int x, int y) 1.140 +{ 1.141 + SDL_SetWindowPosition(win, x, y); 1.142 +} 1.143 + 1.144 +void get_window_pos(int *x, int *y) 1.145 +{ 1.146 + SDL_GetWindowPosition(win, x, y); 1.147 +} 1.148 + 1.149 +void resize_window(int x, int y) 1.150 +{ 1.151 + SDL_SetWindowSize(win, x, y); 1.152 +} 1.153 + 1.154 +void enter_fullscreen() 1.155 +{ 1.156 + SDL_SetWindowFullscreen(win, SDL_WINDOW_FULLSCREEN_DESKTOP); 1.157 +} 1.158 + 1.159 +void leave_fullscreen() 1.160 +{ 1.161 + SDL_SetWindowFullscreen(win, 0); 1.162 +} 1.163 + 1.164 +static bool init() 1.165 +{ 1.166 + if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) { 1.167 + fprintf(stderr, "failed to initialize SDL\n"); 1.168 + return false; 1.169 + } 1.170 + 1.171 + win = SDL_CreateWindow("vrheights", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1.172 + 1280, 800, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE); 1.173 + if(!win) { 1.174 + fprintf(stderr, "failed to create window\n"); 1.175 + return false; 1.176 + } 1.177 + 1.178 + if(!(ctx = SDL_GL_CreateContext(win))) { 1.179 + fprintf(stderr, "failed to create OpenGL context\n"); 1.180 + return false; 1.181 + } 1.182 + 1.183 + if(!game_init()) { 1.184 + return false; 1.185 + } 1.186 + 1.187 + int w, h; 1.188 + SDL_GetWindowSize(win, &w, &h); 1.189 + game_reshape(w, h); 1.190 + return true; 1.191 +} 1.192 + 1.193 +static void cleanup() 1.194 +{ 1.195 + game_cleanup(); 1.196 + SDL_Quit(); 1.197 +} 1.198 + 1.199 +static void handle_event(SDL_Event *ev) 1.200 +{ 1.201 + switch(ev->type) { 1.202 + case SDL_WINDOWEVENT: 1.203 + if(ev->window.event == SDL_WINDOWEVENT_RESIZED) { 1.204 + game_reshape(ev->window.data1, ev->window.data2); 1.205 + } 1.206 + break; 1.207 + 1.208 + case SDL_KEYDOWN: 1.209 + case SDL_KEYUP: 1.210 + game_keyboard(ev->key.keysym.sym, ev->key.state == SDL_PRESSED); 1.211 + break; 1.212 + 1.213 + case SDL_MOUSEBUTTONDOWN: 1.214 + case SDL_MOUSEBUTTONUP: 1.215 + game_mouse_button(ev->button.button, ev->button.state == SDL_PRESSED, ev->button.x, ev->button.y); 1.216 + break; 1.217 + 1.218 + case SDL_MOUSEMOTION: 1.219 + game_mouse_motion(ev->motion.x, ev->motion.y); 1.220 + break; 1.221 + 1.222 + default: 1.223 + break; 1.224 + } 1.225 +}