summaryrefslogtreecommitdiff
path: root/src/render.cpp
diff options
context:
space:
mode:
authorPaweł Redman <pawel.redman@gmail.com>2017-10-07 08:03:44 +0200
committerPaweł Redman <pawel.redman@gmail.com>2017-10-07 08:03:44 +0200
commit9e7d64d052eabcf40d85c3e903aaba44903a380a (patch)
tree0a3a2c7f53884bd035b54812a77d493be058f43c /src/render.cpp
Initial commit.
Diffstat (limited to 'src/render.cpp')
-rw-r--r--src/render.cpp67
1 files changed, 67 insertions, 0 deletions
diff --git a/src/render.cpp b/src/render.cpp
new file mode 100644
index 0000000..218dff0
--- /dev/null
+++ b/src/render.cpp
@@ -0,0 +1,67 @@
+#include "common.hpp"
+
+static sf::RectangleShape wot_rect;
+
+static void draw_tile(sf::RenderWindow *window, float x, float y,
+ world::tile_t *tile)
+{
+ wot_rect.setSize(sf::Vector2f(1.0f, 1.0f));
+ wot_rect.setPosition(sf::Vector2f(x, y));
+ wot_rect.setFillColor(sf::Color(tile->type, 255 - tile->type, tile->type + 128));
+ wot_rect.setOutlineColor(sf::Color::Transparent);
+ window->draw(wot_rect);
+}
+
+static void draw_sector(sf::RenderWindow *window, world::world_t *world,
+ world::sector_index_t index)
+{
+ world::sector_t *sector;
+
+ sector = world->get_sector(index);
+
+ for (ssize_t y = 0; y < SECTOR_SIZE; y++)
+ for (ssize_t x = 0; x < SECTOR_SIZE; x++) {
+ draw_tile(window,
+ index.x * SECTOR_SIZE + x,
+ index.y * SECTOR_SIZE + y,
+ sector->tiles + y * SECTOR_SIZE + x);
+ }
+
+ wot_rect.setSize(sf::Vector2f(SECTOR_SIZE, SECTOR_SIZE));
+ wot_rect.setPosition(sf::Vector2f(index.x * 16, index.y * 16));
+ wot_rect.setOutlineColor(sf::Color::Yellow);
+ wot_rect.setOutlineThickness(0.06f);
+ wot_rect.setFillColor(sf::Color::Transparent);
+ window->draw(wot_rect);
+}
+
+void world::world_t::render(sf::RenderWindow *window)
+{
+ sf::Vector2u size = window->getSize();
+ sf::Vector2f A, B, C, D;
+ sf::Rect<float> bbox;
+ sf::Rect<ssize_t> index_box;
+
+ A = window->mapPixelToCoords(sf::Vector2i(0, 0));
+ B = window->mapPixelToCoords(sf::Vector2i(size.x, 0));
+ C = window->mapPixelToCoords(sf::Vector2i(0, size.y));
+ D = window->mapPixelToCoords(sf::Vector2i(size.x, size.y));
+
+ bbox.left = std::min({A.x, B.x, C.x, D.x});
+ bbox.top = std::min({A.y, B.y, C.y, D.y});
+ bbox.width = std::max({A.x, B.x, C.x, D.x});
+ bbox.height = std::max({A.y, B.y, C.y, D.y});
+
+ index_box.left = floor(bbox.left / SECTOR_SIZE);
+ index_box.top = floor(bbox.top / SECTOR_SIZE);
+ index_box.width = ceil(bbox.width / SECTOR_SIZE);
+ index_box.height = ceil(bbox.height / SECTOR_SIZE);
+
+ for (ssize_t y = index_box.top; y < index_box.height; y++)
+ for (ssize_t x = index_box.left; x < index_box.width; x++)
+ draw_sector(window, this, world::sector_index_t(x, y));
+}
+
+void interface::state_t::render()
+{
+}