Language: CPP
CLI/Utils
SFML was created by Laurent Gomila as an alternative to SDL, offering a cleaner C++ object-oriented API for multimedia development. Its modular design allows developers to use only the components they need, such as graphics, audio, or networking, without pulling in unnecessary dependencies.
SFML (Simple and Fast Multimedia Library) is a multimedia library written in C++ that provides simple APIs for graphics, window management, audio, and networking. It is designed to be user-friendly and portable, ideal for 2D games and multimedia applications.
sudo apt install libsfml-devbrew install sfmlDownload from https://www.sfml-dev.org/download.php and link libraries in your projectSFML provides modules for handling windows, graphics (2D rendering), audio (sound and music), input, and networking. It abstracts platform-specific details, making development portable across systems.
#include <SFML/Graphics.hpp>
int main() {
sf::RenderWindow window(sf::VideoMode(800, 600), "Hello SFML");
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.display();
}
return 0;
}Creates an 800x600 window with an event loop that closes the window when requested.
#include <SFML/Graphics.hpp>
int main() {
sf::RenderWindow window(sf::VideoMode(400, 400), "Shapes");
sf::CircleShape circle(100);
circle.setFillColor(sf::Color::Green);
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(circle);
window.display();
}
return 0;
}Draws a green circle in the window using SFML's 2D graphics module.
#include <SFML/Graphics.hpp>
#include <iostream>
int main() {
sf::RenderWindow window(sf::VideoMode(400, 400), "Keyboard Input");
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
window.close();
}
}
return 0;
}Closes the window when the Escape key is pressed.
#include <SFML/Audio.hpp>
#include <iostream>
int main() {
sf::SoundBuffer buffer;
if (!buffer.loadFromFile("sound.wav")) {
std::cerr << "Error loading sound" << std::endl;
return -1;
}
sf::Sound sound;
sound.setBuffer(buffer);
sound.play();
sf::sleep(sf::seconds(3));
return 0;
}Loads a WAV file and plays it using SFML's audio module.
#include <SFML/Network.hpp>
#include <iostream>
int main() {
sf::TcpSocket socket;
if (socket.connect("example.com", 53000) != sf::Socket::Done) {
std::cerr << "Failed to connect" << std::endl;
} else {
std::cout << "Connected!" << std::endl;
}
return 0;
}Demonstrates SFML’s networking support by connecting to a TCP server.
#include <SFML/Graphics.hpp>
int main() {
sf::RenderWindow window(sf::VideoMode(800, 600), "Sprite Example");
sf::Texture texture;
texture.loadFromFile("sprite.png");
sf::Sprite sprite(texture);
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(sprite);
window.display();
}
return 0;
}Loads an image from a file and displays it as a sprite in the window.
Always check return values of resource loading functions (textures, sounds, etc.).
Keep your game loop structured with clear update and render phases.
Use `sf::Clock` for frame timing and animations.
Free unused resources promptly to save memory.
Organize large projects by separating logic, rendering, and input handling.