diff options
author | Nao Pross <naopross@thearcway.org> | 2018-02-12 00:13:39 +0100 |
---|---|---|
committer | Nao Pross <naopross@thearcway.org> | 2018-02-12 00:13:39 +0100 |
commit | d3e8495212f9008f0c2650fb5ee628b464ca5ac0 (patch) | |
tree | e45ef935e17e24184cce7a75d5a281133d49b271 | |
parent | Update README to add dependencies (diff) | |
download | Subconscious-old-d3e8495212f9008f0c2650fb5ee628b464ca5ac0.tar.gz Subconscious-old-d3e8495212f9008f0c2650fb5ee628b464ca5ac0.zip |
Handle WindowResize event and add zoom
Diffstat (limited to '')
-rw-r--r-- | src/main/cpp/Subconscious.cpp | 44 | ||||
-rw-r--r-- | src/main/cpp/WorldScene.cpp | 37 | ||||
-rw-r--r-- | src/main/headers/Scene.hpp | 14 | ||||
-rw-r--r-- | src/main/headers/WorldScene.hpp | 7 |
4 files changed, 84 insertions, 18 deletions
diff --git a/src/main/cpp/Subconscious.cpp b/src/main/cpp/Subconscious.cpp index 942b0b4..8e1bd02 100644 --- a/src/main/cpp/Subconscious.cpp +++ b/src/main/cpp/Subconscious.cpp @@ -3,6 +3,10 @@ #include <SFML/Graphics.hpp> +// TODO remove (needed for mousewheel debug) +#include <iostream> +#include <iomanip> + /*** public methods ***/ Subconscious::Subconscious() @@ -19,6 +23,35 @@ void Subconscious::run() { _running = true; while (_running && _window.isOpen()) { + + sf::Event event; + + while (_window.pollEvent(event)) { + switch (event.type) { + /*** Keyboard Events ***/ + /*** Mouse Events ***/ + case sf::Event::MouseWheelScrolled: + // my mouse always outputs +/- 1.0000, so the zoom speed + // is by 1/10.0 => .1 + // TODO make this a setting that chan be changed + // (mousewheel sensibility + // + // std::cout << std::fixed << std::setprecision(3) + // << event.mouseWheelScroll.delta << std::endl; + _currentScene->zoom(event.mouseWheelScroll.delta/10.0); + break; + + /*** Window Events ***/ + case sf::Event::Closed: + _window.close(); + break; + + case sf::Event::Resized: + _currentScene->resize(event.size); + break; + } + } + gameUpdate(); gameRender(); } @@ -26,7 +59,7 @@ void Subconscious::run() void Subconscious::demo() { - Scene *demoScene = new WorldScene(); + Scene *demoScene = new WorldScene(_window); _currentScene = demoScene; _scenes.push_back(demoScene); @@ -39,13 +72,6 @@ void Subconscious::demo() void Subconscious::gameUpdate() { - sf::Event event; - - while (_window.pollEvent(event)) { - if (event.type == sf::Event::Closed) { - _window.close(); - } - } } void Subconscious::gameRender() @@ -53,7 +79,7 @@ void Subconscious::gameRender() _window.clear(); if (_currentScene != nullptr) { - _currentScene->render(_window); + _currentScene->render(); } _window.display(); 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) {} diff --git a/src/main/headers/Scene.hpp b/src/main/headers/Scene.hpp index 2b19d31..b7a46e6 100644 --- a/src/main/headers/Scene.hpp +++ b/src/main/headers/Scene.hpp @@ -6,7 +6,19 @@ class Scene { public: - virtual void render(sf::RenderWindow &window) = 0; + constexpr static float MAX_ZOOM = 10; + constexpr static float MIN_ZOOM = .1; + + virtual void render() = 0; + virtual void resize(const sf::Event::SizeEvent &size) = 0; + + virtual void zoom(float factor) {} + +protected: + float _zoom = 1; + sf::RenderWindow &_window; + + Scene(sf::RenderWindow &window) : _window(window) {} }; #endif diff --git a/src/main/headers/WorldScene.hpp b/src/main/headers/WorldScene.hpp index acdda63..09f3120 100644 --- a/src/main/headers/WorldScene.hpp +++ b/src/main/headers/WorldScene.hpp @@ -14,11 +14,12 @@ public: constexpr static int ZOOM_MAX = 100; constexpr static int DEFAULT_TILE_SIZE_PX = 20; - WorldScene(); + WorldScene(sf::RenderWindow &window); - virtual void render(sf::RenderWindow &window); + virtual void render(); + virtual void resize(const sf::Event::SizeEvent &size); - virtual void zoom(int value); + virtual void zoom(float factor); virtual void pan(int dx, int dy); private: |