sdl2_impl 0.1.0
Vendor SDL2 2.32 : IGraphic2Module / IAudioModule
Loading...
Searching...
No Matches
SdlPolygon.hpp
Go to the documentation of this file.
1
11#ifndef SDLPOLYGON_HPP_
12#define SDLPOLYGON_HPP_
13
14//Sdl
15#include <SDL.h>
16
17//Interface
18#include "IPolygon.hpp"
19
20//system : Triangle, Vector2f, epsilond
21#include "Shape.hpp"
22
23#include <algorithm>
24#include <cmath>
25#include <vector>
26
35class SdlPolygon : public graphic::IPolygon {
36
37 public:
38 SdlPolygon(std::vector<Vector2f> points) : _points(points) {
39 triangulate();
40 buildVertices(); //une seule fois : le dessin n'a plus qu'a decaler
41 }
42
43 ~SdlPolygon() = default;
44
45 bool isReady() const override { return !_vertices.empty(); }
46
47 Vector2f getPosition() const override { return _position; }
48 void setPosition(Vector2f position) override { _position = position; }
49
50 Color getColor() const override { return _color; }
51
52 void setColor(Color color) override {
53 _color = color;
54 for (SDL_Vertex &vertex : _vertices)
55 vertex.color = SDL_Color{color.r, color.g, color.b, color.a};
56 }
57
58 std::vector<Vector2f> getPoints() const override { return _points; }
59
60 friend class SdlWindow;
61
62 private:
69 static bool is_convex(Vector2f A, Vector2f B, Vector2f C) {
70 const Vector2f BA = {A.x - B.x, A.y - B.y};
71 const Vector2f BC = {C.x - B.x, C.y - B.y};
72 const double cross = BA.cross(BC);
73
74 return (std::abs(cross) < epsilond) ? false : (cross < 0);
75 }
76
77 static float polygon_area(const std::vector<Vector2f> &pts) {
78 float area = 0;
79
80 for (size_t i = 0; i < pts.size(); ++i) {
81 const Vector2f &a = pts[i];
82 const Vector2f &b = pts[(i + 1) % pts.size()];
83
84 area += (a.x * b.y - b.x * a.y);
85 }
86 return area * 0.5f;
87 }
88
89 void triangulate() {
90 std::vector<Vector2f> tmp = _points;
91
92 if (polygon_area(tmp) < 0)
93 std::reverse(tmp.begin(), tmp.end());
94
95 while (tmp.size() >= 3) {
96 bool earFound = false;
97
98 for (size_t i = 0; i < tmp.size(); i++) {
99 Vector2f A = tmp[i];
100 Vector2f B = tmp[(i + 1) % tmp.size()];
101 Vector2f C = tmp[(i + 2) % tmp.size()];
102 Triangle<double> t = Triangle<double>{A, B, C};
103
104 if (is_convex(A, B, C)) {
105 bool isEar = true;
106
107 for (size_t j = 0; j < tmp.size(); j++) {
108 if (j == i || j == (i + 1) % tmp.size() || j == (i + 2) % tmp.size()) continue;
109 if (t.isInside(tmp[j])) {
110 isEar = false;
111 break;
112 }
113 }
114
115 if (isEar) {
116 _triangles.push_back(Triangle<double>{A, B, C});
117 tmp.erase(tmp.begin() + (i + 1) % tmp.size());
118 earFound = true;
119 break;
120 }
121 }
122 }
123
124 if (!earFound)
125 break;
126 }
127 }
128
129 /* Sommets en coordonnees LOCALES, une seule fois. Le dessin recopie
130 * dans _screen en ajoutant la position : SDL_RenderGeometry n'a pas
131 * de transformation, il faut lui donner des coordonnees ecran. */
132 void buildVertices() {
133 const SDL_Color color{_color.r, _color.g, _color.b, _color.a};
134
135 _vertices.reserve(_triangles.size() * 3);
136 for (const Triangle<double> &t : _triangles)
137 for (const Vector2f &point : {t.p1, t.p2, t.p3})
138 _vertices.push_back(SDL_Vertex{
139 SDL_FPoint{static_cast<float>(point.x), static_cast<float>(point.y)},
140 color,
141 SDL_FPoint{0.f, 0.f}});
142 _screen.resize(_vertices.size());
143 }
144
145 Color _color{255, 0, 0, 255};
146 Vector2f _position{0, 0};
147
148 std::vector<Vector2f> _points;
149 std::vector<Triangle<double>> _triangles;
150
151 std::vector<SDL_Vertex> _vertices;
152 std::vector<SDL_Vertex> _screen;
153};
154
157#endif /* !SDLPOLYGON_HPP_ */
Un contour quelconque, decoupe en triangles une fois pour toutes.
Definition SdlPolygon.hpp:35
~SdlPolygon()=default
bool isReady() const override
Definition SdlPolygon.hpp:45
Color getColor() const override
Definition SdlPolygon.hpp:50
SdlPolygon(std::vector< Vector2f > points)
Definition SdlPolygon.hpp:38
void setColor(Color color) override
Definition SdlPolygon.hpp:52
void setPosition(Vector2f position) override
Definition SdlPolygon.hpp:48
std::vector< Vector2f > getPoints() const override
Definition SdlPolygon.hpp:58
Vector2f getPosition() const override
Definition SdlPolygon.hpp:47
Une fenetre SDL et son renderer, implemente IWindow2.
Definition SdlWindow.hpp:50