Make everything work as before redesign

This commit is contained in:
NaiJi ✨ 2021-06-10 01:47:18 +03:00
parent ed300edcf6
commit 7dc4ef2bb8
6 changed files with 58 additions and 17 deletions

View File

@ -65,5 +65,7 @@ void Application::update()
void Application::draw() void Application::draw()
{ {
_game_window.clear();
_game->draw(_game_window); _game->draw(_game_window);
_game_window.display();
} }

View File

@ -8,6 +8,9 @@ ClassicGame::ClassicGame() :
_timeline(std::make_unique<ClassicTimeline>()), _timeline(std::make_unique<ClassicTimeline>()),
_view_manager(std::make_unique<ClassicViewManager>()) _view_manager(std::make_unique<ClassicViewManager>())
{ {
_timeline->fetchVisibleNotes(_view_manager);
_timeline->init();
_keys_to_buttons = _keys_to_buttons =
{ {
{sf::Keyboard::Up, Button::UP}, // Load from settings {sf::Keyboard::Up, Button::UP}, // Load from settings

View File

@ -1,6 +1,7 @@
#include "classicnote.h" #include "classicnote.h"
#include "classicsprite.h" #include "classicsprite.h"
#include <SFML/Graphics/RenderTarget.hpp> #include <SFML/Graphics/RenderTarget.hpp>
#include <iostream>
ClassicNote::ClassicNote(const std::vector<microsec>& intervals, microsec perfect_offset, ClassicNote::ClassicNote(const std::vector<microsec>& intervals, microsec perfect_offset,
Action action, const Coordinates& coord) : Action action, const Coordinates& coord) :
@ -22,12 +23,16 @@ void ClassicNote::draw(sf::RenderTarget& target, sf::RenderStates states) const
ClassicNote::GRADE ClassicNote::input(ClassicInputType&& input_data) ClassicNote::GRADE ClassicNote::input(ClassicInputType&& input_data)
{ {
auto grade = ClassicNote::GRADE::BAD;
if (input_data == _action) if (input_data == _action)
{ {
return _evaluator.calculatePrecision(input_data.timestamp()); grade = _evaluator.calculatePrecision(input_data.timestamp());
} }
return ClassicNote::GRADE::BAD; std::cout << "User input: " << static_cast<int>(grade) << "\n";
return grade;
} }
Action ClassicNote::action() const Action ClassicNote::action() const
@ -43,6 +48,7 @@ std::shared_ptr<ClassicSprite> ClassicNote::sprite() const noexcept
void ClassicNote::setSprite(const std::shared_ptr<ClassicSprite>& sprite) noexcept void ClassicNote::setSprite(const std::shared_ptr<ClassicSprite>& sprite) noexcept
{ {
_sprite = sprite; _sprite = sprite;
if (_sprite)
_sprite->setCoordinates(_coordinates.x, _coordinates.y); _sprite->setCoordinates(_coordinates.x, _coordinates.y);
} }

View File

@ -14,7 +14,6 @@ ClassicTimeline::ClassicTimeline()
std::string song_filename = "/home/naiji/METEOR.flac"; std::string song_filename = "/home/naiji/METEOR.flac";
_music.openFromFile(song_filename); _music.openFromFile(song_filename);
_music.play();
_music.setVolume(10); _music.setVolume(10);
_timeline.reserve(1000); _timeline.reserve(1000);
@ -25,7 +24,7 @@ ClassicTimeline::ClassicTimeline()
microsec note_input_offset = 412162; microsec note_input_offset = 412162;
microsec bpm_iterator = starting_beat_offset; microsec bpm_iterator = starting_beat_offset;
microsec bpm_end = starting_beat_offset + (interval * amount_of_beats); microsec bpm_end = starting_beat_offset + (interval * amount_of_beats);
_visibility_offset = note_input_offset * 12; _visibility_offset = note_input_offset * 6;
_timeline.emplace_back(new ClassicNote({note_input_offset}, bpm_iterator, Action::PRESS_DOWN, {90, 90})); _timeline.emplace_back(new ClassicNote({note_input_offset}, bpm_iterator, Action::PRESS_DOWN, {90, 90}));
bpm_iterator += interval; bpm_iterator += interval;
@ -42,14 +41,14 @@ ClassicTimeline::ClassicTimeline()
bpm_iterator += interval; bpm_iterator += interval;
} }
expire(_last_visible_note);
expire(_active_note); expire(_active_note);
_top_note = _timeline.begin(); _top_note = _timeline.begin();
_last_visible_note = _top_note;
} }
void ClassicTimeline::init() void ClassicTimeline::init()
{ {
_music.play();
} }
ClassicTimeline::~ClassicTimeline() ClassicTimeline::~ClassicTimeline()
@ -70,8 +69,7 @@ void ClassicTimeline::clear()
void ClassicTimeline::update() void ClassicTimeline::update()
{ {
const auto& offset = currentMusicOffset(); const microsec& offset = currentMusicOffset();
std::cout << "Upadting at: " << offset << '\n';
checkCurrentActiveNote(offset); checkCurrentActiveNote(offset);
checkForNextActiveNote(offset); checkForNextActiveNote(offset);
} }
@ -100,7 +98,7 @@ ClassicTimeline::Iterator ClassicTimeline::getActiveNote() noexcept
return _active_note; return _active_note;
} }
bool ClassicTimeline::isExpired(const Iterator &iterator) bool ClassicTimeline::isExpired(const Iterator &iterator) const
{ {
return iterator == _timeline.end(); return iterator == _timeline.end();
} }
@ -115,15 +113,40 @@ microsec ClassicTimeline::currentMusicOffset() const
return _music.getPlayingOffset().asMicroseconds(); return _music.getPlayingOffset().asMicroseconds();
} }
void ClassicTimeline::discardExpiredNotes(const std::unique_ptr<ClassicViewManager> &view_manager, const microsec &music_offset)
{
if (_top_note == _timeline.begin())
return;
Iterator past_note = _top_note - 1;
std::shared_ptr<ClassicSprite> sprite = (*past_note)->sprite();
while (sprite)
{ // CAREFULLY REWRITE
view_manager->resetNoteSprite(*past_note);
if (past_note == _timeline.begin())
return;
--past_note;
sprite = (*past_note)->sprite();
}
}
bool ClassicTimeline::isVisiblyClose(const Iterator &iterator, const microsec &music_offset) const
{
return ((*iterator)->offset() - _visibility_offset) <= music_offset;
}
void ClassicTimeline::fetchVisibleNotes(const std::unique_ptr<ClassicViewManager>& view_manager) void ClassicTimeline::fetchVisibleNotes(const std::unique_ptr<ClassicViewManager>& view_manager)
{ {
Iterator note_iterator = _top_note; microsec music_offset = currentMusicOffset();
auto music_offset = currentMusicOffset(); discardExpiredNotes(view_manager, music_offset);
while (((*note_iterator)->offset() - _visibility_offset) <= music_offset) Iterator note_iterator = _top_note;
while (isVisiblyClose(note_iterator, music_offset))
{ {
if (note_iterator > _last_visible_note) ClassicNote* note = *note_iterator;
view_manager->initNoteSprite(*note_iterator); if (!note->sprite())
view_manager->initNoteSprite(note);
++note_iterator; ++note_iterator;
} }
@ -132,7 +155,9 @@ void ClassicTimeline::fetchVisibleNotes(const std::unique_ptr<ClassicViewManager
void ClassicTimeline::drawVisibleNotes(sf::RenderWindow &window) const void ClassicTimeline::drawVisibleNotes(sf::RenderWindow &window) const
{ {
if (_last_visible_note == _timeline.end() || _top_note > _last_visible_note) bool no_visible_notes = isExpired(_last_visible_note)
|| _top_note > _last_visible_note;
if (no_visible_notes)
return; return;
Iterator note_to_draw = _top_note; Iterator note_to_draw = _top_note;

View File

@ -26,7 +26,7 @@ public:
Iterator getActiveNote() noexcept; Iterator getActiveNote() noexcept;
bool isExpired(const Iterator& iterator); bool isExpired(const Iterator& iterator) const;
void expire(Iterator& iterator); void expire(Iterator& iterator);
private: private:
@ -41,6 +41,8 @@ private:
void checkCurrentActiveNote(const microsec &music_offset); void checkCurrentActiveNote(const microsec &music_offset);
void checkForNextActiveNote(const microsec &music_offset); void checkForNextActiveNote(const microsec &music_offset);
void discardExpiredNotes(const std::unique_ptr<ClassicViewManager>& view_manager, const microsec &music_offset);
bool isVisiblyClose(const Iterator& iterator, const microsec& music_offset) const;
/* Difference between top and active note is that /* Difference between top and active note is that
* top note is the note handling input right now * top note is the note handling input right now

View File

@ -2,6 +2,7 @@
#include "classicsprite.h" #include "classicsprite.h"
#include "classicnote.h" #include "classicnote.h"
#include <SFML/Graphics/RectangleShape.hpp> #include <SFML/Graphics/RectangleShape.hpp>
#include <iostream>
static constexpr std::size_t RESERVED_SIZE = 20; static constexpr std::size_t RESERVED_SIZE = 20;
@ -62,10 +63,12 @@ void ClassicViewManager::initNoteSprite(ClassicNote* note)
note->setSprite(poll.top()); note->setSprite(poll.top());
poll.pop(); poll.pop();
std::cout << "Taking a sprite from poll.\n";
} }
void ClassicViewManager::resetNoteSprite(ClassicNote* note) void ClassicViewManager::resetNoteSprite(ClassicNote* note)
{ {
_sprite_dispatcher[note->action()].push(note->sprite()); _sprite_dispatcher[note->action()].push(note->sprite());
note->setSprite(nullptr); note->setSprite(nullptr);
std::cout << "Returning a sprite to poll.\n";
} }