C++ Drawing Shapes (SFML)
In this page:
Drawing Circles
sf::CircleShape draws a filled circle by specifying just its radius; SFML computes the outline as a many-sided polygon internally. You control its appearance with setFillColor() and can reposition it anywhere in the window with setPosition().
Example: Drawing Circles
#include <SFML/Graphics.hpp>
int main() {
sf::CircleShape circle(50.f);
circle.setFillColor(sf::Color::Green);
circle.setPosition(100, 100);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Drawing Rectangles
sf::RectangleShape draws a rectangle whose width and height you define with an sf::Vector2f passed to setSize() -- this two-component vector type is used throughout SFML anywhere a 2D size or position is needed.
Example: Drawing Rectangles
#include <SFML/Graphics.hpp>
int main() {
sf::RectangleShape rect;
rect.setSize(sf::Vector2f(120.f, 50.f));
rect.setFillColor(sf::Color::Blue);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Convex and Custom Shapes
sf::ConvexShape lets you draw arbitrary polygons beyond simple circles and rectangles. You must first call setPointCount() to declare how many vertices the shape has, then set each vertex's position individually with setPoint(index, position) -- SFML connects them in order to form the outline.
Example: Convex and Custom Shapes
#include <SFML/Graphics.hpp>
int main() {
sf::ConvexShape triangle;
triangle.setPointCount(3);
triangle.setPoint(0, sf::Vector2f(0, 0));
triangle.setPoint(1, sf::Vector2f(100, 0));
triangle.setPoint(2, sf::Vector2f(50, 100));
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Transforming Shapes
Every SFML shape supports setPosition(), setRotation(), and setScale(), which apply a transform independently of the shape's own geometry -- so you can move, spin, or resize a shape frame-to-frame (for animation) without redefining its underlying points each time.
Example: Transforming Shapes
#include <SFML/Graphics.hpp>
int main() {
sf::CircleShape shape(30.f);
shape.setPosition(50, 50);
shape.setRotation(45.f);
shape.setScale(1.5f, 1.5f);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Coloring and Outlines
Beyond a solid fill color, setOutlineColor() and setOutlineThickness() add a visible border around a shape's edge, which is useful for making overlapping shapes visually distinct or highlighting a shape the user has selected.
Example: Coloring and Outlines
#include <SFML/Graphics.hpp>
int main() {
sf::CircleShape shape(40.f);
shape.setFillColor(sf::Color::Yellow);
shape.setOutlineColor(sf::Color::Black);
shape.setOutlineThickness(3.f);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: