Geometric Shapes of SFML Multimedia Library for C++ Game Development

Before proceeding to the topic of geometric shapes, let’s analyze the framework of a C ++ program that uses the SFML library.
To work with the code, we will use Visual Studio 2022 and the SFML library. You can learn how to connect the SFML library to Visual Studio 2022 by watching the video instruction “Connecting the SFML C++ Library”
SFML library program framework


The variable window is set to type graphics window ( RenderWindow ).
The VideoMode parameter ( 1500, 900 ) determines the size of the graphics window 1500 horizontally and 900 vertically.

L”New project” – name of the graphics window, Style::Default – default window style.
The while (window.isOpen()) loop will run while the graphics window is open.
In the event variable of type Event, an event is stored, for example, clicking on the cross of a window with the mouse or pressing a key on the keyboard.
The while (window.pollEvent(event)) loop runs when an event occurs and writes it using the pollEvent( ) method to the event variable. Further, in the body of the loop, events are processed; only one event can be processed at a time.


The clear(Color::Blue) method clears the graphics window and colors it blue.

The display() method displays the drawn objects using the draw() method in the graphics window.
Rectangle

RectangleShape type that creates a rectangle object with methods and properties for changing its parameters, as well as animating it.

The draw() method draws objects in the graphics window, in the absence of the draw() and display() methods, the graphics window will be empty, the methods are placed inside the while loop (window.isOpen).

The setFillColor() method sets the fill color of the rectangle.


The setPosition() method sets the rectangle coordinates for the shape’s coordinate point, which by default is in the upper left corner of the shape.


The setOutlineThickness() method sets the line thickness of the shape’s outline. The setOutlineColor() method sets the color of the shape’s outline line.


The setRotation() method sets the rotation angle of the shape with the rotation center at the coordinate point.



The setOrigin() method changes the coordinates of a coordinate point and allows you to move them from the top left point to the center of the shape.


The textur variable is set to the Texture type, which makes it possible to load an image into the variable using the LoadFromFile() method.
The image file must be located in the visual studio c++ project directory.

The setTexture(&) method sets the texture for the rectangle.


The rotate() method changes the rotation angle of the shape. The move() method changes the position of the shape horizontally and vertically.

In some cases, when the application is running quickly, you may observe visual artifacts. The reason is that your application’s refresh rate is out of sync with the monitor’s vertical rate, causing the bottom of the previous frame to overlap with the top of the next frame.
The solution is to enable vertical sync using the setVerticalSyncEnabled() method.

A circle


CircleShape is a type that creates a circle object with the same methods and properties as a rectangle.


The setScale() method allows you to scale the circle vertically and horizontally.


The setPointCount() method sets the number of vertices for the circle.
Polygon


ConvexShape type, which creates a convex polygon object with methods and properties the same as for the previous shapes, in the convex variable parameters we specify the number of polygon vertices.
The setPoint() method sets the coordinates for each vertex of the polygon.
Before you start specifying the coordinates of the vertices of the polygon, draw the future figure on checkered paper, so it will be easier to determine the coordinates of the vertices for the polygon. The order in which the points of the polygon are defined is very important. All points must be defined clockwise or counterclockwise. If you determine the coordinates in a chaotic manner, then the shape of the polygon will be incorrect.

You can see a more detailed instruction on SFML geometric shapes by watching the video “SFML C++ Geometric Shapes”.