blob: 6553266a14dcba908212d8794d254ffec3b7a1c5 [file] [log] [blame]
jvanverth2f5bb3a2015-10-05 11:05:07 -07001/*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SampleCode.h"
9#include "Resources.h"
10#include "SkAnimTimer.h"
11#include "SkView.h"
12#include "SkCanvas.h"
13#include "SkRSXform.h"
14#include "SkSurface.h"
15#include "Timer.h"
16
17#include <stdio.h>
18
19static const int kGrid = 100;
20static const int kWidth = 960;
21static const int kHeight = 640;
22
23class DrawShipView : public SampleView {
24 const char* fName;
25
26public:
27 DrawShipView(const char name[]) : fName(name) {
28 fAtlas.reset(GetResourceAsImage("ship.png"));
29 if (!fAtlas) {
30 SkDebugf("\nCould not decode file ship.png. Falling back to penguin mode.\n");
31 fAtlas.reset(GetResourceAsImage("baby_tux.png"));
32 if (!fAtlas) {
33 SkDebugf("\nCould not decode file baby_tux.png. Did you forget"
34 " to set the resourcePath?\n");
35 return;
36 }
37 }
38
39 SkScalar anchorX = fAtlas->width()*0.5f;
40 SkScalar anchorY = fAtlas->height()*0.5f;
41 int currIndex = 0;
42 for (int x = 0; x < kGrid; x++) {
43 for (int y = 0; y < kGrid; y++) {
44 float xPos = (x / (kGrid - 1.0f)) * kWidth;
45 float yPos = (y / (kGrid - 1.0f)) * kWidth;
46
47 fTex[currIndex] = SkRect::MakeLTRB(0.0f, 0.0f,
48 SkIntToScalar(fAtlas->width()),
49 SkIntToScalar(fAtlas->height()));
50 fXform[currIndex] = SkRSXform::MakeFromRadians(2.0f, SK_ScalarPI*0.5f,
51 xPos, yPos, anchorX, anchorY);
52 currIndex++;
53 }
54 }
55 fTex[currIndex] = SkRect::MakeLTRB(0.0f, 0.0f,
56 SkIntToScalar(fAtlas->width()),
57 SkIntToScalar(fAtlas->height()));
58 fXform[currIndex] = SkRSXform::MakeFromRadians(2.0f, SK_ScalarPI*0.5f,
59 kWidth*0.5f, kHeight*0.5f, anchorX, anchorY);
60
61 fCurrentTime = 0;
62 fTimer.start();
63 }
64
65 ~DrawShipView() override {}
66
67protected:
68 // overrides from SkEventSink
69 bool onQuery(SkEvent* evt) override {
70 if (SampleCode::TitleQ(*evt)) {
71 SampleCode::TitleR(evt, fName);
72 return true;
73 }
74 return this->INHERITED::onQuery(evt);
75 }
76
77 void onDrawContent(SkCanvas* canvas) override {
78 const float kCosDiff = 0.99984769515f;
79 const float kSinDiff = 0.01745240643f;
80
81 if (!fAtlas) {
82 return;
83 }
84
85 SkPaint paint;
86 paint.setFilterQuality(kLow_SkFilterQuality);
87 paint.setColor(SK_ColorWHITE);
88 paint.setTextSize(15.0f);
89
90 fTimer.end();
91
92 fTimes[fCurrentTime] = (float)(fTimer.fWall);
93 fCurrentTime = (fCurrentTime + 1) & 0x1f;
94
95 float meanTime = 0.0f;
96 for (int i = 0; i < 32; ++i) {
97 meanTime += fTimes[i];
98 }
99 meanTime /= 32.f;
100 SkString outString("fps: ");
101 SkScalar fps = 1000.f/meanTime;
102 outString.appendScalar(fps);
103 outString.append(" ms: ");
104 outString.appendScalar(meanTime);
105
106 fTimer.start();
107
108 SkScalar anchorX = fAtlas->width()*0.5f;
109 SkScalar anchorY = fAtlas->height()*0.5f;
110 for (int i = 0; i < kGrid*kGrid+1; ++i) {
111 SkScalar c = fXform[i].fSCos;
112 SkScalar s = fXform[i].fSSin;
113
114 SkScalar dx = c*anchorX - s*anchorY;
115 SkScalar dy = s*anchorX + c*anchorY;
116
117 fXform[i].fSCos = kCosDiff*c - kSinDiff*s;
118 fXform[i].fSSin = kSinDiff*c + kCosDiff*s;
119
120 dx -= fXform[i].fSCos*anchorX - fXform[i].fSSin*anchorY;
121 dy -= fXform[i].fSSin*anchorX + fXform[i].fSCos*anchorY;
122 fXform[i].fTx += dx;
123 fXform[i].fTy += dy;
124 }
125
126 canvas->drawAtlas(fAtlas, fXform, fTex, nullptr, kGrid*kGrid+1, SkXfermode::kSrcOver_Mode,
127 nullptr, &paint);
128 canvas->drawText(outString.c_str(), outString.size(), 100.f, 100.f, paint);
129
130 this->inval(nullptr);
131 }
132
133#if 0
134 // TODO: switch over to use this for our animation
135 bool onAnimate(const SkAnimTimer& timer) override {
136 SkScalar angle = SkDoubleToScalar(fmod(timer.secs() * 360 / 24, 360));
137 fAnimatingDrawable->setSweep(angle);
138 return true;
139 }
140#endif
141
142private:
143 SkAutoTUnref<SkImage> fAtlas;
144 SkRSXform fXform[kGrid*kGrid+1];
145 SkRect fTex[kGrid*kGrid+1];
146 WallTimer fTimer;
147 float fTimes[32];
148 int fCurrentTime;
149
150
151 typedef SampleView INHERITED;
152};
153
154//////////////////////////////////////////////////////////////////////////////
155
156DEF_SAMPLE( return new DrawShipView("DrawShip"); )