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