Force update active note on tap

This commit is contained in:
NaiJi ✨ 2021-04-08 19:08:01 +03:00
parent 367316b327
commit a4d7d26e98
5 changed files with 29 additions and 23 deletions

View File

@ -1,5 +1,5 @@
#include "application.h" #include "application.h"
#include "note.h"
#include <SFML/Graphics/Color.hpp> #include <SFML/Graphics/Color.hpp>
#include <SFML/Window/Event.hpp> #include <SFML/Window/Event.hpp>
@ -9,11 +9,12 @@ Application::Application() :
_game_window({1280, 720}, "Test"), _game_window({1280, 720}, "Test"),
_debug(true) _debug(true)
{ {
_font.loadFromFile("VeraMono.ttf"); _font.loadFromFile("/usr/share/qtcreator/fonts/SourceCodePro-Regular.ttf");
_grade.setFont(_font); _grade.setFont(_font);
_grade.setPosition(160, 160); _grade.setPosition(160, 160);
_grade.setFillColor(sf::Color(255, 255, 255, 0)); _grade.setFillColor(sf::Color(255, 0, 0, 0));
_grade.setCharacterSize(35); _grade.setCharacterSize(35);
_grade.setString("NOT INIT");
} }
void Application::run() void Application::run()
@ -22,7 +23,7 @@ void Application::run()
_music.openFromFile(song_filename); _music.openFromFile(song_filename);
_music.play(); _music.play();
_music.setVolume(5); _music.setVolume(2);
_game_window.display(); _game_window.display();
@ -141,25 +142,25 @@ void Application::onKeyPressed(const sf::Keyboard::Key &key)
return; return;
} }
const auto arrow = keyToArrow(key); onTap(keyToArrow(key));
}
void Application::onTap(const Note::Arrow &arrow)
{
if (arrow == Note::Arrow::NONE)
return;
if (arrow != Note::Arrow::NONE)
{ // TODO: SHIT BLOCK.
_debug.spawnGreenPulse(); _debug.spawnGreenPulse();
const auto note = _timeline.getActiveNote();
const auto music_offset = _music.getPlayingOffset().asMicroseconds();
const auto note = _timeline.fetchActiveNote(music_offset);
if (note) if (note)
{ {
// This is obscure. Active note on timeline gets received by last ::update() call.
// there can be 100-200 microseconds delay between onKeyPressed and update...
// Also the problem is that we get music offset by CURRENT music time,
// when active note is activated by music time of last ::update call
// anyway gotta think on it, smh smh smh
const auto music_offset = _music.getPlayingOffset().asMicroseconds();
const auto tap_result = note->onTap(arrow, music_offset); const auto tap_result = note->onTap(arrow, music_offset);
_grade = makeGradeString(tap_result.rating); _grade = makeGradeString(tap_result.rating);
} }
} }
}
void Application::update() void Application::update()
{ {

View File

@ -7,6 +7,7 @@
#include "debughelper.h" #include "debughelper.h"
#include "timeline.h" #include "timeline.h"
#include "note.h"
class Application class Application
{ {
@ -24,11 +25,14 @@ private:
sf::Font _font; sf::Font _font;
sf::Text _grade; sf::Text _grade;
sf::Text _tap_time;
Timeline _timeline; Timeline _timeline;
DebugHelper _debug; DebugHelper _debug;
void startGameLoop(); void startGameLoop();
void onKeyPressed(const sf::Keyboard::Key& key); void onKeyPressed(const sf::Keyboard::Key& key);
void onTap(const Note::Arrow& arrow);
}; };
#endif // APPLICATION_H #endif // APPLICATION_H

View File

@ -6,7 +6,7 @@ DebugHelper::DebugHelper(bool init) :
_green_pulse({460.f, 0.f}, sf::Color(0, 255, 0)), _green_pulse({460.f, 0.f}, sf::Color(0, 255, 0)),
_blue_pulse({460.f, 360.f}, sf::Color(0, 100, 255)) _blue_pulse({460.f, 360.f}, sf::Color(0, 100, 255))
{ {
_font.loadFromFile("VeraMono.ttf"); _font.loadFromFile("/usr/share/qtcreator/fonts/SourceCodePro-Regular.ttf");
_time_print.setFont(_font); _time_print.setFont(_font);
_time_print.setPosition(60, 60); _time_print.setPosition(60, 60);

View File

@ -60,7 +60,8 @@ void Timeline::checkForNextActiveNote(const microsec &microseconds)
} }
} }
const Note* Timeline::getActiveNote() const noexcept const Note* Timeline::fetchActiveNote(const microsec &microseconds) noexcept
{ {
update(microseconds);
return _active_note; return _active_note;
} }

View File

@ -16,7 +16,7 @@ public:
~Timeline(); ~Timeline();
void update(const microsec& microseconds); void update(const microsec& microseconds);
const Note* getActiveNote() const noexcept; const Note* fetchActiveNote(const microsec &microseconds) noexcept;
private: private:
std::vector<Note*> _timeline; std::vector<Note*> _timeline;