Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(488)

Side by Side Diff: experimental/SkiaExamples/HelloSkiaExample.cpp

Issue 16337012: Smallest possible desktop application that uses Skia to render stuff. (Closed) Base URL: https://skia.googlecode.com/svn/trunk
Patch Set: Presentation example Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
10 #include "HelloSkiaExample.h"
11
12 #include "SkDraw.h"
13 #include "SkGradientShader.h"
14 #include "SkGraphics.h"
15 #include "SkUnitMappers.h"
16
17 // The following three methods need to be defined so that your example
18 // runs on Linux and Mac.
19 SkOSWindow* create_sk_window(void* hwnd, int argc, char** argv) {
20 return new HelloSkia(hwnd, argc, argv);
21 }
22
23 void application_init() {
24 SkGraphics::Init();
25 SkEvent::Init();
26 }
27
28 void application_term() {
29 SkEvent::Term();
30 SkGraphics::Term();
31 }
32
33 HelloSkia::HelloSkia(void* hWnd, int argc, char** argv)
34 : INHERITED(hWnd, argc, argv)
35 {
36 fBGColor = SK_ColorWHITE;
37 fRotationAngle = SkIntToScalar(0);
38
39 setupBackend(kGPU_DeviceType);
40 }
41
42 void HelloSkia::draw(SkCanvas* canvas) {
43 // Clear background
44 canvas->drawColor(fBGColor);
45
46 SkPaint paint;
47 paint.setColor(SK_ColorRED); // SkColor is ARGB
48
49 // Draw a rectangle with blue paint
50 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.
51 canvas->drawRect(rect, paint);
52
53 // Set up a linear gradient and draw a circle
54 {
55 SkPoint linearPoints[] = {{0, 0}, {SkIntToScalar(300), SkIntToScalar(300 )}};
56 SkColor linearColors[] = {SK_ColorGREEN, SK_ColorBLACK};
57
58 SkUnitMapper* linearMapper = new SkDiscreteMapper(100);
59 SkAutoUnref lm_deleter(linearMapper);
60
61 SkShader* shader = SkGradientShader::CreateLinear(
62 linearPoints, linearColors, NULL, 2, SkShader::kMirror_TileMode, linearMapper);
63 SkAutoUnref shader_deleter(shader);
64
65 paint.setShader(shader);
66 paint.setFlags(SkPaint::kAntiAlias_Flag);
67
68 canvas->drawCircle(200, 200, 64, paint);
caryclark 2013/06/10 15:48:40 SkIntToScalar
69
70 // Detach shader
71 paint.setShader(NULL);
72 }
73
74
75 // Draw a message with a nice black paint.
76 paint.setFlags(
77 SkPaint::kAntiAlias_Flag |
78 SkPaint::kSubpixelText_Flag | // ... avoid waggly text when rotatin g.
79 SkPaint::kUnderlineText_Flag);
80 paint.setColor(SK_ColorBLACK);
81 paint.setTextSize(SkIntToScalar(20));
82
83 canvas->save();
84
85 static const char message[] = "Hello Skia!!!";
86
87 // Translate and rotate
88 canvas->translate(300, 300);
89 fRotationAngle += SkDoubleToScalar(0.2);
90 if (fRotationAngle > SkDoubleToScalar(360.0)) {
91 fRotationAngle -= SkDoubleToScalar(360.0);
92 }
93 canvas->rotate(fRotationAngle);
94
95 // Draw the text:
96 canvas->drawText(message, strlen(message), 0, 0, paint);
97
98 canvas->restore();
99
100 // Invalidate the window to force a redraw. Poor man's animation mechanism.
101 this->inval(NULL);
102 canvas->flush();
103
104 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.
105 present();
106 }
107 }
108
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698