Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(47)

Side by Side Diff: samplecode/SampleAnimatedText.cpp

Issue 1410663005: Add text animation sample; tweak DrawShip sample (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Some fixes Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
robertphillips 2015/11/06 21:51:14 2015 ?
jvanverth1 2015/11/06 22:06:53 Done.
2 * Copyright 2011 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 "SkView.h"
10 #include "SkCanvas.h"
11 #include "SkUtils.h"
12 #include "SkColorPriv.h"
13 #include "SkColorFilter.h"
14 #include "SkGpuDevice.h"
15 #include "SkRandom.h"
16 #include "SkSystemEventTypes.h"
17 #include "SkTime.h"
18 #include "SkTypeface.h"
19 #include "SkXfermode.h"
20 #include "Timer.h"
21
22 #include "GrContext.h"
23
24 SkRandom gRand;
25
26 static void DrawTheText(SkCanvas* canvas, const char text[], size_t length, SkSc alar x, SkScalar y,
27 const SkPaint& paint) {
28 SkPaint p(paint);
29
30 p.setSubpixelText(true);
robertphillips 2015/11/06 21:51:14 Why are we adding 90 here ?
jvanverth1 2015/11/06 22:06:53 Done.
31 x += SkIntToScalar(90);
32 canvas->drawText(text, length, x, y, p);
33 }
34
robertphillips 2015/11/06 21:51:14 // This sample demonstrates the cache behavior of
jvanverth1 2015/11/06 22:06:53 Done.
35 class AnimatedTextView : public SampleView {
36 public:
37 AnimatedTextView() : fScale(1.0f), fScaleInc(0.1f), fRotation(0.0f), fSizeSc ale(1) {
38 fCurrentTime = 0;
39 fTimer.start();
40 memset(fTimes, 0, sizeof(fTimes));
41 }
42
43 protected:
44 // overrides from SkEventSink
45 bool onQuery(SkEvent* evt) override {
46 if (SampleCode::TitleQ(*evt)) {
47 SampleCode::TitleR(evt, "AnimatedText");
48 return true;
49 }
50
51 SkUnichar uni;
52 if (SampleCode::CharQ(*evt, &uni)) {
53 switch (uni) {
robertphillips 2015/11/06 21:51:14 put in our standard formatting ?
jvanverth1 2015/11/06 22:06:52 Done.
54 case '2': if (fSizeScale == 2) fSizeScale = 1; else fSizeScale = 2; return true;
55 default: break;
56 }
57 }
58 return this->INHERITED::onQuery(evt);
59 }
60
61 void onDrawContent(SkCanvas* canvas) override {
62 SkPaint paint;
63 SkSafeUnref(paint.setTypeface(SkTypeface::CreateFromFile("/skimages/samp lefont.ttf")));
64 paint.setAntiAlias(true);
65 paint.setFilterQuality(kMedium_SkFilterQuality);
66
67 SkString outString("fps: ");
68 fTimer.end();
69
robertphillips 2015/11/06 21:51:14 Isn't this the third instance of this code ? Time
jvanverth1 2015/11/06 22:06:52 Acknowledged.
70 fTimes[fCurrentTime] = (float)(fTimer.fWall);
71 fCurrentTime = (fCurrentTime + 1) & 0x1f;
72
73 float meanTime = 0.0f;
74 for (int i = 0; i < 32; ++i) {
75 meanTime += fTimes[i];
76 }
77 meanTime /= 32.f;
78 SkScalar fps = 1000.f / meanTime;
79 outString.appendScalar(fps);
80 outString.append(" ms: ");
81 outString.appendScalar(meanTime);
82
83 SkString modeString("Text scale: ");
84 modeString.appendU32(fSizeScale);
85 modeString.append("x");
86
87 fTimer.start();
88
89 canvas->save();
90
91 SkBaseDevice* device = canvas->getDevice_just_for_deprecated_compatibili ty_testing();
92 GrContext* grContext = canvas->getGrContext();
93 if (grContext) {
robertphillips 2015/11/06 21:51:14 Is passing in 'paint' useful ?
jvanverth1 2015/11/06 22:06:52 It allows you to change the texture filtering, for
94 grContext->drawFontCache(SkRect::MakeXYWH(512, 10, 512, 512), kA8_Gr MaskFormat, paint,
95 reinterpret_cast<SkGpuDevice*>(device)->acc essRenderTarget());
96 }
97
98 canvas->translate(180, 180);
99 canvas->rotate(fRotation);
100 canvas->scale(fScale, fScale);
101 canvas->translate(-180, -180);
102
103 const char* text = "Hamburgefons";
104 size_t length = strlen(text);
105
106 SkScalar y = SkIntToScalar(0);
107 for (int i = 12; i <= 26; i++) {
108 paint.setTextSize(SkIntToScalar(i*fSizeScale));
robertphillips 2015/11/06 21:51:14 one line ? remove this loop ?
jvanverth1 2015/11/06 22:06:52 Done.
109 for (SkScalar dx = 0; dx <= SkIntToScalar(3) / 4;
robertphillips 2015/11/06 21:51:14 SK_Scalar1 ?
jvanverth1 2015/11/06 22:06:52 Done.
110 dx += SkIntToScalar(1)) {
111 y += paint.getFontSpacing();
robertphillips 2015/11/06 21:51:14 Just draw at 110 ?
jvanverth1 2015/11/06 22:06:53 Done.
112 DrawTheText(canvas, text, length, SkIntToScalar(20) + dx, y, pai nt);
113 }
114 }
115 canvas->restore();
116
117 paint.setTextSize(16);
118 canvas->drawText(outString.c_str(), outString.size(), 512.f, 540.f, pain t);
119 canvas->drawText(modeString.c_str(), modeString.size(), 768.f, 540.f, pa int);
120 }
121
122 bool onAnimate(const SkAnimTimer& timer) override {
robertphillips 2015/11/06 21:51:14 // We add noise to the scale and rotation animatio
jvanverth1 2015/11/06 22:06:52 Done.
123 fRotation += (1.0f + gRand.nextRangeF(-0.1f, 0.1f));
124 fScale += (fScaleInc + gRand.nextRangeF(-0.025f, 0.025f));
robertphillips 2015/11/06 21:51:14 if (fScale >= 2.0f || fScale <= 1.0f) { fScaleI
jvanverth1 2015/11/06 22:06:53 Done.
125 if (fScale >= 2.0f) {
126 fScaleInc = -0.1f;
127 } else if (fScale <= 1.0f) {
128 fScaleInc = 0.1f;
129 }
130 return true;
131 }
132
133 private:
134 float fScale;
135 float fScaleInc;
136 float fRotation;
137 int fSizeScale;
138
139 WallTimer fTimer;
140 float fTimes[32];
141 int fCurrentTime;
142
143
144 typedef SampleView INHERITED;
145 };
146
147 //////////////////////////////////////////////////////////////////////////////
148
149 static SkView* MyFactory() { return new AnimatedTextView; }
150 static SkViewRegister reg(MyFactory);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698