Implemented border for splitscreen view

This commit is contained in:
Maximilian Giller 2023-06-22 23:01:49 +02:00
parent 4ecc53abb3
commit 77dc2f0ceb
8 changed files with 135 additions and 16 deletions

View file

@ -98,7 +98,7 @@ set(SOURCES
src/levels.hpp src/levels.hpp
src/sprites/tiling/tilemap.cpp src/sprites/tiling/tilemap.cpp
src/sprites/tiling/tilemap.hpp 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 set(PHYSICS_00_SOURCES
src/prototypes/physics_00.cpp) src/prototypes/physics_00.cpp)

27
TODO.md
View file

@ -2,26 +2,25 @@
## Next ## Next
- [ ] Split screen - Mutliplayer viewports wrong aspect ratio
## Bugs ## Bugs
- [ ] Player spawns initially at (0,0) and almost fails to jump to target spawn - Player spawns initially at (0,0) and almost fails to jump to target spawn
- [ ] Mutliplayer viewports wrong aspect ratio - Player disconnect in multiplayer sometimes targeting disconnected player or something like that
- [ ] Player disconnect in multiplayer sometimes targetting disconnected player or something like that
## High priority ## High priority
- [ ] Procedural level generation - Procedural level generation
- [ ] Physics - Physics
- [ ] Damage other players on contact - Damage other players on contact
- [ ] Menu (At least before the game starts a player-join-screen) - Menu (At least before the game starts a player-join-screen)
- [ ] Game over screen - Game over screen
- [ ] Score - Score
- [ ] Proper player graphics - Proper player graphics
- [ ] Collectables with graphics - Collectables with graphics
## Low priority ## Low priority
- [ ] Player actions like shrinking hole or throwing bomb - Player actions like shrinking hole or throwing bomb
- [ ] Sound - Sound

View file

@ -37,6 +37,7 @@
#define DEF_TV_VIEW_SIZE_PADDING sf::Vector2f(0.5f, 0.5f) #define DEF_TV_VIEW_SIZE_PADDING sf::Vector2f(0.5f, 0.5f)
#define MP_VIEW_ADD_NEW_PLAYERS true #define MP_VIEW_ADD_NEW_PLAYERS true
#define MP_VIEW_REMOVE_DISCONNECTED_PLAYERS true #define MP_VIEW_REMOVE_DISCONNECTED_PLAYERS true
#define MP_VIEW_BORDER_COLOR sf::Color::Black
// Simulations // Simulations
#define MAPSIM_WALL_THICKNESS 3.f #define MAPSIM_WALL_THICKNESS 3.f

View file

@ -0,0 +1,65 @@
#include <SFML/Graphics/VertexArray.hpp>
#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;
}

View file

@ -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

View file

@ -97,12 +97,18 @@ void MultiplayerView::updateLayout() const
it->second->setViewport({0, 0}, {0.5, 1}); it->second->setViewport({0, 0}, {0.5, 1});
it++; it++;
it->second->setViewport({0.5, 0}, {0.5, 1}); it->second->setViewport({0.5, 0}, {0.5, 1});
borders->createBorders(2);
return; return;
} }
// Calculate layout // Calculate layout
int firstRowViewCount = ceil(viewCount / 2.f); int firstRowViewCount = ceil(viewCount / 2.f);
int secondRowViewCount = viewCount - firstRowViewCount; int secondRowViewCount = viewCount - firstRowViewCount;
borders->createBorders(firstRowViewCount, secondRowViewCount);
int index = 0; int index = 0;
for (auto &view: getViews()) for (auto &view: getViews())
{ {
@ -132,3 +138,17 @@ std::vector<std::shared_ptr<TrackingView>> MultiplayerView::getViews() const
} }
return views; return views;
} }
void MultiplayerView::draw(sf::RenderWindow *window)
{
if (borders != nullptr)
{
borders->draw(window);
}
}
MultiplayerView::MultiplayerView()
: borders(std::make_shared<MultiplayerBorders>()), viewsForPlayers({})
{
}

View file

@ -6,13 +6,16 @@
#include "../game_object.h" #include "../game_object.h"
#include "tracking_view.h" #include "tracking_view.h"
#include "../player/player.hpp" #include "../player/player.hpp"
#include "multiplayer_borders.hpp"
class MultiplayerView : public GameObject class MultiplayerView : public GameObject
{ {
public: public:
static std::shared_ptr<MultiplayerView> getInstance(); static std::shared_ptr<MultiplayerView> getInstance();
MultiplayerView() = default; MultiplayerView();
void draw(sf::RenderWindow *window) override;
void update() override; void update() override;
@ -27,6 +30,8 @@ private:
std::map<int, std::shared_ptr<TrackingView>> viewsForPlayers; // Player ID => View std::map<int, std::shared_ptr<TrackingView>> viewsForPlayers; // Player ID => View
void updateLayout() const; void updateLayout() const;
std::shared_ptr<MultiplayerBorders> borders;
}; };

View file

@ -9,4 +9,7 @@ void LevelRenderer::draw(sf::RenderWindow *window)
view->setViewForWindow(); view->setViewForWindow();
GameObject::draw(window); GameObject::draw(window);
} }
// Render borders between views
MultiplayerView::getInstance()->draw(window);
} }