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

60 lines
2.0 KiB
C++
Raw Normal View History

2026-01-08 19:23:19 +08:00
#include "anime_loader.hpp"
2026-01-09 16:40:30 +08:00
using ::basalt::shared::math::FloatPoint;
2026-01-08 19:23:19 +08:00
namespace basalt::shared::anime_loader {
2026-01-09 16:40:30 +08:00
#pragma region Anime Loader
2026-01-09 16:49:06 +08:00
IAnimeLoader::IAnimeLoader() : status(AnimeLoaderStatus::Ready), time(0) {}
2026-01-09 16:40:30 +08:00
IAnimeLoader::~IAnimeLoader() {}
2026-01-09 16:49:06 +08:00
void IAnimeLoader::load(AnimeLoaderConfig&& config) {
2026-01-09 16:40:30 +08:00
if (this->status != AnimeLoaderStatus::Ready) throw std::runtime_error("unexpected anime loader status");
2026-01-09 16:49:06 +08:00
this->config = std::move(config);
2026-01-09 16:40:30 +08:00
this->status = AnimeLoaderStatus::Loaded;
2026-01-08 19:23:19 +08:00
}
2026-01-09 16:40:30 +08:00
KeyFrameSpan IAnimeLoader::tick() {
if (this->status != AnimeLoaderStatus::Loaded) throw std::runtime_error("unexpected anime loader status");
2026-01-08 19:23:19 +08:00
2026-01-09 16:49:06 +08:00
// Get last frame index
auto last_pair = this->frames.end();
--last_pair;
auto last_index = last_pair->first;
2026-01-09 16:40:30 +08:00
// Accumulate time
++this->time;
2026-01-09 16:49:06 +08:00
if (this->time > last_index) {
2026-01-09 16:40:30 +08:00
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,
};
2026-01-08 19:23:19 +08:00
}
2026-01-09 16:40:30 +08:00
#pragma endregion
} // namespace basalt::shared::anime_loader