bloboland

view src/game.cc @ 4:9021a906c5d3

lots of stuff
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 18 Dec 2012 06:13:09 +0200
parents a39c301cdcce
children 2f4406cc341e
line source
1 #include "game.h"
2 #include "opt.h"
3 #include "opengl.h"
4 #include "level.h"
5 #include "renderer.h"
6 #include "camera.h"
8 static void view_matrix(int eye);
9 static void proj_matrix(int eye);
11 int win_xsz, win_ysz;
13 bool keystate[GAME_MAX_KEYS];
14 bool bnstate[GAME_MAX_BUTTONS];
16 float stereo_focus_dist = 0.25;
17 float stereo_eye_sep = stereo_focus_dist / 30.0;
19 static Level *level;
20 static Renderer *rend;
21 static FpsCamera cam;
23 static const float fog_color[] = {0.76, 0.64, 0.91, 1.0};
24 static Vector3 gravity;
26 bool game_init()
27 {
28 printf("initializing OpenGL state\n");
29 glEnable(GL_CULL_FACE);
30 glEnable(GL_DEPTH_TEST);
31 glEnable(GL_LIGHTING);
32 glEnable(GL_LIGHT0);
34 float lpos[] = {-1, 1, 1, 0};
35 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
37 printf("generating level\n");
38 level = new Level;
39 level->world_size = Vector3(8, 8, 4);
40 level->generate();
42 printf("initializing renderer\n");
43 rend = new Renderer;
44 if(!rend->init(level)) {
45 return false;
46 }
48 cam.input_move(0, 2, 2);
49 cam.input_rotate(0, M_PI / 5, 0);
51 glClearColor(fog_color[0], fog_color[1], fog_color[2], 1.0);
52 glFogfv(GL_FOG_COLOR, fog_color);
54 gravity = Vector3(0, -0.01, 0);
56 return true;
57 }
59 void game_shutdown()
60 {
61 delete rend;
62 delete level;
63 }
65 void game_iter(double dt)
66 {
67 float offs = 4.0 * dt;
68 float dx = 0, dy = 0;
70 // handle key input
71 if(keystate['w'] || keystate['W']) {
72 dy -= offs;
73 }
74 if(keystate['s'] || keystate['S']) {
75 dy += offs;
76 }
77 if(keystate['d'] || keystate['D']) {
78 dx += offs;
79 }
80 if(keystate['a'] || keystate['A']) {
81 dx -= offs;
82 }
84 cam.input_move(dx, 0, dy);
86 for(size_t i=0; i<level->blobs.size(); i++) {
87 Blob *b = &level->blobs[i];
89 b->velocity += gravity * dt;
90 Vector3 npos = b->pos + b->velocity * dt;
92 Vector3 normal;
93 if(level->collision(b->pos, npos, &npos, &normal)) {
94 b->velocity = b->velocity.reflection(normal);
95 }
96 }
97 }
99 void game_render()
100 {
101 rend->set_aspect((float)win_xsz / (float)win_ysz);
103 if(opt.stereo) {
104 glDrawBuffer(GL_BACK_LEFT);
105 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
107 glMatrixMode(GL_PROJECTION);
108 glLoadIdentity();
109 proj_matrix(-1);
110 glMatrixMode(GL_MODELVIEW);
111 glLoadIdentity();
112 view_matrix(-1);
114 rend->render();
116 glDrawBuffer(GL_BACK_RIGHT);
117 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
119 glMatrixMode(GL_PROJECTION);
120 glLoadIdentity();
121 proj_matrix(1);
122 glMatrixMode(GL_MODELVIEW);
123 glLoadIdentity();
124 view_matrix(1);
126 rend->render();
127 } else {
128 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
130 glMatrixMode(GL_PROJECTION);
131 glLoadIdentity();
132 proj_matrix(0);
133 glMatrixMode(GL_MODELVIEW);
134 glLoadIdentity();
135 view_matrix(0);
137 rend->render();
138 }
139 }
141 void game_input_shoot(int bn)
142 {
143 }
145 void game_input_move(float x, float y, float z)
146 {
147 cam.input_move(x, y, z);
148 }
150 void game_input_rot(float x, float y)
151 {
152 cam.input_rotate(x * 6.0, y * 6.0, 0);
153 }
156 static void view_matrix(int eye)
157 {
158 float offs = stereo_eye_sep * eye * 0.5;
159 glTranslatef(-offs, 0, 0);
160 cam.use();
161 }
163 static void proj_matrix(int eye)
164 {
165 static const float fov = M_PI / 4.0;
166 static const float near_clip = 0.1;
167 static const float far_clip = 500.0;
169 float top = near_clip * tan(fov * 0.5);
170 float right = top * (float)win_xsz / (float)win_ysz;
172 float frust_shift = -(float)eye * (stereo_eye_sep * 0.5 * near_clip / stereo_focus_dist);
173 glFrustum(-right + frust_shift, right + frust_shift, -top, top, near_clip, far_clip);
174 }