Created
April 29, 2015 19:35
-
-
Save ThomasLengeling/9900018b8c0c62e3a6da to your computer and use it in GitHub Desktop.
Cinder Texture Sequence
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
#pragma once | |
#include "cinder/app/App.h" | |
#include "cinder/gl/gl.h" | |
#include "cinder/gl/Texture.h" | |
#include "cinder/Log.h" | |
class TextureSequence; | |
typedef std::shared_ptr<TextureSequence> TextureSequenceRef; | |
class TextureSequence{ | |
public: | |
TextureSequence() : | |
looping(false), | |
paused(true), | |
playing(false), | |
complete(false), | |
mCurrentFrame(0) | |
{ | |
} | |
~TextureSequence(){ mTexturesSeq.clear(); } | |
static std::shared_ptr<TextureSequence> create(){ | |
return std::make_shared<TextureSequence>(); | |
} | |
void loadFilesFromDir(std::string filePath){ | |
mTexturesSeq.clear(); | |
ci::fs::path p(filePath); | |
for (ci::fs::directory_iterator it(p); it != ci::fs::directory_iterator(); ++it) | |
{ | |
if (ci::fs::is_regular_file(*it)) | |
{ | |
//Perhaps there is a better way to ignore hidden files | |
std::string fileName = it->path().filename().string(); | |
CI_LOG_V(fileName); | |
if (!(fileName.compare(".DS_Store") == 0)) | |
{ | |
std::string fileN = filePath +"/"+ fileName; | |
CI_LOG_V(fileN); | |
ci::gl::Texture2dRef text = ci::gl::Texture2d::create(ci::loadImage(fileN)); | |
mTexturesSeq.push_back(text); | |
} | |
} | |
} | |
mNumFrames = mTexturesSeq.size(); | |
} | |
void play() | |
{ | |
if (!playing){ | |
mTimeStart = ci::app::getElapsedSeconds(); | |
playing = true; | |
} | |
} | |
void update() | |
{ | |
if (playing){ | |
double elapsed = ci::app::getElapsedSeconds() - mTimeStart; | |
mCurrentFrame = (int)ci::math<float>::floor(elapsed * mNumFrames) % mNumFrames; | |
} | |
} | |
const ci::gl::Texture2dRef & getCurrentFrame() | |
{ | |
if (mCurrentFrame >= 0 && mCurrentFrame < mNumFrames) | |
return mTexturesSeq[mCurrentFrame]; | |
else //return a dommy video? | |
return ci::gl::Texture2d::create(1024, 768); | |
} | |
void setLooping(bool doLoop) { looping = doLoop; } | |
bool isPlaying() { return playing; } | |
bool isPaused() { return paused; } | |
private: | |
std::vector< ci::gl::Texture2dRef> mTexturesSeq; | |
int mNumFrames; | |
double mTimeStart; | |
bool looping; | |
bool paused; | |
bool playing; | |
bool complete; | |
int mCurrentFrame; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment