commit-bot@chromium.org | 7fa22f3 | 2013-06-18 15:37:27 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 Google Inc. |
| 3 | * |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | * |
| 8 | */ |
| 9 | |
sglez@google.com | 43f2b2c | 2013-07-24 17:48:03 +0000 | [diff] [blame] | 10 | #include "SkExample.h" |
commit-bot@chromium.org | 7fa22f3 | 2013-06-18 15:37:27 +0000 | [diff] [blame] | 11 | |
| 12 | #include "SkApplication.h" |
| 13 | #include "SkDraw.h" |
| 14 | #include "SkGradientShader.h" |
| 15 | #include "SkGraphics.h" |
commit-bot@chromium.org | 7fa22f3 | 2013-06-18 15:37:27 +0000 | [diff] [blame] | 16 | |
sglez@google.com | 43f2b2c | 2013-07-24 17:48:03 +0000 | [diff] [blame] | 17 | class HelloSkia : public SkExample { |
| 18 | public: |
| 19 | HelloSkia(SkExampleWindow* window) : SkExample(window) { |
| 20 | fName = "HelloSkia"; |
| 21 | fBGColor = SK_ColorWHITE; |
| 22 | fRotationAngle = SkIntToScalar(0); |
commit-bot@chromium.org | 7fa22f3 | 2013-06-18 15:37:27 +0000 | [diff] [blame] | 23 | |
sglez@google.com | 43f2b2c | 2013-07-24 17:48:03 +0000 | [diff] [blame] | 24 | fWindow->setupBackend(SkExampleWindow::kGPU_DeviceType); |
| 25 | // Another option is software rendering: |
commit-bot@chromium.org | ab9d30c | 2013-10-10 02:53:30 +0000 | [diff] [blame] | 26 | // fWindow->setupBackend(SkExampleWindow::kRaster_DeviceType); |
sglez@google.com | 43f2b2c | 2013-07-24 17:48:03 +0000 | [diff] [blame] | 27 | } |
commit-bot@chromium.org | 7fa22f3 | 2013-06-18 15:37:27 +0000 | [diff] [blame] | 28 | |
sglez@google.com | 43f2b2c | 2013-07-24 17:48:03 +0000 | [diff] [blame] | 29 | protected: |
| 30 | void draw(SkCanvas* canvas) { |
| 31 | // Clear background |
| 32 | canvas->drawColor(fBGColor); |
commit-bot@chromium.org | 7fa22f3 | 2013-06-18 15:37:27 +0000 | [diff] [blame] | 33 | |
sglez@google.com | 43f2b2c | 2013-07-24 17:48:03 +0000 | [diff] [blame] | 34 | SkPaint paint; |
| 35 | paint.setColor(SK_ColorRED); |
commit-bot@chromium.org | 7fa22f3 | 2013-06-18 15:37:27 +0000 | [diff] [blame] | 36 | |
sglez@google.com | 43f2b2c | 2013-07-24 17:48:03 +0000 | [diff] [blame] | 37 | // Draw a rectangle with blue paint |
| 38 | SkRect rect = { |
commit-bot@chromium.org | 7fa22f3 | 2013-06-18 15:37:27 +0000 | [diff] [blame] | 39 | SkIntToScalar(10), SkIntToScalar(10), |
| 40 | SkIntToScalar(128), SkIntToScalar(128) |
sglez@google.com | 43f2b2c | 2013-07-24 17:48:03 +0000 | [diff] [blame] | 41 | }; |
| 42 | canvas->drawRect(rect, paint); |
commit-bot@chromium.org | 7fa22f3 | 2013-06-18 15:37:27 +0000 | [diff] [blame] | 43 | |
sglez@google.com | 43f2b2c | 2013-07-24 17:48:03 +0000 | [diff] [blame] | 44 | // Set up a linear gradient and draw a circle |
| 45 | { |
| 46 | SkPoint linearPoints[] = { |
commit-bot@chromium.org | 7fa22f3 | 2013-06-18 15:37:27 +0000 | [diff] [blame] | 47 | {SkIntToScalar(0), SkIntToScalar(0)}, |
| 48 | {SkIntToScalar(300), SkIntToScalar(300)} |
sglez@google.com | 43f2b2c | 2013-07-24 17:48:03 +0000 | [diff] [blame] | 49 | }; |
| 50 | SkColor linearColors[] = {SK_ColorGREEN, SK_ColorBLACK}; |
commit-bot@chromium.org | 7fa22f3 | 2013-06-18 15:37:27 +0000 | [diff] [blame] | 51 | |
sglez@google.com | 43f2b2c | 2013-07-24 17:48:03 +0000 | [diff] [blame] | 52 | SkShader* shader = SkGradientShader::CreateLinear( |
| 53 | linearPoints, linearColors, NULL, 2, |
commit-bot@chromium.org | 83f23d8 | 2014-05-22 12:27:41 +0000 | [diff] [blame^] | 54 | SkShader::kMirror_TileMode); |
sglez@google.com | 43f2b2c | 2013-07-24 17:48:03 +0000 | [diff] [blame] | 55 | SkAutoUnref shader_deleter(shader); |
commit-bot@chromium.org | 7fa22f3 | 2013-06-18 15:37:27 +0000 | [diff] [blame] | 56 | |
sglez@google.com | 43f2b2c | 2013-07-24 17:48:03 +0000 | [diff] [blame] | 57 | paint.setShader(shader); |
| 58 | paint.setFlags(SkPaint::kAntiAlias_Flag); |
commit-bot@chromium.org | 7fa22f3 | 2013-06-18 15:37:27 +0000 | [diff] [blame] | 59 | |
sglez@google.com | 43f2b2c | 2013-07-24 17:48:03 +0000 | [diff] [blame] | 60 | canvas->drawCircle(SkIntToScalar(200), SkIntToScalar(200), |
| 61 | SkIntToScalar(64), paint); |
commit-bot@chromium.org | 7fa22f3 | 2013-06-18 15:37:27 +0000 | [diff] [blame] | 62 | |
sglez@google.com | 43f2b2c | 2013-07-24 17:48:03 +0000 | [diff] [blame] | 63 | // Detach shader |
| 64 | paint.setShader(NULL); |
commit-bot@chromium.org | 7fa22f3 | 2013-06-18 15:37:27 +0000 | [diff] [blame] | 65 | } |
| 66 | |
sglez@google.com | 43f2b2c | 2013-07-24 17:48:03 +0000 | [diff] [blame] | 67 | // Draw a message with a nice black paint. |
| 68 | paint.setFlags( |
| 69 | SkPaint::kAntiAlias_Flag | |
| 70 | SkPaint::kSubpixelText_Flag | // ... avoid waggly text when rotating. |
| 71 | SkPaint::kUnderlineText_Flag); |
| 72 | paint.setColor(SK_ColorBLACK); |
| 73 | paint.setTextSize(SkIntToScalar(20)); |
| 74 | |
| 75 | canvas->save(); |
| 76 | |
| 77 | static const char message[] = "Hello Skia!!!"; |
| 78 | |
| 79 | // Translate and rotate |
| 80 | canvas->translate(SkIntToScalar(300), SkIntToScalar(300)); |
| 81 | fRotationAngle += SkDoubleToScalar(0.2); |
| 82 | if (fRotationAngle > SkDoubleToScalar(360.0)) { |
| 83 | fRotationAngle -= SkDoubleToScalar(360.0); |
| 84 | } |
| 85 | canvas->rotate(fRotationAngle); |
| 86 | |
| 87 | // Draw the text: |
| 88 | canvas->drawText(message, strlen(message), SkIntToScalar(0), SkIntToScalar(0), paint); |
| 89 | |
| 90 | canvas->restore(); |
| 91 | |
| 92 | // Invalidate the window to force a redraw. Poor man's animation mechanism. |
| 93 | this->fWindow->inval(NULL); |
| 94 | } |
| 95 | |
| 96 | private: |
| 97 | SkScalar fRotationAngle; |
| 98 | SkColor fBGColor; |
commit-bot@chromium.org | 7fa22f3 | 2013-06-18 15:37:27 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
sglez@google.com | 43f2b2c | 2013-07-24 17:48:03 +0000 | [diff] [blame] | 101 | static SkExample* MyFactory(SkExampleWindow* window) { |
| 102 | return new HelloSkia(window); |
commit-bot@chromium.org | 7fa22f3 | 2013-06-18 15:37:27 +0000 | [diff] [blame] | 103 | } |
sglez@google.com | 43f2b2c | 2013-07-24 17:48:03 +0000 | [diff] [blame] | 104 | |
| 105 | // Register this class as a Skia Example. |
| 106 | SkExample::Registry registry(MyFactory); |