blob: f61978246f49b7b9774ef008b53f9850da9d03ff [file] [log] [blame]
caryclark52edc4d2015-02-02 12:55:14 -08001/*
Brian Osmaneff04b52017-11-21 13:18:02 -05002* 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*/
caryclark52edc4d2015-02-02 12:55:14 -08007
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "example/HelloWorld.h"
caryclark52edc4d2015-02-02 12:55:14 -08009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#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"
caryclark52edc4d2015-02-02 12:55:14 -080015
Brian Osmaneff04b52017-11-21 13:18:02 -050016using namespace sk_app;
17
18Application* Application::Create(int argc, char** argv, void* platformData) {
19 return new HelloWorld(argc, argv, platformData);
20}
21
Brian Osmaneff04b52017-11-21 13:18:02 -050022HelloWorld::HelloWorld(int argc, char** argv, void* platformData)
23 : fBackendType(Window::kNativeGL_BackendType)
24 , fRotationAngle(0) {
caryclark52edc4d2015-02-02 12:55:14 -080025 SkGraphics::Init();
Brian Osmaneff04b52017-11-21 13:18:02 -050026
27 fWindow = Window::CreateNativeWindow(platformData);
28 fWindow->setRequestedDisplayParams(DisplayParams());
29
30 // register callbacks
Brian Osman80fc07e2017-12-08 16:45:43 -050031 fWindow->pushLayer(this);
Brian Osmaneff04b52017-11-21 13:18:02 -050032
33 fWindow->attach(fBackendType);
caryclark52edc4d2015-02-02 12:55:14 -080034}
35
Brian Osmaneff04b52017-11-21 13:18:02 -050036HelloWorld::~HelloWorld() {
37 fWindow->detach();
38 delete fWindow;
caryclark52edc4d2015-02-02 12:55:14 -080039}
40
Brian Osmaneff04b52017-11-21 13:18:02 -050041void HelloWorld::updateTitle() {
Brian Salomonbdecacf2018-02-02 20:32:49 -050042 if (!fWindow || fWindow->sampleCount() <= 1) {
Brian Osmaneff04b52017-11-21 13:18:02 -050043 return;
caryclark52edc4d2015-02-02 12:55:14 -080044 }
45
Brian Osmaneff04b52017-11-21 13:18:02 -050046 SkString title("Hello World ");
47 title.append(Window::kRaster_BackendType == fBackendType ? "Raster" : "OpenGL");
48 fWindow->setTitle(title.c_str());
caryclark52edc4d2015-02-02 12:55:14 -080049}
50
Brian Osmaneff04b52017-11-21 13:18:02 -050051void HelloWorld::onBackendCreated() {
52 this->updateTitle();
53 fWindow->show();
54 fWindow->inval();
caryclark52edc4d2015-02-02 12:55:14 -080055}
56
Robert Phillips9882dae2019-03-04 11:00:10 -050057void HelloWorld::onPaint(SkSurface* surface) {
58 auto canvas = surface->getCanvas();
59
caryclark52edc4d2015-02-02 12:55:14 -080060 // Clear background
Brian Osmaneff04b52017-11-21 13:18:02 -050061 canvas->clear(SK_ColorWHITE);
caryclark52edc4d2015-02-02 12:55:14 -080062
63 SkPaint paint;
64 paint.setColor(SK_ColorRED);
65
66 // Draw a rectangle with red paint
reedc6f28f72016-03-14 12:22:10 -070067 SkRect rect = SkRect::MakeXYWH(10, 10, 128, 128);
caryclark52edc4d2015-02-02 12:55:14 -080068 canvas->drawRect(rect, paint);
69
70 // Set up a linear gradient and draw a circle
71 {
Brian Osmaneff04b52017-11-21 13:18:02 -050072 SkPoint linearPoints[] = { { 0, 0 }, { 300, 300 } };
73 SkColor linearColors[] = { SK_ColorGREEN, SK_ColorBLACK };
74 paint.setShader(SkGradientShader::MakeLinear(linearPoints, linearColors, nullptr, 2,
Mike Reedfae8fce2019-04-03 10:27:45 -040075 SkTileMode::kMirror));
Brian Osmaneff04b52017-11-21 13:18:02 -050076 paint.setAntiAlias(true);
caryclark52edc4d2015-02-02 12:55:14 -080077
78 canvas->drawCircle(200, 200, 64, paint);
79
80 // Detach shader
reedc6f28f72016-03-14 12:22:10 -070081 paint.setShader(nullptr);
caryclark52edc4d2015-02-02 12:55:14 -080082 }
83
Brian Osmaneff04b52017-11-21 13:18:02 -050084 // Draw a message with a nice black paint
Mike Reeda6d1c0a2019-01-03 23:29:38 -050085 SkFont font;
86 font.setSubpixel(true);
87 font.setSize(20);
caryclark52edc4d2015-02-02 12:55:14 -080088 paint.setColor(SK_ColorBLACK);
caryclark52edc4d2015-02-02 12:55:14 -080089
90 canvas->save();
caryclark52edc4d2015-02-02 12:55:14 -080091 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
Brian Osmaneff04b52017-11-21 13:18:02 -0500101 // Draw the text
Ben Wagner51e15a62019-05-07 15:38:46 -0400102 canvas->drawSimpleText(message, strlen(message), SkTextEncoding::kUTF8, 0, 0, font, paint);
caryclark52edc4d2015-02-02 12:55:14 -0800103
104 canvas->restore();
105}
106
Brian Osmaneff04b52017-11-21 13:18:02 -0500107void HelloWorld::onIdle() {
108 // Just re-paint continously
109 fWindow->inval();
caryclark52edc4d2015-02-02 12:55:14 -0800110}
111
Hal Canaryb1f411a2019-08-29 10:39:22 -0400112bool HelloWorld::onChar(SkUnichar c, skui::ModifierKey modifiers) {
Brian Osmaneff04b52017-11-21 13:18:02 -0500113 if (' ' == c) {
114 fBackendType = Window::kRaster_BackendType == fBackendType ? Window::kNativeGL_BackendType
115 : Window::kRaster_BackendType;
116 fWindow->detach();
117 fWindow->attach(fBackendType);
caryclark52edc4d2015-02-02 12:55:14 -0800118 }
119 return true;
120}