28#include <SFML/Graphics/Shape.hpp>
29#include <SFML/Graphics/GraphicsContext.hpp>
40myIsFillEnabled (true),
41myIsOutlineEnabled(true),
45 myPoints.push_back(Point());
54 AddPoint(Vector2f(X, Y), Col, OutlineCol);
63 myPoints.push_back(Point(Position, Col, OutlineCol));
73 return static_cast<unsigned int>(myPoints.size() - 1);
83 myIsFillEnabled = Enable;
93 myIsOutlineEnabled = Enable;
102 myPoints[Index + 1].Position = Position;
103 myIsCompiled =
false;
121 myPoints[Index + 1].Col = Col;
122 myIsCompiled =
false;
131 myPoints[Index + 1].OutlineCol = OutlineCol;
132 myIsCompiled =
false;
150 return myPoints[Index + 1].Position;
159 return myPoints[Index + 1].Col;
168 return myPoints[Index + 1].OutlineCol;
186 Vector2f P1(P1X, P1Y);
187 Vector2f P2(P2X, P2Y);
191 ComputeNormal(P1, P2, Normal);
192 Normal *= Thickness / 2;
196 S.AddPoint(P1 - Normal, Col, OutlineCol);
197 S.AddPoint(P2 - Normal, Col, OutlineCol);
198 S.AddPoint(P2 + Normal, Col, OutlineCol);
199 S.AddPoint(P1 + Normal, Col, OutlineCol);
200 S.SetOutlineWidth(Outline);
214 return Shape::Line(P1.
x, P1.
y, P2.
x, P2.
y, Thickness, Col, Outline, OutlineCol);
225 S.AddPoint(Vector2f(P1X, P1Y), Col, OutlineCol);
226 S.AddPoint(Vector2f(P2X, P1Y), Col, OutlineCol);
227 S.AddPoint(Vector2f(P2X, P2Y), Col, OutlineCol);
228 S.AddPoint(Vector2f(P1X, P2Y), Col, OutlineCol);
229 S.SetOutlineWidth(Outline);
252 static const int NbSegments = 40;
256 Vector2f Center(X, Y);
257 for (
int i = 0; i < NbSegments; ++i)
259 float Angle = i * 2 * 3.141592654f / NbSegments;
260 Vector2f Offset(cos(Angle), sin(Angle));
262 S.AddPoint(Center + Offset * Radius, Col, OutlineCol);
266 S.SetOutlineWidth(Outline);
278 return Shape::Circle(Center.
x, Center.
y, Radius, Col, Outline, OutlineCol);
288 if (myPoints.size() < 4)
293 const_cast<Shape*
>(
this)->Compile();
296 GLCheck(glDisable(GL_TEXTURE_2D));
301 glBegin(GL_TRIANGLE_FAN);
303 for (std::vector<Point>::const_iterator i = myPoints.begin(); i != myPoints.end(); ++i)
306 glColor4f(PointColor.
r / 255.f, PointColor.
g / 255.f, PointColor.
b / 255.f, PointColor.
a / 255.f);
307 glVertex2f(i->Position.x, i->Position.y);
312 glColor4f(PointColor.
r / 255.f, PointColor.
g / 255.f, PointColor.
b / 255.f, PointColor.
a / 255.f);
313 glVertex2f(myPoints[1].Position.x, myPoints[1].Position.y);
319 if (myIsOutlineEnabled)
321 glBegin(GL_TRIANGLE_STRIP);
323 for (std::size_t i = 1; i < myPoints.size(); ++i)
326 glColor4f(PointColor.
r / 255.f, PointColor.
g / 255.f, PointColor.
b / 255.f, PointColor.
a / 255.f);
327 glVertex2f(myPoints[i].Position.x, myPoints[i].Position.y);
328 glColor4f(PointColor.
r / 255.f, PointColor.
g / 255.f, PointColor.
b / 255.f, PointColor.
a / 255.f);
329 glVertex2f(myPoints[i].Position.x + myPoints[i].Normal.x * myOutline, myPoints[i].Position.y + myPoints[i].Normal.y * myOutline);
334 glColor4f(PointColor.
r / 255.f, PointColor.
g / 255.f, PointColor.
b / 255.f, PointColor.
a / 255.f);
335 glVertex2f(myPoints[1].Position.x, myPoints[1].Position.y);
336 glColor4f(PointColor.
r / 255.f, PointColor.
g / 255.f, PointColor.
b / 255.f, PointColor.
a / 255.f);
337 glVertex2f(myPoints[1].Position.x + myPoints[1].Normal.x * myOutline, myPoints[1].Position.y + myPoints[1].Normal.y * myOutline);
350 float NbPoints =
static_cast<float>(myPoints.size() - 1);
351 float R = 0, G = 0, B = 0, A = 0;
352 Point Center(Vector2f(0, 0),
Color(0, 0, 0, 0));
353 for (std::size_t i = 1; i < myPoints.size(); ++i)
355 Center.Position += myPoints[i].Position / NbPoints;
356 R += myPoints[i].Col.r / NbPoints;
357 G += myPoints[i].Col.g / NbPoints;
358 B += myPoints[i].Col.b / NbPoints;
359 A += myPoints[i].Col.a / NbPoints;
361 Center.Col.r =
static_cast<Uint8
>(R);
362 Center.Col.g =
static_cast<Uint8
>(G);
363 Center.Col.b =
static_cast<Uint8
>(B);
364 Center.Col.a =
static_cast<Uint8
>(A);
365 myPoints[0] = Center;
368 for (std::size_t i = 1; i < myPoints.size(); ++i)
371 Point& P0 = (i == 1) ? myPoints[myPoints.size() - 1] : myPoints[i - 1];
372 Point& P1 = myPoints[i];
373 Point& P2 = (i == myPoints.size() - 1) ? myPoints[1] : myPoints[i + 1];
376 Vector2f Normal1, Normal2;
377 if (!ComputeNormal(P0.Position, P1.Position, Normal1) || !ComputeNormal(P1.Position, P2.Position, Normal2))
381 float Factor = 1.f + (Normal1.x * Normal2.x + Normal1.y * Normal2.y);
382 P1.Normal = (Normal1 + Normal2) / Factor;
385 float Dot = (P1.Position.x - Center.Position.x) * P1.Normal.
x + (P1.Position.y - Center.Position.y) * P1.Normal.y;
387 P1.Normal = -P1.Normal;
397bool Shape::ComputeNormal(
const Vector2f& P1,
const Vector2f& P2, Vector2f& Normal)
399 Normal.x = P1.y - P2.y;
400 Normal.y = P2.x - P1.x;
402 float Len = sqrt(Normal.x * Normal.x + Normal.y * Normal.y);
416Shape::Point::Point(
const Vector2f& Pos,
const Color& C,
const Color& OutlineC) :
Color is an utility class for manipulating 32-bits RGBA colors.
Uint8 a
Alpha (transparency) component.
const Color & GetColor() const
Get the color of the object.
static Shape Circle(float X, float Y, float Radius, const Color &Col, float Outline=0.f, const Color &OutlineCol=sf::Color(0, 0, 0))
Create a shape made of a single circle (use floats).
unsigned int GetNbPoints() const
Get the number of points composing the shape.
Shape()
Default constructor.
const Vector2f & GetPointPosition(unsigned int Index) const
Get the position of a point.
void EnableFill(bool Enable)
Enable or disable filling the shape.
float GetOutlineWidth() const
Get the width of the shape outline.
void SetOutlineWidth(float Width)
Change the width of the shape outline.
static Shape Line(float P1X, float P1Y, float P2X, float P2Y, float Thickness, const Color &Col, float Outline=0.f, const Color &OutlineCol=sf::Color(0, 0, 0))
Create a shape made of a single line (use floats).
static Shape Rectangle(float P1X, float P1Y, float P2X, float P2Y, const Color &Col, float Outline=0.f, const Color &OutlineCol=sf::Color(0, 0, 0))
Create a shape made of a single rectangle (use floats).
void AddPoint(float X, float Y, const Color &Col=Color(255, 255, 255), const Color &OutlineCol=Color(0, 0, 0))
Add a point to the shape.
void SetPointOutlineColor(unsigned int Index, const Color &OutlineCol)
Set the outline color of a point.
void SetPointColor(unsigned int Index, const Color &Col)
Set the color of a point.
const Color & GetPointColor(unsigned int Index) const
Get the color of a point.
virtual void Render(RenderTarget &Target) const
/see Drawable::Render
void SetPointPosition(unsigned int Index, const Vector2f &Position)
Set the position of a point.
void EnableOutline(bool Enable)
Enable or disable drawing the shape outline.
const Color & GetPointOutlineColor(unsigned int Index) const
Get the outline color of a point.
T x
X coordinate of the vector.
T y
Y coordinate of the vector.