Build a Tower Defense Game using C++, Raylib & A* Pathfinding

Lesson 1 of 8

Raylib: Setup and Installation

Raylib is another popular C/C++ library for 2D and 3D graphics, and it takes a genuinely different approach than SFML. SFML models everything as objects, you create an sf::RectangleShape, configure it, then call window.draw(shape). Raylib is immediate mode, there are no shape objects to create at all, you just call a function like DrawRectangle(...) with the position and color every single frame, and it appears.

Installing Raylib

  • Ubuntu/Debian: sudo apt install libraylib-dev
  • macOS (Homebrew): brew install raylib
  • Windows: install via vcpkg install raylib, or grab prebuilt binaries from raylib.com and point your IDE at them.

Compiling with Raylib

g++ main.cpp -o tower_defense -lraylib -lGL -lm -lpthread -ldl -lrt -lX11
./tower_defense

(macOS links different system frameworks instead of -lGL/-lX11, and Windows links -lopengl32 -lgdi32 -lwinmm, the raylib install docs cover the exact flags for your platform.)

Opening a window

#include "raylib.h"

int main() {
    InitWindow(800, 600, "Tower Defense");
    SetTargetFPS(60);

    while (!WindowShouldClose()) {
        BeginDrawing();
        ClearBackground(RAYWHITE);

        DrawText("Hello, Raylib!", 300, 280, 20, DARKGRAY);

        EndDrawing();
    }

    CloseWindow();
    return 0;
}

Reading it line by line

  • InitWindow(800, 600, "Tower Defense") opens an 800x600 window, no separate object to store, hold onto, or pass around, raylib just tracks "the window" globally.
  • WindowShouldClose() is both the loop condition and the close-button/Escape-key check in one call, there's no separate event queue to drain like SFML's pollEvent.
  • BeginDrawing() / EndDrawing() bracket every frame's drawing calls, everything between them, in the order you call it, is what appears on screen.
  • ClearBackground(RAYWHITE) wipes the previous frame, exactly like SFML's window.clear(...), skip it and you'd see a smeared trail.
  • DrawText(...) needs no font object, no sf::Text, no sf::Font::loadFromFile, raylib ships a default font and draws text with a single function call.

NOTE

Immediate mode isn't "better" than SFML's object model, it's a different tradeoff. Objects (SFML) let you configure something once and reuse it; immediate mode (raylib) means every frame is self-contained, nothing to keep in sync, at the cost of passing the same arguments again every single frame. Both are widely used in real games, this course is a chance to feel the difference firsthand.

📝 Raylib Setup Quiz

Passing score: 70%
  1. 1.What does "immediate mode" mean in raylib, compared to SFML?

  2. 2.WindowShouldClose() handles both the close button and checking whether the loop should keep running.

  3. 3.Every frame's drawing calls must be wrapped between Begin____() and End____().