Separate sprite graphics from note logic

This commit is contained in:
NaiJi ✨ 2022-01-18 04:15:59 +01:00
parent 017f400ad9
commit c576686752
8 changed files with 166 additions and 13 deletions

View File

@ -8,10 +8,9 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}/shared)
# check SFML by some compilation flag or idk
file(GLOB_RECURSE SOURCES "sfml/*.cpp" "sfml/*.h")
find_package(SFML REQUIRED graphics window system)
find_package(SFML REQUIRED graphics window system audio)
include_directories(${SFML_INCL_DIR} ${CMAKE_SOURCE_DIR}/include)
target_link_libraries(project-kyoku sfml-system sfml-audio sfml-graphics sfml-network)
include_directories(${SFML_INCL_DIR} ${CMAKE_SOURCE_DIR}/include)
######
add_library(impl STATIC ${SOURCES})

View File

@ -22,6 +22,6 @@ public:
virtual void display() override;
protected:
std::shared_ptr<sf::RenderTarget> _render_target;
sf::RenderTarget * const _render_target;
sf::Text _text;
};

View File

@ -1,20 +1,18 @@
#pragma once
#include "core/spritecontainer.h"
#include "core/time.h"
#include "classicmode/classicactions.h"
#include "graphics/classicspritefactory.h"
class ClassicSprite;
#include <memory>
#include <vector>
struct ArrowElement;
class ClassicGraphicsManager : public std::enable_shared_from_this<ClassicGraphicsManager>
{
public:
virtual ~ClassicGraphicsManager() = default;
explicit ClassicGraphicsManager(const std::shared_ptr<ClassicSpriteFactory>& factory, const kku::microsec& visibility_offset) :
_sprite_container({Type::UP, Type::DOWN,
Type::LEFT, Type::RIGHT},
factory),
explicit ClassicGraphicsManager(const kku::microsec& visibility_offset) :
_visibility_offset(visibility_offset)
{}
@ -25,6 +23,5 @@ public:
virtual void update(const kku::microsec& offset) = 0;
protected:
kku::SpriteContainer<Type, ClassicSpriteFactory, ClassicSprite> _sprite_container;
kku::microsec _visibility_offset;
};

View File

@ -9,7 +9,10 @@
ClassicSceneGraphicsManager::ClassicSceneGraphicsManager(const std::shared_ptr<kku::Timeline<ClassicNote>>& timeline,
const std::shared_ptr<ClassicSpriteFactory>& factory,
const kku::microsec& visibility_offset) :
ClassicGraphicsManager(factory, visibility_offset),
ClassicGraphicsManager(visibility_offset),
_sprite_container({Type::UP, Type::DOWN,
Type::LEFT, Type::RIGHT},
factory),
_timeline(timeline)
{
_timeline->expire(_first);

View File

@ -2,7 +2,9 @@
#include "game/classicnote.h"
#include "graphics/classicgraphicsmanager.h"
#include "graphics/classicspritefactory.h"
#include "core/timeline.h"
#include "core/spritecontainer.h"
class ClassicSprite;
@ -20,6 +22,7 @@ public:
virtual void setGraphics(std::vector<ArrowElement>& elements, kku::TimeRange&& range) override;
protected:
kku::SpriteContainer<Type, ClassicSpriteFactory, ClassicSprite> _sprite_container;
using Iterator = kku::Timeline<ClassicNote>::Iterator;
Iterator _first;

View File

@ -0,0 +1,112 @@
#include "classictimelinegraphicsmanager.h"
#include "graphics/classicsprite.h"
#include "game/arrowelement.h"
#include "graphics/animations/classicflyinganimationscenario.h"
#include "graphics/animations/classicdyinganimationscenario.h"
ClassicTimelineGraphicsManager::ClassicTimelineGraphicsManager(const std::shared_ptr<kku::Timeline<ClassicNote>>& timeline,
const std::shared_ptr<ClassicSpriteFactory>& factory,
const kku::microsec& visibility_offset) :
ClassicGraphicsManager(factory, visibility_offset),
_timeline(timeline),
_display_offset(0)
{
_timeline->expire(_first);
_timeline->expire(_last);
_sprites[Type::NONE] = nullptr;
_sprites[Type::UP] = factory->getSprite(Type::UP);
_sprites[Type::RIGHT] = factory->getSprite(Type::RIGHT);
_sprites[Type::DOWN] = factory->getSprite(Type::DOWN);
_sprites[Type::LEFT] = factory->getSprite(Type::LEFT);
//_sprites[Type::SLIDER_RIGHT] = factory->getSprite(Type::SLIDER_RIGHT);
//_sprites[Type::SLIDER_LEFT] = factory->getSprite(Type::SLIDER_LEFT);
}
void ClassicTimelineGraphicsManager::display() const
{
if (nothingToDraw())
return;
for (auto it = _first; it != _last; ++it)
{
const auto& note = (*it);
note->display(this);
}
}
void ClassicTimelineGraphicsManager::update(const kku::microsec &offset)
{
_timeline->expire(_first);
_timeline->expire(_last);
fetchLastNote(offset);
fetchFirstNote(offset);
_display_offset = offset;
}
void ClassicTimelineGraphicsManager::display(const std::vector<ArrowElement>& elements) const
{
for (std::size_t i = 0; i < elements.size(); ++i)
{
auto sprite = *_sprites.at(elements.at(i).type)
if (i >= 1)
{
//const auto& neighbor_sprite = elements[i - 1].sprite;
//const auto c1 = neighbor_sprite->trailPosition();
//const auto c2 = sprite->trailPosition();
//_render_target->draw(makeLine(c1, c2));
}
sprite->setPosition(kku::Position{});
sprite->display();
}
}
void ClassicTimelineGraphicsManager::fetchFirstNote(const kku::microsec& offset)
{
if (nothingToDraw())
return;
Iterator note_iterator = _first;
while (!_timeline->isExpired(note_iterator) && isVisiblyClose(note_iterator, offset))
{
--note_iterator;
}
_first = note_iterator;
}
void ClassicTimelineGraphicsManager::fetchLastNote(const kku::microsec& offset)
{
Iterator note_iterator = _timeline->getTopNote();
while (!_timeline->isExpired(note_iterator) && isVisiblyClose(note_iterator, offset))
{
if (nothingToDraw())
_first = note_iterator;
++note_iterator;
}
_last = note_iterator;
}
bool ClassicTimelineGraphicsManager::nothingToDraw() const noexcept
{
return _timeline->isExpired(_first)
|| _timeline->isExpired(_last);
}
bool ClassicTimelineGraphicsManager::isVisiblyClose(const Iterator& iterator, const kku::microsec& music_offset) const noexcept
{
const auto& perfect_offset = (*iterator)->getPerfectOffset();
return ((perfect_offset - _visibility_offset) <= music_offset)
|| ((perfect_offset + (_visibility_offset / 4.)) >= music_offset);
}

View File

@ -0,0 +1,37 @@
#pragma once
#include "game/classicnote.h"
#include "graphics/classicgraphicsmanager.h"
#include "core/timeline.h"
class ClassicSprite;
class ClassicTimelineGraphicsManager : public ClassicGraphicsManager
{
public:
explicit ClassicTimelineGraphicsManager(const std::shared_ptr<kku::Timeline<ClassicNote>>& timeline,
const std::shared_ptr<ClassicSpriteFactory>& factory,
const kku::microsec& visibility_offset);
virtual void display() const override;
virtual void update(const kku::microsec& offset) override;
virtual void display(const std::vector<ArrowElement>& elements) const override;
protected:
using Iterator = kku::Timeline<ClassicNote>::Iterator;
Iterator _first;
Iterator _last;
const std::shared_ptr<kku::Timeline<ClassicNote>> _timeline;
kku::microsec _display_offset;
mutable std::map<std::shared_ptr<ClassicSprite>, Type::COUNT> _sprites;
inline bool nothingToDraw() const noexcept;
inline bool isVisiblyClose(const Iterator& iterator, const kku::microsec& music_offset) const noexcept;
void fetchFirstNote(const kku::microsec& offset);
void fetchLastNote(const kku::microsec& offset);
};

View File

@ -25,5 +25,7 @@ enum class Type
LEFT,
SLIDER_RIGHT,
SLIDER_LEFT
SLIDER_LEFT,
COUNT
};