From 77dc2f0ceb116e9991a973d2de283599380f62a5 Mon Sep 17 00:00:00 2001 From: Maximilian Giller Date: Thu, 22 Jun 2023 23:01:49 +0200 Subject: [PATCH] Implemented border for splitscreen view --- CMakeLists.txt | 2 +- TODO.md | 27 +++++----- src/config.h | 1 + src/game/camera/multiplayer_borders.cpp | 65 +++++++++++++++++++++++++ src/game/camera/multiplayer_borders.hpp | 26 ++++++++++ src/game/camera/multiplayer_view.cpp | 20 ++++++++ src/game/camera/multiplayer_view.hpp | 7 ++- src/game/level/level_renderer.cpp | 3 ++ 8 files changed, 135 insertions(+), 16 deletions(-) create mode 100644 src/game/camera/multiplayer_borders.cpp create mode 100644 src/game/camera/multiplayer_borders.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index d5e9b16..2d26d04 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -98,7 +98,7 @@ set(SOURCES src/levels.hpp src/sprites/tiling/tilemap.cpp src/sprites/tiling/tilemap.hpp - src/sprites/tiling/tilemap_config.hpp src/sprites/tiling/tileset_config.hpp src/sprites/tiling/tileset.cpp src/sprites/tiling/tileset.hpp src/game/frame_counter.cpp src/game/frame_counter.hpp src/game/level/level_renderer.cpp src/game/level/level_renderer.hpp src/sprites/skymap/skymap.cpp src/sprites/skymap/skymap.hpp src/game/camera/multiplayer_view.cpp src/game/camera/multiplayer_view.hpp) + src/sprites/tiling/tilemap_config.hpp src/sprites/tiling/tileset_config.hpp src/sprites/tiling/tileset.cpp src/sprites/tiling/tileset.hpp src/game/frame_counter.cpp src/game/frame_counter.hpp src/game/level/level_renderer.cpp src/game/level/level_renderer.hpp src/sprites/skymap/skymap.cpp src/sprites/skymap/skymap.hpp src/game/camera/multiplayer_view.cpp src/game/camera/multiplayer_view.hpp src/game/camera/multiplayer_borders.cpp src/game/camera/multiplayer_borders.hpp) set(PHYSICS_00_SOURCES src/prototypes/physics_00.cpp) diff --git a/TODO.md b/TODO.md index cac0177..a7626ee 100644 --- a/TODO.md +++ b/TODO.md @@ -2,26 +2,25 @@ ## Next -- [ ] Split screen +- Mutliplayer viewports wrong aspect ratio ## Bugs -- [ ] Player spawns initially at (0,0) and almost fails to jump to target spawn -- [ ] Mutliplayer viewports wrong aspect ratio -- [ ] Player disconnect in multiplayer sometimes targetting disconnected player or something like that +- 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 -- [ ] Procedural level generation -- [ ] Physics -- [ ] Damage other players on contact -- [ ] Menu (At least before the game starts a player-join-screen) -- [ ] Game over screen -- [ ] Score -- [ ] Proper player graphics -- [ ] Collectables with graphics +- Procedural level generation +- Physics +- Damage other players on contact +- Menu (At least before the game starts a player-join-screen) +- Game over screen +- Score +- Proper player graphics +- Collectables with graphics ## Low priority -- [ ] Player actions like shrinking hole or throwing bomb -- [ ] Sound +- Player actions like shrinking hole or throwing bomb +- Sound diff --git a/src/config.h b/src/config.h index 8751436..0d3e706 100644 --- a/src/config.h +++ b/src/config.h @@ -37,6 +37,7 @@ #define DEF_TV_VIEW_SIZE_PADDING sf::Vector2f(0.5f, 0.5f) #define MP_VIEW_ADD_NEW_PLAYERS true #define MP_VIEW_REMOVE_DISCONNECTED_PLAYERS true +#define MP_VIEW_BORDER_COLOR sf::Color::Black // Simulations #define MAPSIM_WALL_THICKNESS 3.f diff --git a/src/game/camera/multiplayer_borders.cpp b/src/game/camera/multiplayer_borders.cpp new file mode 100644 index 0000000..4e450b2 --- /dev/null +++ b/src/game/camera/multiplayer_borders.cpp @@ -0,0 +1,65 @@ +#include +#include "multiplayer_borders.hpp" +#include "../../config.h" + +MultiplayerBorders::MultiplayerBorders(int firstRowViewCount, int secondRowViewCount) + : view(new sf::View(sf::Vector2f(0.5f, 0.5f), sf::Vector2f(1, 1))) +{ + createBorders(firstRowViewCount, secondRowViewCount); +} + +void MultiplayerBorders::createBorders(int firstRowViewCount, int secondRowViewCount) +{ + int lineCount = firstRowViewCount + secondRowViewCount - 1; + if (lineCount <= 0) + { + return; + } + + lines = sf::VertexArray(sf::Lines, lineCount * 2); + float lineHeight = secondRowViewCount > 0 ? 0.5f : 1.f; + + // First row + int firstRowLineCount = firstRowViewCount - 1; + createRowOfLines(0, firstRowLineCount, 0, lineHeight); + + if (secondRowViewCount <= 0) + { + return; + } + + // Horizontal separator + createLine(firstRowLineCount, + {0, lineHeight}, + {1, lineHeight}); + + // Second row + int secondRowLineCount = secondRowViewCount - 1; + createRowOfLines(firstRowLineCount + 1, secondRowLineCount, lineHeight, lineHeight); +} + +void MultiplayerBorders::draw(sf::RenderWindow *window) +{ + window->setView(*view); + window->draw(lines); +} + +void MultiplayerBorders::createRowOfLines(int startIndex, int count, float topOffset, float height) +{ + for (int i = 0; i < count; i++) + { + float horizontalProgress = (float) (i + 1) / (float) (count + 1); + createLine(startIndex + i, + {horizontalProgress, topOffset}, + {horizontalProgress, topOffset + height}); + } +} + +void MultiplayerBorders::createLine(int lineIndex, sf::Vector2f firstPoint, sf::Vector2f secondPoint) +{ + lines[lineIndex * 2].position = firstPoint; + lines[lineIndex * 2 + 1].position = secondPoint; + + lines[lineIndex * 2].color = MP_VIEW_BORDER_COLOR; + lines[lineIndex * 2 + 1].color = MP_VIEW_BORDER_COLOR; +} diff --git a/src/game/camera/multiplayer_borders.hpp b/src/game/camera/multiplayer_borders.hpp new file mode 100644 index 0000000..8166589 --- /dev/null +++ b/src/game/camera/multiplayer_borders.hpp @@ -0,0 +1,26 @@ +#ifndef HOLESOME_MULTIPLAYER_BORDERS_HPP +#define HOLESOME_MULTIPLAYER_BORDERS_HPP + + +#include "../game_object.h" + +class MultiplayerBorders : public GameObject +{ +public: + explicit MultiplayerBorders(int firstRowViewCount = 0, int secondRowViewCount = 0); + + void draw(sf::RenderWindow *window) override; + + void createBorders(int firstRowViewCount, int secondRowViewCount = 0); + +private: + sf::VertexArray lines{}; + sf::View* view = nullptr; + + void createRowOfLines(int startIndex, int count, float topOffset, float height); + + void createLine(int lineIndex, sf::Vector2f firstPoint, sf::Vector2f secondPoint); +}; + + +#endif //HOLESOME_MULTIPLAYER_BORDERS_HPP diff --git a/src/game/camera/multiplayer_view.cpp b/src/game/camera/multiplayer_view.cpp index 3ea46d4..bff02ed 100644 --- a/src/game/camera/multiplayer_view.cpp +++ b/src/game/camera/multiplayer_view.cpp @@ -97,12 +97,18 @@ void MultiplayerView::updateLayout() const it->second->setViewport({0, 0}, {0.5, 1}); it++; it->second->setViewport({0.5, 0}, {0.5, 1}); + + borders->createBorders(2); + return; } // Calculate layout int firstRowViewCount = ceil(viewCount / 2.f); int secondRowViewCount = viewCount - firstRowViewCount; + + borders->createBorders(firstRowViewCount, secondRowViewCount); + int index = 0; for (auto &view: getViews()) { @@ -132,3 +138,17 @@ std::vector> MultiplayerView::getViews() const } return views; } + +void MultiplayerView::draw(sf::RenderWindow *window) +{ + if (borders != nullptr) + { + borders->draw(window); + } +} + +MultiplayerView::MultiplayerView() + : borders(std::make_shared()), viewsForPlayers({}) +{ + +} diff --git a/src/game/camera/multiplayer_view.hpp b/src/game/camera/multiplayer_view.hpp index 7fcb6e7..6a608a8 100644 --- a/src/game/camera/multiplayer_view.hpp +++ b/src/game/camera/multiplayer_view.hpp @@ -6,13 +6,16 @@ #include "../game_object.h" #include "tracking_view.h" #include "../player/player.hpp" +#include "multiplayer_borders.hpp" class MultiplayerView : public GameObject { public: static std::shared_ptr getInstance(); - MultiplayerView() = default; + MultiplayerView(); + + void draw(sf::RenderWindow *window) override; void update() override; @@ -27,6 +30,8 @@ private: std::map> viewsForPlayers; // Player ID => View void updateLayout() const; + + std::shared_ptr borders; }; diff --git a/src/game/level/level_renderer.cpp b/src/game/level/level_renderer.cpp index df84f1f..8d8a77f 100644 --- a/src/game/level/level_renderer.cpp +++ b/src/game/level/level_renderer.cpp @@ -9,4 +9,7 @@ void LevelRenderer::draw(sf::RenderWindow *window) view->setViewForWindow(); GameObject::draw(window); } + + // Render borders between views + MultiplayerView::getInstance()->draw(window); }