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 "SkAnimTimer.h" | |
10 #include "SkView.h" | |
11 #include "SkCanvas.h" | |
12 #include "SkDrawable.h" | |
13 #include "SkPath.h" | |
14 #include "SkRandom.h" | |
15 #include "SkRSXform.h" | |
16 #include "SkSurface.h" | |
17 | |
18 static SkImage* make_atlas() { | |
robertphillips
2015/06/15 16:12:55
defined kAtlasSize as 512 somewhere and pass it in
reed1
2015/06/15 18:52:33
Acknowledged.
| |
19 static int N = 512; | |
20 SkImageInfo info = SkImageInfo::MakeN32Premul(N, N); | |
21 SkAutoTUnref<SkSurface> surface(SkSurface::NewRaster(info)); | |
22 SkCanvas* canvas = surface->getCanvas(); | |
23 | |
24 SkPaint paint; | |
25 paint.setAntiAlias(true); | |
26 SkRandom rand; | |
27 | |
robertphillips
2015/06/15 16:12:55
const int kCellSize = 32;
const SkScalar kHalf = k
reed1
2015/06/15 18:52:33
Acknowledged.
| |
28 const SkScalar half = 16; | |
29 const char* s = "01234567890!@#$%^&*=+<>?abcdefghijklmnopqrstuvwxyzABCDEFGHI JKLMNOPQRSTUVWXYZ"; | |
30 paint.setTextSize(28); | |
31 paint.setTextAlign(SkPaint::kCenter_Align); | |
32 int i = 0; | |
33 for (int y = 0; y < N; y += 32) { | |
34 for (int x = 0; x < N; x += 32) { | |
35 paint.setColor(rand.nextU()); | |
36 paint.setAlpha(0xFF); | |
37 int index = i % strlen(s); | |
38 canvas->drawText(&s[index], 1, x + half, y + half + half/2, paint); | |
39 i += 1; | |
40 } | |
41 } | |
42 return surface->newImageSnapshot(); | |
43 } | |
44 | |
45 class DrawAtlasDrawable : public SkDrawable { | |
46 struct Rec { | |
47 SkPoint fCenter; | |
48 SkVector fVelocity; | |
49 SkScalar fScale; | |
50 SkScalar fDScale; | |
51 SkScalar fRadian; | |
52 SkScalar fDRadian; | |
53 SkScalar fAlpha; | |
54 SkScalar fDAlpha; | |
55 | |
56 void advance(const SkRect& bounds) { | |
57 fCenter += fVelocity; | |
58 if (fCenter.fX > bounds.right()) { | |
59 SkASSERT(fVelocity.fX > 0); | |
60 fVelocity.fX = -fVelocity.fX; | |
61 } else if (fCenter.fX < bounds.left()) { | |
62 SkASSERT(fVelocity.fX < 0); | |
63 fVelocity.fX = -fVelocity.fX; | |
64 } | |
65 if (fCenter.fY > bounds.bottom()) { | |
66 SkASSERT(fVelocity.fY > 0); | |
67 fVelocity.fY = -fVelocity.fY; | |
68 } else if (fCenter.fY < bounds.top()) { | |
69 SkASSERT(fVelocity.fY < 0); | |
70 fVelocity.fY = -fVelocity.fY; | |
71 } | |
72 | |
73 fScale += fDScale; | |
robertphillips
2015/06/15 16:12:54
SK_ScalarHalf ?
reed1
2015/06/15 18:52:33
Not sure. I was playing around with the 2 & 1/2
| |
74 if (fScale > 2 || fScale < SK_Scalar1/2) { | |
75 fDScale = -fDScale; | |
76 } | |
77 | |
78 fRadian += fDRadian; | |
79 fRadian = SkScalarMod(fRadian, 2 * SK_ScalarPI); | |
80 | |
81 fAlpha += fDAlpha; | |
82 if (fAlpha > 1) { | |
83 fAlpha = 1; | |
84 fDAlpha = -fDAlpha; | |
85 } else if (fAlpha < 0) { | |
86 fAlpha = 0; | |
87 fDAlpha = -fDAlpha; | |
88 } | |
89 } | |
90 | |
91 SkRSXform asRSXform() const { | |
92 SkMatrix m; | |
93 m.setTranslate(-8, -8); | |
94 m.postScale(fScale, fScale); | |
95 m.postRotate(SkRadiansToDegrees(fRadian)); | |
96 m.postTranslate(fCenter.fX, fCenter.fY); | |
97 | |
98 SkRSXform x; | |
99 x.fSCos = m.getScaleX(); | |
100 x.fSSin = m.getSkewY(); | |
101 x.fTx = m.getTranslateX(); | |
102 x.fTy = m.getTranslateY(); | |
103 return x; | |
104 } | |
105 }; | |
106 | |
107 enum { | |
108 N = 256, | |
109 }; | |
110 | |
111 SkAutoTUnref<SkImage> fAtlas; | |
112 Rec fRec[N]; | |
113 SkRect fTex[N]; | |
114 SkRect fBounds; | |
115 | |
116 public: | |
117 DrawAtlasDrawable(const SkRect& r) : fBounds(r) { | |
118 SkRandom rand; | |
119 fAtlas.reset(make_atlas()); | |
120 const SkScalar half = 8; | |
121 const SkScalar speed = 5; | |
122 int i = 0; | |
robertphillips
2015/06/15 16:12:54
512 -> kAtlasSize ?
32 -> kCellSize ?
reed1
2015/06/15 18:52:33
Acknowledged.
| |
123 for (int y = 0; y < 512; y += 32) { | |
124 for (int x = 0; x < 512; x += 32) { | |
robertphillips
2015/06/15 16:12:55
SkASSERT(i < N); ?
reed1
2015/06/15 18:52:33
Acknowledged.
| |
125 fTex[i].setXYWH(x, y, 32, 32); | |
126 | |
127 fRec[i].fCenter.set(x/2 + half, y/2 + half); | |
128 fRec[i].fVelocity.fX = rand.nextSScalar1() * speed; | |
129 fRec[i].fVelocity.fY = rand.nextSScalar1() * speed; | |
130 fRec[i].fScale = 1; | |
131 fRec[i].fDScale = rand.nextSScalar1() / 4; | |
132 fRec[i].fRadian = 0; | |
133 fRec[i].fDRadian = rand.nextSScalar1() / 8; | |
134 fRec[i].fAlpha = rand.nextUScalar1(); | |
135 fRec[i].fDAlpha = rand.nextSScalar1() / 10; | |
136 i += 1; | |
137 } | |
138 } | |
robertphillips
2015/06/15 16:12:54
Rm this debugf ?
| |
139 SkDebugf("total %d\n", i); | |
140 } | |
141 | |
142 void onDraw(SkCanvas* canvas) override { | |
143 SkRSXform xform[N]; | |
144 SkColor colors[N]; | |
145 | |
146 for (int i = 0; i < N; ++i) { | |
147 fRec[i].advance(fBounds); | |
148 xform[i] = fRec[i].asRSXform(); | |
149 colors[i] = SkColorSetARGB(fRec[i].fAlpha * 0xFF, 0xFF, 0xFF, 0xFF); | |
150 } | |
151 SkPaint paint; | |
152 paint.setFilterQuality(kLow_SkFilterQuality); | |
153 paint.setAntiAlias(true); | |
154 canvas->drawAtlas(fAtlas, xform, fTex, colors, N, NULL, &paint); | |
155 } | |
156 | |
157 SkRect onGetBounds() override { | |
robertphillips
2015/06/15 16:12:54
Why not just fBounds ?
reed1
2015/06/15 18:52:33
bounds outset by max scale * max cell radius
| |
158 return SkRect::MakeLTRB(-1000, -1000, 1000, 1000); | |
159 } | |
robertphillips
2015/06/15 16:12:55
private:
typedef SkDrawable INHERITED;
?
reed1
2015/06/15 18:52:33
Done.
| |
160 }; | |
161 | |
162 class DrawAtlasView : public SampleView { | |
163 SkDrawable* fDrawable; | |
164 | |
165 public: | |
166 DrawAtlasView() { | |
167 fDrawable = new DrawAtlasDrawable(SkRect::MakeWH(640, 480)); | |
168 } | |
169 | |
170 virtual ~DrawAtlasView() override { | |
171 fDrawable->unref(); | |
172 } | |
173 | |
174 protected: | |
175 bool onQuery(SkEvent* evt) override { | |
176 if (SampleCode::TitleQ(*evt)) { | |
177 SampleCode::TitleR(evt, "DrawAtlas"); | |
178 return true; | |
179 } | |
180 return this->INHERITED::onQuery(evt); | |
181 } | |
182 | |
183 void onDrawContent(SkCanvas* canvas) override { | |
184 canvas->drawDrawable(fDrawable); | |
185 this->inval(NULL); | |
186 } | |
187 | |
robertphillips
2015/06/15 16:12:54
Shouldn't we be using this ?
| |
188 #if 0 | |
189 bool onAnimate(const SkAnimTimer& timer) override { | |
190 SkScalar angle = SkDoubleToScalar(fmod(timer.secs() * 360 / 24, 360)); | |
191 fAnimatingDrawable->setSweep(angle); | |
192 return true; | |
193 } | |
194 #endif | |
195 | |
196 private: | |
197 typedef SampleView INHERITED; | |
198 }; | |
199 | |
200 ////////////////////////////////////////////////////////////////////////////// | |
201 | |
robertphillips
2015/06/15 16:12:55
We don't have a macro for this ?
| |
202 static SkView* MyFactory() { return new DrawAtlasView; } | |
203 static SkViewRegister reg(MyFactory); | |
OLD | NEW |