summaryrefslogtreecommitdiff
path: root/src/render.cpp
blob: d63ca6fed6d250486873facd79e36629c86e6730 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "common.hpp"
#include <list>

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, tile->type, tile->type));
	wot_rect.setOutlineColor(sf::Color::Transparent);
	window->draw(wot_rect);
}

static void draw_sector(sf::RenderWindow *window, world::sector_t *sector)
{
	for (ssize_t y = 0; y < SECTOR_SIZE; y++)
	for (ssize_t x = 0; x < SECTOR_SIZE; x++)
		draw_tile(window,
		          sector->bounds.left + x, sector->bounds.top + y,
		          sector->tiles + y * SECTOR_SIZE + x);

	wot_rect.setSize(sf::Vector2f(SECTOR_SIZE, SECTOR_SIZE));
	wot_rect.setPosition(sf::Vector2f(sector->bounds.left, sector->bounds.top));
	wot_rect.setOutlineColor(sf::Color::Yellow);
	wot_rect.setOutlineThickness(0.06f);
	wot_rect.setFillColor(sf::Color::Transparent);
	window->draw(wot_rect);
}

static void draw_sector_entities(sf::RenderWindow *window, world::sector_t *sector)
{
	for (world::entity_t *ent : sector->ents)
		ent->render(window);
}

void game::state_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;
	std::list<world::sector_t*> sectors;

	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++) {
		world::sector_index_t index(x, y);
		world::sector_t *sector;

		sector = world.get_sector(index);
		sectors.push_back(sector);
	}

	for (auto *sector : sectors)
		draw_sector(window, sector);

	for (auto *sector : sectors)
		draw_sector_entities(window, sector);
}

void game::human_t::render(sf::RenderWindow *window)
{
	wot_rect.setPosition(bounds.left, bounds.top);
	wot_rect.setSize(sf::Vector2f(bounds.width, bounds.height));
	wot_rect.setFillColor(sf::Color::Red);
	wot_rect.setOutlineColor(sf::Color::Transparent);
	window->draw(wot_rect);
}

void interface::state_t::render()
{
}