Skip to content

Instantly share code, notes, and snippets.

@gregkepler
Last active December 7, 2015 20:46
Show Gist options
  • Save gregkepler/71ece81a37397d55e088 to your computer and use it in GitHub Desktop.
Save gregkepler/71ece81a37397d55e088 to your computer and use it in GitHub Desktop.

Revisions

  1. gregkepler revised this gist Nov 19, 2015. No changes.
  2. gregkepler revised this gist Nov 19, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion GifApp.cpp
    Original file line number Diff line number Diff line change
    @@ -26,7 +26,7 @@ void AnimatedGifsApp::setup()
    mGif = make_shared<AnimatedGif>( loadAsset( gif ), 30 );

    // or
    // mGif = make_shared<AnimatedGif>();
    // mGif = make_shared<AnimatedGif>();
    // mGif->load( loadAsset( gif ) );
    // mGif->setFrameRate( 60 );
    }
  3. gregkepler created this gist Nov 19, 2015.
    36 changes: 36 additions & 0 deletions AnimatedGif.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    #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();
    }
    }
    31 changes: 31 additions & 0 deletions AnimatedGif.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    #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 );


    };
    46 changes: 46 additions & 0 deletions GifApp.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,46 @@
    #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 )