From 6f49fecab4c14fff6d9fd3f988d27d720907cf28 Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Thu, 22 Jun 2023 23:14:16 +0200 Subject: [PATCH] Fixed aspect ratio on split screen views --- TODO.md | 8 +++---- src/game/camera/tracking_view.cpp | 35 +++++++++++-------------------- src/game/camera/tracking_view.h | 5 +---- 3 files changed, 17 insertions(+), 31 deletions(-) diff --git a/TODO.md b/TODO.md index a7626ee..1ac6651 100644 --- a/TODO.md +++ b/TODO.md @@ -2,11 +2,10 @@ ## Next -- Mutliplayer viewports wrong aspect ratio +- Player spawns initially at (0,0) and almost fails to jump to target spawn ## Bugs -- Player spawns initially at (0,0) and almost fails to jump to target spawn - Player disconnect in multiplayer sometimes targeting disconnected player or something like that ## High priority @@ -14,13 +13,14 @@ - Procedural level generation - Physics - Damage other players on contact -- Menu (At least before the game starts a player-join-screen) +- Players-join-screen before the game starts - Game over screen -- Score - Proper player graphics - Collectables with graphics ## Low priority +- Score - Player actions like shrinking hole or throwing bomb +- Menu - Sound diff --git a/src/game/camera/tracking_view.cpp b/src/game/camera/tracking_view.cpp index 6f72ceb..99d4be5 100644 --- a/src/game/camera/tracking_view.cpp +++ b/src/game/camera/tracking_view.cpp @@ -3,11 +3,9 @@ #include "../player/player_collection.hpp" TrackingView::TrackingView(TrackingViewOptions options) : options(options), - view(nullptr), - hasViewChanged(false), + view(new sf::View()), trackables({}) { - initializeView(); marker = new CircleObject(DB_CIRCLE_RADIUS, sf::Color::Yellow); } @@ -26,20 +24,6 @@ void TrackingView::lateUpdate() followTrackables(); adjustSizeToTrackables(); } - - // Update window if necessary - if (hasViewChanged) - { - this->setViewForWindow(); - hasViewChanged = false; - } -} - -void TrackingView::initializeView() -{ - auto size = Game::getInstance()->window->getView().getSize(); - view = new sf::View({0, 0}, size); - hasViewChanged = true; } void TrackingView::setSize(sf::Vector2f newSize) @@ -67,7 +51,6 @@ void TrackingView::setSize(sf::Vector2f newSize) } view->setSize(newSize); - hasViewChanged = true; } sf::Vector2f TrackingView::getSize() const @@ -75,10 +58,11 @@ sf::Vector2f TrackingView::getSize() const return view->getSize(); } -sf::Vector2f TrackingView::getWindowSize() const +sf::Vector2f TrackingView::getSizeInWindow() const { auto size = Game::getInstance()->window->getSize(); - return {static_cast(size.x), static_cast(size.y)}; + auto viewPort = view->getViewport(); + return {static_cast(size.x * viewPort.width), static_cast(size.y * viewPort.height)}; } sf::Vector2f TrackingView::getCenter() const @@ -125,7 +109,6 @@ void TrackingView::moveCenter(sf::Vector2 delta) } view->move(delta); - hasViewChanged = true; } void TrackingView::draw(sf::RenderWindow *window) @@ -141,6 +124,12 @@ void TrackingView::draw(sf::RenderWindow *window) void TrackingView::addTrackable(const std::shared_ptr &trackable) { trackables.push_back(trackable); + + // Set initial values if first trackable + if (trackables.size() == 1) + { + view->setCenter(trackable->getTrackablePosition()); + } } void TrackingView::processTrackableStates() @@ -199,7 +188,7 @@ void TrackingView::adjustSizeToTrackables() newViewSize = restrainToBounds(newViewSize); // Extend view to match aspect ratio - auto windowSize = getWindowSize(); + auto windowSize = getSizeInWindow(); auto aspectRatio = windowSize.x / windowSize.y; if (newViewSize.x / newViewSize.y > aspectRatio) { @@ -261,7 +250,7 @@ float TrackingView::getRadius(float threshold) const return threshold; } - auto windowSize = getWindowSize(); + auto windowSize = getSizeInWindow(); float smallestSide = std::min(windowSize.x, windowSize.y); // Only half of the side, since we are calculating radius diff --git a/src/game/camera/tracking_view.h b/src/game/camera/tracking_view.h index 0764df8..7e21708 100644 --- a/src/game/camera/tracking_view.h +++ b/src/game/camera/tracking_view.h @@ -35,14 +35,11 @@ public: private: sf::View *view{}; - bool hasViewChanged{}; TrackingViewOptions options; std::vector> trackables; CircleObject *marker; - void initializeView(); - void followTrackables(); void moveCenter(sf::Vector2 delta); @@ -61,7 +58,7 @@ private: float getRadius(float threshold) const; - sf::Vector2f getWindowSize() const; + sf::Vector2f getSizeInWindow() const; };