โœฆ JVDesignStudio ยท No Sign-Up Required

C++ & SFML Cheat Sheet

A one-page printable reference for variables, SFML window setup, drawing shapes, event handling and collision the most-used C++ snippets for 2D games. Pin it next to your monitor!

๐Ÿ“ฆ Variables & Types

int health = 100;
float speed = 4.5f;
std::string playerName = "Pip";
bool isAlive = true;
const int MAX_HP = 100;
auto score = 0; // type inferred as int

Use auto to let the compiler infer the type. const values cannot be changed after declaration.

๐ŸชŸ SFML Window Setup

#include <SFML/Graphics.hpp>

int main() {
    sf::RenderWindow window(sf::VideoMode(800, 600), "My Game");
    window.setFramerateLimit(60);

    while (window.isOpen()) {
        // 1. handle events
        // 2. update game logic
        // 3. draw
        window.clear();
        window.display();
    }
    return 0;
}

๐ŸŸฆ Drawing Shapes

sf::RectangleShape rect(sf::Vector2f(50, 50));
rect.setFillColor(sf::Color::Red);
rect.setPosition(100, 100);

sf::CircleShape circle(25.f);
circle.setFillColor(sf::Color::Green);
circle.setPosition(200, 100);

window.clear(sf::Color::Black);
window.draw(rect);
window.draw(circle);
window.display();

๐ŸŽฎ Event Handling

sf::Event event;
while (window.pollEvent(event)) {
    if (event.type == sf::Event::Closed)
        window.close();

    if (event.type == sf::Event::KeyPressed) {
        if (event.key.code == sf::Keyboard::Space)
            Jump();
    }
}

Always pollEvent every frame, even if you don't act on every event, otherwise the window appears frozen.

๐Ÿ“ Vectors & Collision

sf::Vector2f position(100.f, 200.f);
sf::Vector2f velocity(2.f, 0.f);
position += velocity;

if (playerSprite.getGlobalBounds()
        .intersects(enemySprite.getGlobalBounds())) {
    // collision happened!
    TakeDamage();
}

๐Ÿ” Loops & Conditionals

if (health <= 0) {
    isAlive = false;
} else if (health < 20) {
    std::cout << "Low HP!" << std::endl;
} else {
    std::cout << "OK" << std::endl;
}

for (int i = 0; i < 10; i++) { }
while (ammo > 0) { ammo--; }

// range-based for loop
for (auto& enemy : enemies) {
    enemy.Update();
}

๐Ÿงฉ Functions & Classes

int Add(int a, int b) {
    return a + b;
}

class Player {
public:
    Player(int startHp) : health(startHp) {}
    void TakeDamage(int amount) {
        health -= amount;
    }
private:
    int health;
};

Player* p = new Player(100); // pointer
p->TakeDamage(10);

๐Ÿ› ๏ธ Common Patterns: Minimal SFML Game Loop

#include <SFML/Graphics.hpp>

int main() {
    sf::RenderWindow window(sf::VideoMode(800, 600), "Mini Game");
    window.setFramerateLimit(60);

    sf::RectangleShape player(sf::Vector2f(40, 40));
    player.setFillColor(sf::Color::Cyan);
    player.setPosition(380, 280);

    float speed = 3.f;

    while (window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
            player.move(speed, 0);
        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
            player.move(-speed, 0);

        window.clear(sf::Color::Black);
        window.draw(player);
        window.display();
    }
    return 0;
}

Want to build a whole game?

Follow one of our free step-by-step workshops in the Workshop hub.

๐Ÿ”ง Browse Workshops โ†’