Skip to content

Instantly share code, notes, and snippets.

@gregkepler
Last active December 7, 2015 20:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gregkepler/71ece81a37397d55e088 to your computer and use it in GitHub Desktop.
Save gregkepler/71ece81a37397d55e088 to your computer and use it in GitHub Desktop.
Cinder Simple Animated Gif Player class
#include "AnimatedGif.h"
using namespace ci;
using namespace ci::app;
using namespace std;
void AnimatedGif::init( ci::DataSourceRef filePath, int frameRate ){
if( filePath ){
load( filePath );
}
mFrameRate = frameRate;
mCurrentFrame = 0;
}
void AnimatedGif::load( ci::DataSourceRef filePath ){
ci::ImageSource::Options options;
options.index( 0 );
ImageSourceRef img = loadImage( filePath, options );
int count = img->getCount();
for( int i = 0; i < count; ++i ) {
SurfaceRef frame = Surface::create( loadImage( filePath, options ) );
mGifFrames.push_back( frame );
options.index( options.getIndex() + 1 );
}
}
void AnimatedGif::update()
{
if( getElapsedFrames() % int( float( getFrameRate() ) / float( mFrameRate ) ) == 0 ){
mCurrentFrameTexture = gl::Texture::create( *mGifFrames[mCurrentFrame] );
mCurrentFrame++;
mCurrentFrame %= mGifFrames.size();
}
}
#include "cinder/app/AppBase.h"
#include "cinder/Surface.h"
#include "cinder/ImageIo.h"
#include "cinder/gl/Texture.h"
typedef std::shared_ptr<class AnimatedGif> AnimatedGifRef;
class AnimatedGif {
public:
AnimatedGif(){ init( nullptr ); };
AnimatedGif( ci::DataSourceRef filePath ){ init( filePath ); };
AnimatedGif( ci::DataSourceRef filePath, int frameRate ){ init( filePath, frameRate ); };
~AnimatedGif() {};
void load( ci::DataSourceRef filePath );
void update();
ci::gl::TextureRef getCurrentFrame() { return mCurrentFrameTexture; };
void setFrameRate( int frameRate ) { mFrameRate = frameRate; };
private:
std::vector<ci::SurfaceRef> mGifFrames;
int mCurrentFrame;
int mFrameRate;
ci::gl::TextureRef mCurrentFrameTexture;
void init( ci::DataSourceRef filePath = nullptr, int frameRate = 60 );
};
#include "cinder/app/App.h"
#include "cinder/app/RendererGl.h"
#include "cinder/gl/gl.h"
#include "cinder/gl/Texture.h"
#include "cinder/Log.h"
#include "AnimatedGif.h"
using namespace ci;
using namespace ci::app;
using namespace std;
class AnimatedGifsApp : public App {
public:
void setup() override;
void update() override;
void draw() override;
AnimatedGifRef mGif;
};
void AnimatedGifsApp::setup()
{
std::string gif = "animated_gif.gif";
// simple animated gif class
mGif = make_shared<AnimatedGif>( loadAsset( gif ), 30 );
// or
// mGif = make_shared<AnimatedGif>();
// mGif->load( loadAsset( gif ) );
// mGif->setFrameRate( 60 );
}
void AnimatedGifsApp::update()
{
mGif->update();
}
void AnimatedGifsApp::draw()
{
gl::clear( Color( 0, 0, 0 ) );
gl::draw( mGif->getCurrentFrame() );
}
CINDER_APP( AnimatedGifsApp, RendererGl )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment