From 7dc4ef2bb82a966e17af5c7669e450040c87b8f4 Mon Sep 17 00:00:00 2001 From: NaiJi Date: Thu, 10 Jun 2021 01:47:18 +0300 Subject: [PATCH] Make everything work as before redesign --- src/application.cpp | 2 + src/classicgame/classicgame.cpp | 3 ++ src/classicgame/classicnote.cpp | 12 ++++-- src/classicgame/classictimeline.cpp | 51 +++++++++++++++++++------- src/classicgame/classictimeline.h | 4 +- src/classicgame/classicviewmanager.cpp | 3 ++ 6 files changed, 58 insertions(+), 17 deletions(-) diff --git a/src/application.cpp b/src/application.cpp index dc82972..c9003bf 100644 --- a/src/application.cpp +++ b/src/application.cpp @@ -65,5 +65,7 @@ void Application::update() void Application::draw() { + _game_window.clear(); _game->draw(_game_window); + _game_window.display(); } diff --git a/src/classicgame/classicgame.cpp b/src/classicgame/classicgame.cpp index 609b495..69a678f 100644 --- a/src/classicgame/classicgame.cpp +++ b/src/classicgame/classicgame.cpp @@ -8,6 +8,9 @@ ClassicGame::ClassicGame() : _timeline(std::make_unique()), _view_manager(std::make_unique()) { + _timeline->fetchVisibleNotes(_view_manager); + _timeline->init(); + _keys_to_buttons = { {sf::Keyboard::Up, Button::UP}, // Load from settings diff --git a/src/classicgame/classicnote.cpp b/src/classicgame/classicnote.cpp index 339016c..84c4734 100644 --- a/src/classicgame/classicnote.cpp +++ b/src/classicgame/classicnote.cpp @@ -1,6 +1,7 @@ #include "classicnote.h" #include "classicsprite.h" #include +#include ClassicNote::ClassicNote(const std::vector& intervals, microsec perfect_offset, 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) { + auto grade = ClassicNote::GRADE::BAD; + 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(grade) << "\n"; + + return grade; } Action ClassicNote::action() const @@ -43,7 +48,8 @@ std::shared_ptr ClassicNote::sprite() const noexcept void ClassicNote::setSprite(const std::shared_ptr& sprite) noexcept { _sprite = sprite; - _sprite->setCoordinates(_coordinates.x, _coordinates.y); + if (_sprite) + _sprite->setCoordinates(_coordinates.x, _coordinates.y); } inline const Coordinates& ClassicNote::getCoordinates() const noexcept diff --git a/src/classicgame/classictimeline.cpp b/src/classicgame/classictimeline.cpp index 484f304..7020b91 100644 --- a/src/classicgame/classictimeline.cpp +++ b/src/classicgame/classictimeline.cpp @@ -14,7 +14,6 @@ ClassicTimeline::ClassicTimeline() std::string song_filename = "/home/naiji/METEOR.flac"; _music.openFromFile(song_filename); - _music.play(); _music.setVolume(10); _timeline.reserve(1000); @@ -25,7 +24,7 @@ ClassicTimeline::ClassicTimeline() microsec note_input_offset = 412162; microsec bpm_iterator = starting_beat_offset; 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})); bpm_iterator += interval; @@ -42,14 +41,14 @@ ClassicTimeline::ClassicTimeline() bpm_iterator += interval; } + expire(_last_visible_note); expire(_active_note); _top_note = _timeline.begin(); - _last_visible_note = _top_note; } void ClassicTimeline::init() { - + _music.play(); } ClassicTimeline::~ClassicTimeline() @@ -70,8 +69,7 @@ void ClassicTimeline::clear() void ClassicTimeline::update() { - const auto& offset = currentMusicOffset(); - std::cout << "Upadting at: " << offset << '\n'; + const microsec& offset = currentMusicOffset(); checkCurrentActiveNote(offset); checkForNextActiveNote(offset); } @@ -100,7 +98,7 @@ ClassicTimeline::Iterator ClassicTimeline::getActiveNote() noexcept return _active_note; } -bool ClassicTimeline::isExpired(const Iterator &iterator) +bool ClassicTimeline::isExpired(const Iterator &iterator) const { return iterator == _timeline.end(); } @@ -115,15 +113,40 @@ microsec ClassicTimeline::currentMusicOffset() const return _music.getPlayingOffset().asMicroseconds(); } +void ClassicTimeline::discardExpiredNotes(const std::unique_ptr &view_manager, const microsec &music_offset) +{ + if (_top_note == _timeline.begin()) + return; + + Iterator past_note = _top_note - 1; + std::shared_ptr 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& view_manager) { - Iterator note_iterator = _top_note; - auto music_offset = currentMusicOffset(); + microsec 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) - view_manager->initNoteSprite(*note_iterator); + ClassicNote* note = *note_iterator; + if (!note->sprite()) + view_manager->initNoteSprite(note); + ++note_iterator; } @@ -132,7 +155,9 @@ void ClassicTimeline::fetchVisibleNotes(const std::unique_ptr _last_visible_note) + bool no_visible_notes = isExpired(_last_visible_note) + || _top_note > _last_visible_note; + if (no_visible_notes) return; Iterator note_to_draw = _top_note; diff --git a/src/classicgame/classictimeline.h b/src/classicgame/classictimeline.h index 9c66fcc..c6f05c2 100644 --- a/src/classicgame/classictimeline.h +++ b/src/classicgame/classictimeline.h @@ -26,7 +26,7 @@ public: Iterator getActiveNote() noexcept; - bool isExpired(const Iterator& iterator); + bool isExpired(const Iterator& iterator) const; void expire(Iterator& iterator); private: @@ -41,6 +41,8 @@ private: void checkCurrentActiveNote(const microsec &music_offset); void checkForNextActiveNote(const microsec &music_offset); + void discardExpiredNotes(const std::unique_ptr& view_manager, const microsec &music_offset); + bool isVisiblyClose(const Iterator& iterator, const microsec& music_offset) const; /* Difference between top and active note is that * top note is the note handling input right now diff --git a/src/classicgame/classicviewmanager.cpp b/src/classicgame/classicviewmanager.cpp index b7bad3e..86147dd 100644 --- a/src/classicgame/classicviewmanager.cpp +++ b/src/classicgame/classicviewmanager.cpp @@ -2,6 +2,7 @@ #include "classicsprite.h" #include "classicnote.h" #include +#include static constexpr std::size_t RESERVED_SIZE = 20; @@ -62,10 +63,12 @@ void ClassicViewManager::initNoteSprite(ClassicNote* note) note->setSprite(poll.top()); poll.pop(); + std::cout << "Taking a sprite from poll.\n"; } void ClassicViewManager::resetNoteSprite(ClassicNote* note) { _sprite_dispatcher[note->action()].push(note->sprite()); note->setSprite(nullptr); + std::cout << "Returning a sprite to poll.\n"; }