panning
[ld42_outofspace] / src / gamescr.cc
index d0fae08..3eec738 100644 (file)
@@ -11,9 +11,7 @@
 
 /* NOTES:
  * - whistle hhgg music
- * - colliding particles merge
  * - select objects and center camera on them
- * - tesselate only where necessary
  */
 
 struct Particle {
@@ -22,6 +20,8 @@ struct Particle {
        Vec2 pos;
        Vec2 vel;
 
+       float vis_height;
+
        Particle *next, *prev;
 };
 
@@ -66,35 +66,42 @@ struct QuadMesh {
 #define CONTRIB_THRES  0.005
 #define CONTRIB_RANGE(m) sqrt((m) / CONTRIB_THRES)
 
+/* gravitational strength */
+#define GRAV_STR       16.0f
+
+static int pos_to_grid_x_noclamp(float x);
+static int pos_to_grid_y_noclamp(float y);
 static int pos_to_grid(float x, float y);
 static Vec2 grid_to_pos(int gx, int gy);
 
 static void calc_contrib_bounds(const Vec2 &pos, float mass, Rect *rect);
 static void add_influence(const Vec2 &pos, float mass, float radius, const Rect &cbox);
 
+static Vec2 calc_field_grad(int gidx);
+
 static void destroy_quadmesh(QuadMesh *m);
 static void draw_quadmesh(const QuadMesh *m, unsigned int prim = GL_QUADS);
 static void gen_quad_plane(QuadMesh *mesh, float width, float height, int usub, int vsub);
 
 static void spawn_particle(const Vec2 &pos, const Vec2 &vel, float mass);
-static void add_particle(Particle *p, int grid_idx);
-static void remove_particle(Particle *p, int grid_idx);
+static void add_particle(Particle *p);
+static void remove_particle(Particle *p);
 static Particle *alloc_particle();
 void free_particle(Particle *p);
 
+static bool pause;
+
 static float grid[GRID_SIZE * GRID_SIZE];
 static Particle *grid_part[GRID_SIZE * GRID_SIZE];
 static Texture *grid_tex;
 
-#define NUM_PARTICLE_LISTS     16
-static Particle *plist[NUM_PARTICLE_LISTS];
-#define GRID_IDX_PLIST(idx)    ((idx) * NUM_PARTICLE_LISTS / (GRID_SIZE * GRID_SIZE))
+static Particle *plist;
 
 static std::vector<Emitter*> emitters;
 
 static Texture *gvis_tex;      // texture tile for visualizing a grid
 static unsigned int field_sdr;
-static int tess_level = 32;
+static int tess_level = 64;
 static float field_scale = 16.0f;
 
 static QuadMesh field_mesh;
@@ -102,11 +109,14 @@ static QuadMesh field_mesh;
 static Mesh *pmesh;
 static unsigned int particle_sdr;
 
+static Vec2 cam_pos;
 static float cam_theta;
 static float cam_dist = 100.0f;
-static Vec2 *targ_pos;
+static Vec2 *targ_pos = &cam_pos;
 static Mat4 view_matrix, proj_matrix;
 
+static bool wireframe;
+
 // emitter placement data (filled by event handlers, completed in update)
 static bool emit_place_pending;
 static Vec2 emit_place_pos;
@@ -170,6 +180,66 @@ void GameScreen::destroy()
 
 static void simstep()
 {
+       if(pause) return;
+
+       // move existing particles
+       Particle *p = plist;
+       while(p) {
+               // calculate the field gradient at the particle position
+               int gidx = pos_to_grid(p->pos.x, p->pos.y);
+               Vec2 grad = calc_field_grad(gidx) * GRAV_STR;
+
+               p->vel += grad * SIM_DT;
+               p->pos += p->vel * SIM_DT;
+
+               // if it moved outside of the simulation field, remove it
+               int gx = pos_to_grid_x_noclamp(p->pos.x);
+               int gy = pos_to_grid_y_noclamp(p->pos.y);
+               if(gx < 0 || gx >= GRID_SIZE || gy < 0 || gy >= GRID_SIZE) {
+                       Particle *next = p->next;
+                       grid_part[gidx] = 0;
+                       remove_particle(p);
+                       free_particle(p);
+                       p = next;
+                       continue;
+               }
+
+               // find the grid cell it's moving to
+               int gidx_next = pos_to_grid(p->pos.x, p->pos.y);
+               p->vis_height = 0.0f;//-grid[gidx_next] * field_scale;
+
+               if(gidx_next == gidx) {
+                       p = p->next;
+                       continue;
+               }
+
+               Particle *destp = grid_part[gidx_next];
+               if(destp) {
+                       assert(destp != p);
+                       // another particle at the destination, merge them
+                       destp->vel += p->vel;
+                       destp->mass += p->mass;
+                       destp->radius = MASS_TO_RADIUS(destp->mass);
+
+                       // ... and remove it
+                       grid_part[gidx] = 0;
+                       Particle *next = p->next;
+                       remove_particle(p);
+                       free_particle(p);
+                       p = next;
+               } else {
+                       // destination is empty, go there
+                       if(gidx != gidx_next) {
+                               grid_part[gidx] = 0;
+                               grid_part[gidx_next] = p;
+                       }
+
+                       p = p->next;
+               }
+       }
+
+       // TODO destroy particles which left the simulation field
+
        // spawn particles
        int num_emitters = emitters.size();
        for(int i=0; i<num_emitters; i++) {
@@ -205,7 +275,6 @@ static void simstep()
        }
 
        // calculate gravitational field - assume field within radius constant: m / r^2
-
        // first clear the field, and then add contributions
        memset(grid, 0, sizeof grid);
 
@@ -220,14 +289,12 @@ static void simstep()
        }
 
        // contribution of particles
-       for(int i=0; i<NUM_PARTICLE_LISTS; i++) {
-               Particle *p = plist[i];
-               while(p) {
-                       Rect cbox;
-                       calc_contrib_bounds(p->pos, p->mass, &cbox);
-                       add_influence(p->pos, p->mass, p->radius, cbox);
-                       p = p->next;
-               }
+       p = plist;
+       while(p) {
+               Rect cbox;
+               calc_contrib_bounds(p->pos, p->mass, &cbox);
+               add_influence(p->pos, p->mass, p->radius, cbox);
+               p = p->next;
        }
 
        // update texture
@@ -245,8 +312,8 @@ static void update()
                Emitter *em = new Emitter;
                em->pos = emit_place_pos;
                em->mass = 100;
-               em->rate = 1;
-               em->chunk = 0.05 * em->mass;
+               em->rate = 10;
+               em->chunk = 0.001 * em->mass;
                em->angle = -1;
                em->spread = 0;
                em->spawn_pending = 0;
@@ -295,11 +362,17 @@ void GameScreen::draw()
        glMatrixMode(GL_MODELVIEW);
        glLoadMatrixf(view_matrix[0]);
 
-       glPushAttrib(GL_ENABLE_BIT);
-       glDisable(GL_CULL_FACE);
-
        // draw gravitational field
-       glEnable(GL_TEXTURE_2D);
+       if(wireframe) {
+               glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
+
+               float amb[] = {0.5, 0.5, 0.5, 1.0};
+               glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb);
+       } else {
+               float amb[] = {0.01, 0.01, 0.01, 1.0};
+               glLightModelfv(GL_LIGHT_MODEL_AMBIENT, amb);
+       }
+
        bind_texture(gvis_tex, 0);
        bind_texture(grid_tex, 1);
 
@@ -307,29 +380,29 @@ void GameScreen::draw()
        glPatchParameteri(GL_PATCH_VERTICES, 4);
        draw_quadmesh(&field_mesh, GL_PATCHES);
 
+       if(wireframe) {
+               glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
+       }
+
        // draw particles
        glUseProgram(particle_sdr);
 
-       for(int i=0; i<NUM_PARTICLE_LISTS; i++) {
-               Particle *p = plist[i];
-               while(p) {
-                       int gidx = pos_to_grid(p->pos.x, p->pos.y);
+       Particle *p = plist;
+       while(p) {
+               int gidx = pos_to_grid(p->pos.x, p->pos.y);
 
-                       glPushMatrix();
-                       glTranslatef(p->pos.x, -grid[gidx] * field_scale, p->pos.y);
-                       glScalef(p->radius, p->radius, p->radius);
+               glPushMatrix();
+               glTranslatef(p->pos.x, p->vis_height, p->pos.y);
+               glScalef(p->radius, p->radius, p->radius);
 
-                       pmesh->draw();
+               pmesh->draw();
 
-                       glPopMatrix();
-                       p = p->next;
-               }
+               glPopMatrix();
+               p = p->next;
        }
 
        glUseProgram(0);
 
-       glPopAttrib();
-
        assert(glGetError() == GL_NO_ERROR);
 }
 
@@ -380,6 +453,14 @@ void GameScreen::keyboard(int key, bool pressed)
                        set_uniform_float(field_sdr, "field_scale", field_scale);
                        break;
 
+               case 'w':
+                       wireframe = !wireframe;
+                       break;
+
+               case ' ':
+                       pause = !pause;
+                       break;
+
                default:
                        break;
                }
@@ -401,6 +482,13 @@ void GameScreen::mmotion(int x, int y)
        prev_x = x;
        prev_y = y;
 
+       if(game_bnstate(0)) {
+               float pan_speed = pow(cam_dist, 1.5) * 0.00035; // magic
+               Vec2 dir = rotate(Vec2(dx, dy) * pan_speed, deg_to_rad(cam_theta));
+               cam_pos.x -= dir.x;
+               cam_pos.y -= dir.y;
+       }
+
        if(game_bnstate(2)) {
                cam_theta += dx * 0.5;
        }
@@ -413,11 +501,20 @@ void GameScreen::mwheel(int dir, int x, int y)
        if(cam_dist > MAX_CAM_DIST) cam_dist = MAX_CAM_DIST;
 }
 
+static int pos_to_grid_x_noclamp(float x)
+{
+       return ((x / (float)FIELD_SIZE) + 0.5f) * (float)GRID_SIZE;
+}
+
+static int pos_to_grid_y_noclamp(float y)
+{
+       return ((y / (float)FIELD_SIZE) + 0.5f) * (float)GRID_SIZE;
+}
 
 static int pos_to_grid(float x, float y)
 {
-       int gx = ((x / (float)FIELD_SIZE) + 0.5f) * (float)GRID_SIZE;
-       int gy = ((y / (float)FIELD_SIZE) + 0.5f) * (float)GRID_SIZE;
+       int gx = pos_to_grid_x_noclamp(x);
+       int gy = pos_to_grid_y_noclamp(y);
 
        if(gx < 0) gx = 0;
        if(gx >= GRID_SIZE) gx = GRID_SIZE - 1;
@@ -480,6 +577,24 @@ static void add_influence(const Vec2 &pos, float mass, float radius, const Rect
        }
 }
 
+static Vec2 calc_field_grad(int gidx)
+{
+       int gx = GRID_X(gidx);
+       int gy = GRID_Y(gidx);
+
+       int nidx = ((gx + 1 >= GRID_SIZE ? gx : gx + 1) << GRID_BITS) | gy;
+       int pidx = ((gx > 0 ? gx - 1 : gx) << GRID_BITS) | gy;
+       float dfdx = grid[nidx] - grid[pidx];
+
+       nidx = (gx << GRID_BITS) | (gy + 1 >= GRID_SIZE ? gy : gy + 1);
+       pidx = (gx << GRID_BITS) | (gy > 0 ? gy - 1 : gy);
+       float dfdy = grid[nidx] - grid[pidx];
+
+       return Vec2(dfdx, dfdy);
+}
+
+
+// ---- quad mesh operations ----
 
 static void destroy_quadmesh(QuadMesh *m)
 {
@@ -549,9 +664,9 @@ static void gen_quad_plane(QuadMesh *m, float width, float height, int usub, int
                                int idx = i * vverts + j;
 
                                *iptr++ = idx;
-                               *iptr++ = idx + vverts;
-                               *iptr++ = idx + vverts + 1;
                                *iptr++ = idx + 1;
+                               *iptr++ = idx + vverts + 1;
+                               *iptr++ = idx + vverts;
                        }
 
                        v += dv;
@@ -594,32 +709,34 @@ static void spawn_particle(const Vec2 &pos, const Vec2 &vel, float mass)
                p->radius = MASS_TO_RADIUS(mass);
                grid_part[gidx] = p;
 
-               add_particle(p, gidx);
+               add_particle(p);
        }
 }
 
-static void add_particle(Particle *p, int grid_idx)
+static void add_particle(Particle *p)
 {
-       int listidx = GRID_IDX_PLIST(grid_idx);
-       p->next = plist[listidx];
+       if(plist) plist->prev = p;
+
+       p->next = plist;
        p->prev = 0;
-       plist[listidx] = p;
+       plist = p;
 }
 
-static void remove_particle(Particle *p, int grid_idx)
+static void remove_particle(Particle *p)
 {
-       Particle *prev = p->prev;
-       Particle *next = p->next;
-
-       p->prev = p->next = 0;
-       if(next) next->prev = prev;
+       assert(plist->prev == 0);
 
-       if(prev) {
-               prev->next = next;
-       } else {
-               int listidx = GRID_IDX_PLIST(grid_idx);
-               plist[listidx] = next;
+       if(plist == p) {
+               assert(p->prev == 0);
+               plist = p->next;
+       }
+       if(p->prev) {
+               p->prev->next = p->next;
        }
+       if(p->next) {
+               p->next->prev = p->prev;
+       }
+       p->prev = p->next = 0;
 }
 
 // particle allocator