|
|
|
@ -3,14 +3,29 @@
@@ -3,14 +3,29 @@
|
|
|
|
|
|
|
|
|
|
namespace pengine { |
|
|
|
|
|
|
|
|
|
PixelColour::PixelColour(short r, short b, short g, short a) { |
|
|
|
|
const Uint8 part_colours[][3] = { |
|
|
|
|
{255,255,255}, // BASIC
|
|
|
|
|
{0,0,255}, // SAND
|
|
|
|
|
{252,250,148} // SAND
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
Particle *get_new_particle(ParticleID p) { |
|
|
|
|
switch (p) { |
|
|
|
|
case ParticleID::SAND: |
|
|
|
|
return new Sand(); |
|
|
|
|
case ParticleID::BASIC: |
|
|
|
|
return new BasicParticle(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
PixelColour::PixelColour(short r, short g, short b, short a) { |
|
|
|
|
this->r = r; |
|
|
|
|
this->g = g; |
|
|
|
|
this->b = b; |
|
|
|
|
this->a = a; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
PixelColour::PixelColour(short r, short b, short g) { |
|
|
|
|
PixelColour::PixelColour(short r, short g, short b) { |
|
|
|
|
this->r = r; |
|
|
|
|
this->g = g; |
|
|
|
|
this->b = b; |
|
|
|
@ -24,6 +39,11 @@ namespace pengine {
@@ -24,6 +39,11 @@ namespace pengine {
|
|
|
|
|
this->a = c.a; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
PixelColour::PixelColour(const Uint8 p[3]) { |
|
|
|
|
PixelColour(p[0], p[1], p[2]); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
SDL_Color PixelColour::get_sdlcol() { |
|
|
|
|
SDL_Color c; |
|
|
|
|
c.r = r; |
|
|
|
@ -42,29 +62,66 @@ namespace pengine {
@@ -42,29 +62,66 @@ namespace pengine {
|
|
|
|
|
ysize = ys; |
|
|
|
|
|
|
|
|
|
grid = new Particle *[xs * ys] {};
|
|
|
|
|
pixels = new Uint32[xs * ys] {};
|
|
|
|
|
pixels = new Uint32[xs * ys];
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Grid::~Grid() { |
|
|
|
|
delete grid; |
|
|
|
|
delete pixels; |
|
|
|
|
Grid::~Grid()
|
|
|
|
|
{ |
|
|
|
|
for (int i = 0; i < xsize * ysize; i++) { |
|
|
|
|
if (grid[i]) |
|
|
|
|
delete grid[i]; |
|
|
|
|
} |
|
|
|
|
delete[] grid; |
|
|
|
|
delete[] pixels; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Grid::update() { |
|
|
|
|
void Grid::update()
|
|
|
|
|
{ |
|
|
|
|
static bool updated = false; |
|
|
|
|
|
|
|
|
|
for (int i = 0; i < xsize * ysize; i++) { |
|
|
|
|
if (grid[i]) { |
|
|
|
|
grid[i]->update(); |
|
|
|
|
if (grid[i] != nullptr) { |
|
|
|
|
if (grid[i]->updated != updated) { |
|
|
|
|
grid[i]->updated = updated; |
|
|
|
|
grid[i]->update(this, i % xsize, i / xsize); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
}
|
|
|
|
|
updated = !updated; |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Grid::add_particle_at(int x, int y, ParticleID p)
|
|
|
|
|
{ |
|
|
|
|
if (x >= xsize || y >= ysize || x < 0 || y < 0) { |
|
|
|
|
std::cout << "invalid position " << x << " " << y << std::endl; |
|
|
|
|
throw "invalid boundaries"; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (grid[x + y * xsize]) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
grid[x + y * xsize] = get_new_particle(p); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Particle * Grid::get_particle(int x, int y) { |
|
|
|
|
if (!inside_grid(x,y)) { |
|
|
|
|
return nullptr; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return grid[x + xsize * y]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Grid::update_texture(SDL_Texture *texture) { |
|
|
|
|
PixelColour defaultcol = PixelColour(255,0,255,255); |
|
|
|
|
PixelColour defaultcol = PixelColour(255,0,0,0); |
|
|
|
|
PixelColour defaultcoll = PixelColour(255,255,0,0); |
|
|
|
|
for (int i = 0; i < xsize; i++) { |
|
|
|
|
for (int j = 0; j < ysize; j++) { |
|
|
|
|
int p = j * xsize + i; |
|
|
|
|
if (grid[p]) { |
|
|
|
|
// pixels[i] = grid[i]->get_colour().get_pixelcol();
|
|
|
|
|
pixels[p] = defaultcoll.pixel; |
|
|
|
|
} else { |
|
|
|
|
pixels[p] = defaultcol.get_pixelcol(); |
|
|
|
|
} |
|
|
|
@ -73,6 +130,56 @@ namespace pengine {
@@ -73,6 +130,56 @@ namespace pengine {
|
|
|
|
|
// SDL_Rect r {.x = 0, .y = 0, .w = xsize, .h = ysize};
|
|
|
|
|
SDL_UpdateTexture(texture, NULL, pixels, xsize * sizeof(Uint32)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool Grid::inside_grid(int x, int y) { |
|
|
|
|
if (x >= xsize - 1 || y >= ysize - 1 || x < 1 || y < 1) { |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
bool Grid::move_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)) { |
|
|
|
|
grid[xb + yb * xsize] = grid[xa + ya * xsize]; |
|
|
|
|
grid[xa + ya * xsize] = nullptr; |
|
|
|
|
return true; |
|
|
|
|
}
|
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void Sand::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; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
BasicParticle::BasicParticle() : Particle(ParticleID::BASIC) {} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void BasicParticle::update(Grid * const g, int x, int y) { |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
PixelColour BasicParticle::get_colour() { |
|
|
|
|
return PixelColour {part_colours[1]}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|