vrshoot

view src/game.cc @ 1:e7ca128b8713

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