Skip to content

Instantly share code, notes, and snippets.

@HalCanary
Last active May 18, 2017 14:23
Show Gist options
  • Save HalCanary/b4ac8b9521f39fedf6053deb70f78ec5 to your computer and use it in GitHub Desktop.
Save HalCanary/b4ac8b9521f39fedf6053deb70f78ec5 to your computer and use it in GitHub Desktop.
/*
* Copyright 2017 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef animator_DEFINED
#define animator_DEFINED
#include <memory>
#include "skia.h"
struct Animator {
virtual ~Animator() {}
virtual void draw(SkCanvas*, double frame) = 0;
};
std::unique_ptr<Animator> MakeAnimator(SkISize, const SkImage*);
#endif // animator_DEFINED
////////////////////////////////////////////////////////////////////////////////
//struct Draw : public Animator {
// const SkImage* image;
// SkISize size;
// // INSERT FIELDS HERE
// Draw(SkISize s, const SkImage* i) : image(i), size(s) {
// // INSERT CODE HERE
// }
// void draw(SkCanvas* canvas, double frame) override {
// // INSERT CODE HERE
// }
//};
//
//std::unique_ptr<Animator> MakeAnimator(SkISize s, const SkImage* i) {
// return std::unique_ptr<Animator>(new Draw(s, i));
//}
////////////////////////////////////////////////////////////////////////////////
struct Draw : public Animator {
const SkImage* image;
SkISize size;
SkPath star;
SkPaint paint;
Draw(SkISize s, const SkImage* i) : image(i), size(s) {
const SkScalar R = 0.45f * SkTMin(size.width(), size.height());
star.moveTo(R, 0);
for (int i = 1; i < 8; ++i) {
SkScalar a = 2.6927937f * i;
star.lineTo(R * cos(a), R * sin(a));
}
paint.setShader(image->makeShader(SkShader::kRepeat_TileMode,
SkShader::kRepeat_TileMode));
}
void draw(SkCanvas* canvas, double frame) override {
canvas->translate(0.5 * size.width(), 0.5 * size.height());
canvas->rotate(frame * 360);
canvas->clear(SkColorSetARGB(255, 255, 255, 255));
canvas->drawPath(star, paint);
}
};
std::unique_ptr<Animator> MakeAnimator(SkISize s, const SkImage* i) {
return std::unique_ptr<Animator>(new Draw(s, i));
}
////////////////////////////////////////////////////////////////////////////////
static void output(int i, SkData* data) {
// For sandboxed applications, change this function to just dump to stdout.
static const char outputPrefix[] = "/tmp/animator";
auto outputPath = SkStringPrintf("%s_%04d.png", outputPrefix, i);
SkFILEWStream ofile(outputPath.c_str());
if (!ofile.isValid()) { abort(); }
ofile.write(data->data(), data->size());
}
int main() {
static const char imagePath[] = "resources/mandrill_512_q075.jpg";
const int frameCount = 60;
const SkISize size = {256, 256};
auto image = SkImage::MakeFromEncoded(SkData::MakeFromFileName(imagePath));
auto animator = MakeAnimator(size, image.get());
auto surface = SkSurface::MakeRasterN32Premul(size.width(), size.height());
double frameLength = 1.0 / frameCount;
for (int i = 0; i < frameCount; ++i) {
SkCanvas* canvas = surface->getCanvas();
SkAutoCanvasRestore autoCanvasRestore(canvas, true);
animator->draw(canvas, i * frameLength);
sk_sp<SkData> encoded(surface->makeImageSnapshot()->encode());
output(i, encoded.get());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment