bloboland

view src/game.cc @ 5:2f4406cc341e

meh
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 19 Dec 2012 02:37:20 +0200
parents 9021a906c5d3
children
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;
25 #define DAMPING 0.95
27 bool game_init()
28 {
29 printf("initializing OpenGL state\n");
30 glEnable(GL_CULL_FACE);
31 glEnable(GL_DEPTH_TEST);
32 glEnable(GL_LIGHTING);
33 glEnable(GL_LIGHT0);
35 float lpos[] = {-1, 1, 1, 0};
36 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
38 printf("generating level\n");
39 level = new Level;
40 level->world_size = Vector3(8, 8, 4);
41 level->generate();
43 printf("initializing renderer\n");
44 rend = new Renderer;
45 if(!rend->init(level)) {
46 return false;
47 }
49 cam.input_move(0, 2, 2);
50 cam.input_rotate(0, M_PI / 5, 0);
52 glClearColor(fog_color[0], fog_color[1], fog_color[2], 1.0);
53 glFogfv(GL_FOG_COLOR, fog_color);
55 gravity = Vector3(0, -0.5, 0);
57 return true;
58 }
60 void game_shutdown()
61 {
62 delete rend;
63 delete level;
64 }
66 void game_iter(double dt)
67 {
68 float offs = 4.0 * dt;
69 float dx = 0, dy = 0;
71 // handle key input
72 if(keystate['w'] || keystate['W']) {
73 dy -= offs;
74 }
75 if(keystate['s'] || keystate['S']) {
76 dy += offs;
77 }
78 if(keystate['d'] || keystate['D']) {
79 dx += offs;
80 }
81 if(keystate['a'] || keystate['A']) {
82 dx -= offs;
83 }
85 cam.input_move(dx, 0, dy);
87 for(size_t i=0; i<level->blobs.size(); i++) {
88 Blob *b = &level->blobs[i];
90 b->velocity += gravity * dt;
91 Vector3 npos = b->pos + b->velocity * dt;
93 Vector3 normal;
94 if(level->collision(b->pos, npos, &npos, &normal)) {
95 printf("%d: COLLISION (%.2f %.2f %.2f)\n", (int)i, npos.x, npos.y, npos.z);
96 b->velocity = b->velocity.reflection(normal) * DAMPING;
97 }
98 b->pos = npos;
99 }
100 rend->prepare();
101 }
103 void game_render()
104 {
105 rend->set_aspect((float)win_xsz / (float)win_ysz);
107 if(opt.stereo) {
108 glDrawBuffer(GL_BACK_LEFT);
109 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
111 glMatrixMode(GL_PROJECTION);
112 glLoadIdentity();
113 proj_matrix(-1);
114 glMatrixMode(GL_MODELVIEW);
115 glLoadIdentity();
116 view_matrix(-1);
118 rend->render();
120 glDrawBuffer(GL_BACK_RIGHT);
121 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
123 glMatrixMode(GL_PROJECTION);
124 glLoadIdentity();
125 proj_matrix(1);
126 glMatrixMode(GL_MODELVIEW);
127 glLoadIdentity();
128 view_matrix(1);
130 rend->render();
131 } else {
132 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
134 glMatrixMode(GL_PROJECTION);
135 glLoadIdentity();
136 proj_matrix(0);
137 glMatrixMode(GL_MODELVIEW);
138 glLoadIdentity();
139 view_matrix(0);
141 rend->render();
142 }
143 }
145 void game_input_shoot(int bn)
146 {
147 }
149 void game_input_move(float x, float y, float z)
150 {
151 cam.input_move(x, y, z);
152 }
154 void game_input_rot(float x, float y)
155 {
156 cam.input_rotate(x * 6.0, y * 6.0, 0);
157 }
160 static void view_matrix(int eye)
161 {
162 float offs = stereo_eye_sep * eye * 0.5;
163 glTranslatef(-offs, 0, 0);
164 cam.use();
165 }
167 static void proj_matrix(int eye)
168 {
169 static const float fov = M_PI / 4.0;
170 static const float near_clip = 0.1;
171 static const float far_clip = 500.0;
173 float top = near_clip * tan(fov * 0.5);
174 float right = top * (float)win_xsz / (float)win_ysz;
176 float frust_shift = -(float)eye * (stereo_eye_sep * 0.5 * near_clip / stereo_focus_dist);
177 glFrustum(-right + frust_shift, right + frust_shift, -top, top, near_clip, far_clip);
178 }