project-kyoku/src/classicgame/classictimeline.cpp

110 lines
2.8 KiB
C++
Raw Normal View History

2021-04-08 15:51:59 -04:00
#include <iostream>
2021-06-07 14:19:58 -04:00
#include "classicactions.h"
#include "classictimeline.h"
#include "note.h"
2021-04-08 15:51:59 -04:00
2021-06-07 14:19:58 -04:00
ClassicTimeline::ClassicTimeline()
{
// BPM of METEOR is 170.
// Length is 1:14
// I calculated that the time between beats is about 1412162 microseconds
_timeline.reserve(1000);
microsec starting_beat_offset = 372162;
int amount_of_beats = 209;
microsec interval = 1412162;
microsec note_input_offset = 412162;
microsec bpm_iterator = starting_beat_offset;
microsec bpm_end = starting_beat_offset + (interval * amount_of_beats);
2021-04-17 12:14:36 -04:00
_visibility_offset = note_input_offset * 12;
2021-06-07 14:19:58 -04:00
_timeline.emplace_back(new Note(bpm_iterator, note_input_offset, Button::DOWN));
2021-04-17 12:14:36 -04:00
bpm_iterator += interval;
2021-06-07 14:19:58 -04:00
_timeline.emplace_back(new Note(bpm_iterator, note_input_offset, Button::LEFT));
2021-04-17 12:14:36 -04:00
bpm_iterator += interval;
2021-06-07 14:19:58 -04:00
_timeline.emplace_back(new Note(bpm_iterator, note_input_offset, Button::LEFT));
2021-04-17 12:14:36 -04:00
bpm_iterator += interval;
while (bpm_iterator < bpm_end)
{
_timeline.emplace_back(new Note(bpm_iterator, note_input_offset));
bpm_iterator += interval;
}
_active_note = nullptr;
_last_visible_note = _timeline.end();
_top_note = _timeline.begin();
2021-04-17 12:14:36 -04:00
_last_visible_note = _top_note;
_view_manager->initNoteGraphics(*_top_note);
prepareNotesToDraw(0);
}
void Timeline::prepareNotesToDraw(const microsec &music_offset)
{
auto note_iterator = _top_note;
2021-04-17 12:14:36 -04:00
while (((*note_iterator)->offset() - _visibility_offset) <= music_offset)
{
++note_iterator;
2021-04-17 12:14:36 -04:00
if (note_iterator > _last_visible_note)
_view_manager->initNoteGraphics((*note_iterator));
}
_last_visible_note = note_iterator;
}
Timeline::~Timeline()
{
clear();
}
void Timeline::clear()
{
for (auto note : _timeline)
delete note;
_timeline.clear();
_top_note = _timeline.end();
_last_visible_note = _timeline.end();
_active_note = nullptr;
Note::resetPrecisionQualifier();
}
void Timeline::update(const microsec &music_offset)
{
checkCurrentActiveNote(music_offset);
checkForNextActiveNote(music_offset);
prepareNotesToDraw(music_offset);
}
void Timeline::checkCurrentActiveNote(const microsec &music_offset)
{
if (_active_note && !_active_note->isActive(music_offset))
{
_active_note = nullptr;
2021-04-17 12:14:36 -04:00
(*_top_note)->resetSprite();
++_top_note;
}
}
void Timeline::checkForNextActiveNote(const microsec &music_offset)
{
if (!_active_note && (*_top_note)->isActive(music_offset))
{
std::cout << "New active note: " << music_offset << '\n';
_active_note = *_top_note;
}
}
2021-04-15 11:03:35 -04:00
Note* Timeline::fetchActiveNote(const microsec &music_offset) noexcept
{
std::cout << "Clicked at: " << music_offset << '\n';
update(music_offset);
return _active_note;
}