Start refactoring graphics manager

This commit is contained in:
NaiJi ✨ 2021-12-21 20:07:19 +03:00
parent 4be1429546
commit ff07b628ac
20 changed files with 117 additions and 135 deletions

View File

@ -11,7 +11,6 @@ public:
virtual bool isActive(const microsec& offset) const = 0;
virtual void update(const microsec& music_offset) = 0;
virtual void draw() const = 0;
virtual void putToGame(const microsec& offset) = 0;
virtual bool isInGame() const = 0;

View File

@ -128,6 +128,11 @@ public:
iterator = _timeline.end();
}
inline Iterator getTopNote() const noexcept
{
return _top_note;
}
private:
std::set<TNote*, NotePtrCompt> _timeline;
microsec _current_offset;

View File

@ -1,18 +1,18 @@
#include "shared/classicmode/classicfactory.h"
#include "game/classicgame.h"
#include "graphics/classicgraphicsmanager.h"
#include "game/classicgraphicsmanager.h"
#include "tools/music.h"
#include "editor/classiceditor.h"
#include <SFML/Graphics/RenderWindow.hpp>
std::unique_ptr<Game> classic::initGame(sf::RenderWindow& game_window)
std::unique_ptr<Game> classic::initGame()
{
return std::make_unique<ClassicGame>(std::make_unique<ClassicGraphicsManager>(game_window));
return std::make_unique<ClassicGame>();
}
std::unique_ptr<Editor> classic::initEditor(sf::RenderWindow& game_window)
std::unique_ptr<Editor> classic::initEditor()
{
return std::make_unique<ClassicEditor>(std::make_unique<ClassicGraphicsManager>(game_window));
return std::make_unique<ClassicEditor>();
}

View File

@ -1,13 +1,10 @@
#include "classiceditor.h"
ClassicEditor::ClassicEditor(std::shared_ptr<ClassicGraphicsManager>&& manager) :
_graphics_manager(manager),
ClassicEditor::ClassicEditor() :
_selected_type(Type::UP),
_current_time(0),
_scroll_step(500000)
{
_context.graphics_manager = _graphics_manager;
std::set<MockClassicNote*, NotePtrCompt> set = {};
// VISIBILITY 1648648

View File

@ -12,7 +12,7 @@ class ClassicGraphicsManager;
class ClassicEditor : public Editor
{
public:
explicit ClassicEditor(std::shared_ptr<ClassicGraphicsManager>&& manager);
explicit ClassicEditor();
virtual void input(PlayerInput&& inputdata) override;
virtual void update(UpdateData&& updatedata) override;

View File

@ -1,5 +1,5 @@
#include "mockclassicnote.h"
#include "graphics/classicgraphicsmanager.h"
#include "game/classicgraphicsmanager.h"
// Replace with interface by dependency injection
#include "graphics/classicflyinganimationscenario.h"
@ -21,12 +21,6 @@ MockClassicNote::MockClassicNote(MockArrowNoteInitializer&& init) :
{
_elements[i].coordinates = init.elements[i].coordinates;
_elements[i].type = init.elements[i].type;
// Animations will be injected into note.
_elements[i].animations[State::NONE] = nullptr;
_elements[i].animations[State::FLYING] = std::make_shared<ClassicFlyingAnimationScenario>();
_elements[i].animations[State::DYING] = std::make_shared<ClassicDyingAnimationScenario>();
_elements[i].animations[State::DEAD] = nullptr;
}
}
@ -50,14 +44,6 @@ bool MockClassicNote::shouldRemove() const
void MockClassicNote::putToGame(const microsec &music_offset)
{
_state = State::FLYING; (void)music_offset;
for (auto& element : _elements)
{
element.sprite = _context->graphics_manager->getSprite(element.type);
element.sprite->setCoordinates(element.coordinates);
element.sprite->setTrailCoordinates(Coordinates(0.f, 9.f));
element.animations[_state]->launch(element.sprite, offset() - 1648648, offset());
}
}
void MockClassicNote::update(const microsec &music_offset)
@ -78,13 +64,3 @@ void MockClassicNote::update(const microsec &music_offset)
element.animations[_state]->update(music_offset);
}
void MockClassicNote::draw() const
{
for (std::size_t i = 0; i < _elements.size(); ++i)
{
if (i >= 1)
_context->graphics_manager->drawLine(_elements[i-1].sprite->trailCoordinates(), _elements[i].sprite->trailCoordinates());
_context->graphics_manager->draw(_elements[i].sprite);
}
}

View File

@ -31,7 +31,6 @@ public:
virtual void putToGame(const microsec &music_offset) override final;
virtual void update(const microsec &music_offset) override final;
virtual void draw() const override final;
private:
struct MockElement

View File

@ -1,5 +1,5 @@
#include "classicarrownote.h"
#include "graphics/classicgraphicsmanager.h"
#include "game/classicgraphicsmanager.h"
#include "holdmanager.h"
// Replace with interface by dependency injection
@ -18,26 +18,12 @@ ClassicArrowNote::ClassicArrowNote(ArrowNoteInitializer&& init) :
_elements[i].keys = init.elements[i].keys;
_elements[i].coordinates = init.elements[i].element.coordinates;
_elements[i].type = init.elements[i].element.type;
// Animations will be injected into note.
_elements[i].animations[State::NONE] = nullptr;
_elements[i].animations[State::FLYING] = std::make_shared<ClassicFlyingAnimationScenario>();
_elements[i].animations[State::DYING] = std::make_shared<ClassicDyingAnimationScenario>();
_elements[i].animations[State::DEAD] = nullptr;
}
}
void ClassicArrowNote::putToGame(const microsec &music_offset)
{
_state = State::FLYING;
for (auto& element : _elements)
{
element.sprite = _context->graphics_manager->getSprite(element.type);
element.sprite->setCoordinates(element.coordinates);
element.sprite->setTrailCoordinates(Coordinates( 0.f, 9.f ));
element.animations[_state]->launch(element.sprite, music_offset, offset());
}
_state = State::FLYING; (void)music_offset;
}
void ClassicArrowNote::input(PlayerInput&& inputdata)
@ -80,17 +66,6 @@ void ClassicArrowNote::input(PlayerInput&& inputdata)
std::cout << "User input: " << static_cast<int>(grade) << "\n";
}
void ClassicArrowNote::draw() const
{
for (std::size_t i = 0; i < _elements.size(); ++i)
{
if (i >= 1)
_context->graphics_manager->drawLine(_elements[i-1].sprite->trailCoordinates(), _elements[i].sprite->trailCoordinates());
_context->graphics_manager->draw(_elements[i].sprite);
}
}
void ClassicArrowNote::update(const microsec& music_offset)
{
switch (_state)

View File

@ -12,7 +12,6 @@ public:
virtual void putToGame(const microsec& music_offset) override;
virtual void update(const microsec &music_offset) override;
virtual void input(PlayerInput&& inputdata) override;
virtual void draw() const override;
bool allElementsPressed() const;
bool isPressedAs(sf::Keyboard::Key key) const;

View File

@ -1,12 +1,11 @@
#include "classicgame.h"
#include "classicnote.h"
#include "classicmapcreator.h"
#include "graphics/classicgraphicsmanager.h"
#include "game/classicgraphicsmanager.h"
#include "holdmanager.h"
ClassicGame::ClassicGame(std::shared_ptr<ClassicGraphicsManager>&& manager) :
_graphics_manager(std::move(manager)),
_hold_manager(std::make_unique<HoldManager>(_graphics_manager))
ClassicGame::ClassicGame() :
_hold_manager(std::make_unique<HoldManager>())
{
_slap_buffer.loadFromFile("Tick.ogg");
_slap.setBuffer(_slap_buffer);
@ -57,7 +56,6 @@ ClassicGame::~ClassicGame()
void ClassicGame::run()
{
_context.hold_manager = _hold_manager;
_context.graphics_manager = _graphics_manager;
auto beatmap = classic::createBeatmap("aa", _context);
_timeline.setNotes(beatmap.notes);

View File

@ -20,7 +20,7 @@ class HoldManager;
class ClassicGame final : public Game
{
public:
explicit ClassicGame(std::shared_ptr<ClassicGraphicsManager>&& manager);
explicit ClassicGame();
virtual ~ClassicGame() override;
virtual void run() override;
@ -34,7 +34,7 @@ private:
std::map<Type, Action> _buttons_to_pressed_actions;
std::map<Type, Action> _buttons_to_released_actions;
std::shared_ptr<ClassicGraphicsManager> _graphics_manager;
std::unique_ptr<ClassicGraphicsManager> _graphics_manager;
std::shared_ptr<HoldManager> _hold_manager;
Timeline<ClassicNote> _timeline;

View File

@ -0,0 +1,60 @@
#include "classicgraphicsmanager.h"
#include "graphics/classicsprite.h"
ClassicGraphicsManager::ClassicGraphicsManager(Timeline<ClassicNote> &timeline, const microsec& visibility_offset) :
_sprite_container({Type::UP, Type::DOWN,
Type::LEFT, Type::RIGHT},
std::make_unique<ClassicSpriteFactory>()),
_timeline(&timeline),
_visibility_offset(visibility_offset)
{
_timeline->expire(_first);
_timeline->expire(_last);
}
void ClassicGraphicsManager::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
if (nothingToDraw())
return;
std::for_each(_first, _last,
[&target, &states](const auto& note)
{
target.draw(*note->sprite(), states);
});
}
std::shared_ptr<ClassicSprite> ClassicGraphicsManager::getSprite(Type type)
{
return _sprite_container.getSprite(type);
}
void ClassicGraphicsManager::update(const microsec &offset)
{
Iterator note_iterator = _timeline->getTopNote();
while (!_timeline->isExpired(note_iterator) && isVisiblyClose(note_iterator, offset))
{
if (nothingToDraw())
_first = note_iterator;
auto note = *note_iterator;
if (!note->isInGame())
note->putToGame(offset);
++note_iterator;
}
_last = note_iterator;
}
bool ClassicGraphicsManager::nothingToDraw() const noexcept
{
return _timeline->isExpired(_first)
|| _timeline->isExpired(_last);
}
bool ClassicGraphicsManager::isVisiblyClose(const Iterator& iterator, const microsec& music_offset) const noexcept
{
return ((iterator)->offset() - _visibility_offset) <= music_offset;
}

View File

@ -0,0 +1,35 @@
#pragma once
#include "spritecontainer.h"
#include "classicmode/classicactions.h"
#include "graphics/classicspritefactory.h"
#include "classicnote.h"
#include "core/timeline.h"
#include <SFML/Graphics/RenderTarget.hpp>
class ClassicSprite;
class ClassicGraphicsManager : public sf::Drawable
{
public:
explicit ClassicGraphicsManager(Timeline<ClassicNote>& timeline, const microsec& visibility_offset);
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
std::shared_ptr<ClassicSprite> getSprite(Type type);
void update(const microsec& offset);
private:
using Iterator = Timeline<ClassicNote>::Iterator;
Iterator _first;
Iterator _last;
SpriteContainer<Type, ClassicSpriteFactory, ClassicSprite> _sprite_container;
Timeline<ClassicNote> * const _timeline;
microsec _visibility_offset;
inline bool nothingToDraw() const noexcept;
inline bool isVisiblyClose(const Iterator& iterator, const microsec& music_offset) const noexcept;
};

View File

@ -37,7 +37,6 @@ public:
virtual void putToGame(const microsec &music_offset) override = 0;
virtual void update(const microsec &music_offset) override = 0;
virtual void draw() const override = 0;
virtual void input(PlayerInput&& inputdata) = 0;

View File

@ -3,10 +3,6 @@
#include <iostream>
HoldManager::HoldManager(const std::shared_ptr<ClassicGraphicsManager>& graphics_manager) :
_graphics_manager(graphics_manager)
{}
void HoldManager::emplace(ClassicArrowNote* note)
{
_notes_on_hold.emplace_back(note);

View File

@ -10,7 +10,6 @@ class ClassicGraphicsManager;
class HoldManager
{
public:
explicit HoldManager(const std::shared_ptr<ClassicGraphicsManager>& graphics_manager);
void emplace(ClassicArrowNote* note);
void checkRelease(sf::Keyboard::Key released_key);

View File

@ -1,29 +0,0 @@
#include "classicgraphicsmanager.h"
#include "classicsprite.h"
ClassicGraphicsManager::ClassicGraphicsManager(sf::RenderTarget& target) :
_sprite_container({Type::UP, Type::DOWN,
Type::LEFT, Type::RIGHT},
std::make_unique<ClassicSpriteFactory>()),
_render_target(target)
{}
std::shared_ptr<ClassicSprite> ClassicGraphicsManager::getSprite(Type type)
{
return _sprite_container.getSprite(type);
}
void ClassicGraphicsManager::draw(const std::shared_ptr<ClassicSprite>& sprite)
{
_render_target.draw(*sprite);
}
void ClassicGraphicsManager::drawLine(const Coordinates &p1, const Coordinates &p2)
{
sf::VertexArray line(sf::LinesStrip, 2);
line[0].color = sf::Color::Yellow;
line[0].position = {p1.x + 10, p1.y};
line[1].color = sf::Color::Blue;
line[1].position = {p2.x + 10, p2.y};
_render_target.draw(line);
}

View File

@ -1,24 +0,0 @@
#pragma once
#include "spritecontainer.h"
#include "classicmode/classicactions.h"
#include "classicspritefactory.h"
#include <SFML/Graphics/RenderTarget.hpp>
class ClassicSprite;
class ClassicNote;
class ClassicGraphicsManager
{
public:
explicit ClassicGraphicsManager(sf::RenderTarget& target);
std::shared_ptr<ClassicSprite> getSprite(Type type);
void draw(const std::shared_ptr<ClassicSprite> &sprite);
void drawLine(const Coordinates &p1, const Coordinates &p2);
private:
SpriteContainer<Type, ClassicSpriteFactory, ClassicSprite> _sprite_container;
sf::RenderTarget& _render_target;
};

View File

@ -2,11 +2,9 @@
#include <memory>
class ClassicGraphicsManager;
class HoldManager;
struct Context
{
std::shared_ptr<ClassicGraphicsManager> graphics_manager;
std::shared_ptr<HoldManager> hold_manager;
};

View File

@ -10,6 +10,6 @@ namespace sf { class RenderWindow; }
namespace classic
{
std::unique_ptr<Game> initGame(sf::RenderWindow& game_window);
std::unique_ptr<Editor> initEditor(sf::RenderWindow& game_window);
std::unique_ptr<Game> initGame();
std::unique_ptr<Editor> initEditor();
}