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
|
|
|
|
|
|
|
#include "HelloWorld.h"
|
|
|
|
|
2015-10-09 20:36:42 +00:00
|
|
|
#include "GrContext.h"
|
2015-02-02 20:55:14 +00:00
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkGraphics.h"
|
|
|
|
|
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() {
|
|
|
|
if (!fWindow || fWindow->sampleCount() < 0) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-11-21 18:18:02 +00:00
|
|
|
void HelloWorld::onPaint(SkCanvas* canvas) {
|
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,
|
|
|
|
SkShader::kMirror_TileMode));
|
|
|
|
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
|
|
|
|
paint.setSubpixelText(true);
|
2015-02-02 20:55:14 +00:00
|
|
|
paint.setColor(SK_ColorBLACK);
|
|
|
|
paint.setTextSize(20);
|
|
|
|
|
|
|
|
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
|
2015-02-02 20:55:14 +00:00
|
|
|
canvas->drawText(message, strlen(message), 0, 0, paint);
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-11-21 18:18:02 +00:00
|
|
|
bool HelloWorld::onChar(SkUnichar c, uint32_t modifiers) {
|
|
|
|
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;
|
|
|
|
}
|