Last active
December 31, 2020 07:06
-
-
Save ongaeshi/43f42e9d8654766c950029f4c8f5d805 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# include <Siv3D.hpp> | |
// アニメーション描画用のクラス | |
struct AnimationTexture | |
{ | |
Array<Texture> textures; | |
// フレームの時間 | |
Array<int32> delays; | |
int32 duration = 0; | |
explicit operator bool() const noexcept | |
{ | |
return !textures.isEmpty(); | |
} | |
Size size() const noexcept | |
{ | |
if (!textures) | |
{ | |
return Size(0, 0); | |
} | |
return textures.front().size(); | |
} | |
size_t frames() const noexcept | |
{ | |
return textures.size(); | |
} | |
size_t getFrameIndex(int32 timeMillisec) const noexcept | |
{ | |
return AnimatedGIFReader::MillisecToIndex(timeMillisec, delays, duration); | |
} | |
const Texture& getTexture(int32 timeMillisec) const noexcept | |
{ | |
return textures[getFrameIndex(timeMillisec)]; | |
} | |
}; | |
void Main() | |
{ | |
AnimationTexture animation; | |
{ | |
// GIF ファイルを開く | |
const AnimatedGIFReader gif(U"example/sample.gif"); | |
if (!gif) | |
{ | |
throw Error(U"Failed to open a gif file"); | |
} | |
Array<Image> images; | |
// GIF アニメーションを読み込み | |
if (gif.read(images, animation.delays, animation.duration)) | |
{ | |
// Image を Texture に変換 | |
animation.textures = images.map([](const Image& i) { return Texture(i); }); | |
} | |
else | |
{ | |
throw Error(U"Failed to load a gif animation"); | |
} | |
} | |
// 画像のサイズ、フレーム数、アニメーションの長さ(ミリ秒) | |
// Print << U"{}, {} frames ({} ms)"_fmt(animation.size(), animation.frames(), animation.duration); | |
const Point pos(10, 10); | |
double time = 0.0; | |
auto button = U"▶"; | |
double rate = 1.0f; | |
auto loopTime = animation.duration * 0.001; | |
const Font font(20); | |
while (System::Update()) | |
{ | |
time += Scene::DeltaTime() * rate; | |
if (time > loopTime) { | |
time -= loopTime; | |
} | |
if (SimpleGUI::Button(button, Vec2(20, 500))) { | |
if (button == U"▶") { | |
rate = 0.0f; | |
button = U"⏹️"; | |
} else { | |
rate = 1.0f; | |
button = U"▶"; | |
} | |
} | |
if (SimpleGUI::Slider(time, 0.0, loopTime, Vec2(180, 500), 600)) { | |
rate = 0.0f; | |
button = U"⏹️"; | |
} | |
font(U"{:3.2f}"_fmt(time)).draw(100, 500); | |
animation.getTexture(static_cast<int32>(time * 1000)).scaled(1.5).draw(pos); | |
// animation.getTexture(static_cast<int32>(time * 1000)).draw(pos); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment