Chromium Code Reviews| Index: experimental/SkiaExamples/HelloSkiaExample.cpp |
| diff --git a/experimental/SkiaExamples/HelloSkiaExample.cpp b/experimental/SkiaExamples/HelloSkiaExample.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8477162cb4845647c7561188a829d8d79cf78480 |
| --- /dev/null |
| +++ b/experimental/SkiaExamples/HelloSkiaExample.cpp |
| @@ -0,0 +1,108 @@ |
| +/* |
| + * Copyright 2013 Google Inc. |
| + * |
| + * |
| + * Use of this source code is governed by a BSD-style license that can be |
| + * found in the LICENSE file. |
| + * |
| + */ |
| + |
| +#include "HelloSkiaExample.h" |
| + |
| +#include "SkDraw.h" |
| +#include "SkGradientShader.h" |
| +#include "SkGraphics.h" |
| +#include "SkUnitMappers.h" |
| + |
| +// The following three methods need to be defined so that your example |
| +// runs on Linux and Mac. |
| +SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) { |
| + return new HelloSkia(hwnd, argc, argv); |
| +} |
| + |
| +void application_init() { |
| + SkGraphics::Init(); |
| + SkEvent::Init(); |
| +} |
| + |
| +void application_term() { |
| + SkEvent::Term(); |
| + SkGraphics::Term(); |
| +} |
| + |
| +HelloSkia::HelloSkia(void* hWnd, int argc, char** argv) |
| + : INHERITED(hWnd, argc, argv) |
| +{ |
| + fBGColor = SK_ColorWHITE; |
| + fRotationAngle = SkIntToScalar(0); |
| + |
| + setupBackend(kGPU_DeviceType); |
| +} |
| + |
| +void HelloSkia::draw(SkCanvas* canvas) { |
| + // Clear background |
| + canvas->drawColor(fBGColor); |
| + |
| + SkPaint paint; |
| + paint.setColor(SK_ColorRED); // SkColor is ARGB |
| + |
| + // Draw a rectangle with blue paint |
| + SkRect rect = {10, 10, 128, 128}; |
|
caryclark
2013/06/10 15:48:40
these need SkIntToScalar functions
sglez
2013/06/11 04:24:38
Thanks; did a regex substitution on the file.
|
| + canvas->drawRect(rect, paint); |
| + |
| + // Set up a linear gradient and draw a circle |
| + { |
| + SkPoint linearPoints[] = {{0, 0}, {SkIntToScalar(300), SkIntToScalar(300)}}; |
| + SkColor linearColors[] = {SK_ColorGREEN, SK_ColorBLACK}; |
| + |
| + SkUnitMapper* linearMapper = new SkDiscreteMapper(100); |
| + SkAutoUnref lm_deleter(linearMapper); |
| + |
| + SkShader* shader = SkGradientShader::CreateLinear( |
| + linearPoints, linearColors, NULL, 2, SkShader::kMirror_TileMode, linearMapper); |
| + SkAutoUnref shader_deleter(shader); |
| + |
| + paint.setShader(shader); |
| + paint.setFlags(SkPaint::kAntiAlias_Flag); |
| + |
| + canvas->drawCircle(200, 200, 64, paint); |
|
caryclark
2013/06/10 15:48:40
SkIntToScalar
|
| + |
| + // Detach shader |
| + paint.setShader(NULL); |
| + } |
| + |
| + |
| + // 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)); |
| + |
| + canvas->save(); |
| + |
| + static const char message[] = "Hello Skia!!!"; |
| + |
| + // Translate and rotate |
| + canvas->translate(300, 300); |
| + fRotationAngle += SkDoubleToScalar(0.2); |
| + if (fRotationAngle > SkDoubleToScalar(360.0)) { |
| + fRotationAngle -= SkDoubleToScalar(360.0); |
| + } |
| + canvas->rotate(fRotationAngle); |
| + |
| + // Draw the text: |
| + canvas->drawText(message, strlen(message), 0, 0, paint); |
| + |
| + canvas->restore(); |
| + |
| + // Invalidate the window to force a redraw. Poor man's animation mechanism. |
| + this->inval(NULL); |
| + canvas->flush(); |
| + |
| + if (getDeviceType() == kGPU_DeviceType) { |
|
caryclark
2013/06/10 15:48:40
since the base class does this, call it here
sglez
2013/06/11 04:24:38
Done.
|
| + present(); |
| + } |
| +} |
| + |