diff options
Diffstat (limited to 'src/main/cpp')
-rw-r--r-- | src/main/cpp/Subconscious.cpp | 44 | ||||
-rw-r--r-- | src/main/cpp/WorldScene.cpp | 37 |
2 files changed, 67 insertions, 14 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) {} |