forked from NaiJi/project-kyoku
Start refactoring graphics manager
parent
4be1429546
commit
ff07b628ac
@ -1,18 +1,18 @@
|
|||||||
#include "shared/classicmode/classicfactory.h"
|
#include "shared/classicmode/classicfactory.h"
|
||||||
#include "game/classicgame.h"
|
#include "game/classicgame.h"
|
||||||
#include "graphics/classicgraphicsmanager.h"
|
#include "game/classicgraphicsmanager.h"
|
||||||
#include "tools/music.h"
|
#include "tools/music.h"
|
||||||
|
|
||||||
#include "editor/classiceditor.h"
|
#include "editor/classiceditor.h"
|
||||||
|
|
||||||
#include <SFML/Graphics/RenderWindow.hpp>
|
#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>();
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
}
|
@ -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;
|
||||||
|
};
|
@ -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);
|
|
||||||
}
|
|
@ -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;
|
|
||||||
};
|
|
Loading…
Reference in New Issue