Browse Source

big tool

master
alistair 3 years ago
parent
commit
e6b1ce33a6
  1. 71
      game.cpp
  2. 117
      particles.cpp
  3. 6
      particles.h

71
game.cpp

@ -51,6 +51,7 @@ class PaintTool { @@ -51,6 +51,7 @@ class PaintTool {
bool paused = false;
};
class Game {
GameWindow win;
Grid grid;
@ -58,6 +59,7 @@ class Game { @@ -58,6 +59,7 @@ class Game {
SDL_Renderer *ren;
int internal_sz[2];
int window_sz[2];
struct su_pair {int x; int y;} coord;
std::vector<Particle *>particles = std::vector<Particle *>();
PaintTool tool = PaintTool();
@ -97,18 +99,65 @@ class Game { @@ -97,18 +99,65 @@ class Game {
grid.update();
}
void draw_tool() {
if (tool.size == 1) {
int i = coord.x;
int j = coord.y;
if (grid.inside_grid(i, j)) {
auto colour = PixelColour(grid.pixels[i + j * grid.xsize]);
colour.r = 255 - colour.r;
colour.g = 255 - colour.g;
colour.b = 255 - colour.b;
grid.pixels[i + j * grid.xsize] = colour.get_pixelcol();
}
return;
}
for (int i = coord.x - tool.size / 2; i < coord.x + tool.size / 2; i++) {
for (int j = coord.y - tool.size / 2; j < coord.y + tool.size / 2; j++) {
if (grid.inside_grid(i, j)) {
auto colour = PixelColour(grid.pixels[i + j * grid.xsize]);
colour.r = 255 - colour.r;
colour.g = 255 - colour.g;
colour.b = 255 - colour.b;
grid.pixels[i + j * grid.xsize] = colour.get_pixelcol();
}
}
}
}
void add_particles() {
if (tool.size <= 1) {
if (grid.inside_grid(coord.x, coord.y)) {
grid.add_particle_at(coord.x, coord.y,
particles[tool.particle_selected]->id);
}
return;
}
for (int i = coord.x - tool.size / 2; i < coord.x + tool.size / 2; i++) {
for (int j = coord.y - tool.size / 2; j < coord.y + tool.size / 2; j++) {
if (grid.inside_grid(i, j)) {
grid.add_particle_at(i, j, particles[tool.particle_selected]->id);
}
}
}
}
void draw() {
SDL_SetRenderDrawColor(ren, 40, 40, 40, 255);
SDL_RenderClear(ren);
grid.update_texture(grid_texture);
grid.update_texture();
draw_tool();
SDL_UpdateTexture(grid_texture, NULL, grid.pixels, grid.xsize * sizeof(Uint32));
SDL_RenderCopy(ren, grid_texture, NULL, NULL);
SDL_RenderPresent(ren);
}
struct su_pair {
int x;
int y;
};
struct su_pair get_click_location_in_grid(int x, int y) {
struct su_pair p;
@ -118,7 +167,6 @@ class Game { @@ -118,7 +167,6 @@ class Game {
}
void run() {
struct su_pair coord;
while (true) {
SDL_Event event;
while(SDL_PollEvent(&event)) {
@ -144,23 +192,26 @@ class Game { @@ -144,23 +192,26 @@ class Game {
tool.particle_selected = 3;
};
break;
case SDL_MOUSEMOTION:
coord = get_click_location_in_grid(event.motion.x, event.motion.y);
break;
case SDL_KEYUP:
break;
case SDL_MOUSEBUTTONDOWN:
tool.mouse_state = 1;
break;
case SDL_MOUSEMOTION:
break;
case SDL_MOUSEBUTTONUP:
tool.mouse_state = 0;
break;
case SDL_MOUSEWHEEL:
tool.size += event.wheel.y;
break;
}
}
if (tool.mouse_state) {
coord = get_click_location_in_grid(event.motion.x, event.motion.y);
grid.add_particle_at(coord.x, coord.y, particles[tool.particle_selected]->id);
add_particles();
}
if (!tool.paused) {

117
particles.cpp

@ -23,28 +23,50 @@ namespace pengine { @@ -23,28 +23,50 @@ namespace pengine {
"Rock"
};
Particle *get_new_particle(ParticleID p) {
switch (p) {
case ParticleID::SAND:
return new Sand();
struct particle_data BasicParticle::default_data(ParticleID id)
{
struct particle_data datap;
switch (id) {
case ParticleID::WATER:
return new Water();
case ParticleID::BASIC:
return new BasicParticle();
case ParticleID::STEAM:
return new Steam();
datap = {.density=10,
.liquid = true,
.temperature=20,
.boiling_point = 100,
.gas_state = ParticleID::STEAM
};
break;
case ParticleID::LAVA:
return new Lava();
datap = {.density=20, .liquid = true, .temperature=5000,
.boiling_point = 10000,.freezing_point=200, .solid_state=ParticleID::ROCK};
break;
case ParticleID::STEAM:
datap = {.density=0, .liquid = false, .temperature=150,
.liquid_state = ParticleID::WATER};
break;
case ParticleID::SAND:
datap = {.density=100, .liquid = false, .temperature=20};
break;
case ParticleID::ROCK:
return new Rock();
datap = {.density=150, .liquid = false, .temperature=20,
.melting_point = 700, .liquid_state=ParticleID::LAVA};
break;
default:
std::cout << "Unknown particle create\n";
throw "unknown particle";
datap = {.temperature = 0};
break;
}
return datap;
}
State BasicParticle::get_state() {
BasicParticle::BasicParticle(ParticleID id) : Particle(id)
{
this->id = id;
data = default_data(id);
};
State
BasicParticle::get_state()
{
switch(id) {
case (ParticleID::STEAM):
return State::GAS;
@ -61,6 +83,30 @@ namespace pengine { @@ -61,6 +83,30 @@ namespace pengine {
}
}
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();
case ParticleID::STEAM:
return new Steam();
case ParticleID::LAVA:
return new Lava();
case ParticleID::ROCK:
return new Rock();
default:
std::cout << "Unknown particle create\n";
throw "unknown particle";
}
}
PixelColour::PixelColour(short r, short g, short b, short a) {
this->r = r;
this->g = g;
@ -68,6 +114,10 @@ namespace pengine { @@ -68,6 +114,10 @@ namespace pengine {
this->a = a;
}
PixelColour::PixelColour(Uint32 c) {
pixel = c;
}
PixelColour::PixelColour(short r, short g, short b) {
this->r = r;
this->g = g;
@ -170,7 +220,7 @@ namespace pengine { @@ -170,7 +220,7 @@ namespace pengine {
return grid[x + xsize * y];
}
void Grid::update_texture(SDL_Texture *texture) {
void Grid::update_texture() {
PixelColour defaultcol = PixelColour(0,0,0,0);
for (int i = 0; i < xsize; i++) {
for (int j = 0; j < ysize; j++) {
@ -183,8 +233,6 @@ namespace pengine { @@ -183,8 +233,6 @@ 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) {
@ -311,39 +359,6 @@ namespace pengine { @@ -311,39 +359,6 @@ namespace pengine {
BasicParticle::BasicParticle() : Particle(ParticleID::BASIC) {}
struct particle_data BasicParticle::default_data(ParticleID id) {
struct particle_data datap;
switch (id) {
case ParticleID::WATER:
datap = {.density=10, .liquid = true, .temperature=20,
.boiling_point = 100, .gas_state = ParticleID::STEAM};
break;
case ParticleID::LAVA:
datap = {.density=20, .liquid = true, .temperature=5000,
.boiling_point = 10000,.freezing_point=200, .solid_state=ParticleID::ROCK};
break;
case ParticleID::STEAM:
datap = {.density=0, .liquid = false, .temperature=150,
.liquid_state = ParticleID::WATER};
break;
case ParticleID::SAND:
datap = {.density=100, .liquid = false, .temperature=20};
break;
case ParticleID::ROCK:
datap = {.density=150, .liquid = false, .temperature=20,
.melting_point = 700, .liquid_state=ParticleID::LAVA};
break;
default:
datap = {.temperature = 0};
break;
}
return datap;
}
BasicParticle::BasicParticle(ParticleID id) : Particle(id) {
this->id = id;
data = default_data(id);
};
void BasicParticle::update(Grid * const g, int x, int y) {
return;

6
particles.h

@ -42,6 +42,7 @@ class PixelColour { @@ -42,6 +42,7 @@ class PixelColour {
PixelColour(short r, short g, short b);
PixelColour(const Uint8[3]);
PixelColour(SDL_Color c);
PixelColour(Uint32 c);
};
enum class State {
@ -66,10 +67,9 @@ class Particle; @@ -66,10 +67,9 @@ class Particle;
class Grid {
private:
public:
Particle **grid;
Uint32 *pixels;
public:
int xsize;
int ysize;
@ -77,7 +77,7 @@ class Grid { @@ -77,7 +77,7 @@ class Grid {
virtual ~Grid();
void update();
void update_texture(SDL_Texture *texture);
void update_texture();
void add_particle_at(int x, int y, ParticleID p);
bool move_particle(int xa, int ya, int xb, int yb);

Loading…
Cancel
Save