2013-06-18 15:37:27 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-07-24 17:48:03 +00:00
|
|
|
#include "SkExample.h"
|
2013-06-18 15:37:27 +00:00
|
|
|
|
|
|
|
#include "SkApplication.h"
|
|
|
|
#include "SkDraw.h"
|
|
|
|
#include "SkGradientShader.h"
|
|
|
|
#include "SkGraphics.h"
|
|
|
|
#include "SkUnitMappers.h"
|
|
|
|
|
2013-07-24 17:48:03 +00:00
|
|
|
class HelloSkia : public SkExample {
|
|
|
|
public:
|
|
|
|
HelloSkia(SkExampleWindow* window) : SkExample(window) {
|
|
|
|
fName = "HelloSkia";
|
|
|
|
fBGColor = SK_ColorWHITE;
|
|
|
|
fRotationAngle = SkIntToScalar(0);
|
2013-06-18 15:37:27 +00:00
|
|
|
|
2013-07-24 17:48:03 +00:00
|
|
|
fWindow->setupBackend(SkExampleWindow::kGPU_DeviceType);
|
|
|
|
// Another option is software rendering:
|
|
|
|
// setupBackend(SkExampleWindow::kRaster_DeviceType);
|
|
|
|
}
|
2013-06-18 15:37:27 +00:00
|
|
|
|
2013-07-24 17:48:03 +00:00
|
|
|
protected:
|
|
|
|
void draw(SkCanvas* canvas) {
|
|
|
|
// Clear background
|
|
|
|
canvas->drawColor(fBGColor);
|
2013-06-18 15:37:27 +00:00
|
|
|
|
2013-07-24 17:48:03 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setColor(SK_ColorRED);
|
2013-06-18 15:37:27 +00:00
|
|
|
|
2013-07-24 17:48:03 +00:00
|
|
|
// Draw a rectangle with blue paint
|
|
|
|
SkRect rect = {
|
2013-06-18 15:37:27 +00:00
|
|
|
SkIntToScalar(10), SkIntToScalar(10),
|
|
|
|
SkIntToScalar(128), SkIntToScalar(128)
|
2013-07-24 17:48:03 +00:00
|
|
|
};
|
|
|
|
canvas->drawRect(rect, paint);
|
2013-06-18 15:37:27 +00:00
|
|
|
|
2013-07-24 17:48:03 +00:00
|
|
|
// Set up a linear gradient and draw a circle
|
|
|
|
{
|
|
|
|
SkPoint linearPoints[] = {
|
2013-06-18 15:37:27 +00:00
|
|
|
{SkIntToScalar(0), SkIntToScalar(0)},
|
|
|
|
{SkIntToScalar(300), SkIntToScalar(300)}
|
2013-07-24 17:48:03 +00:00
|
|
|
};
|
|
|
|
SkColor linearColors[] = {SK_ColorGREEN, SK_ColorBLACK};
|
2013-06-18 15:37:27 +00:00
|
|
|
|
2013-07-24 17:48:03 +00:00
|
|
|
SkUnitMapper* linearMapper = new SkDiscreteMapper(100);
|
|
|
|
SkAutoUnref lm_deleter(linearMapper);
|
2013-06-18 15:37:27 +00:00
|
|
|
|
2013-07-24 17:48:03 +00:00
|
|
|
SkShader* shader = SkGradientShader::CreateLinear(
|
|
|
|
linearPoints, linearColors, NULL, 2,
|
|
|
|
SkShader::kMirror_TileMode, linearMapper);
|
|
|
|
SkAutoUnref shader_deleter(shader);
|
2013-06-18 15:37:27 +00:00
|
|
|
|
2013-07-24 17:48:03 +00:00
|
|
|
paint.setShader(shader);
|
|
|
|
paint.setFlags(SkPaint::kAntiAlias_Flag);
|
2013-06-18 15:37:27 +00:00
|
|
|
|
2013-07-24 17:48:03 +00:00
|
|
|
canvas->drawCircle(SkIntToScalar(200), SkIntToScalar(200),
|
|
|
|
SkIntToScalar(64), paint);
|
2013-06-18 15:37:27 +00:00
|
|
|
|
2013-07-24 17:48:03 +00:00
|
|
|
// Detach shader
|
|
|
|
paint.setShader(NULL);
|
|
|
|
}
|
2013-06-18 15:37:27 +00:00
|
|
|
|
2013-07-24 17:48:03 +00:00
|
|
|
// Draw a message with a nice black paint.
|
|
|
|
paint.setFlags(
|
|
|
|
SkPaint::kAntiAlias_Flag |
|
|
|
|
SkPaint::kSubpixelText_Flag | // ... avoid waggly text when rotating.
|
|
|
|
SkPaint::kUnderlineText_Flag);
|
|
|
|
paint.setColor(SK_ColorBLACK);
|
|
|
|
paint.setTextSize(SkIntToScalar(20));
|
2013-06-18 15:37:27 +00:00
|
|
|
|
2013-07-24 17:48:03 +00:00
|
|
|
canvas->save();
|
2013-06-18 15:37:27 +00:00
|
|
|
|
2013-07-24 17:48:03 +00:00
|
|
|
static const char message[] = "Hello Skia!!!";
|
2013-06-18 15:37:27 +00:00
|
|
|
|
2013-07-24 17:48:03 +00:00
|
|
|
// Translate and rotate
|
|
|
|
canvas->translate(SkIntToScalar(300), SkIntToScalar(300));
|
|
|
|
fRotationAngle += SkDoubleToScalar(0.2);
|
|
|
|
if (fRotationAngle > SkDoubleToScalar(360.0)) {
|
|
|
|
fRotationAngle -= SkDoubleToScalar(360.0);
|
|
|
|
}
|
|
|
|
canvas->rotate(fRotationAngle);
|
2013-06-18 15:37:27 +00:00
|
|
|
|
2013-07-24 17:48:03 +00:00
|
|
|
// Draw the text:
|
|
|
|
canvas->drawText(message, strlen(message), SkIntToScalar(0), SkIntToScalar(0), paint);
|
2013-06-18 15:37:27 +00:00
|
|
|
|
2013-07-24 17:48:03 +00:00
|
|
|
canvas->restore();
|
2013-06-18 15:37:27 +00:00
|
|
|
|
2013-07-24 17:48:03 +00:00
|
|
|
// Invalidate the window to force a redraw. Poor man's animation mechanism.
|
|
|
|
this->fWindow->inval(NULL);
|
|
|
|
}
|
2013-06-18 15:37:27 +00:00
|
|
|
|
2013-07-24 17:48:03 +00:00
|
|
|
private:
|
|
|
|
SkScalar fRotationAngle;
|
|
|
|
SkColor fBGColor;
|
2013-06-18 15:37:27 +00:00
|
|
|
};
|
|
|
|
|
2013-07-24 17:48:03 +00:00
|
|
|
static SkExample* MyFactory(SkExampleWindow* window) {
|
|
|
|
return new HelloSkia(window);
|
2013-06-18 15:37:27 +00:00
|
|
|
}
|
2013-07-24 17:48:03 +00:00
|
|
|
|
|
|
|
// Register this class as a Skia Example.
|
|
|
|
SkExample::Registry registry(MyFactory);
|