2021-04-08 15:51:59 -04:00
|
|
|
#include <iostream>
|
2021-06-07 14:19:58 -04:00
|
|
|
#include "classicactions.h"
|
|
|
|
#include "classictimeline.h"
|
2021-06-08 14:32:36 -04:00
|
|
|
#include "classicnote.h"
|
2021-06-09 14:08:58 -04:00
|
|
|
#include "classicviewmanager.h"
|
|
|
|
#include <SFML/Graphics/RenderTarget.hpp>
|
2021-04-08 15:51:59 -04:00
|
|
|
|
2021-06-07 14:19:58 -04:00
|
|
|
ClassicTimeline::ClassicTimeline()
|
2021-04-08 09:34:03 -04:00
|
|
|
{
|
|
|
|
// BPM of METEOR is 170.
|
|
|
|
// Length is 1:14
|
|
|
|
// I calculated that the time between beats is about 1412162 microseconds
|
|
|
|
|
2021-06-08 14:32:36 -04:00
|
|
|
std::string song_filename = "/home/naiji/METEOR.flac";
|
|
|
|
|
|
|
|
_music.openFromFile(song_filename);
|
2021-06-09 14:08:58 -04:00
|
|
|
_music.setVolume(10);
|
2021-06-08 14:32:36 -04:00
|
|
|
|
2021-04-09 09:28:45 -04:00
|
|
|
_timeline.reserve(1000);
|
|
|
|
|
2021-04-08 09:34:03 -04:00
|
|
|
microsec starting_beat_offset = 372162;
|
|
|
|
int amount_of_beats = 209;
|
2021-04-09 09:28:45 -04:00
|
|
|
microsec interval = 1412162;
|
2021-04-08 09:34:03 -04:00
|
|
|
microsec note_input_offset = 412162;
|
2021-04-09 09:28:45 -04:00
|
|
|
microsec bpm_iterator = starting_beat_offset;
|
|
|
|
microsec bpm_end = starting_beat_offset + (interval * amount_of_beats);
|
2021-06-09 18:47:18 -04:00
|
|
|
_visibility_offset = note_input_offset * 6;
|
2021-04-08 09:34:03 -04:00
|
|
|
|
2021-06-09 14:08:58 -04:00
|
|
|
_timeline.emplace_back(new ClassicNote({note_input_offset}, bpm_iterator, Action::PRESS_DOWN, {90, 90}));
|
2021-04-17 12:14:36 -04:00
|
|
|
bpm_iterator += interval;
|
|
|
|
|
2021-06-09 14:08:58 -04:00
|
|
|
_timeline.emplace_back(new ClassicNote({note_input_offset}, bpm_iterator, Action::PRESS_LEFT, {190, 90}));
|
2021-04-17 12:14:36 -04:00
|
|
|
bpm_iterator += interval;
|
|
|
|
|
2021-06-09 14:08:58 -04:00
|
|
|
_timeline.emplace_back(new ClassicNote({note_input_offset}, bpm_iterator, Action::PRESS_LEFT, {290, 90}));
|
2021-04-17 12:14:36 -04:00
|
|
|
bpm_iterator += interval;
|
|
|
|
|
2021-04-09 09:28:45 -04:00
|
|
|
while (bpm_iterator < bpm_end)
|
2021-04-08 09:34:03 -04:00
|
|
|
{
|
2021-06-09 14:08:58 -04:00
|
|
|
_timeline.emplace_back(new ClassicNote({note_input_offset}, bpm_iterator, Action::PRESS_UP, {390, 390}));
|
2021-04-09 09:28:45 -04:00
|
|
|
bpm_iterator += interval;
|
2021-04-08 09:34:03 -04:00
|
|
|
}
|
|
|
|
|
2021-06-09 18:47:18 -04:00
|
|
|
expire(_last_visible_note);
|
2021-06-08 14:32:36 -04:00
|
|
|
expire(_active_note);
|
2021-04-08 09:34:03 -04:00
|
|
|
_top_note = _timeline.begin();
|
|
|
|
}
|
|
|
|
|
2021-06-09 14:08:58 -04:00
|
|
|
void ClassicTimeline::init()
|
|
|
|
{
|
2021-06-09 18:47:18 -04:00
|
|
|
_music.play();
|
2021-06-09 14:08:58 -04:00
|
|
|
}
|
|
|
|
|
2021-06-08 14:32:36 -04:00
|
|
|
ClassicTimeline::~ClassicTimeline()
|
2021-04-09 09:28:45 -04:00
|
|
|
{
|
|
|
|
clear();
|
|
|
|
}
|
|
|
|
|
2021-06-08 14:32:36 -04:00
|
|
|
void ClassicTimeline::clear()
|
2021-04-08 09:34:03 -04:00
|
|
|
{
|
|
|
|
for (auto note : _timeline)
|
|
|
|
delete note;
|
|
|
|
|
|
|
|
_timeline.clear();
|
2021-06-08 14:32:36 -04:00
|
|
|
expire(_top_note);
|
|
|
|
expire(_last_visible_note);
|
|
|
|
expire(_active_note);
|
2021-04-08 09:34:03 -04:00
|
|
|
}
|
|
|
|
|
2021-06-08 14:32:36 -04:00
|
|
|
void ClassicTimeline::update()
|
2021-04-08 09:34:03 -04:00
|
|
|
{
|
2021-06-09 18:47:18 -04:00
|
|
|
const microsec& offset = currentMusicOffset();
|
2021-06-08 14:32:36 -04:00
|
|
|
checkCurrentActiveNote(offset);
|
|
|
|
checkForNextActiveNote(offset);
|
2021-04-08 09:34:03 -04:00
|
|
|
}
|
|
|
|
|
2021-06-08 14:32:36 -04:00
|
|
|
void ClassicTimeline::checkCurrentActiveNote(const microsec &music_offset)
|
2021-04-08 09:34:03 -04:00
|
|
|
{
|
2021-06-08 14:32:36 -04:00
|
|
|
if (!isExpired(_active_note) && !(*_active_note)->isActive(music_offset))
|
2021-04-08 09:34:03 -04:00
|
|
|
{
|
2021-06-08 14:32:36 -04:00
|
|
|
expire(_active_note);
|
2021-04-08 09:34:03 -04:00
|
|
|
++_top_note;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-08 14:32:36 -04:00
|
|
|
void ClassicTimeline::checkForNextActiveNote(const microsec &music_offset)
|
2021-04-08 09:34:03 -04:00
|
|
|
{
|
2021-06-08 14:32:36 -04:00
|
|
|
if (isExpired(_active_note) && (*_top_note)->isActive(music_offset))
|
2021-04-08 09:34:03 -04:00
|
|
|
{
|
2021-04-09 09:28:45 -04:00
|
|
|
std::cout << "New active note: " << music_offset << '\n';
|
2021-06-08 14:32:36 -04:00
|
|
|
_active_note = _top_note;
|
2021-04-08 09:34:03 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-08 14:32:36 -04:00
|
|
|
ClassicTimeline::Iterator ClassicTimeline::getActiveNote() noexcept
|
2021-04-08 09:34:03 -04:00
|
|
|
{
|
2021-06-08 14:32:36 -04:00
|
|
|
update();
|
2021-04-08 09:34:03 -04:00
|
|
|
return _active_note;
|
|
|
|
}
|
2021-06-08 14:32:36 -04:00
|
|
|
|
2021-06-09 18:47:18 -04:00
|
|
|
bool ClassicTimeline::isExpired(const Iterator &iterator) const
|
2021-06-08 14:32:36 -04:00
|
|
|
{
|
|
|
|
return iterator == _timeline.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClassicTimeline::expire(Iterator &iterator)
|
|
|
|
{
|
|
|
|
iterator = _timeline.end();
|
|
|
|
}
|
|
|
|
|
|
|
|
microsec ClassicTimeline::currentMusicOffset() const
|
|
|
|
{
|
|
|
|
return _music.getPlayingOffset().asMicroseconds();
|
|
|
|
}
|
|
|
|
|
2021-06-09 18:47:18 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-06-08 14:32:36 -04:00
|
|
|
void ClassicTimeline::fetchVisibleNotes(const std::unique_ptr<ClassicViewManager>& view_manager)
|
|
|
|
{
|
2021-06-09 18:47:18 -04:00
|
|
|
microsec music_offset = currentMusicOffset();
|
|
|
|
discardExpiredNotes(view_manager, music_offset);
|
2021-06-08 14:32:36 -04:00
|
|
|
|
2021-06-09 18:47:18 -04:00
|
|
|
Iterator note_iterator = _top_note;
|
|
|
|
while (isVisiblyClose(note_iterator, music_offset))
|
2021-06-08 14:32:36 -04:00
|
|
|
{
|
2021-06-09 18:47:18 -04:00
|
|
|
ClassicNote* note = *note_iterator;
|
|
|
|
if (!note->sprite())
|
|
|
|
view_manager->initNoteSprite(note);
|
|
|
|
|
2021-06-09 14:08:58 -04:00
|
|
|
++note_iterator;
|
2021-06-08 14:32:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
_last_visible_note = note_iterator;
|
|
|
|
}
|
2021-06-09 14:08:58 -04:00
|
|
|
|
|
|
|
void ClassicTimeline::drawVisibleNotes(sf::RenderWindow &window) const
|
|
|
|
{
|
2021-06-09 18:47:18 -04:00
|
|
|
bool no_visible_notes = isExpired(_last_visible_note)
|
|
|
|
|| _top_note > _last_visible_note;
|
|
|
|
if (no_visible_notes)
|
2021-06-09 14:08:58 -04:00
|
|
|
return;
|
|
|
|
|
|
|
|
Iterator note_to_draw = _top_note;
|
|
|
|
while (note_to_draw != (_last_visible_note))
|
|
|
|
{
|
|
|
|
window.draw(*(*note_to_draw));
|
|
|
|
++note_to_draw;
|
|
|
|
}
|
|
|
|
}
|