vrshoot
diff src/game.cc @ 0:b2f14e535253
initial commit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 01 Feb 2014 19:58:19 +0200 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/game.cc Sat Feb 01 19:58:19 2014 +0200 1.3 @@ -0,0 +1,241 @@ 1.4 +#include <vector> 1.5 +#include <assert.h> 1.6 + 1.7 +#include "opengl.h" 1.8 +#include "game.h" 1.9 +#include "input.h" 1.10 +#include "datapath.h" 1.11 +#include "shader.h" 1.12 +#include "sdrman.h" 1.13 +#include "unistate.h" 1.14 +#include "scr_debug.h" 1.15 +#include "scr_game.h" 1.16 +#include "logger.h" 1.17 +#include "mesh.h" 1.18 +#include "audio/audio.h" 1.19 +#include "timer.h" 1.20 + 1.21 +static void toggle_debug(); 1.22 + 1.23 +static char *def_argv[] = { (char*)"game", 0 }; 1.24 + 1.25 +static int argc = 1; 1.26 +static char **argv = def_argv; 1.27 +static int width, height; 1.28 + 1.29 +static std::vector<Screen*> screens; 1.30 +static Screen *debug_screen; 1.31 + 1.32 +static void standard_state_uniforms(); 1.33 +static bool create_screens(); 1.34 + 1.35 +ShaderProg *defsdr; 1.36 +Timer timer; 1.37 + 1.38 +void game_set_args(int argc, char **argv) 1.39 +{ 1.40 + ::argc = argc; 1.41 + ::argv = argv; 1.42 +} 1.43 + 1.44 +bool game_init() 1.45 +{ 1.46 + init_opengl(); 1.47 + CHECKGLERR; 1.48 + 1.49 + /* 1.50 +#ifdef __GLEW_H__ 1.51 + if(GLEW_ARB_multisample) { 1.52 + info_log("enabling multisampling\n"); 1.53 + glEnable(GL_MULTISAMPLE); 1.54 + } else { 1.55 + info_log("no multisampling support, will try old-style smoothing\n"); 1.56 + // line smoothing enabled when needed (mostly by tools) 1.57 + } 1.58 +#endif 1.59 + */ 1.60 + 1.61 + // The configuration file is expected to be on the root 1.62 + // of our data archive. 1.63 + add_data_path("data"); 1.64 + add_data_path("sdr"); 1.65 + 1.66 + standard_state_uniforms(); 1.67 + 1.68 + if(!(defsdr = get_sdrprog("default.v.glsl", "default.p.glsl"))) { 1.69 + error_log("failed to load default shader, aborting\n"); 1.70 + return false; 1.71 + } 1.72 + 1.73 + glEnable(GL_DEPTH_TEST); 1.74 + glEnable(GL_CULL_FACE); 1.75 + 1.76 + init_audio(); 1.77 + 1.78 + if(!create_screens()) { 1.79 + error_log("failed to create scenes\n"); 1.80 + return false; 1.81 + } 1.82 +#if defined(TARGET_IPHONE) && !defined(NDEBUG) 1.83 + //toggle_debug(); 1.84 +#endif 1.85 + 1.86 + return true; 1.87 +} 1.88 + 1.89 +void game_cleanup() 1.90 +{ 1.91 + destroy_audio(); 1.92 + 1.93 + delete defsdr; 1.94 + 1.95 + // pop all screens 1.96 + while(current_screen()) { 1.97 + pop_screen(); 1.98 + } 1.99 + // and then destroy them 1.100 + for(size_t i=0; i<screens.size(); i++) { 1.101 + delete screens[i]; 1.102 + } 1.103 + delete debug_screen; 1.104 +} 1.105 + 1.106 +void game_display() 1.107 +{ 1.108 + unsigned long msec = timer.get_msec(); 1.109 + 1.110 + // set current global time state uniform 1.111 + static int st_time_idx = -1; 1.112 + if(st_time_idx == -1) { 1.113 + st_time_idx = get_unistate_index("st_time"); 1.114 + } 1.115 + set_unistate(st_time_idx, (float)msec / 1000.0f); 1.116 + 1.117 + current_screen()->update(msec); 1.118 + 1.119 + glClearColor(0, 0, 0, 1); 1.120 + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 1.121 + 1.122 + current_screen()->pre_draw(); 1.123 + current_screen()->display(); 1.124 + current_screen()->post_draw(); 1.125 + 1.126 + swap_buffers(); 1.127 + CHECKGLERR; 1.128 +} 1.129 + 1.130 +void game_reshape(int x, int y) 1.131 +{ 1.132 + width = x; 1.133 + height = y; 1.134 + 1.135 + // set framebuffer size uniform 1.136 + static int st_fbsize_idx = -1; 1.137 + if(st_fbsize_idx == -1) { 1.138 + st_fbsize_idx = get_unistate_index("st_fbsize"); 1.139 + } 1.140 + set_unistate(st_fbsize_idx, Vector2(x, y)); 1.141 + 1.142 + glViewport(0, 0, x, y); 1.143 + current_screen()->reshape(x, y); 1.144 +} 1.145 + 1.146 +void game_key(int key, bool pressed) 1.147 +{ 1.148 + // special key processing 1.149 + if(pressed) { 1.150 + switch(key) { 1.151 + case 27: 1.152 + exit(0); 1.153 + 1.154 + case '`': 1.155 + toggle_debug(); 1.156 + break; 1.157 + 1.158 + default: 1.159 + break; 1.160 + } 1.161 + } 1.162 + set_key_state(key, pressed); 1.163 + current_screen()->keyboard(key, pressed); 1.164 +} 1.165 + 1.166 + 1.167 +int get_screen_width() 1.168 +{ 1.169 + return width; 1.170 +} 1.171 + 1.172 +int get_screen_height() 1.173 +{ 1.174 + return height; 1.175 +} 1.176 + 1.177 + 1.178 + 1.179 +static void standard_state_uniforms() 1.180 +{ 1.181 + // setup all the standard state uniforms 1.182 + add_unistate("st_world_matrix", ST_MATRIX4); 1.183 + add_unistate("st_world_matrix_transpose", ST_MATRIX4); 1.184 + //add_unistate("st_world_matrix_inverse", ST_MATRIX4); 1.185 + add_unistate("st_world_matrix3", ST_MATRIX3); 1.186 + 1.187 + add_unistate("st_view_matrix", ST_MATRIX4); 1.188 + add_unistate("st_view_matrix_transpose", ST_MATRIX4); 1.189 + //add_unistate("st_view_matrix_inverse", ST_MATRIX4); 1.190 + add_unistate("st_view_matrix3", ST_MATRIX3); 1.191 + 1.192 + add_unistate("st_proj_matrix", ST_MATRIX4); 1.193 + //add_unistate("st_proj_matrix_inverse", ST_MATRIX4); 1.194 + 1.195 + add_unistate("st_tex_matrix", ST_MATRIX4); 1.196 + //add_unistate("st_tex_matrix_inverse", ST_MATRIX4); 1.197 + 1.198 + //add_unistate("st_mvp_matrix", ST_MATRIX4); 1.199 + //add_unistate("st_mvp_matrix_inverse", ST_MATRIX4); 1.200 + 1.201 + add_unistate("st_fbsize", ST_FLOAT2); 1.202 + 1.203 + add_unistate("st_time", ST_FLOAT); 1.204 + 1.205 + add_unistate("st_light_pos", ST_FLOAT3); 1.206 + add_unistate("st_light_color", ST_FLOAT3); 1.207 + add_unistate("st_light_radius", ST_FLOAT); 1.208 + 1.209 + add_unistate("st_mtl_diffuse", ST_FLOAT3); 1.210 + add_unistate("st_mtl_specular", ST_FLOAT3); 1.211 + add_unistate("st_mtl_shininess", ST_FLOAT); 1.212 + add_unistate("st_mtl_alpha", ST_FLOAT); 1.213 +} 1.214 + 1.215 +static bool create_screens() 1.216 +{ 1.217 + Screen *scr; 1.218 + 1.219 + // create the debug overlay screen 1.220 + debug_screen = new DebugScreen; 1.221 + if(!debug_screen->init()) { 1.222 + return false; 1.223 + } 1.224 + 1.225 + // create the starting screen last 1.226 + scr = new GameScreen; 1.227 + if(!scr->init()) { 1.228 + return false; 1.229 + } 1.230 + screens.push_back(scr); 1.231 + 1.232 + info_log("start-screen: %s\n", scr->get_name()); 1.233 + push_screen(scr); 1.234 + return true; 1.235 +} 1.236 + 1.237 +static void toggle_debug() 1.238 +{ 1.239 + if(dynamic_cast<DebugScreen*>(current_screen())) { 1.240 + pop_screen(); 1.241 + } else { 1.242 + push_screen(debug_screen); 1.243 + } 1.244 +}