caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 1 | /* |
Brian Osman | eff04b5 | 2017-11-21 13:18:02 -0500 | [diff] [blame] | 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 | */ |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 7 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "example/HelloWorld.h" |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 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" |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 15 | |
Brian Osman | eff04b5 | 2017-11-21 13:18:02 -0500 | [diff] [blame] | 16 | using namespace sk_app; |
| 17 | |
| 18 | Application* Application::Create(int argc, char** argv, void* platformData) { |
| 19 | return new HelloWorld(argc, argv, platformData); |
| 20 | } |
| 21 | |
Brian Osman | eff04b5 | 2017-11-21 13:18:02 -0500 | [diff] [blame] | 22 | HelloWorld::HelloWorld(int argc, char** argv, void* platformData) |
| 23 | : fBackendType(Window::kNativeGL_BackendType) |
| 24 | , fRotationAngle(0) { |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 25 | SkGraphics::Init(); |
Brian Osman | eff04b5 | 2017-11-21 13:18:02 -0500 | [diff] [blame] | 26 | |
| 27 | fWindow = Window::CreateNativeWindow(platformData); |
| 28 | fWindow->setRequestedDisplayParams(DisplayParams()); |
| 29 | |
| 30 | // register callbacks |
Brian Osman | 80fc07e | 2017-12-08 16:45:43 -0500 | [diff] [blame] | 31 | fWindow->pushLayer(this); |
Brian Osman | eff04b5 | 2017-11-21 13:18:02 -0500 | [diff] [blame] | 32 | |
| 33 | fWindow->attach(fBackendType); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 34 | } |
| 35 | |
Brian Osman | eff04b5 | 2017-11-21 13:18:02 -0500 | [diff] [blame] | 36 | HelloWorld::~HelloWorld() { |
| 37 | fWindow->detach(); |
| 38 | delete fWindow; |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 39 | } |
| 40 | |
Brian Osman | eff04b5 | 2017-11-21 13:18:02 -0500 | [diff] [blame] | 41 | void HelloWorld::updateTitle() { |
Brian Salomon | bdecacf | 2018-02-02 20:32:49 -0500 | [diff] [blame] | 42 | if (!fWindow || fWindow->sampleCount() <= 1) { |
Brian Osman | eff04b5 | 2017-11-21 13:18:02 -0500 | [diff] [blame] | 43 | return; |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 44 | } |
| 45 | |
Brian Osman | eff04b5 | 2017-11-21 13:18:02 -0500 | [diff] [blame] | 46 | SkString title("Hello World "); |
| 47 | title.append(Window::kRaster_BackendType == fBackendType ? "Raster" : "OpenGL"); |
| 48 | fWindow->setTitle(title.c_str()); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 49 | } |
| 50 | |
Brian Osman | eff04b5 | 2017-11-21 13:18:02 -0500 | [diff] [blame] | 51 | void HelloWorld::onBackendCreated() { |
| 52 | this->updateTitle(); |
| 53 | fWindow->show(); |
| 54 | fWindow->inval(); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 55 | } |
| 56 | |
Robert Phillips | 9882dae | 2019-03-04 11:00:10 -0500 | [diff] [blame] | 57 | void HelloWorld::onPaint(SkSurface* surface) { |
| 58 | auto canvas = surface->getCanvas(); |
| 59 | |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 60 | // Clear background |
Brian Osman | eff04b5 | 2017-11-21 13:18:02 -0500 | [diff] [blame] | 61 | canvas->clear(SK_ColorWHITE); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 62 | |
| 63 | SkPaint paint; |
| 64 | paint.setColor(SK_ColorRED); |
| 65 | |
| 66 | // Draw a rectangle with red paint |
reed | c6f28f7 | 2016-03-14 12:22:10 -0700 | [diff] [blame] | 67 | SkRect rect = SkRect::MakeXYWH(10, 10, 128, 128); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 68 | canvas->drawRect(rect, paint); |
| 69 | |
| 70 | // Set up a linear gradient and draw a circle |
| 71 | { |
Brian Osman | eff04b5 | 2017-11-21 13:18:02 -0500 | [diff] [blame] | 72 | SkPoint linearPoints[] = { { 0, 0 }, { 300, 300 } }; |
| 73 | SkColor linearColors[] = { SK_ColorGREEN, SK_ColorBLACK }; |
| 74 | paint.setShader(SkGradientShader::MakeLinear(linearPoints, linearColors, nullptr, 2, |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 75 | SkTileMode::kMirror)); |
Brian Osman | eff04b5 | 2017-11-21 13:18:02 -0500 | [diff] [blame] | 76 | paint.setAntiAlias(true); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 77 | |
| 78 | canvas->drawCircle(200, 200, 64, paint); |
| 79 | |
| 80 | // Detach shader |
reed | c6f28f7 | 2016-03-14 12:22:10 -0700 | [diff] [blame] | 81 | paint.setShader(nullptr); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 82 | } |
| 83 | |
Brian Osman | eff04b5 | 2017-11-21 13:18:02 -0500 | [diff] [blame] | 84 | // Draw a message with a nice black paint |
Mike Reed | a6d1c0a | 2019-01-03 23:29:38 -0500 | [diff] [blame] | 85 | SkFont font; |
| 86 | font.setSubpixel(true); |
| 87 | font.setSize(20); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 88 | paint.setColor(SK_ColorBLACK); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 89 | |
| 90 | canvas->save(); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 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 | |
Brian Osman | eff04b5 | 2017-11-21 13:18:02 -0500 | [diff] [blame] | 101 | // Draw the text |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 102 | canvas->drawSimpleText(message, strlen(message), SkTextEncoding::kUTF8, 0, 0, font, paint); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 103 | |
| 104 | canvas->restore(); |
| 105 | } |
| 106 | |
Brian Osman | eff04b5 | 2017-11-21 13:18:02 -0500 | [diff] [blame] | 107 | void HelloWorld::onIdle() { |
| 108 | // Just re-paint continously |
| 109 | fWindow->inval(); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Hal Canary | b1f411a | 2019-08-29 10:39:22 -0400 | [diff] [blame] | 112 | bool HelloWorld::onChar(SkUnichar c, skui::ModifierKey modifiers) { |
Brian Osman | eff04b5 | 2017-11-21 13:18:02 -0500 | [diff] [blame] | 113 | if (' ' == c) { |
| 114 | fBackendType = Window::kRaster_BackendType == fBackendType ? Window::kNativeGL_BackendType |
| 115 | : Window::kRaster_BackendType; |
| 116 | fWindow->detach(); |
| 117 | fWindow->attach(fBackendType); |
caryclark | 52edc4d | 2015-02-02 12:55:14 -0800 | [diff] [blame] | 118 | } |
| 119 | return true; |
| 120 | } |