doing stuff
[ld42_outofspace] / src / gamescr.cc
index 0ae1672..26afe42 100644 (file)
+#include <vector>
+#include <gmath/gmath.h>
 #include "game.h"
 #include "screen.h"
 #include "opengl.h"
+#include "texture.h"
+
+/* NOTES:
+ * - whistle hhgg music
+ * - colliding particles merge
+ * - select objects and center camera on them
+ */
+
+struct Particle {
+       float mass;
+       Vec2 pos;
+       Vec2 vel;
+};
+
+struct Emitter {
+       Vec2 pos;
+       float mass;
+       float rate, chunk;
+       float angle, spread;
+};
+
+
+#define SIM_DT         0.016
+
+#define GRID_SIZE      4096
+#define GRID_BITS      12
+#define GRID_X(idx)    (((idx) >> GRID_BITS) & (GRID_SIZE - 1))
+#define GRID_Y(idx)    ((idx) & (GRID_SIZE - 1))
+
+#define FIELD_SIZE     2048
+#define MIN_CAM_DIST   1.0f
+#define MAX_CAM_DIST   350.0f
+
+static int pos_to_grid(float x, float y);
+
+static float grid[GRID_SIZE * GRID_SIZE];
+static Particle grid_part[GRID_SIZE * GRID_SIZE];
+
+static std::vector<Emitter> emitters;
+
+static Texture *grid_tex;
+
+static float cam_theta;
+static float cam_dist = 100.0f;
+static Vec2 *targ_pos;
+static Mat4 view_matrix, proj_matrix;
+
 
 bool GameScreen::init()
 {
+       grid_tex = new Texture;
+       if(!grid_tex->load("data/purple_grid.png")) {
+               return false;
+       }
+       grid_tex->set_anisotropy(glcaps.max_aniso);
+
        return true;
 }
 
 void GameScreen::destroy()
 {
+       delete grid_tex;
+}
+
+static void simstep()
+{
 }
 
+static void update()
+{
+       static float interval;
+
+       interval += frame_dt;
+       if(interval >= SIM_DT) {
+               interval -= SIM_DT;
+               simstep();
+       }
+
+       // update projection matrix
+       proj_matrix.perspective(deg_to_rad(60.0f), win_aspect, 0.5, 5000.0);
+
+       // update view matrix
+       Vec3 targ;
+       if(targ_pos) {
+               targ.x = targ_pos->x;
+               targ.z = targ_pos->y;
+       }
+
+       float theta = -deg_to_rad(cam_theta);
+       Vec3 camdir = Vec3(sin(theta) * cam_dist, pow(cam_dist * 0.1, 2.0) + 0.5, cos(theta) * cam_dist);
+       Vec3 campos = targ + camdir;
+
+       view_matrix.inv_lookat(campos, targ, Vec3(0, 1, 0));
+}
 
 void GameScreen::draw()
 {
-       glClearColor(1, 0, 0, 1);
+       update();
+
+       glClearColor(0.01, 0.01, 0.01, 1);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+
+       glMatrixMode(GL_PROJECTION);
+       glLoadMatrixf(proj_matrix[0]);
+       glMatrixMode(GL_MODELVIEW);
+       glLoadMatrixf(view_matrix[0]);
+
+       glPushAttrib(GL_ENABLE_BIT);
+       glDisable(GL_LIGHTING);
+       glDisable(GL_CULL_FACE);
+
+       glEnable(GL_TEXTURE_2D);
+       bind_texture(grid_tex);
+
+       float maxu = FIELD_SIZE / 32.0f;
+       float maxv = FIELD_SIZE / 32.0f;
+       float hsz = FIELD_SIZE * 0.5f;
+
+       glBegin(GL_QUADS);
+       glColor3f(1, 1, 1);
+       glTexCoord2f(0, 0);
+       glVertex3f(-hsz, 0, -hsz);
+       glTexCoord2f(maxu, 0);
+       glVertex3f(hsz, 0, -hsz);
+       glTexCoord2f(maxu, maxv);
+       glVertex3f(hsz, 0, hsz);
+       glTexCoord2f(0, maxv);
+       glVertex3f(-hsz, 0, hsz);
+       glEnd();
+
+       glPopAttrib();
+}
+
+void GameScreen::reshape(int x, int y)
+{
 }
 
 
+
 void GameScreen::keyboard(int key, bool pressed)
 {
        if(pressed) {
@@ -33,14 +156,29 @@ void GameScreen::keyboard(int key, bool pressed)
        }
 }
 
+static int prev_x, prev_y;
+
 void GameScreen::mbutton(int bn, bool pressed, int x, int y)
 {
+       prev_x = x;
+       prev_y = y;
 }
 
 void GameScreen::mmotion(int x, int y)
 {
+       int dx = x - prev_x;
+       int dy = y - prev_y;
+       prev_x = x;
+       prev_y = y;
+
+       if(game_bnstate(2)) {
+               cam_theta += dx * 0.5;
+       }
 }
 
 void GameScreen::mwheel(int dir, int x, int y)
 {
+       cam_dist -= dir * cam_dist * 0.05f;
+       if(cam_dist <= MIN_CAM_DIST) cam_dist = MIN_CAM_DIST;
+       if(cam_dist > MAX_CAM_DIST) cam_dist = MAX_CAM_DIST;
 }