OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 * | 7 * |
8 */ | 8 */ |
9 | 9 |
10 #include "BaseExample.h" | 10 #include "SkExample.h" |
11 | 11 |
12 #include "SkApplication.h" | 12 #include "SkApplication.h" |
13 #include "SkDraw.h" | 13 #include "SkDraw.h" |
14 #include "SkGradientShader.h" | 14 #include "SkGradientShader.h" |
15 #include "SkGraphics.h" | 15 #include "SkGraphics.h" |
16 #include "SkUnitMappers.h" | 16 #include "SkUnitMappers.h" |
17 | 17 |
18 class HelloSkia : public BaseExample { | 18 class HelloSkia : public SkExample { |
19 public: | 19 public: |
20 HelloSkia(void* hWnd, int argc, char** argv) | 20 HelloSkia(SkExampleWindow* window) : SkExample(window) { |
21 : INHERITED(hWnd, argc, argv) | 21 fName = "HelloSkia"; |
| 22 fBGColor = SK_ColorWHITE; |
| 23 fRotationAngle = SkIntToScalar(0); |
| 24 |
| 25 fWindow->setupBackend(SkExampleWindow::kGPU_DeviceType); |
| 26 // Another option is software rendering: |
| 27 // setupBackend(SkExampleWindow::kRaster_DeviceType); |
| 28 } |
| 29 |
| 30 protected: |
| 31 void draw(SkCanvas* canvas) { |
| 32 // Clear background |
| 33 canvas->drawColor(fBGColor); |
| 34 |
| 35 SkPaint paint; |
| 36 paint.setColor(SK_ColorRED); |
| 37 |
| 38 // Draw a rectangle with blue paint |
| 39 SkRect rect = { |
| 40 SkIntToScalar(10), SkIntToScalar(10), |
| 41 SkIntToScalar(128), SkIntToScalar(128) |
| 42 }; |
| 43 canvas->drawRect(rect, paint); |
| 44 |
| 45 // Set up a linear gradient and draw a circle |
22 { | 46 { |
23 fBGColor = SK_ColorWHITE; | 47 SkPoint linearPoints[] = { |
24 fRotationAngle = SkIntToScalar(0); | 48 {SkIntToScalar(0), SkIntToScalar(0)}, |
| 49 {SkIntToScalar(300), SkIntToScalar(300)} |
| 50 }; |
| 51 SkColor linearColors[] = {SK_ColorGREEN, SK_ColorBLACK}; |
25 | 52 |
26 setupBackend(kGPU_DeviceType); | 53 SkUnitMapper* linearMapper = new SkDiscreteMapper(100); |
27 // Another option is software rendering: | 54 SkAutoUnref lm_deleter(linearMapper); |
28 // setupBackend(kRaster_DeviceType); | 55 |
| 56 SkShader* shader = SkGradientShader::CreateLinear( |
| 57 linearPoints, linearColors, NULL, 2, |
| 58 SkShader::kMirror_TileMode, linearMapper); |
| 59 SkAutoUnref shader_deleter(shader); |
| 60 |
| 61 paint.setShader(shader); |
| 62 paint.setFlags(SkPaint::kAntiAlias_Flag); |
| 63 |
| 64 canvas->drawCircle(SkIntToScalar(200), SkIntToScalar(200), |
| 65 SkIntToScalar(64), paint); |
| 66 |
| 67 // Detach shader |
| 68 paint.setShader(NULL); |
29 } | 69 } |
30 | 70 |
31 protected: | 71 // Draw a message with a nice black paint. |
32 virtual void draw(SkCanvas* canvas) SK_OVERRIDE { | 72 paint.setFlags( |
33 // Clear background | 73 SkPaint::kAntiAlias_Flag | |
34 canvas->drawColor(fBGColor); | 74 SkPaint::kSubpixelText_Flag | // ... avoid waggly text when rot
ating. |
| 75 SkPaint::kUnderlineText_Flag); |
| 76 paint.setColor(SK_ColorBLACK); |
| 77 paint.setTextSize(SkIntToScalar(20)); |
35 | 78 |
36 SkPaint paint; | 79 canvas->save(); |
37 paint.setColor(SK_ColorRED); | |
38 | 80 |
39 // Draw a rectangle with blue paint | 81 static const char message[] = "Hello Skia!!!"; |
40 SkRect rect = { | |
41 SkIntToScalar(10), SkIntToScalar(10), | |
42 SkIntToScalar(128), SkIntToScalar(128) | |
43 }; | |
44 canvas->drawRect(rect, paint); | |
45 | 82 |
46 // Set up a linear gradient and draw a circle | 83 // Translate and rotate |
47 { | 84 canvas->translate(SkIntToScalar(300), SkIntToScalar(300)); |
48 SkPoint linearPoints[] = { | 85 fRotationAngle += SkDoubleToScalar(0.2); |
49 {SkIntToScalar(0), SkIntToScalar(0)}, | 86 if (fRotationAngle > SkDoubleToScalar(360.0)) { |
50 {SkIntToScalar(300), SkIntToScalar(300)} | 87 fRotationAngle -= SkDoubleToScalar(360.0); |
51 }; | 88 } |
52 SkColor linearColors[] = {SK_ColorGREEN, SK_ColorBLACK}; | 89 canvas->rotate(fRotationAngle); |
53 | 90 |
54 SkUnitMapper* linearMapper = new SkDiscreteMapper(100); | 91 // Draw the text: |
55 SkAutoUnref lm_deleter(linearMapper); | 92 canvas->drawText(message, strlen(message), SkIntToScalar(0), SkIntToScal
ar(0), paint); |
56 | 93 |
57 SkShader* shader = SkGradientShader::CreateLinear( | 94 canvas->restore(); |
58 linearPoints, linearColors, NULL, 2, | |
59 SkShader::kMirror_TileMode, linearMapper); | |
60 SkAutoUnref shader_deleter(shader); | |
61 | 95 |
62 paint.setShader(shader); | 96 // Invalidate the window to force a redraw. Poor man's animation mechani
sm. |
63 paint.setFlags(SkPaint::kAntiAlias_Flag); | 97 this->fWindow->inval(NULL); |
| 98 } |
64 | 99 |
65 canvas->drawCircle(SkIntToScalar(200), SkIntToScalar(200), | 100 private: |
66 SkIntToScalar(64), paint); | 101 SkScalar fRotationAngle; |
67 | 102 SkColor fBGColor; |
68 // Detach shader | |
69 paint.setShader(NULL); | |
70 } | |
71 | |
72 | |
73 // Draw a message with a nice black paint. | |
74 paint.setFlags( | |
75 SkPaint::kAntiAlias_Flag | | |
76 SkPaint::kSubpixelText_Flag | // ... avoid waggly text when
rotating. | |
77 SkPaint::kUnderlineText_Flag); | |
78 paint.setColor(SK_ColorBLACK); | |
79 paint.setTextSize(SkIntToScalar(20)); | |
80 | |
81 canvas->save(); | |
82 | |
83 static const char message[] = "Hello Skia!!!"; | |
84 | |
85 // Translate and rotate | |
86 canvas->translate(SkIntToScalar(300), SkIntToScalar(300)); | |
87 fRotationAngle += SkDoubleToScalar(0.2); | |
88 if (fRotationAngle > SkDoubleToScalar(360.0)) { | |
89 fRotationAngle -= SkDoubleToScalar(360.0); | |
90 } | |
91 canvas->rotate(fRotationAngle); | |
92 | |
93 // Draw the text: | |
94 canvas->drawText(message, strlen(message), SkIntToScalar(0), SkIntTo
Scalar(0), paint); | |
95 | |
96 canvas->restore(); | |
97 | |
98 // Invalidate the window to force a redraw. Poor man's animation mec
hanism. | |
99 this->inval(NULL); | |
100 | |
101 INHERITED::draw(canvas); | |
102 } | |
103 | |
104 private: | |
105 SkScalar fRotationAngle; | |
106 SkColor fBGColor; | |
107 typedef BaseExample INHERITED; | |
108 }; | 103 }; |
109 | 104 |
110 SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) { | 105 static SkExample* MyFactory(SkExampleWindow* window) { |
111 return new HelloSkia(hwnd, argc, argv); | 106 return new HelloSkia(window); |
112 } | 107 } |
| 108 |
| 109 // Register this class as a Skia Example. |
| 110 SkExample::Registry registry(MyFactory); |
OLD | NEW |