2015-10-05 18:05:07 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2015 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
#include "Sample.h"
|
2015-10-05 18:05:07 +00:00
|
|
|
#include "Resources.h"
|
|
|
|
#include "SkAnimTimer.h"
|
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkRSXform.h"
|
|
|
|
#include "SkSurface.h"
|
|
|
|
#include "Timer.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
static const int kGrid = 100;
|
|
|
|
static const int kWidth = 960;
|
|
|
|
static const int kHeight = 640;
|
|
|
|
|
2015-10-07 17:43:05 +00:00
|
|
|
typedef void (*DrawAtlasProc)(SkCanvas*, SkImage*, const SkRSXform[], const SkRect[],
|
|
|
|
const SkColor[], int, const SkRect*, const SkPaint*);
|
|
|
|
|
|
|
|
static void draw_atlas(SkCanvas* canvas, SkImage* atlas, const SkRSXform xform[],
|
|
|
|
const SkRect tex[], const SkColor colors[], int count, const SkRect* cull,
|
|
|
|
const SkPaint* paint) {
|
2016-10-28 19:42:34 +00:00
|
|
|
canvas->drawAtlas(atlas, xform, tex, colors, count, SkBlendMode::kModulate, cull, paint);
|
2015-10-07 17:43:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void draw_atlas_sim(SkCanvas* canvas, SkImage* atlas, const SkRSXform xform[],
|
|
|
|
const SkRect tex[], const SkColor colors[], int count, const SkRect* cull,
|
|
|
|
const SkPaint* paint) {
|
|
|
|
for (int i = 0; i < count; ++i) {
|
|
|
|
SkMatrix matrix;
|
|
|
|
matrix.setRSXform(xform[i]);
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-10-07 17:43:05 +00:00
|
|
|
canvas->save();
|
|
|
|
canvas->concat(matrix);
|
|
|
|
canvas->drawImageRect(atlas, tex[i], tex[i].makeOffset(-tex[i].x(), -tex[i].y()), paint,
|
|
|
|
SkCanvas::kFast_SrcRectConstraint);
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-05 18:05:07 +00:00
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
class DrawShipView : public Sample {
|
2015-10-05 18:05:07 +00:00
|
|
|
public:
|
2015-10-07 17:43:05 +00:00
|
|
|
DrawShipView(const char name[], DrawAtlasProc proc) : fName(name), fProc(proc) {
|
2017-12-08 15:21:31 +00:00
|
|
|
fAtlas = GetResourceAsImage("images/ship.png");
|
2015-10-05 18:05:07 +00:00
|
|
|
if (!fAtlas) {
|
|
|
|
SkDebugf("\nCould not decode file ship.png. Falling back to penguin mode.\n");
|
2017-12-08 15:21:31 +00:00
|
|
|
fAtlas = GetResourceAsImage("images/baby_tux.png");
|
2015-10-05 18:05:07 +00:00
|
|
|
if (!fAtlas) {
|
|
|
|
SkDebugf("\nCould not decode file baby_tux.png. Did you forget"
|
|
|
|
" to set the resourcePath?\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-10-05 18:05:07 +00:00
|
|
|
SkScalar anchorX = fAtlas->width()*0.5f;
|
|
|
|
SkScalar anchorY = fAtlas->height()*0.5f;
|
|
|
|
int currIndex = 0;
|
|
|
|
for (int x = 0; x < kGrid; x++) {
|
|
|
|
for (int y = 0; y < kGrid; y++) {
|
|
|
|
float xPos = (x / (kGrid - 1.0f)) * kWidth;
|
|
|
|
float yPos = (y / (kGrid - 1.0f)) * kWidth;
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-10-05 18:05:07 +00:00
|
|
|
fTex[currIndex] = SkRect::MakeLTRB(0.0f, 0.0f,
|
|
|
|
SkIntToScalar(fAtlas->width()),
|
|
|
|
SkIntToScalar(fAtlas->height()));
|
2015-11-08 16:07:24 +00:00
|
|
|
fXform[currIndex] = SkRSXform::MakeFromRadians(0.1f, SK_ScalarPI*0.5f,
|
2015-10-05 18:05:07 +00:00
|
|
|
xPos, yPos, anchorX, anchorY);
|
|
|
|
currIndex++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fTex[currIndex] = SkRect::MakeLTRB(0.0f, 0.0f,
|
|
|
|
SkIntToScalar(fAtlas->width()),
|
|
|
|
SkIntToScalar(fAtlas->height()));
|
2015-11-08 16:07:24 +00:00
|
|
|
fXform[currIndex] = SkRSXform::MakeFromRadians(0.5f, SK_ScalarPI*0.5f,
|
2015-10-05 18:05:07 +00:00
|
|
|
kWidth*0.5f, kHeight*0.5f, anchorX, anchorY);
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-10-05 18:05:07 +00:00
|
|
|
fCurrentTime = 0;
|
|
|
|
fTimer.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
~DrawShipView() override {}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-10-05 18:05:07 +00:00
|
|
|
protected:
|
2018-08-08 15:36:17 +00:00
|
|
|
bool onQuery(Sample::Event* evt) override {
|
|
|
|
if (Sample::TitleQ(*evt)) {
|
|
|
|
Sample::TitleR(evt, fName);
|
2015-10-05 18:05:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this->INHERITED::onQuery(evt);
|
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-10-05 18:05:07 +00:00
|
|
|
void onDrawContent(SkCanvas* canvas) override {
|
|
|
|
const float kCosDiff = 0.99984769515f;
|
|
|
|
const float kSinDiff = 0.01745240643f;
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-10-05 18:05:07 +00:00
|
|
|
if (!fAtlas) {
|
|
|
|
return;
|
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-10-05 18:05:07 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setFilterQuality(kLow_SkFilterQuality);
|
|
|
|
paint.setColor(SK_ColorWHITE);
|
|
|
|
paint.setTextSize(15.0f);
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-10-05 18:05:07 +00:00
|
|
|
fTimer.end();
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-10-05 18:05:07 +00:00
|
|
|
fTimes[fCurrentTime] = (float)(fTimer.fWall);
|
|
|
|
fCurrentTime = (fCurrentTime + 1) & 0x1f;
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-10-05 18:05:07 +00:00
|
|
|
float meanTime = 0.0f;
|
|
|
|
for (int i = 0; i < 32; ++i) {
|
|
|
|
meanTime += fTimes[i];
|
|
|
|
}
|
|
|
|
meanTime /= 32.f;
|
|
|
|
SkString outString("fps: ");
|
|
|
|
SkScalar fps = 1000.f/meanTime;
|
|
|
|
outString.appendScalar(fps);
|
|
|
|
outString.append(" ms: ");
|
|
|
|
outString.appendScalar(meanTime);
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-10-05 18:05:07 +00:00
|
|
|
fTimer.start();
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-10-05 18:05:07 +00:00
|
|
|
SkScalar anchorX = fAtlas->width()*0.5f;
|
|
|
|
SkScalar anchorY = fAtlas->height()*0.5f;
|
|
|
|
for (int i = 0; i < kGrid*kGrid+1; ++i) {
|
|
|
|
SkScalar c = fXform[i].fSCos;
|
|
|
|
SkScalar s = fXform[i].fSSin;
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-10-05 18:05:07 +00:00
|
|
|
SkScalar dx = c*anchorX - s*anchorY;
|
|
|
|
SkScalar dy = s*anchorX + c*anchorY;
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-10-05 18:05:07 +00:00
|
|
|
fXform[i].fSCos = kCosDiff*c - kSinDiff*s;
|
|
|
|
fXform[i].fSSin = kSinDiff*c + kCosDiff*s;
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2015-10-05 18:05:07 +00:00
|
|
|
dx -= fXform[i].fSCos*anchorX - fXform[i].fSSin*anchorY;
|
|
|
|
dy -= fXform[i].fSSin*anchorX + fXform[i].fSCos*anchorY;
|
|
|
|
fXform[i].fTx += dx;
|
|
|
|
fXform[i].fTy += dy;
|
|
|
|
}
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
fProc(canvas, fAtlas.get(), fXform, fTex, nullptr, kGrid*kGrid+1, nullptr, &paint);
|
2015-11-08 16:07:24 +00:00
|
|
|
paint.setColor(SK_ColorBLACK);
|
|
|
|
canvas->drawRect(SkRect::MakeXYWH(0, 0, 200, 24), paint);
|
|
|
|
paint.setColor(SK_ColorWHITE);
|
2017-04-28 19:35:12 +00:00
|
|
|
canvas->drawString(outString, 5, 15, paint);
|
2015-10-05 18:05:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
// TODO: switch over to use this for our animation
|
|
|
|
bool onAnimate(const SkAnimTimer& timer) override {
|
|
|
|
SkScalar angle = SkDoubleToScalar(fmod(timer.secs() * 360 / 24, 360));
|
|
|
|
fAnimatingDrawable->setSweep(angle);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
private:
|
2015-10-07 17:43:05 +00:00
|
|
|
const char* fName;
|
|
|
|
DrawAtlasProc fProc;
|
2016-03-29 16:03:52 +00:00
|
|
|
|
2016-03-17 17:51:11 +00:00
|
|
|
sk_sp<SkImage> fAtlas;
|
2015-10-05 18:05:07 +00:00
|
|
|
SkRSXform fXform[kGrid*kGrid+1];
|
|
|
|
SkRect fTex[kGrid*kGrid+1];
|
|
|
|
WallTimer fTimer;
|
|
|
|
float fTimes[32];
|
|
|
|
int fCurrentTime;
|
2016-03-29 16:03:52 +00:00
|
|
|
|
|
|
|
|
2018-08-08 15:36:17 +00:00
|
|
|
typedef Sample INHERITED;
|
2015-10-05 18:05:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2015-10-07 17:43:05 +00:00
|
|
|
DEF_SAMPLE( return new DrawShipView("DrawShip", draw_atlas); )
|
|
|
|
DEF_SAMPLE( return new DrawShipView("DrawShipSim", draw_atlas_sim); )
|