OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SampleCode.h" | 8 #include "SampleCode.h" |
9 #include "Resources.h" | 9 #include "Resources.h" |
10 #include "SkAnimTimer.h" | 10 #include "SkAnimTimer.h" |
11 #include "SkView.h" | 11 #include "SkView.h" |
12 #include "SkCanvas.h" | 12 #include "SkCanvas.h" |
13 #include "SkRSXform.h" | 13 #include "SkRSXform.h" |
14 #include "SkSurface.h" | 14 #include "SkSurface.h" |
15 #include "Timer.h" | 15 #include "Timer.h" |
16 | 16 |
17 #include <stdio.h> | 17 #include <stdio.h> |
18 | 18 |
19 static const int kGrid = 100; | 19 static const int kGrid = 100; |
20 static const int kWidth = 960; | 20 static const int kWidth = 960; |
21 static const int kHeight = 640; | 21 static const int kHeight = 640; |
22 | 22 |
23 typedef void (*DrawAtlasProc)(SkCanvas*, SkImage*, const SkRSXform[], const SkRe ct[], | |
robertphillips
2015/10/07 18:07:00
tab over ?
| |
24 const SkColor[], int, const SkRect*, const SkPaint*); | |
25 | |
26 static void draw_atlas(SkCanvas* canvas, SkImage* atlas, const SkRSXform xform[] , | |
27 const SkRect tex[], const SkColor colors[], int count, co nst SkRect* cull, | |
28 const SkPaint* paint) { | |
29 canvas->drawAtlas(atlas, xform, tex, colors, count, SkXfermode::kModulate_Mo de, cull, paint); | |
30 } | |
31 | |
32 static void draw_atlas_sim(SkCanvas* canvas, SkImage* atlas, const SkRSXform xfo rm[], | |
robertphillips
2015/10/07 18:07:00
\n ?
| |
33 const SkRect tex[], const SkColor colors[], int count , const SkRect* cull, | |
34 const SkPaint* paint) { | |
35 for (int i = 0; i < count; ++i) { | |
36 SkMatrix matrix; | |
37 matrix.setRSXform(xform[i]); | |
38 | |
39 canvas->save(); | |
40 canvas->concat(matrix); | |
41 canvas->drawImageRect(atlas, tex[i], tex[i].makeOffset(-tex[i].x(), -tex [i].y()), paint, | |
42 SkCanvas::kFast_SrcRectConstraint); | |
43 canvas->restore(); | |
44 } | |
45 } | |
46 | |
47 | |
23 class DrawShipView : public SampleView { | 48 class DrawShipView : public SampleView { |
24 const char* fName; | |
25 | |
26 public: | 49 public: |
27 DrawShipView(const char name[]) : fName(name) { | 50 DrawShipView(const char name[], DrawAtlasProc proc) : fName(name), fProc(pro c) { |
28 fAtlas.reset(GetResourceAsImage("ship.png")); | 51 fAtlas.reset(GetResourceAsImage("ship.png")); |
29 if (!fAtlas) { | 52 if (!fAtlas) { |
30 SkDebugf("\nCould not decode file ship.png. Falling back to penguin mode.\n"); | 53 SkDebugf("\nCould not decode file ship.png. Falling back to penguin mode.\n"); |
31 fAtlas.reset(GetResourceAsImage("baby_tux.png")); | 54 fAtlas.reset(GetResourceAsImage("baby_tux.png")); |
32 if (!fAtlas) { | 55 if (!fAtlas) { |
33 SkDebugf("\nCould not decode file baby_tux.png. Did you forget" | 56 SkDebugf("\nCould not decode file baby_tux.png. Did you forget" |
34 " to set the resourcePath?\n"); | 57 " to set the resourcePath?\n"); |
35 return; | 58 return; |
36 } | 59 } |
37 } | 60 } |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
116 | 139 |
117 fXform[i].fSCos = kCosDiff*c - kSinDiff*s; | 140 fXform[i].fSCos = kCosDiff*c - kSinDiff*s; |
118 fXform[i].fSSin = kSinDiff*c + kCosDiff*s; | 141 fXform[i].fSSin = kSinDiff*c + kCosDiff*s; |
119 | 142 |
120 dx -= fXform[i].fSCos*anchorX - fXform[i].fSSin*anchorY; | 143 dx -= fXform[i].fSCos*anchorX - fXform[i].fSSin*anchorY; |
121 dy -= fXform[i].fSSin*anchorX + fXform[i].fSCos*anchorY; | 144 dy -= fXform[i].fSSin*anchorX + fXform[i].fSCos*anchorY; |
122 fXform[i].fTx += dx; | 145 fXform[i].fTx += dx; |
123 fXform[i].fTy += dy; | 146 fXform[i].fTy += dy; |
124 } | 147 } |
125 | 148 |
126 canvas->drawAtlas(fAtlas, fXform, fTex, nullptr, kGrid*kGrid+1, SkXfermo de::kSrcOver_Mode, | 149 fProc(canvas, fAtlas, fXform, fTex, nullptr, kGrid*kGrid+1, nullptr, &pa int); |
127 nullptr, &paint); | |
128 canvas->drawText(outString.c_str(), outString.size(), 100.f, 100.f, pain t); | 150 canvas->drawText(outString.c_str(), outString.size(), 100.f, 100.f, pain t); |
129 | 151 |
130 this->inval(nullptr); | 152 this->inval(nullptr); |
131 } | 153 } |
132 | 154 |
133 #if 0 | 155 #if 0 |
134 // TODO: switch over to use this for our animation | 156 // TODO: switch over to use this for our animation |
135 bool onAnimate(const SkAnimTimer& timer) override { | 157 bool onAnimate(const SkAnimTimer& timer) override { |
136 SkScalar angle = SkDoubleToScalar(fmod(timer.secs() * 360 / 24, 360)); | 158 SkScalar angle = SkDoubleToScalar(fmod(timer.secs() * 360 / 24, 360)); |
137 fAnimatingDrawable->setSweep(angle); | 159 fAnimatingDrawable->setSweep(angle); |
138 return true; | 160 return true; |
139 } | 161 } |
140 #endif | 162 #endif |
141 | 163 |
142 private: | 164 private: |
165 const char* fName; | |
166 DrawAtlasProc fProc; | |
167 | |
143 SkAutoTUnref<SkImage> fAtlas; | 168 SkAutoTUnref<SkImage> fAtlas; |
144 SkRSXform fXform[kGrid*kGrid+1]; | 169 SkRSXform fXform[kGrid*kGrid+1]; |
145 SkRect fTex[kGrid*kGrid+1]; | 170 SkRect fTex[kGrid*kGrid+1]; |
146 WallTimer fTimer; | 171 WallTimer fTimer; |
147 float fTimes[32]; | 172 float fTimes[32]; |
148 int fCurrentTime; | 173 int fCurrentTime; |
149 | 174 |
150 | 175 |
151 typedef SampleView INHERITED; | 176 typedef SampleView INHERITED; |
152 }; | 177 }; |
153 | 178 |
154 ////////////////////////////////////////////////////////////////////////////// | 179 ////////////////////////////////////////////////////////////////////////////// |
155 | 180 |
156 DEF_SAMPLE( return new DrawShipView("DrawShip"); ) | 181 DEF_SAMPLE( return new DrawShipView("DrawShip", draw_atlas); ) |
182 DEF_SAMPLE( return new DrawShipView("DrawShipSim", draw_atlas_sim); ) | |
OLD | NEW |