OLD | NEW |
---|---|
(Empty) | |
1 /* | |
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" | |
robertphillips
2015/10/02 21:29:18
Do we need SkDrawable.h or SkPath.h ?
jvanverth1
2015/10/05 17:04:49
Done.
| |
13 #include "SkDrawable.h" | |
14 #include "SkPath.h" | |
15 #include "SkRandom.h" | |
16 #include "SkRSXform.h" | |
17 #include "SkSurface.h" | |
18 #include "Timer.h" | |
19 | |
20 #include <stdio.h> | |
21 | |
22 #define GRID 100 | |
23 | |
24 class DrawShipView : public SampleView { | |
25 const char* fName; | |
26 | |
27 public: | |
28 DrawShipView(const char name[]) : fName(name) { | |
29 fAtlas.reset(GetResourceAsImage("ship.png")); | |
30 if (!fAtlas) { | |
31 SkDebugf("\nCould not decode file ship.png. Falling back to penguin mode.\n"); | |
32 fAtlas.reset(GetResourceAsImage("baby_tux.png")); | |
33 if (!fAtlas) { | |
34 SkDebugf("\nCould not decode file baby_tux.png. Did you forget" | |
35 " to set the resourcePath?\n"); | |
36 return; | |
37 } | |
38 } | |
39 | |
40 SkScalar anchorX = fAtlas->width()*0.5f; | |
41 SkScalar anchorY = fAtlas->height()*0.5f; | |
42 int currIndex = 0; | |
43 for (int x = 0; x < GRID; x++) { | |
44 for (int y = 0; y < GRID; y++) { | |
robertphillips
2015/10/02 21:29:18
Where is 960 coming from? Can it be a informativel
jvanverth1
2015/10/05 17:04:49
Done.
| |
45 float xPos = (x / (GRID - 1.0)) * 960; | |
46 float yPos = (y / (GRID - 1.0)) * 960; | |
47 | |
48 fTex[currIndex] = SkRect::MakeLTRB(0.0f, 0.0f, fAtlas->width(), fAtlas->height()); | |
49 fXform[currIndex] = SkRSXform::MakeFromRadians(2.0f, SK_ScalarPI *0.5f, | |
robertphillips
2015/10/02 21:29:18
add a space ?
jvanverth1
2015/10/05 17:04:49
Done.
| |
50 xPos, yPos, anchor X, anchorY); | |
51 currIndex++; | |
52 } | |
53 } | |
54 fTex[currIndex] = SkRect::MakeLTRB(0.0f, 0.0f, fAtlas->width(), fAtlas-> height()); | |
55 fXform[currIndex] = SkRSXform::MakeFromRadians(2.0f, SK_ScalarPI*0.5f, | |
56 960*0.5f, 640*0.5f, anchor X, anchorY); | |
57 | |
58 fCurrentTime = 0; | |
59 fTimer.start(); | |
60 } | |
61 | |
62 ~DrawShipView() override {} | |
63 | |
64 protected: | |
65 // overrides from SkEventSink | |
66 bool onQuery(SkEvent* evt) override { | |
67 if (SampleCode::TitleQ(*evt)) { | |
68 SampleCode::TitleR(evt, fName); | |
69 return true; | |
70 } | |
71 return this->INHERITED::onQuery(evt); | |
72 } | |
73 | |
74 void onDrawContent(SkCanvas* canvas) override { | |
75 const float kCosDiff = 0.99984769515f; | |
76 const float kSinDiff = 0.01745240643f; | |
77 | |
78 if (!fAtlas) { | |
79 return; | |
80 } | |
81 | |
82 SkPaint paint; | |
83 paint.setFilterQuality(kLow_SkFilterQuality); | |
84 | |
85 fTimer.end(); | |
86 | |
87 fTimes[fCurrentTime] = (float)(fTimer.fWall); | |
88 fCurrentTime = (fCurrentTime + 1) & 0x1f; | |
89 | |
90 float meanTime = 0.0f; | |
91 for (int i = 0; i < 32; ++i) { | |
92 meanTime += fTimes[i]; | |
93 } | |
94 meanTime /= 32.f; | |
robertphillips
2015/10/02 21:29:18
Use SkString here ?
jvanverth1
2015/10/05 17:04:49
Done.
| |
95 char outString[64]; | |
96 float fps = 1000.f/meanTime; | |
97 sprintf(outString, "fps: %f ms: %f", fps, meanTime); | |
98 | |
99 fTimer.start(); | |
100 | |
101 SkScalar anchorX = fAtlas->width()*0.5f; | |
102 SkScalar anchorY = fAtlas->height()*0.5f; | |
103 for (int i = 0; i < GRID*GRID+1; ++i) { | |
104 SkScalar c = fXform[i].fSCos; | |
105 SkScalar s = fXform[i].fSSin; | |
106 | |
107 SkScalar dx = c*anchorX - s*anchorY; | |
108 SkScalar dy = s*anchorX + c*anchorY; | |
109 | |
110 fXform[i].fSCos = kCosDiff*c - kSinDiff*s; | |
111 fXform[i].fSSin = kSinDiff*c + kCosDiff*s; | |
112 | |
113 dx -= fXform[i].fSCos*anchorX - fXform[i].fSSin*anchorY; | |
114 dy -= fXform[i].fSSin*anchorX + fXform[i].fSCos*anchorY; | |
115 fXform[i].fTx += dx; | |
116 fXform[i].fTy += dy; | |
117 } | |
118 | |
119 canvas->drawAtlas(fAtlas, fXform, fTex, nullptr, GRID*GRID+1, SkXfermode ::kSrcOver_Mode, | |
120 nullptr, &paint); | |
121 canvas->drawText(outString, strlen(outString), 100.f, 100.f, paint); | |
122 | |
123 this->inval(nullptr); | |
124 } | |
125 | |
126 #if 0 | |
127 // TODO: switch over to use this for our animation | |
128 bool onAnimate(const SkAnimTimer& timer) override { | |
129 SkScalar angle = SkDoubleToScalar(fmod(timer.secs() * 360 / 24, 360)); | |
130 fAnimatingDrawable->setSweep(angle); | |
131 return true; | |
132 } | |
133 #endif | |
134 | |
135 private: | |
136 SkAutoTUnref<SkImage> fAtlas; | |
137 SkRSXform fXform[GRID*GRID+1]; | |
138 SkRect fTex[GRID*GRID+1]; | |
139 WallTimer fTimer; | |
140 float fTimes[32]; | |
141 int fCurrentTime; | |
142 | |
143 | |
144 typedef SampleView INHERITED; | |
145 }; | |
146 | |
147 ////////////////////////////////////////////////////////////////////////////// | |
148 | |
149 DEF_SAMPLE( return new DrawShipView("DrawShip"); ) | |
OLD | NEW |