1
0
Files
BasaltMeter/BasaltPresenter/Shared/basalt/anime_loader.cpp

60 lines
2.0 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include "anime_loader.hpp"
using ::basalt::shared::math::FloatPoint;
namespace basalt::shared::anime_loader {
#pragma region Anime Loader
IAnimeLoader::IAnimeLoader() : status(AnimeLoaderStatus::Ready), time(0) {}
IAnimeLoader::~IAnimeLoader() {}
void IAnimeLoader::load(AnimeLoaderConfig&& config) {
if (this->status != AnimeLoaderStatus::Ready) throw std::runtime_error("unexpected anime loader status");
this->config = std::move(config);
this->status = AnimeLoaderStatus::Loaded;
}
KeyFrameSpan IAnimeLoader::tick() {
if (this->status != AnimeLoaderStatus::Loaded) throw std::runtime_error("unexpected anime loader status");
// Get last frame index
auto last_pair = this->frames.end();
--last_pair;
auto last_index = last_pair->first;
// Accumulate time
++this->time;
if (this->time > last_index) {
this->time = 0;
}
// 首先用lower_bound获取到不小于自身的项目肯定能获取到
auto lower_bound = this->frames.lower_bound(this->time);
// 获取下一帧,如果没有下一帧,就转换为自身
auto upper_bound = lower_bound;
++upper_bound;
if (upper_bound == this->frames.end()) {
upper_bound = lower_bound;
}
// 计算归一化时间
FloatPoint next_time = static_cast<FloatPoint>(this->time - lower_bound->first)
/ static_cast<FloatPoint>(upper_bound->first - lower_bound->first + 1);
FloatPoint prev_time = static_cast<FloatPoint>(1) - next_time;
// 返回结构
return KeyFrameSpan{
.prev_time = prev_time,
.next_time = next_time,
.prev_position = lower_bound->second.position,
.next_position = upper_bound->second.position,
.prev_rotation = lower_bound->second.rotation,
.next_rotation = upper_bound->second.rotation,
};
}
#pragma endregion
} // namespace basalt::shared::anime_loader