1 /*
2  * Copyright 2011 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "gm/gm.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/core/SkColor.h"
11 #include "include/core/SkPaint.h"
12 #include "include/core/SkPoint.h"
13 #include "include/core/SkScalar.h"
14 #include "include/core/SkSize.h"
15 #include "include/core/SkString.h"
16 #include "include/utils/SkRandom.h"
17 
18 #include <stddef.h>
19 
20 namespace skiagm {
21 
22 class PointsGM : public GM {
23 public:
PointsGM()24     PointsGM() {}
25 
26 protected:
27 
onShortName()28     SkString onShortName() override {
29         return SkString("points");
30     }
31 
onISize()32     SkISize onISize() override {
33         return SkISize::Make(640, 490);
34     }
35 
fill_pts(SkPoint pts[],size_t n,SkRandom * rand)36     static void fill_pts(SkPoint pts[], size_t n, SkRandom* rand) {
37         for (size_t i = 0; i < n; i++) {
38             // Compute these independently and store in variables, rather
39             // than in the parameter-passing expression, to get consistent
40             // evaluation order across compilers.
41             SkScalar y = rand->nextUScalar1() * 480;
42             SkScalar x = rand->nextUScalar1() * 640;
43             pts[i].set(x, y);
44         }
45     }
46 
onDraw(SkCanvas * canvas)47     void onDraw(SkCanvas* canvas) override {
48         canvas->translate(SK_Scalar1, SK_Scalar1);
49 
50         SkRandom rand;
51         SkPaint  p0, p1, p2, p3;
52         const size_t n = 99;
53 
54         p0.setColor(SK_ColorRED);
55         p1.setColor(SK_ColorGREEN);
56         p2.setColor(SK_ColorBLUE);
57         p3.setColor(SK_ColorWHITE);
58 
59         p0.setStrokeWidth(SkIntToScalar(4));
60         p2.setStrokeCap(SkPaint::kRound_Cap);
61         p2.setStrokeWidth(SkIntToScalar(6));
62 
63         SkPoint* pts = new SkPoint[n];
64         fill_pts(pts, n, &rand);
65 
66         canvas->drawPoints(SkCanvas::kPolygon_PointMode, n, pts, p0);
67         canvas->drawPoints(SkCanvas::kLines_PointMode, n, pts, p1);
68         canvas->drawPoints(SkCanvas::kPoints_PointMode, n, pts, p2);
69         canvas->drawPoints(SkCanvas::kPoints_PointMode, n, pts, p3);
70 
71         delete[] pts;
72     }
73 
74 private:
75     using INHERITED = GM;
76 };
77 
78 //////////////////////////////////////////////////////////////////////////////
79 
80 DEF_GM( return new PointsGM; )
81 
82 }  // namespace skiagm
83