diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/editor.cpp | 23 | ||||
| -rw-r--r-- | src/editor.hpp | 8 | ||||
| -rw-r--r-- | src/level.cpp (renamed from src/floor.cpp) | 19 | ||||
| -rw-r--r-- | src/level.hpp (renamed from src/floor.hpp) | 21 | ||||
| -rw-r--r-- | src/main.cpp | 37 | ||||
| -rw-r--r-- | src/package.cpp | 29 | ||||
| -rw-r--r-- | src/package.hpp | 35 |
7 files changed, 140 insertions, 32 deletions
diff --git a/src/editor.cpp b/src/editor.cpp index f19cf65..74632b7 100644 --- a/src/editor.cpp +++ b/src/editor.cpp @@ -13,6 +13,7 @@ #include "imgui-SFML.h" #include "imgui.h" +#include "level.hpp" #include "nfd.h" #include "nfd.hpp" #include "tileset.hpp" @@ -68,13 +69,17 @@ namespace silly::editor { void Editor::update(sf::RenderWindow &window) { // tile placement - if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) || - sf::Mouse::isButtonPressed(sf::Mouse::Button::Right)) { + if ((sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) || + sf::Mouse::isButtonPressed(sf::Mouse::Button::Right)) && + !this->package.get_levels().empty()) { sf::Vector2f mousePosition = window.mapPixelToCoords(sf::Mouse::getPosition(window)); - for (int x = 0; x < this->floor.get_width(); x++) { - for (int y = 0; y < this->floor.get_height(); y++) { + TileLevel &level = this->package.get_current_level(); + TileFloor &floor = level.get_current_floor(); + + for (int x = 0; x < floor.get_width(); x++) { + for (int y = 0; y < floor.get_height(); y++) { int rx = x * TILE_WIDTH, ry = y * TILE_HEIGHT; if ((rx < mousePosition.x && mousePosition.x < rx + TILE_WIDTH) && @@ -87,9 +92,9 @@ namespace silly::editor { if (this->selectedTile.has_value()) { auto tile = this->selectedTile.value(); if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)) { - this->floor.place_tile(tile, pos, this->rotation); + floor.place_tile(tile, pos, this->rotation); } else if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Right)) { - this->floor.remove_tile(tile->type, pos); + floor.remove_tile(tile->type, pos); } } } @@ -162,8 +167,8 @@ namespace silly::editor { int columns = ImGui::GetContentRegionAvail().x / (imageSize + padding); int count = 0; - for (auto it = this->tileset.get_tiles().begin(); - it != this->tileset.get_tiles().end(); ++it) { + for (auto it = this->package.get_tileset().get_tiles().begin(); + it != this->package.get_tileset().get_tiles().end(); ++it) { if (ImGui::ImageButton(std::to_string(it->get()->id).c_str(), it->get()->texture, sf::Vector2f(imageSize, imageSize))) { @@ -244,7 +249,7 @@ namespace silly::editor { // -- Tile creation -- if (ImGui::Button("Create a new tile")) { - this->tileset.add_tile(state.path, state.type); + this->package.get_tileset().add_tile(state.path, state.type); ImGui::SetWindowCollapsed(true); } diff --git a/src/editor.hpp b/src/editor.hpp index 13f9cac..1e055e6 100644 --- a/src/editor.hpp +++ b/src/editor.hpp @@ -8,7 +8,7 @@ #include <optional> #include <string> -#include "floor.hpp" +#include "package.hpp" #include "tileset.hpp" namespace silly::editor { @@ -20,8 +20,7 @@ namespace silly::editor { class Editor { public: - Editor(Tileset &tileset, TileFloor &floor) - : tileset(tileset), floor(floor) {} + Editor(LevelPackage &package) : package(package) {} void update(sf::RenderWindow &window); void update(const sf::Event &event, sf::RenderWindow &window); @@ -29,8 +28,7 @@ namespace silly::editor { const float get_zoom() const; private: - Tileset &tileset; - TileFloor &floor; + LevelPackage &package; float rotation; // need for world movement and zoom diff --git a/src/floor.cpp b/src/level.cpp index aacf77e..bee3297 100644 --- a/src/floor.cpp +++ b/src/level.cpp @@ -1,4 +1,4 @@ -#include "floor.hpp" +#include "level.hpp" #include <SFML/Graphics/RectangleShape.hpp> #include <SFML/Graphics/RenderWindow.hpp> @@ -7,6 +7,7 @@ #include <SFML/System/Vector2.hpp> #include <algorithm> #include <iterator> +#include <string> #include "tileset.hpp" @@ -70,4 +71,20 @@ namespace silly::editor { const int TileFloor::get_width() const { return this->width; } const int TileFloor::get_height() const { return this->height; } + + void TileLevel::add_floor(TileFloor &floor) { this->floors.push_back(floor); } + + void TileLevel::move_to_floor(int floor_id) { + this->current_floor = std::min(floor_id, (int)this->floors.size() - 1); + } + + TileFloor &TileLevel::get_current_floor() { + return this->floors.at(this->current_floor); + } + + const std::vector<TileFloor> &TileLevel::get_floors() const { + return this->floors; + } + + const std::string &TileLevel::get_name() const { return this->name; } }
\ No newline at end of file diff --git a/src/floor.hpp b/src/level.hpp index 4236166..2364413 100644 --- a/src/floor.hpp +++ b/src/level.hpp @@ -3,6 +3,7 @@ #include <SFML/Graphics/RenderWindow.hpp> #include <SFML/System/Vector2.hpp> #include <memory> +#include <string> #include <vector> #include "tileset.hpp" @@ -43,4 +44,24 @@ namespace silly::editor { int activeLayerIndex; std::vector<TileLayer> layers; }; + + class TileLevel { + public: + TileLevel(const std::string &name) : name(name) {} + ~TileLevel() = default; + + void add_floor(TileFloor &floor); + void move_to_floor(int floor_id); + + TileFloor &get_current_floor(); + const std::vector<TileFloor> &get_floors() const; + + const std::string &get_name() const; + + private: + const std::string name; + + int current_floor; + std::vector<TileFloor> floors; + }; }
\ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 084671a..cc28392 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,8 +11,9 @@ #include <optional> #include "editor.hpp" -#include "floor.hpp" +#include "level.hpp" #include "nfd.hpp" +#include "package.hpp" #include "tileset.hpp" int main() { @@ -22,10 +23,8 @@ int main() { window.setFramerateLimit(60); ImGui::SFML::Init(window); - silly::editor::Tileset tileset; - - silly::editor::TileFloor tileFloor(30, 30); - silly::editor::Editor editor(tileset, tileFloor); + silly::editor::LevelPackage package("test"); + silly::editor::Editor editor(package); sf::Clock deltaClock; while (window.isOpen()) { @@ -42,7 +41,7 @@ int main() { // (cv pasted from // https://www.sfml-dev.org/tutorials/3.0/graphics/view/#showing-more-when-the-window-is-resized) // catch the resize events - if (const auto* resized = event->getIf<sf::Event::Resized>()) { + if (const auto *resized = event->getIf<sf::Event::Resized>()) { // update the view to the new size of the window sf::FloatRect visibleArea({0.f, 0.f}, sf::Vector2f(resized->size)); sf::View view(visibleArea); @@ -62,17 +61,21 @@ int main() { window.clear(); - tileFloor.render(window); - - // rendering grid - for (int x = 0; x < tileFloor.get_width(); x++) { - for (int y = 0; y < tileFloor.get_height(); y++) { - sf::RectangleShape shape({size, size}); - shape.setFillColor(sf::Color::Transparent); - shape.setOutlineColor(sf::Color(80, 80, 80)); - shape.setOutlineThickness(1.0f); - shape.setPosition({x * size, y * size}); - window.draw(shape); + if (!package.get_levels().empty()) { + silly::editor::TileLevel &level = package.get_current_level(); + silly::editor::TileFloor &floor = level.get_current_floor(); + floor.render(window); + + // rendering grid + for (int x = 0; x < floor.get_width(); x++) { + for (int y = 0; y < floor.get_height(); y++) { + sf::RectangleShape shape({size, size}); + shape.setFillColor(sf::Color::Transparent); + shape.setOutlineColor(sf::Color(80, 80, 80)); + shape.setOutlineThickness(1.0f); + shape.setPosition({x * size, y * size}); + window.draw(shape); + } } } diff --git a/src/package.cpp b/src/package.cpp new file mode 100644 index 0000000..a9d51b8 --- /dev/null +++ b/src/package.cpp @@ -0,0 +1,29 @@ +#include "package.hpp" + +#include <algorithm> + +namespace silly::editor { + Tileset &LevelPackage::get_tileset() { return this->tileset; } + + void LevelPackage::add_level(TileLevel level) { + this->levels.push_back(level); + } + + TileLevel &LevelPackage::get_current_level() { + return this->levels.at(this->currentLevelIndex); + } + + void LevelPackage::move_to_level_index(int index) { + this->currentLevelIndex = std::min(index, (int)this->levels.size() - 1); + } + + const int LevelPackage::get_current_level_index() const { + return this->currentLevelIndex; + } + + const std::vector<TileLevel> &LevelPackage::get_levels() const { + return this->levels; + } + + const std::string &LevelPackage::get_name() const { return this->name; } +}
\ No newline at end of file diff --git a/src/package.hpp b/src/package.hpp new file mode 100644 index 0000000..d847f83 --- /dev/null +++ b/src/package.hpp @@ -0,0 +1,35 @@ +#pragma once + +#include <string> +#include <vector> + +#include "level.hpp" +#include "tileset.hpp" + +namespace silly::editor { + class LevelPackage { + public: + LevelPackage(const std::string &name) : name(name) {} + ~LevelPackage() = default; + + Tileset &get_tileset(); + + void add_level(TileLevel level); + TileLevel &get_current_level(); + + void move_to_level_index(int index); + + const int get_current_level_index() const; + const std::vector<TileLevel> &get_levels() const; + + const std::string &get_name() const; + + private: + const std::string name; + + Tileset tileset; + std::vector<TileLevel> levels; + + int currentLevelIndex; + }; +}
\ No newline at end of file |
