From d3e8495212f9008f0c2650fb5ee628b464ca5ac0 Mon Sep 17 00:00:00 2001 From: Nao Pross Date: Mon, 12 Feb 2018 00:13:39 +0100 Subject: Handle WindowResize event and add zoom --- src/main/cpp/WorldScene.cpp | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) (limited to 'src/main/cpp/WorldScene.cpp') diff --git a/src/main/cpp/WorldScene.cpp b/src/main/cpp/WorldScene.cpp index ab1c4d0..973c6e6 100644 --- a/src/main/cpp/WorldScene.cpp +++ b/src/main/cpp/WorldScene.cpp @@ -1,11 +1,11 @@ #include "WorldScene.hpp" -WorldScene::WorldScene() +WorldScene::WorldScene(sf::RenderWindow &window) : Scene(window) { _tileSize = DEFAULT_TILE_SIZE_PX; } -void WorldScene::render(sf::RenderWindow &window) +void WorldScene::render() { sf::RectangleShape rect; @@ -17,12 +17,39 @@ void WorldScene::render(sf::RenderWindow &window) for (const Tile &tile : map.tiles()) { rect.setPosition(tile.x * _tileSize, tile.y * _tileSize); - window.draw(rect); + _window.draw(rect); } } -void WorldScene::zoom(int value) -{} +void WorldScene::resize(const sf::Event::SizeEvent &size) +{ + auto oldView = _window.getView(); + sf::View resizedView(oldView.getCenter(), sf::Vector2f(size.width, size.height)); + + _window.setView(resizedView); +} + +void WorldScene::zoom(float factor) +{ + _zoom += factor; + + if (_zoom < MIN_ZOOM) { + _zoom = MIN_ZOOM; + } + + if (_zoom > MAX_ZOOM) { + _zoom = MAX_ZOOM; + } + + sf::View view = _window.getView(); + + view.setSize( + _window.getSize().x * 1 / _zoom, + _window.getSize().y * 1 / _zoom + ); + + _window.setView(view); +} void WorldScene::pan(int dx, int dy) {} -- cgit v1.2.1