2015-02-02 20:55:14 +00:00
|
|
|
/*
|
2017-11-21 18:18:02 +00:00
|
|
|
* Copyright 2017 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
2015-02-02 20:55:14 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "example/HelloWorld.h"
|
2015-02-02 20:55:14 +00:00
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkCanvas.h"
|
|
|
|
#include "include/core/SkFont.h"
|
|
|
|
#include "include/core/SkGraphics.h"
|
|
|
|
#include "include/core/SkSurface.h"
|
|
|
|
#include "include/effects/SkGradientShader.h"
|
2015-02-02 20:55:14 +00:00
|
|
|
|
2017-11-21 18:18:02 +00:00
|
|
|
using namespace sk_app;
|
2015-02-02 20:55:14 +00:00
|
|
|
|
2017-11-21 18:18:02 +00:00
|
|
|
Application* Application::Create(int argc, char** argv, void* platformData) {
|
|
|
|
return new HelloWorld(argc, argv, platformData);
|
2015-02-02 20:55:14 +00:00
|
|
|
}
|
|
|
|
|
2017-11-21 18:18:02 +00:00
|
|
|
HelloWorld::HelloWorld(int argc, char** argv, void* platformData)
|
|
|
|
: fBackendType(Window::kNativeGL_BackendType)
|
|
|
|
, fRotationAngle(0) {
|
|
|
|
SkGraphics::Init();
|
2015-02-02 20:55:14 +00:00
|
|
|
|
2017-11-21 18:18:02 +00:00
|
|
|
fWindow = Window::CreateNativeWindow(platformData);
|
|
|
|
fWindow->setRequestedDisplayParams(DisplayParams());
|
2015-02-02 20:55:14 +00:00
|
|
|
|
2017-11-21 18:18:02 +00:00
|
|
|
// register callbacks
|
2017-12-08 21:45:43 +00:00
|
|
|
fWindow->pushLayer(this);
|
2015-02-02 20:55:14 +00:00
|
|
|
|
2017-11-21 18:18:02 +00:00
|
|
|
fWindow->attach(fBackendType);
|
|
|
|
}
|
2015-02-02 20:55:14 +00:00
|
|
|
|
2017-11-21 18:18:02 +00:00
|
|
|
HelloWorld::~HelloWorld() {
|
|
|
|
fWindow->detach();
|
|
|
|
delete fWindow;
|
|
|
|
}
|
2015-02-02 20:55:14 +00:00
|
|
|
|
2017-11-21 18:18:02 +00:00
|
|
|
void HelloWorld::updateTitle() {
|
2018-02-03 01:32:49 +00:00
|
|
|
if (!fWindow || fWindow->sampleCount() <= 1) {
|
2017-11-21 18:18:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkString title("Hello World ");
|
|
|
|
title.append(Window::kRaster_BackendType == fBackendType ? "Raster" : "OpenGL");
|
|
|
|
fWindow->setTitle(title.c_str());
|
2015-02-02 20:55:14 +00:00
|
|
|
}
|
|
|
|
|
2017-11-21 18:18:02 +00:00
|
|
|
void HelloWorld::onBackendCreated() {
|
|
|
|
this->updateTitle();
|
|
|
|
fWindow->show();
|
|
|
|
fWindow->inval();
|
2015-02-02 20:55:14 +00:00
|
|
|
}
|
|
|
|
|
2019-03-04 16:00:10 +00:00
|
|
|
void HelloWorld::onPaint(SkSurface* surface) {
|
|
|
|
auto canvas = surface->getCanvas();
|
|
|
|
|
2015-02-02 20:55:14 +00:00
|
|
|
// Clear background
|
2017-11-21 18:18:02 +00:00
|
|
|
canvas->clear(SK_ColorWHITE);
|
2015-02-02 20:55:14 +00:00
|
|
|
|
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorRED);
|
|
|
|
|
|
|
|
// Draw a rectangle with red paint
|
2016-03-14 19:22:10 +00:00
|
|
|
SkRect rect = SkRect::MakeXYWH(10, 10, 128, 128);
|
2015-02-02 20:55:14 +00:00
|
|
|
canvas->drawRect(rect, paint);
|
|
|
|
|
|
|
|
// Set up a linear gradient and draw a circle
|
|
|
|
{
|
2017-11-21 18:18:02 +00:00
|
|
|
SkPoint linearPoints[] = { { 0, 0 }, { 300, 300 } };
|
|
|
|
SkColor linearColors[] = { SK_ColorGREEN, SK_ColorBLACK };
|
|
|
|
paint.setShader(SkGradientShader::MakeLinear(linearPoints, linearColors, nullptr, 2,
|
2019-04-03 14:27:45 +00:00
|
|
|
SkTileMode::kMirror));
|
2017-11-21 18:18:02 +00:00
|
|
|
paint.setAntiAlias(true);
|
2015-02-02 20:55:14 +00:00
|
|
|
|
|
|
|
canvas->drawCircle(200, 200, 64, paint);
|
|
|
|
|
|
|
|
// Detach shader
|
2016-03-14 19:22:10 +00:00
|
|
|
paint.setShader(nullptr);
|
2015-02-02 20:55:14 +00:00
|
|
|
}
|
|
|
|
|
2017-11-21 18:18:02 +00:00
|
|
|
// Draw a message with a nice black paint
|
2019-01-04 04:29:38 +00:00
|
|
|
SkFont font;
|
|
|
|
font.setSubpixel(true);
|
|
|
|
font.setSize(20);
|
2015-02-02 20:55:14 +00:00
|
|
|
paint.setColor(SK_ColorBLACK);
|
|
|
|
|
|
|
|
canvas->save();
|
|
|
|
static const char message[] = "Hello World";
|
|
|
|
|
|
|
|
// Translate and rotate
|
|
|
|
canvas->translate(300, 300);
|
|
|
|
fRotationAngle += 0.2f;
|
|
|
|
if (fRotationAngle > 360) {
|
|
|
|
fRotationAngle -= 360;
|
|
|
|
}
|
|
|
|
canvas->rotate(fRotationAngle);
|
|
|
|
|
2017-11-21 18:18:02 +00:00
|
|
|
// Draw the text
|
2019-05-07 19:38:46 +00:00
|
|
|
canvas->drawSimpleText(message, strlen(message), SkTextEncoding::kUTF8, 0, 0, font, paint);
|
2015-02-02 20:55:14 +00:00
|
|
|
|
|
|
|
canvas->restore();
|
|
|
|
}
|
|
|
|
|
2017-11-21 18:18:02 +00:00
|
|
|
void HelloWorld::onIdle() {
|
|
|
|
// Just re-paint continously
|
|
|
|
fWindow->inval();
|
2015-02-02 20:55:14 +00:00
|
|
|
}
|
|
|
|
|
2019-08-29 14:39:22 +00:00
|
|
|
bool HelloWorld::onChar(SkUnichar c, skui::ModifierKey modifiers) {
|
2017-11-21 18:18:02 +00:00
|
|
|
if (' ' == c) {
|
|
|
|
fBackendType = Window::kRaster_BackendType == fBackendType ? Window::kNativeGL_BackendType
|
|
|
|
: Window::kRaster_BackendType;
|
|
|
|
fWindow->detach();
|
|
|
|
fWindow->attach(fBackendType);
|
2015-02-02 20:55:14 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|