Android release v6.7_preview1
[xcsoar.git] / src / Topography / ShapeRenderer.hpp
blobb7c97f6a1644492be5ee659afcfc05016012f14b
1 /*
2 Copyright_License {
4 XCSoar Glide Computer - http://www.xcsoar.org/
5 Copyright (C) 2000-2013 The XCSoar Project
6 A detailed list of copyright holders can be found in the file "AUTHORS".
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #ifndef SHAPE_RENDERER_HPP
25 #define SHAPE_RENDERER_HPP
27 #include "Screen/Pen.hpp"
28 #include "Screen/Point.hpp"
29 #include "Util/NonCopyable.hpp"
30 #include "Util/AllocatedArray.hpp"
31 #include "Screen/Canvas.hpp"
32 #include "Screen/Brush.hpp"
34 #include <assert.h>
36 /**
37 * A helper class optimized for doing bulk draws on OpenGL.
39 class ShapeRenderer : private NonCopyable {
40 AllocatedArray<RasterPoint> points;
41 unsigned num_points;
43 const Pen *pen;
44 const Brush *brush;
46 enum { NONE, OUTLINE, SOLID } mode;
48 public:
49 void Configure(const Pen *_pen, const Brush *_brush) {
50 pen = _pen;
51 brush = _brush;
52 mode = NONE;
54 num_points = 0;
57 void Begin(unsigned n) {
58 assert(num_points == 0);
60 points.GrowDiscard(((n - 1) | 0x3ff) + 1);
63 void AddPoint(RasterPoint pt) {
64 assert(num_points < points.size());
66 points[num_points++] = pt;
69 /**
70 * Adds the point only if it a few pixels distant from the previous
71 * one. Useful to reduce the complexity of small figures.
73 void AddPointIfDistant(RasterPoint pt) {
74 assert(num_points < points.size());
76 if (num_points == 0 || manhattan_distance(points[num_points - 1], pt) >= 8)
77 AddPoint(pt);
80 void FinishPolyline(Canvas &canvas) {
81 if (mode != OUTLINE) {
82 canvas.Select(*pen);
83 mode = OUTLINE;
86 canvas.DrawPolyline(points.begin(), num_points);
88 num_points = 0;
91 void FinishPolygon(Canvas &canvas) {
92 if (mode != SOLID) {
93 canvas.SelectNullPen();
94 canvas.Select(*brush);
95 mode = SOLID;
98 canvas.DrawPolygon(points.begin(), num_points);
100 num_points = 0;
103 void Commit() {
104 assert(num_points == 0);
108 #endif