Browse Source

density

master
alistair 3 years ago
parent
commit
c69c3f569b
  1. 34
      game.cpp
  2. 104
      particles.cpp
  3. 27
      particles.h

34
game.cpp

@ -42,6 +42,12 @@ class GameWindow { @@ -42,6 +42,12 @@ class GameWindow {
};
class PaintTool {
public:
int particle_selected = 0;
int size = 1;
};
class Game {
GameWindow win;
Grid grid;
@ -50,19 +56,29 @@ class Game { @@ -50,19 +56,29 @@ class Game {
int internal_sz[2];
int window_sz[2];
std::vector<Particle *>particles = std::vector<Particle *>();
PaintTool tool = PaintTool();
public:
Game(int x, int y, int gx, int gy):
win{GameWindow {x,y,"game"}},
grid{Grid{gx, gy}}
{
ren = win.renderer;
/* texture for drawing particles into */
grid_texture = SDL_CreateTexture(ren, SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STATIC, gx,gy);
internal_sz[0] = gx;
internal_sz[1] = gy;
window_sz[0] = x;
window_sz[1] = y;
particles.push_back(new Sand());
particles.push_back(new Water());
}
~Game() {
@ -88,8 +104,8 @@ class Game { @@ -88,8 +104,8 @@ class Game {
struct su_pair get_click_location_in_grid(int x, int y) {
struct su_pair p;
p.x = internal_sz[0] * x/window_sz[0];
p.y = internal_sz[1] * y / window_sz[1];
p.x = internal_sz[0] * x/window_sz[0] % internal_sz[0];
p.y = internal_sz[1] * y / window_sz[1] % internal_sz[1];
return p;
}
@ -102,17 +118,23 @@ class Game { @@ -102,17 +118,23 @@ class Game {
case SDL_QUIT:
return;
case SDL_KEYDOWN:
if (event.key.keysym.scancode == SDL_SCANCODE_1) {
tool.particle_selected = 0;
};
if (event.key.keysym.scancode == SDL_SCANCODE_2) {
tool.particle_selected = 1;
};
break;
case SDL_KEYUP:
break;
case SDL_MOUSEBUTTONDOWN:
{
coord = get_click_location_in_grid(event.button.x, event.button.y);
grid.add_particle_at(coord.x, coord.y, ParticleID::SAND);
grid.add_particle_at(coord.x, coord.y, particles[tool.particle_selected]->id);
break;
}
case SDL_MOUSEMOTION:
coord = get_click_location_in_grid(event.motion.x, event.motion.y);
if (event.motion.state & SDL_BUTTON_LMASK) {
grid.add_particle_at(coord.x, coord.y, ParticleID::SAND);
grid.add_particle_at(coord.x, coord.y, particles[tool.particle_selected]->id);
}
break;
case SDL_MOUSEBUTTONUP:

104
particles.cpp

@ -6,13 +6,21 @@ namespace pengine { @@ -6,13 +6,21 @@ namespace pengine {
Uint8 part_colours[][3] = {
{255,255,255}, // BASIC
{252,250,148}, // SAND
{0,0,255} // SAND
{70,90,255} // WATER
};
std::string part_names[] = {
"default",
"Sand"
"Water",
};
Particle *get_new_particle(ParticleID p) {
switch (p) {
case ParticleID::SAND:
return new Sand();
case ParticleID::WATER:
return new Water();
case ParticleID::BASIC:
return new BasicParticle();
}
@ -59,7 +67,7 @@ namespace pengine { @@ -59,7 +67,7 @@ namespace pengine {
xsize = xs;
ysize = ys;
grid = new Particle *[xs * ys] {};
grid = new Particle *[xs * ys] {0};
pixels = new Uint32[xs * ys];
}
@ -77,14 +85,19 @@ namespace pengine { @@ -77,14 +85,19 @@ namespace pengine {
{
static bool updated = false;
for (int i = 0; i < xsize * ysize; i++) {
if (grid[i] != nullptr) {
if (grid[i]->updated != updated) {
grid[i]->updated = updated;
grid[i]->update(this, i % xsize, i / xsize);
for (int i = 0; i < xsize; i++) {
for (int j = 0; j < ysize; j++) {
int p = j * xsize + i;
if (grid[p] != nullptr) {
if (grid[p]->updated != updated) {
grid[p]->updated = updated;
grid[p]->update(this, i, j);
}
}
}
}
}
updated = !updated;
}
@ -155,16 +168,60 @@ namespace pengine { @@ -155,16 +168,60 @@ namespace pengine {
return false;
}
bool Grid::swap_particle(int xa, int ya, int xb, int yb) {
if (!inside_grid(xb, yb)) {
return false;
}
if (!inside_grid(xa, ya)) {
return false;
}
if ((get_particle(xa,ya) != nullptr) || (get_particle(xb,yb) != nullptr)) {
Particle *temp = grid[xb + yb * xsize];
grid[xb + yb * xsize] = grid[xa + ya * xsize];
grid[xa + ya * xsize] = temp;
return true;
}
return false;
}
void Sand::update(Grid * const g, int x, int y) {
if (g->move_particle(x, y, x, y+1)) {
Particle *p;
p = g->get_particle(x, y+1);
if (p) {
if (p->density < density) {
g->swap_particle(x, y, x, y+1);
return;
}
} else {
g->swap_particle(x, y, x, y+1);
return;
}
if (g->move_particle(x, y, x-1, y+1)) {
p = g->get_particle(x-1, y+1);
if (p) {
if (p->density < density) {
g->swap_particle(x, y, x-1, y+1);
return;
}
} else {
g->swap_particle(x, y, x-1, y+1);
return;
}
if (g->move_particle(x, y, x+1, y+1)) {
}
p = g->get_particle(x+1, y+1);
if (p) {
if (p->density < density) {
g->swap_particle(x, y, x-1, y+1);
return;
}
} else {
g->swap_particle(x, y, x+1, y+1);
return;
}
}
}
BasicParticle::BasicParticle() : Particle(ParticleID::BASIC) {}
@ -178,6 +235,27 @@ namespace pengine { @@ -178,6 +235,27 @@ namespace pengine {
return PixelColour {part_colours[((int)this->id)]};
}
std::string BasicParticle::get_name() {
return part_names[((int)this->id)];
}
void Water::update(Grid * const g, int x, int y) {
if (g->move_particle(x, y, x, y+1)) {
return;
}
if (g->move_particle(x, y, x-1, y+1)) {
return;
}
if (g->move_particle(x, y, x+1, y+1)) {
return;
}
if (g->move_particle(x, y, x+1, y)) {
return;
}
if (g->move_particle(x, y, x-1, y)) {
return;
}
}
}

27
particles.h

@ -10,6 +10,7 @@ @@ -10,6 +10,7 @@
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <vector>
#include <string>
namespace pengine {
@ -17,6 +18,7 @@ enum class ParticleID @@ -17,6 +18,7 @@ enum class ParticleID
{
BASIC = 0,
SAND = 1,
WATER = 2,
};
class PixelColour {
@ -57,6 +59,7 @@ class Grid { @@ -57,6 +59,7 @@ class Grid {
void add_particle_at(int x, int y, ParticleID p);
bool move_particle(int xa, int ya, int xb, int yb);
bool swap_particle(int xa, int ya, int xb, int yb);
bool inside_grid(int x, int y);
Particle *get_particle(int x, int y);
// std::vector<Particle *> get_particles_near(int x, int y);
@ -65,29 +68,47 @@ class Grid { @@ -65,29 +68,47 @@ class Grid {
class Particle
{
public:
bool updated = 0;
double density;
bool updated = false;
enum ParticleID id;
virtual void update(Grid *const, int x, int y) = 0;
virtual PixelColour get_colour() = 0;
Particle(ParticleID id) : id{id} {};
virtual std::string get_name() = 0;
};
class BasicParticle : public Particle
{
public:
double density = 2;
BasicParticle();
BasicParticle(ParticleID id) : Particle(id) {};
void update(Grid *const, int x, int y) override;
PixelColour get_colour() override;
std::string get_name() override;
};
class Sand:public BasicParticle {
public:
/* data */
double density = 2;
void update(Grid *const, int x, int y) override;
Sand() : BasicParticle(ParticleID::SAND) {};
};
class Water:public BasicParticle {
public:
/* data */
double density = 1;
void update(Grid *const, int x, int y) override;
Water() : BasicParticle(ParticleID::WATER) {};
};
}
#endif

Loading…
Cancel
Save