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 event handler for clicking on the cross of the graphical window, the Close() method closes the window.
The event handler for clicking on the cross of the graphical window, the Close() method closes the window.
Clearing the Graphics Window
Clearing the Graphics Window

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

Displaying a picture on the screen
Displaying a picture on the screen

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

Rectangle

Variable recr of type rectangle with dimensions 200 x 100
Variable recr of type rectangle with dimensions 200 x 100

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

Clearing the graphics window, drawing two objects recr, rect1 and outputting objects to the graphics window
Clearing the graphics window, drawing two objects recr, rect1 and outputting objects to the graphics window

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).

Sets the fill color in RGB format
Sets the fill color in RGB format

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

Fill options: regular, RGB, RGB with alpha channel
Fill options: regular, RGB, RGB with alpha channel
Setting rectangle coordinates
Setting rectangle coordinates

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.

setPosition(300,100)
setPosition(300,100)
Line thickness and color of the rectangle outline
Line thickness and color of the rectangle outline

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

Figure rotation angle 90 degrees
Figure rotation angle 90 degrees

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

Rotation of a figure with the center of rotation at the top left point
Rotation of a figure with the center of rotation at the top left point
Move coordinate point
Move 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.

Offset the center of rotation to the center of the rectangle
Offset the center of rotation to the center of the rectangle
Setting the texture for the rectangle
Setting the texture for the rectangle

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.

Project directory
Project directory

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

Rectangle with brick texture
Rectangle with brick texture
Changes the rotation angle of the shape by one degree
Changes the rotation angle of the shape by one degree

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

Rectangle moving and rotating animation
Rectangle moving and rotating animation

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.

Enable vertical sync
Enable vertical sync

A circle

Draw a circle with a radius of 100 and color it with green.
Draw a circle with a radius of 100 and color it with green.

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

Zooming in on the circle vertically
Zooming in on the circle vertically

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

Sets the vertices of a circle
Sets the vertices of a circle

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”.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *