X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=ld42_outofspace;a=blobdiff_plain;f=src%2Fgamescr.cc;h=3eec73850974095685e50b26589ba70b2255afa4;hp=abd78651324e3b97077cc1630fea0373541cb0a7;hb=8ac9e0e139b1f523c333ed2c33b81291f54def09;hpb=31858c81e045e619d62d015058ad46cc2376c7b5 diff --git a/src/gamescr.cc b/src/gamescr.cc index abd7865..3eec738 100644 --- a/src/gamescr.cc +++ b/src/gamescr.cc @@ -11,9 +11,7 @@ /* NOTES: * - whistle hhgg music - * - colliding particles merge * - select objects and center camera on them - * - tesselate only where necessary */ struct Particle { @@ -68,6 +66,11 @@ 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); @@ -86,6 +89,8 @@ 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; @@ -104,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; @@ -172,16 +180,30 @@ 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); + 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; @@ -291,7 +313,7 @@ static void update() em->pos = emit_place_pos; em->mass = 100; em->rate = 10; - em->chunk = 0.01 * em->mass; + em->chunk = 0.001 * em->mass; em->angle = -1; em->spread = 0; em->spawn_pending = 0; @@ -340,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); @@ -352,6 +380,10 @@ 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); @@ -371,8 +403,6 @@ void GameScreen::draw() glUseProgram(0); - glPopAttrib(); - assert(glGetError() == GL_NO_ERROR); } @@ -423,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; } @@ -444,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; } @@ -456,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; @@ -610,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;