• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright 2017 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 "example/HelloWorld.h"
9 
10 #include "include/core/SkCanvas.h"
11 #include "include/core/SkFont.h"
12 #include "include/core/SkGraphics.h"
13 #include "include/core/SkSurface.h"
14 #include "include/effects/SkGradientShader.h"
15 
16 using namespace sk_app;
17 
Create(int argc,char ** argv,void * platformData)18 Application* Application::Create(int argc, char** argv, void* platformData) {
19     return new HelloWorld(argc, argv, platformData);
20 }
21 
HelloWorld(int argc,char ** argv,void * platformData)22 HelloWorld::HelloWorld(int argc, char** argv, void* platformData)
23         : fBackendType(Window::kNativeGL_BackendType)
24         , fRotationAngle(0) {
25     SkGraphics::Init();
26 
27     fWindow = Window::CreateNativeWindow(platformData);
28     fWindow->setRequestedDisplayParams(DisplayParams());
29 
30     // register callbacks
31     fWindow->pushLayer(this);
32 
33     fWindow->attach(fBackendType);
34 }
35 
~HelloWorld()36 HelloWorld::~HelloWorld() {
37     fWindow->detach();
38     delete fWindow;
39 }
40 
updateTitle()41 void HelloWorld::updateTitle() {
42     if (!fWindow || fWindow->sampleCount() <= 1) {
43         return;
44     }
45 
46     SkString title("Hello World ");
47     title.append(Window::kRaster_BackendType == fBackendType ? "Raster" : "OpenGL");
48     fWindow->setTitle(title.c_str());
49 }
50 
onBackendCreated()51 void HelloWorld::onBackendCreated() {
52     this->updateTitle();
53     fWindow->show();
54     fWindow->inval();
55 }
56 
onPaint(SkSurface * surface)57 void HelloWorld::onPaint(SkSurface* surface) {
58     auto canvas = surface->getCanvas();
59 
60     // Clear background
61     canvas->clear(SK_ColorWHITE);
62 
63     SkPaint paint;
64     paint.setColor(SK_ColorRED);
65 
66     // Draw a rectangle with red paint
67     SkRect rect = SkRect::MakeXYWH(10, 10, 128, 128);
68     canvas->drawRect(rect, paint);
69 
70     // Set up a linear gradient and draw a circle
71     {
72         SkPoint linearPoints[] = { { 0, 0 }, { 300, 300 } };
73         SkColor linearColors[] = { SK_ColorGREEN, SK_ColorBLACK };
74         paint.setShader(SkGradientShader::MakeLinear(linearPoints, linearColors, nullptr, 2,
75                                                      SkTileMode::kMirror));
76         paint.setAntiAlias(true);
77 
78         canvas->drawCircle(200, 200, 64, paint);
79 
80         // Detach shader
81         paint.setShader(nullptr);
82     }
83 
84     // Draw a message with a nice black paint
85     SkFont font;
86     font.setSubpixel(true);
87     font.setSize(20);
88     paint.setColor(SK_ColorBLACK);
89 
90     canvas->save();
91     static const char message[] = "Hello World";
92 
93     // Translate and rotate
94     canvas->translate(300, 300);
95     fRotationAngle += 0.2f;
96     if (fRotationAngle > 360) {
97         fRotationAngle -= 360;
98     }
99     canvas->rotate(fRotationAngle);
100 
101     // Draw the text
102     canvas->drawSimpleText(message, strlen(message), SkTextEncoding::kUTF8, 0, 0, font, paint);
103 
104     canvas->restore();
105 }
106 
onIdle()107 void HelloWorld::onIdle() {
108     // Just re-paint continously
109     fWindow->inval();
110 }
111 
onChar(SkUnichar c,skui::ModifierKey modifiers)112 bool HelloWorld::onChar(SkUnichar c, skui::ModifierKey modifiers) {
113     if (' ' == c) {
114         fBackendType = Window::kRaster_BackendType == fBackendType ? Window::kNativeGL_BackendType
115                                                                    : Window::kRaster_BackendType;
116         fWindow->detach();
117         fWindow->attach(fBackendType);
118     }
119     return true;
120 }
121