While I am working on a project with Urho3D, I have found there’s no built-in way to play animated gifs, so I write my own. Since this is a general problem, I’d like to share my work with the community.
In the zip file, which can be found at the end of this post, there’re 5 files.
The AnimatedGif.h/cpp defines a new type of Resource. It uses hidefromkgb’s gif_load.h (public domain C library) to decode the gif.
The FrameAnimation.h/cpp defines a new type of component. It uses AnimatedGif to play the animation.
(1) copy the files under the same folder and add to your project. If you want to place them separately, change the #include accordingly.
(2) register the types before use, for example in the constructor of your application, write
(3) code as if they are Urho3D built-in types. For example:
//Setup 2D Scene…
auto pPic = _gameScene->CreateChild(“test_gif”);
auto pFrAni = pPic->CreateComponent< FrameAnimation >();
auto pGif = GetSubsystem< ResourceCache >()->GetResource< AnimatedGif >(“Data/Urho2D/test.gif”);
Check this gif for result. Each time I click the button, a new gif will be created.
(browser may show the gif slower than normal)
(1) if the gif is large, consider doing a backgroundloading with resource cache first.
(2) MSVC user: since it uses a C library, you may need to change IDE grammar check from C++ to C.
(3) MSVC user: there’re a lot of comments in CHINESE, which is my mother tongue. They are mostly about background knowledge on the media format, rationale and C library usage. If you just want to play a gif, don’t bother. But you need to change the encoding from UTF-8 to UTF-8 BOM, or else MSVC will output strange errors. Personally I think it’s a flaw in the compiler. Apple Clang and g++ don’t have this issue. Another way is to just remove all comments.
The forum doesn’t allow zip file uploading, so I have put the file on my website for your access.
The codes are released under MIT license.
Hope this can help some people in the future.