Test Implemetation of Flood Fill.
This commit is contained in:
@@ -20,6 +20,24 @@ Vector2i EditorApp::IndexToCell(int index, int width)
|
||||
return {x, y};
|
||||
}
|
||||
|
||||
|
||||
void EditorApp::FloodFill(int x, int y, int tileid)
|
||||
{
|
||||
|
||||
if (GetTile(x, y) == -1)
|
||||
{
|
||||
SetTile(x, y, tileid);
|
||||
if (GetTile(x-1, y) == -1) FloodFill(x-1, y, tileid);
|
||||
if (GetTile(x+1, y) == -1) FloodFill(x+1, y, tileid);
|
||||
if (GetTile(x, y-1) == -1) FloodFill(x, y-1, tileid);
|
||||
if (GetTile(x, y+1) == -1) FloodFill(x, y+1, tileid);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
int EditorApp::CellToIndex(Vector2i cell, int width)
|
||||
{
|
||||
return cell.y*width + cell.x;
|
||||
@@ -400,6 +418,23 @@ void EditorApp::CameraUpdate(float elapsed)
|
||||
|
||||
}
|
||||
|
||||
int EditorApp::GetTile(const Vector2i& cell)
|
||||
{
|
||||
if (cell.x < 0) return -2;
|
||||
if (cell.y < 0) return -2;
|
||||
|
||||
// out of bounds horizontally
|
||||
if (cell.x > grid_rows) return -2;
|
||||
|
||||
// out of bounds vertically
|
||||
if (cell.y > grid_cols) return -2;
|
||||
|
||||
return grid[cell.x][cell.y];
|
||||
}
|
||||
|
||||
int EditorApp::GetTile(int x, int y) { return GetTile({x, y}); }
|
||||
void EditorApp::SetTile(int x, int y, int tileID) { SetTile({x, y}, tileID);}
|
||||
|
||||
void EditorApp::SetTile(const Vector2i& cell, int tileID)
|
||||
{
|
||||
|
||||
@@ -418,6 +453,7 @@ void EditorApp::SetTile(const Vector2i& cell, int tileID)
|
||||
|
||||
void EditorApp::PlaceTileBrush(const Vector2i& cell)
|
||||
{
|
||||
|
||||
if (cell.x < 0) return;
|
||||
|
||||
if (cell.y < 0) return;
|
||||
@@ -428,7 +464,11 @@ void EditorApp::PlaceTileBrush(const Vector2i& cell)
|
||||
// out of bounds vertically
|
||||
if (cell.y > grid_cols) return;
|
||||
|
||||
SetTile(cell, selected_quad);
|
||||
|
||||
if (IsKeyDown(Keys::B))
|
||||
FloodFill(cell.x, cell.y, selected_quad);
|
||||
else
|
||||
SetTile(cell, selected_quad);
|
||||
}
|
||||
|
||||
void EditorApp::EraseTile(const Vector2i& cell)
|
||||
|
1
src/Layer.cpp
Normal file
1
src/Layer.cpp
Normal file
@@ -0,0 +1 @@
|
||||
#include <Layer.hpp>
|
@@ -1,7 +1,13 @@
|
||||
#include <Tileset.hpp>
|
||||
|
||||
Tileset::Tileset():
|
||||
tile_width(0),tile_height(0), tile_spacing(0),
|
||||
tex_width(0), tex_height(0), rows(0), cols(0),
|
||||
tile_count(0), tiles_per_row(0)
|
||||
{ }
|
||||
|
||||
Tileset::Tileset(const std::string& name, const std::filesystem::path& texture_path, int tex_width, int tex_height, int tile_width, int tile_height,
|
||||
int tile_spacing)
|
||||
int tile_spacing)
|
||||
{
|
||||
this->name = name;
|
||||
this->file_path = std::format("{}.tileset", name);
|
||||
@@ -60,11 +66,15 @@ void Tileset::Deserialize(const json::value& json)
|
||||
this->tex_width = (int)json["texture-width"].number.value();
|
||||
this->tex_height = (int)json["texture-height"].number.value();
|
||||
|
||||
/*auto tilemeta = json["tiledata"].as_array();
|
||||
for (json::value& v : tilemeta)
|
||||
if (json.as_object().contains("tiles"))
|
||||
{
|
||||
this->tile_metadata.push_back(v);
|
||||
}*/
|
||||
auto tilemeta = json["tiles"].as_array();
|
||||
for (json::value& v : tilemeta)
|
||||
{
|
||||
// this->tiles.push_back(v);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
this->rows = J3ML::Math::Floor(this->tex_width / this->tile_width);
|
||||
this->cols = J3ML::Math::Floor(this->tex_height / this->tile_height);
|
||||
@@ -104,7 +114,7 @@ json::value Tileset::Serialize() const
|
||||
|
||||
tileset["quads"] = quad_array;*/
|
||||
|
||||
/*json::array tiles;
|
||||
json::array tiles;
|
||||
for (auto& tile : this->tiles)
|
||||
{
|
||||
json::object tile_json;
|
||||
@@ -119,7 +129,7 @@ json::value Tileset::Serialize() const
|
||||
tile_json["quad"] = quad;
|
||||
tiles.push_back(tile_json);
|
||||
}
|
||||
tileset["tiles"] = tiles;*/
|
||||
tileset["tiles"] = tiles;
|
||||
|
||||
return tileset;
|
||||
}
|
||||
|
Reference in New Issue
Block a user