Files
dl_old/dl/render/dl_animation.cpp
2026-03-24 19:02:04 +08:00

200 lines
2.9 KiB
C++
Raw Permalink 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 "dl_animation.h"
#include "system/dl_time.h"
namespace dl
{
void Animation::Replay(unsigned tkc)
{
if (!tkc)
tkc = g_time->GetFrameTkc();
_state = AnimationState::NORMAL;
_tkcBegin = tkc;
_tkcPlayed = 0;
_cur = 0;
_count = 0;
}
void Animation::Play(unsigned tkc)
{
if (!tkc)
tkc = g_time->GetFrameTkc();
_state = AnimationState::NORMAL;
if (_tkcPlayed <= tkc)
{
_tkcBegin = tkc - _tkcPlayed;
}
else
{
assert(0);
_tkcBegin = tkc;
}
}
void Animation::Pause(unsigned tkc)
{
if (!tkc)
tkc = g_time->GetFrameTkc();
_state = AnimationState::PAUSE;
_tkcPlayed = _get_played(tkc);
}
void Animation::Stop()
{
_state = AnimationState::STOP;
_tkcPlayed = 0;
_cur = 0;
_count = 0;
}
void Animation::Render(unsigned tkc)
{
if (!tkc)
tkc = g_time->GetFrameTkc();
auto fn_play_end = [&](){
_tkcBegin = tkc;
switch (_end)
{
case dl::AnimationEnd::STOP:
_vecSpr.back()->Render();
break;
case dl::AnimationEnd::DISAPPEAR:
break;
case dl::AnimationEnd::START:
_vecSpr[0]->Render();
break;
default:
assert(0);
}
};
if (_vecSpr.empty())
return;
if (IsPlayEnd())
{
fn_play_end();
return;
}
if (tkc < _tkcBegin)
{// 时间倒流
assert(0);
_vecSpr[_cur]->Render();
return;
}
if (_state == AnimationState::PAUSE)
{
_vecSpr[_cur]->Render();
return;
}
else if (_state == AnimationState::STOP)
{
_vecSpr[0]->Render();
return;
}
do
{
unsigned ftime = _vecFtime[_cur];
unsigned d = _get_played(tkc);
if (d >= ftime)
{
// 下一帧
++_cur;
if (_cur < _vecSpr.size())
{
_tkcBegin += ftime;
}
else
{// 循环完一次
_tkcBegin = tkc;
_cur = 0;
++_count;
// 超出次数,结束处理
if (IsPlayEnd())
{
fn_play_end();
return;
}
}
}
else
break;
} while (true);
_vecSpr[_cur]->Render();
}
void Animation::PushBack(gcSprite spr, unsigned ftime)
{
spr->SetOrder(_order);
spr->SetColor(_color);
_vecSpr.push_back(spr);
_vecFtime.push_back(ftime);
}
void Animation::SetFrameTime(unsigned ftime)
{
for (auto& iter : _vecFtime)
{
iter = ftime;
}
}
unsigned Animation::GetFrameTime(size_t i)
{
if (i >= _vecFtime.size())
return 0;
return _vecFtime[i];
}
std::string Animation::Dump(unsigned tkc)
{
if (!tkc)
tkc = g_time->GetFrameTkc();
size_t t = (size_t)_get_played(tkc);
size_t t_max = (size_t)GetFrameTime(_cur);
//assert(t < t_max);
std::string str_t;
str_t.resize(t, ':');
str_t.resize(t_max, '.');
std::string str_k;
str_k.resize(_cur, ':');
str_k.resize(_vecFtime.size(), '.');
return fmt::format(
"{}({})\n"
"帧配置:{}\n"
"倍数:{}\n"
"播放次数:{}/{}\n"
"当前帧:{}/{}\n"
"进度:{}\n"
"总进度:{}\n",
_name, _dir,
dl::to_string(_vecFtime),
_speed, _count, _countMax,
_cur, _vecFtime.size(),
str_t, str_k);
}
unsigned Animation::_get_played(unsigned tkc)
{
if (tkc < _tkcBegin)
{
assert(0);
return 0;
}
return tkc - _tkcBegin;
}
}