blob: c152db85bd9f97ec3a320dff47f69f1b5a052ad9 (
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
|
#include "WorldScene.hpp"
WorldScene::WorldScene(sf::RenderWindow &window) : Scene(window, Scene::Type::WORLD)
{
_tileSize = DEFAULT_TILE_SIZE_PX;
}
void WorldScene::render()
{
sf::RectangleShape rect;
rect.setSize(sf::Vector2f(_tileSize, _tileSize));
rect.setFillColor(sf::Color::Green);
// negative thickness to make the outline toward inside
rect.setOutlineThickness(-.5);
rect.setOutlineColor(sf::Color::Black);
for (const Tile &tile : map.tiles()) {
rect.setPosition(tile.x * _tileSize + _panX, tile.y * _tileSize + _panY);
_window.draw(rect);
}
}
void WorldScene::resize(const sf::Event::SizeEvent &size)
{
auto oldView = _window.getView();
sf::View resizedView(oldView.getCenter(), sf::Vector2f(size.width, size.height) / _zoom);
_window.setView(resizedView);
}
void WorldScene::zoom(float factor)
{
_zoom += factor;
if (_zoom < MIN_ZOOM) {
_zoom = MIN_ZOOM;
return;
}
if (_zoom > MAX_ZOOM) {
_zoom = MAX_ZOOM;
return;
}
sf::View view = _window.getView();
view.setSize(static_cast<sf::Vector2f>(_window.getSize()) / _zoom);
_window.setView(view);
}
void WorldScene::pan(int dx, int dy)
{
_panX = _panX + dx;
_panY = _panY + dy;
sf::View view = _window.getView();
view.setCenter(
view.getCenter().x + _panX,
view.getCenter().y + _panY
);
}
|