summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/floor.cpp60
-rw-r--r--src/floor.hpp20
-rw-r--r--src/main.cpp10
3 files changed, 82 insertions, 8 deletions
diff --git a/src/floor.cpp b/src/floor.cpp
index fac9ec6..f081008 100644
--- a/src/floor.cpp
+++ b/src/floor.cpp
@@ -1,6 +1,62 @@
#include "floor.hpp"
+#include <SFML/Graphics/RectangleShape.hpp>
+#include <SFML/Graphics/RenderWindow.hpp>
+#include <SFML/System/Vector2.hpp>
+#include <SFML/Window/Mouse.hpp>
+#include <algorithm>
+#include <iterator>
+
namespace silly::editor {
- const int Floor::get_width() const { return this->width; }
- const int Floor::get_height() const { return this->height; }
+ void TileFloor::update(const sf::RenderWindow &window) {
+ if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left) ||
+ sf::Mouse::isButtonPressed(sf::Mouse::Button::Right)) {
+ sf::Vector2i mousePosition = sf::Mouse::getPosition(window);
+
+ for (int x = 0; x < this->get_width(); x++) {
+ for (int y = 0; y < this->get_height(); y++) {
+ int rx = x * 16, ry = y * 16;
+
+ if ((rx < mousePosition.x && mousePosition.x < rx + 16) &&
+ (ry < mousePosition.y && mousePosition.y < ry + 16)) {
+ sf::Vector2i pos(x, y);
+
+ if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)) {
+ this->place_tile(pos);
+ } else if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Right)) {
+ this->remove_tile(pos);
+ }
+ }
+ }
+ }
+ }
+ }
+
+ void TileFloor::render(sf::RenderWindow &window) const {
+ std::for_each(
+ this->tiles.begin(), this->tiles.end(), [&window](const Tile &t) {
+ sf::RectangleShape shape({16, 16});
+ shape.setFillColor(sf::Color(255, 190, 190));
+ shape.setPosition({t.position.x * 16.0f, t.position.y * 16.0f});
+ window.draw(shape);
+ });
+ }
+
+ void TileFloor::place_tile(const sf::Vector2i &position) {
+ if (!std::any_of(
+ this->tiles.begin(), this->tiles.end(),
+ [&position](const Tile &t) { return t.position == position; })) {
+ this->tiles.push_back({position});
+ }
+ }
+ void TileFloor::remove_tile(const sf::Vector2i &position) {
+ this->tiles.resize(std::distance(
+ this->tiles.begin(),
+ std::remove_if(
+ this->tiles.begin(), this->tiles.end(),
+ [&position](const Tile &t) { return t.position == position; })));
+ }
+
+ const int TileFloor::get_width() const { return this->width; }
+ const int TileFloor::get_height() const { return this->height; }
} \ No newline at end of file
diff --git a/src/floor.hpp b/src/floor.hpp
index 6389dff..cf91ac5 100644
--- a/src/floor.hpp
+++ b/src/floor.hpp
@@ -1,15 +1,29 @@
#pragma once
+#include <SFML/Graphics/RenderWindow.hpp>
+#include <SFML/System/Vector2.hpp>
+#include <vector>
+
namespace silly::editor {
- class Floor {
+ struct Tile {
+ sf::Vector2i position;
+ };
+
+ class TileFloor {
public:
- Floor(int width, int height) : width(width), height(height) {}
- ~Floor() = default;
+ TileFloor(int width, int height) : width(width), height(height) {}
+ ~TileFloor() = default;
+
+ void update(const sf::RenderWindow &window);
+ void render(sf::RenderWindow &window) const;
+ void place_tile(const sf::Vector2i &position);
+ void remove_tile(const sf::Vector2i &position);
const int get_width() const;
const int get_height() const;
private:
int width, height;
+ std::vector<Tile> tiles;
};
} \ No newline at end of file
diff --git a/src/main.cpp b/src/main.cpp
index 3eb444c..0a3432b 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -17,7 +17,7 @@ int main() {
window.setFramerateLimit(60);
ImGui::SFML::Init(window);
- silly::editor::Floor floor(30, 30);
+ silly::editor::TileFloor tileFloor(30, 30);
sf::Clock deltaClock;
while (window.isOpen()) {
@@ -44,13 +44,17 @@ int main() {
}
}
+ tileFloor.update(window);
+
ImGui::SFML::Update(window, deltaClock.restart());
window.clear();
+ tileFloor.render(window);
+
// rendering grid
- for (int x = 0; x < floor.get_width(); x++) {
- for (int y = 0; y < floor.get_height(); y++) {
+ 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));