Last updated on

Raylib Tutorial: Getting Started with Raylib on CLion 2024 Using vcpkg


Introduction

Raylib is a simple and beginner-friendly C/C++ library for game development and multimedia programming. It’s designed to get you up and running quickly without complicated setup, making it perfect for learning game programming or prototyping projects.

In this tutorial, I’ll walk you through installing Raylib with vcpkg, setting it up in CLion 2024, and building your first project where you move a rectangle with the arrow keys. The goal is to keep everything straightforward so you can focus on coding, not fighting with configurations.


Prerequisites

Before we begin, make sure you have these installed:

  • CLion (or any IDE with CMake support)
  • CMake (usually bundled with CLion)
  • vcpkg (Microsoft’s C++ package manager)

Step 1: Install vcpkg

Clone the vcpkg repository:

git clone https://github.com/microsoft/vcpkg.git
cd vcpkg

Bootstrap vcpkg:

  • On Windows:

    .\bootstrap-vcpkg.bat
  • On macOS/Linux:

    ./bootstrap-vcpkg.sh

Integrate with your IDE:

./vcpkg integrate install

This makes vcpkg work smoothly with CMake inside CLion.


Step 2: Install Raylib with vcpkg

Run:

vcpkg install raylib

This fetches and builds Raylib plus its dependencies, ready for use in your projects.


Step 3: Create a New Project in CLion

  1. Open CLion.
  2. Create a new C++ project.
  3. Choose CMake as the type.
  4. Give it a name and path.

You’ll see a CMakeLists.txt file — we’ll configure it next.


Step 4: Configure CMakeLists.txt

Here’s the setup for Raylib:

cmake_minimum_required(VERSION 3.10)
project(RaylibExample)

set(CMAKE_CXX_STANDARD 17)

find_package(raylib CONFIG REQUIRED)

add_executable(RaylibExample main.cpp)

target_link_libraries(RaylibExample PRIVATE raylib::raylib)

This:

  • Sets C++17.
  • Finds Raylib via vcpkg.
  • Links Raylib to your executable.

Step 5: Write the Code

Create a main.cpp:

#include <raylib.h>

int main() {
    InitWindow(800, 600, "Raylib Example");

    Rectangle rect = { 350, 250, 100, 100 };
    int speed = 5;

    while (!WindowShouldClose()) {
        if (IsKeyDown(KEY_RIGHT)) rect.x += speed;
        if (IsKeyDown(KEY_LEFT)) rect.x -= speed;
        if (IsKeyDown(KEY_DOWN)) rect.y += speed;
        if (IsKeyDown(KEY_UP)) rect.y -= speed;

        BeginDrawing();
        ClearBackground(RAYWHITE);
        DrawRectangleRec(rect, BLUE);
        DrawText("Move the rectangle with arrow keys", 10, 10, 20, DARKGRAY);
        EndDrawing();
    }

    CloseWindow();
    return 0;
}

When you run it, you’ll see a rectangle that moves with arrow keys.


Step 6: Build and Run

  • Build: press Ctrl + F9 or the build button.
  • Run: press Shift + F10 or click the run button.

If everything is set up correctly, a window will open with your rectangle ready to move.


Conclusion

You’ve just built your first Raylib project in CLion using vcpkg. With vcpkg managing dependencies and CMake handling builds, the workflow stays smooth so you can focus on writing code.

Raylib is fantastic for beginners and prototyping in C++ — simple, fun, and powerful. Now that you’ve got the basics, dive deeper into Raylib’s documentation and start building your own 2D or 3D projects.


References