diff options
Diffstat (limited to 'samplecode/SampleAnimator.cpp')
-rw-r--r-- | samplecode/SampleAnimator.cpp | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/samplecode/SampleAnimator.cpp b/samplecode/SampleAnimator.cpp new file mode 100644 index 0000000000..76b1975f92 --- /dev/null +++ b/samplecode/SampleAnimator.cpp @@ -0,0 +1,88 @@ +#include "SampleCode.h" +#include "SkView.h" +#include "SkCanvas.h" + +#include "SkAnimator.h" +#include "SkStream.h" + +class SkAnimatorView : public SkView { +public: + SkAnimatorView(); + virtual ~SkAnimatorView(); + + void setURIBase(const char dir[]); + + SkAnimator* getAnimator() const { return fAnimator; } + + bool decodeFile(const char path[]); + bool decodeMemory(const void* buffer, size_t size); + bool decodeStream(SkStream* stream); + +protected: + // overrides + virtual void onDraw(SkCanvas*); + +private: + SkString fBaseURI; + SkAnimator* fAnimator; + + typedef SkView INHERITED; +}; + +SkAnimatorView::SkAnimatorView() : fAnimator(NULL) { +} + +SkAnimatorView::~SkAnimatorView() { + delete fAnimator; +} + +void SkAnimatorView::setURIBase(const char dir[]) { + fBaseURI.set(dir); +} + +bool SkAnimatorView::decodeFile(const char path[]) { + SkFILEStream* is = new SkFILEStream(path); + SkAutoUnref aur(is); + return is->isValid() && this->decodeStream(is); +} + +bool SkAnimatorView::decodeMemory(const void* buffer, size_t size) { + SkMemoryStream* is = new SkMemoryStream(buffer, size); + SkAutoUnref aur(is); + return this->decodeStream(is); +} + +bool SkAnimatorView::decodeStream(SkStream* stream) { + delete fAnimator; + fAnimator = new SkAnimator; + fAnimator->setURIBase(fBaseURI.c_str()); + if (!fAnimator->decodeStream(stream)) { + delete fAnimator; + fAnimator = NULL; + return false; + } + return true; +} + +#include "SkTime.h" + +void SkAnimatorView::onDraw(SkCanvas* canvas) { + if (fAnimator) { + canvas->drawColor(SK_ColorWHITE); + fAnimator->draw(canvas, SkTime::GetMSecs()); + this->inval(NULL); + } +} + +////////////////////////////////////////////////////////////////////////////// + +static SkView* MyFactory() { + SkAnimatorView* av = new SkAnimatorView; +// av->decodeFile("/skimages/test.xml"); + av->setURIBase("/skia/trunk/animations/"); + av->decodeFile("/skia/trunk/animations/movie#1.xml"); + return av; +} + +static SkViewRegister reg(MyFactory); + |