bloboland

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