vrshoot

annotate src/game.cc @ 3:c179c72369be

rename candy->vr
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 03 Feb 2014 08:52:13 +0200
parents
children
rev   line source
nuclear@0 1 #include <vector>
nuclear@0 2 #include <assert.h>
nuclear@0 3
nuclear@0 4 #include "opengl.h"
nuclear@0 5 #include "game.h"
nuclear@0 6 #include "input.h"
nuclear@0 7 #include "datapath.h"
nuclear@0 8 #include "shader.h"
nuclear@0 9 #include "sdrman.h"
nuclear@0 10 #include "unistate.h"
nuclear@0 11 #include "scr_debug.h"
nuclear@0 12 #include "scr_game.h"
nuclear@0 13 #include "logger.h"
nuclear@0 14 #include "mesh.h"
nuclear@0 15 #include "audio/audio.h"
nuclear@0 16 #include "timer.h"
nuclear@0 17
nuclear@0 18 static void toggle_debug();
nuclear@0 19
nuclear@0 20 static char *def_argv[] = { (char*)"game", 0 };
nuclear@0 21
nuclear@0 22 static int argc = 1;
nuclear@0 23 static char **argv = def_argv;
nuclear@0 24 static int width, height;
nuclear@0 25
nuclear@0 26 static std::vector<Screen*> screens;
nuclear@0 27 static Screen *debug_screen;
nuclear@0 28
nuclear@0 29 static void standard_state_uniforms();
nuclear@0 30 static bool create_screens();
nuclear@0 31
nuclear@0 32 ShaderProg *defsdr;
nuclear@0 33 Timer timer;
nuclear@0 34
nuclear@0 35 void game_set_args(int argc, char **argv)
nuclear@0 36 {
nuclear@0 37 ::argc = argc;
nuclear@0 38 ::argv = argv;
nuclear@0 39 }
nuclear@0 40
nuclear@0 41 bool game_init()
nuclear@0 42 {
nuclear@0 43 init_opengl();
nuclear@0 44 CHECKGLERR;
nuclear@0 45
nuclear@0 46 /*
nuclear@0 47 #ifdef __GLEW_H__
nuclear@0 48 if(GLEW_ARB_multisample) {
nuclear@0 49 info_log("enabling multisampling\n");
nuclear@0 50 glEnable(GL_MULTISAMPLE);
nuclear@0 51 } else {
nuclear@0 52 info_log("no multisampling support, will try old-style smoothing\n");
nuclear@0 53 // line smoothing enabled when needed (mostly by tools)
nuclear@0 54 }
nuclear@0 55 #endif
nuclear@0 56 */
nuclear@0 57
nuclear@0 58 // The configuration file is expected to be on the root
nuclear@0 59 // of our data archive.
nuclear@0 60 add_data_path("data");
nuclear@0 61 add_data_path("sdr");
nuclear@0 62
nuclear@0 63 standard_state_uniforms();
nuclear@0 64
nuclear@0 65 if(!(defsdr = get_sdrprog("default.v.glsl", "default.p.glsl"))) {
nuclear@0 66 error_log("failed to load default shader, aborting\n");
nuclear@0 67 return false;
nuclear@0 68 }
nuclear@0 69
nuclear@0 70 glEnable(GL_DEPTH_TEST);
nuclear@0 71 glEnable(GL_CULL_FACE);
nuclear@0 72
nuclear@0 73 init_audio();
nuclear@0 74
nuclear@0 75 if(!create_screens()) {
nuclear@0 76 error_log("failed to create scenes\n");
nuclear@0 77 return false;
nuclear@0 78 }
nuclear@0 79 #if defined(TARGET_IPHONE) && !defined(NDEBUG)
nuclear@0 80 //toggle_debug();
nuclear@0 81 #endif
nuclear@0 82
nuclear@0 83 return true;
nuclear@0 84 }
nuclear@0 85
nuclear@0 86 void game_cleanup()
nuclear@0 87 {
nuclear@0 88 destroy_audio();
nuclear@0 89
nuclear@0 90 delete defsdr;
nuclear@0 91
nuclear@0 92 // pop all screens
nuclear@0 93 while(current_screen()) {
nuclear@0 94 pop_screen();
nuclear@0 95 }
nuclear@0 96 // and then destroy them
nuclear@0 97 for(size_t i=0; i<screens.size(); i++) {
nuclear@0 98 delete screens[i];
nuclear@0 99 }
nuclear@0 100 delete debug_screen;
nuclear@0 101 }
nuclear@0 102
nuclear@0 103 void game_display()
nuclear@0 104 {
nuclear@0 105 unsigned long msec = timer.get_msec();
nuclear@0 106
nuclear@0 107 // set current global time state uniform
nuclear@0 108 static int st_time_idx = -1;
nuclear@0 109 if(st_time_idx == -1) {
nuclear@0 110 st_time_idx = get_unistate_index("st_time");
nuclear@0 111 }
nuclear@0 112 set_unistate(st_time_idx, (float)msec / 1000.0f);
nuclear@0 113
nuclear@0 114 current_screen()->update(msec);
nuclear@0 115
nuclear@0 116 glClearColor(0, 0, 0, 1);
nuclear@0 117 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
nuclear@0 118
nuclear@0 119 current_screen()->pre_draw();
nuclear@0 120 current_screen()->display();
nuclear@0 121 current_screen()->post_draw();
nuclear@0 122
nuclear@0 123 swap_buffers();
nuclear@0 124 CHECKGLERR;
nuclear@0 125 }
nuclear@0 126
nuclear@0 127 void game_reshape(int x, int y)
nuclear@0 128 {
nuclear@0 129 width = x;
nuclear@0 130 height = y;
nuclear@0 131
nuclear@0 132 // set framebuffer size uniform
nuclear@0 133 static int st_fbsize_idx = -1;
nuclear@0 134 if(st_fbsize_idx == -1) {
nuclear@0 135 st_fbsize_idx = get_unistate_index("st_fbsize");
nuclear@0 136 }
nuclear@0 137 set_unistate(st_fbsize_idx, Vector2(x, y));
nuclear@0 138
nuclear@0 139 glViewport(0, 0, x, y);
nuclear@0 140 current_screen()->reshape(x, y);
nuclear@0 141 }
nuclear@0 142
nuclear@0 143 void game_key(int key, bool pressed)
nuclear@0 144 {
nuclear@0 145 // special key processing
nuclear@0 146 if(pressed) {
nuclear@0 147 switch(key) {
nuclear@0 148 case 27:
nuclear@0 149 exit(0);
nuclear@0 150
nuclear@0 151 case '`':
nuclear@0 152 toggle_debug();
nuclear@0 153 break;
nuclear@0 154
nuclear@0 155 default:
nuclear@0 156 break;
nuclear@0 157 }
nuclear@0 158 }
nuclear@0 159 set_key_state(key, pressed);
nuclear@0 160 current_screen()->keyboard(key, pressed);
nuclear@0 161 }
nuclear@0 162
nuclear@0 163
nuclear@0 164 int get_screen_width()
nuclear@0 165 {
nuclear@0 166 return width;
nuclear@0 167 }
nuclear@0 168
nuclear@0 169 int get_screen_height()
nuclear@0 170 {
nuclear@0 171 return height;
nuclear@0 172 }
nuclear@0 173
nuclear@0 174
nuclear@0 175
nuclear@0 176 static void standard_state_uniforms()
nuclear@0 177 {
nuclear@0 178 // setup all the standard state uniforms
nuclear@0 179 add_unistate("st_world_matrix", ST_MATRIX4);
nuclear@0 180 add_unistate("st_world_matrix_transpose", ST_MATRIX4);
nuclear@0 181 //add_unistate("st_world_matrix_inverse", ST_MATRIX4);
nuclear@0 182 add_unistate("st_world_matrix3", ST_MATRIX3);
nuclear@0 183
nuclear@0 184 add_unistate("st_view_matrix", ST_MATRIX4);
nuclear@0 185 add_unistate("st_view_matrix_transpose", ST_MATRIX4);
nuclear@0 186 //add_unistate("st_view_matrix_inverse", ST_MATRIX4);
nuclear@0 187 add_unistate("st_view_matrix3", ST_MATRIX3);
nuclear@0 188
nuclear@0 189 add_unistate("st_proj_matrix", ST_MATRIX4);
nuclear@0 190 //add_unistate("st_proj_matrix_inverse", ST_MATRIX4);
nuclear@0 191
nuclear@0 192 add_unistate("st_tex_matrix", ST_MATRIX4);
nuclear@0 193 //add_unistate("st_tex_matrix_inverse", ST_MATRIX4);
nuclear@0 194
nuclear@0 195 //add_unistate("st_mvp_matrix", ST_MATRIX4);
nuclear@0 196 //add_unistate("st_mvp_matrix_inverse", ST_MATRIX4);
nuclear@0 197
nuclear@0 198 add_unistate("st_fbsize", ST_FLOAT2);
nuclear@0 199
nuclear@0 200 add_unistate("st_time", ST_FLOAT);
nuclear@0 201
nuclear@0 202 add_unistate("st_light_pos", ST_FLOAT3);
nuclear@0 203 add_unistate("st_light_color", ST_FLOAT3);
nuclear@0 204 add_unistate("st_light_radius", ST_FLOAT);
nuclear@0 205
nuclear@0 206 add_unistate("st_mtl_diffuse", ST_FLOAT3);
nuclear@0 207 add_unistate("st_mtl_specular", ST_FLOAT3);
nuclear@0 208 add_unistate("st_mtl_shininess", ST_FLOAT);
nuclear@0 209 add_unistate("st_mtl_alpha", ST_FLOAT);
nuclear@0 210 }
nuclear@0 211
nuclear@0 212 static bool create_screens()
nuclear@0 213 {
nuclear@0 214 Screen *scr;
nuclear@0 215
nuclear@0 216 // create the debug overlay screen
nuclear@0 217 debug_screen = new DebugScreen;
nuclear@0 218 if(!debug_screen->init()) {
nuclear@0 219 return false;
nuclear@0 220 }
nuclear@0 221
nuclear@0 222 // create the starting screen last
nuclear@0 223 scr = new GameScreen;
nuclear@0 224 if(!scr->init()) {
nuclear@0 225 return false;
nuclear@0 226 }
nuclear@0 227 screens.push_back(scr);
nuclear@0 228
nuclear@0 229 info_log("start-screen: %s\n", scr->get_name());
nuclear@0 230 push_screen(scr);
nuclear@0 231 return true;
nuclear@0 232 }
nuclear@0 233
nuclear@0 234 static void toggle_debug()
nuclear@0 235 {
nuclear@0 236 if(dynamic_cast<DebugScreen*>(current_screen())) {
nuclear@0 237 pop_screen();
nuclear@0 238 } else {
nuclear@0 239 push_screen(debug_screen);
nuclear@0 240 }
nuclear@0 241 }