0% found this document useful (0 votes)
2 views11 pages

SFML Notes

The document discusses the advantages of using C++ for game development, highlighting its direct compilation to machine code and predictable performance. It introduces the Simple and Fast Multimedia Library (SFML), detailing its graphics, window, and audio modules, along with code examples for creating windows, shapes, and handling events. Additionally, it covers key classes and functions in SFML, such as FloatRect, RectangleShape, and event handling mechanisms.

Uploaded by

satyamkrop123ok
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views11 pages

SFML Notes

The document discusses the advantages of using C++ for game development, highlighting its direct compilation to machine code and predictable performance. It introduces the Simple and Fast Multimedia Library (SFML), detailing its graphics, window, and audio modules, along with code examples for creating windows, shapes, and handling events. Additionally, it covers key classes and functions in SFML, such as FloatRect, RectangleShape, and event handling mechanisms.

Uploaded by

satyamkrop123ok
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

SFML

Why use the C++ language for gaming.


Direct Compilation to Machine Code
1.C++ is a compiled language. A compiler like GNU Compiler Collection (GCC) or Microsoft
Visual C++ translates C++ source code directly into machine code (binary instructions).
[Link] CPU executes these instructions directly. There is no intermediate layer like interpretation in
many other languages.
3. Languages such as Java use a virtual machine (JVM), and Python is interpreted. This adds extra
translation steps and runtime overhead.
4. C++ programs run directly on the operating system without needing a virtual machine.
5. C++ allows stack allocation, heap allocation using new and delete and no compulsory garbage
collector.
6. Since there is no automatic garbage collection pause (like in Java or some other languages),
performance remains predictable and fast.
[Same is true for C ]

SFML (Simple and Fast Multimedia Library)

#include<SFML/[Link]>
using namespace sf;

namespace sf

[Link]
RenderWindow, RenderTarget, Drawable , Sprite , Texture, Image, Shape, CircleShape,
RectangleShape, Font, Text, Color, View, Vertex, Vertex Array.
2. Window Module <SFML/[Link]>
3. Audio module <SFML/[Link]>

#include<SFML/[Link]>
using namespace sf;
int main(){
Videomode vm(1920, 1080);
RenderWindow window(vm, “Timber”, Style::Fullscreen);
//main game loop
while(window. isOpen){
if(Keyboard::isKeyPressed(Keyboard::Escape))
[Link](); }
return 0; }
[Link]() removes everything that was drawn in the previous frame and fills the window with a
background color (default is black).

Other options:
[Link](Color::White);
Other colors:
Color::Black
Color::Red
Color::Blue
Color::Green

1. Frames per Second (FPS)


FPS means how many times the game loop runs in one second.
FPS = 5000 frames per second
So the time taken for one frame is:
time per frame = 1/FPS = 1/5000 = 0.0002sec

Srand((int)time(0))
rand()

time(0) returns a value of type time_t.


It is converted to int using type casting.
time(0) is the calendar time not the cpu time. It changes every second and we will get a different
random number.

Text class

Methods:
setFont()
Assigns a font to the text.
[Link](font);
setString()
text. setString(“Hello”)
setCharacterSize()
[Link](75);
setFillColor()
[Link](sf::Color::Green);
Color::Red
Color::Green
Color::Blue
Color::Yellow
Color::White
Color::Black

setStyle()
[Link](sf::Text::Bold);
sf::Text::Regular
sf::Text::Bold
sf::Text::Italic
sf::Text::Underlined
sf::Text::StrikeThrough
[Link](sf::Text::Bold | sf::Text::Underlined);

setPosition()
[Link](500,300);

setOrigin()
Changes the origin point of the text.
[Link](100,50);

getLocalBounds()
Returns the size of the text rectangle.
sf::FloatRect rect = [Link]();
Values returned: left , top, width, height
[Link]([Link]/2, [Link]/2);
SFML
classes

FloatRect

In SFML (Simple and Fast Multimedia Library), the FloatRect class is used to represent a rectangle
with floating-point precision.
->used to describe the position and size of objects in 2D space, such as for collision detection,
rendering bounds, or UI elements.

Constructors
 FloatRect(float left, float top, float width, float height): Constructs a FloatRect with the given
position (left, top) and size (width, height).

Data Members
 float left: The X-coordinate of the top-left corner of the rectangle.
 float top: The Y-coordinate of the top-left corner of the rectangle.
 float width: The width of the rectangle.
 float height: The height of the rectangle.

Member Functions
 getPosition(): Returns a sf::Vector2f representing the position of the top-left corner of the
rectangle.
 getSize(): Returns a sf::Vector2f representing the size of the rectangle (width, height).
 intersects(const sf::FloatRect& other): Checks if the current FloatRect intersects with another
FloatRect. Returns true if they intersect, otherwise false.
 contains(float x, float y): Checks if a point (x, y) is inside the rectangle.
 contains(const sf::Vector2f& point): Checks if a sf::Vector2f point is inside the rectangle.
 setPosition(const sf::Vector2f& position): Moves the rectangle to a new position using a
sf::Vector2f vector.
 setSize(float width, float height): Sets the size (width and height) of the rectangle.
 setSize(const sf::Vector2f& size): Sets the size of the rectangle using a sf::Vector2f vector.
RectangleShape
Key Functions of sf::RectangleShape:
1. Constructor:
 sf::RectangleShape(sf::Vector2f size) — Initializes a rectangle with the given width and
height.
2. Set Position:
 [Link](float x, float y) — Sets the position of the rectangle on the window.
3. Set Fill Color:
 [Link](const sf::Color& color) — Sets the fill color (inside the rectangle).
4. Set Outline:
 [Link](const sf::Color& color) — Sets the outline color (border
around the rectangle).
 [Link](float thickness) — Sets the outline thickness.
5. Getters (optional):
 sf::Vector2f getPosition() — Gets the current position of the rectangle.
 sf::Vector2f getSize() — Gets the current size of the rectangle.

#include <SFML/[Link]>

int main() {
// Create a window
sf::RenderWindow window(sf::VideoMode(800, 600), "RectangleShape Example");

// Create a rectangle shape with specific size


sf::RectangleShape rectangle(sf::Vector2f(200.f, 100.f)); // Width 200, Height 100

// Set the position of the rectangle


[Link](300.f, 250.f);

// Set the fill color of the rectangle (optional)


[Link](sf::Color::Green);

// Set the outline color and thickness (optional)


[Link](sf::Color::Black);
[Link](5.f);
// Start the game loop
while ([Link]()) {
// Process events
sf::Event event;
while ([Link](event)) {
if ([Link] == sf::Event::Closed)
[Link]();
}

Circle Shape

#include <SFML/[Link]>
int main() {
// Create a window
sf::RenderWindow window(sf::VideoMode(800, 600), "RectangleShape Example");

// Create a rectangle shape with specific size


sf::CircleShape circle(100.f); // Width 200, Height 100

// Set the position of the circle


[Link](300.f, 250.f);

// Set the fill color of the rectangle (optional)


[Link](sf::Color::Green);

// Set the outline color and thickness (optional)


[Link](sf::Color::Black);
[Link](5.f);

// Start the game loop


while ([Link]()) {
// Process events
sf::Event event;
while ([Link](event)) {
if ([Link] == sf::Event::Closed)
[Link](); }

[Link](sf::Color::Cyan);
// Draw the circle
[Link](circle);

// Display everything in the window


[Link](); }
return 0; }
Event class

In SFML, the sf::Event class is used to handle various types of events that occur during the execution
of a program, such as user input (keyboard, mouse), window events (resize, close), or system events
(focus lost). It is a central part of SFML's event-handling system.

Basic Concept
 Event Loop: SFML provides an event loop where you can check for events and respond
accordingly.
 Events are polled from the window using the pollEvent() function.

Event Types in SFML


The sf::Event class contains different event types. Each type corresponds to a specific action that may
occur. These types are contained within the sf::Event structure, and you can check which event
occurred by inspecting the type.
Here’s an overview of some commonly used sf::Event types:
1. Window Events:
 sf::Event::Closed: Triggered when the window is closed (e.g., the user clicks the close button).
 sf::Event::Resized: Triggered when the window is resized. You get the new size of the window.
 sf::Event::LostFocus: Triggered when the window loses focus (e.g., when the user switches to
another window).
 sf::Event::GainedFocus: Triggered when the window gains focus again after losing it.
2. Keyboard Events:
 sf::Event::KeyPressed: Triggered when a key is pressed.
 sf::Event::KeyReleased: Triggered when a key is released.
These events are accompanied by the actual key that was pressed or released, which you can access via
the sf::Event::[Link] property (e.g., sf::Keyboard::A).
3. Mouse Events:
 sf::Event::MouseButtonPressed: Triggered when a mouse button is pressed.
 sf::Event::MouseButtonReleased: Triggered when a mouse button is released.
 sf::Event::MouseMoved: Triggered when the mouse moves within the window.
 sf::Event::MouseEntered: Triggered when the mouse enters the window.
 sf::Event::MouseLeft: Triggered when the mouse leaves the window.
These mouse events provide information like which button was pressed, and the mouse position.
4. Other Events:
 sf::Event::TextEntered: Triggered when a character is entered (e.g., from the keyboard).
 sf::Event::JoystickButtonPressed, sf::Event::JoystickButtonReleased, sf::Event::JoystickMoved:
Triggered by joystick input.

Event class

In SFML, the sf::Event class is used to handle various types of events that occur during the execution
of a program, such as user input (keyboard, mouse), window events (resize, close), or system events
(focus lost). It is a central part of SFML's event-handling system.

Basic Concept
 Event Loop: SFML provides an event loop where you can check for events and respond
accordingly.
 Events are polled from the window using the pollEvent() function.

Event Types in SFML


The sf::Event class contains different event types. Each type corresponds to a specific action that may
occur. These types are contained within the sf::Event structure, and you can check which event
occurred by inspecting the type.
Here’s an overview of some commonly used sf::Event types:
1. Window Events:
 sf::Event::Closed: Triggered when the window is closed (e.g., the user clicks the close button).
 sf::Event::Resized: Triggered when the window is resized. You get the new size of the window.
 sf::Event::LostFocus: Triggered when the window loses focus (e.g., when the user switches to
another window).
 sf::Event::GainedFocus: Triggered when the window gains focus again after losing it.
2. Keyboard Events:
 sf::Event::KeyPressed: Triggered when a key is pressed.
 sf::Event::KeyReleased: Triggered when a key is released.
These events are accompanied by the actual key that was pressed or released, which you can access via
the sf::Event::[Link] property (e.g., sf::Keyboard::A).
3. Mouse Events:
 sf::Event::MouseButtonPressed: Triggered when a mouse button is pressed.
 sf::Event::MouseButtonReleased: Triggered when a mouse button is released.
 sf::Event::MouseMoved: Triggered when the mouse moves within the window.
 sf::Event::MouseEntered: Triggered when the mouse enters the window.
 sf::Event::MouseLeft: Triggered when the mouse leaves the window.
These mouse events provide information like which button was pressed, and the mouse position.
4. Other Events:
 sf::Event::TextEntered: Triggered when a character is entered (e.g., from the keyboard).
 sf::Event::JoystickButtonPressed, sf::Event::JoystickButtonReleased, sf::Event::JoystickMoved:
Triggered by joystick input.

5. Making the mouse cursor visible:


window. setMouseCursorVisible(true)
Example code:
#include <SFML/[Link]>
#include<iostream>
using namespace sf;
using
int main() {
// Create a window
RenderWindow window(VideoMode(800, 600), "Event Handling Example");
while ([Link]()) { // Main loop
Event event;
while ([Link](event)) {
switch (event . type) { // Handle different types of events
case Event::Closed:
[Link]();
break;
case Event::KeyPressed:
if ([Link] == Keyboard::Escape) {
[Link](); // Close the window if Escape key is pressed
}
break;
case Event::MouseButtonPressed:
if (event . MouseButton . button == Mouse::Left) {
// Print mouse position when the left button is pressed
cout << "Mouse clicked at ("
<< event . MouseButton . x << ", "
<< [Link] .y << ")" << endl; }
break;
case Event::MouseMoved:
// Print new mouse position when moved
cout << "Mouse moved to ("< [Link] .x << ", "
<< [Link] .y << ")" << endl;
break;

case Event::TextEntered:
// Print the text entered (character)
cout << "Character entered: " << static_cast<char>([Link]) <<endl;
break;
default:
break; } }

[Link]();
[Link](); }
return 0; }

Global and Local bounds

#include<SFML/[Link]>
#include<iostream>
using namespace sf;
using namespace std;
int main(){
RectangleShape m_Shape;
m_Shape.setSize(Vector2f(50, 20));
m_Shape.setPosition(150, 300);
//m_Shape.setOrigin(20, 30);
FloatRect bound = m_Shape.getGlobalBounds();
cout<<"Left= "<<[Link]<<endl;
cout<<"Top= "<<[Link]<<endl;
cout<<"width= "<<[Link]<<endl;
cout<<"Height= "<<[Link]<<endl;

FloatRect bound1 = m_Shape.getLocalBounds();


cout<<"Left= "<<[Link]<<endl;
cout<<"Top= "<<[Link]<<endl;
cout<<"width= "<<[Link]<<endl;
cout<<"Height= "<<[Link]<<endl;

return 0;
}

Output

Left= 150 Top= 300


width= 50 Height= 20
Left= 0 Top= 0
width= 50 Height= 20 //local bounds

With origin set:


Left= 130 Top= 270
width= 50 Height= 20
Left= 0 Top= 0
width= 50 Height= 20 //local bounds

VideoMode::getDesktopMode()
sf::VideoMode provides a static function to get the mode currently used by the desktop:
getDesktopMode(). This allows to build windows with the same size or pixel depth as the current
resolution.
The function returns the current screen resolution of the user's monitor.
It gives you a VideoMode object containing:
 width
 height
 bitsPerPixel

atan2()
In computing and mathematics, the function atan2 is the 2-argument arctangent.

You might also like