Using C++ Bindings to Raylib

Raylib is a great game engine, but by default it only exposes a C API, and generally, C is not that productive to work in as it’s really hard to build meaningful abstractions, it lacks a featureful standard library, and many quality of life features such as cross-platform threading, a solid collections library, smart pointers, and RAII (I could go on). Being unable to write in any paradigm but imperative is annoying as well. Luckily, there are many bindings for other languages that abstract over the C API for you. The C++ bindings seem feature complete, and, because of backwards compatibility, you can just tap into the C API or write your own abstractions if something doesn’t work the way you need it to. Here’s how you can get setup with them.


Using C++ For Fun and For Profit™


In order to work with the C++ bindings, you first need to install raylib as a dependency. There are different ways to do this, but I prefer to use CMake with FetchContent. The below code is adapted from the official linked example in the documentation.

CMakeLists.txt
1
cmake_minimum_required(VERSION 3.11) # FetchContent is available in 3.11+
2
project(example)
3
4
set(CMAKE_CXX_STANDARD 17)
5
6
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
7
8
# Dependencies
9
set(RAYLIB_VERSION 5.0)
10
find_package(raylib ${RAYLIB_VERSION} QUIET) # QUIET or REQUIRED
11
if (NOT raylib_FOUND) # If there's none, fetch and build raylib
12
include(FetchContent)
13
FetchContent_Declare(
14
raylib
15
DOWNLOAD_EXTRACT_TIMESTAMP OFF
16
URL https://github.com/raysan5/raylib/archive/refs/tags/${RAYLIB_VERSION}.tar.gz
17
)
18
FetchContent_GetProperties(raylib)
19
if (NOT raylib_POPULATED) # Have we downloaded raylib yet?
20
set(FETCHCONTENT_QUIET NO)
21
FetchContent_MakeAvailable(raylib)
22
set(BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) # don't build the supplied examples
23
endif()
24
endif()
25
26
# Our Project
27
28
add_executable(${PROJECT_NAME} main.cc)
29
target_link_libraries(${PROJECT_NAME} raylib)
30
31
# Web Configurations
32
if (${PLATFORM} STREQUAL "Web")
33
# Tell Emscripten to build an example.html file.
34
set_target_properties(${PROJECT_NAME} PROPERTIES SUFFIX ".html")
35
endif()
36
37
# Checks if OSX and links appropriate frameworks (Only required on MacOS)
38
if (APPLE)
39
target_link_libraries(${PROJECT_NAME} "-framework CoreVideo")
40
target_link_libraries(${PROJECT_NAME} "-framework IOKit")
41
target_link_libraries(${PROJECT_NAME} "-framework Cocoa")
42
target_link_libraries(${PROJECT_NAME} "-framework OpenGL")
43
target_link_libraries(${PROJECT_NAME} "-framework GLUT")
44
endif()

To add raylib-cpp as a dependency and to be able to #include <raylib-cpp.hpp>, you’ll need to add the following to your CMakeLists.txt

CMakeLists.txt
1
cmake_minimum_required(VERSION 3.11) # FetchContent is available in 3.11+
2
project(example)
3
4
set(CMAKE_CXX_STANDARD 17)
5
6
# Generate compile_commands.json
7
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
8
9
# Dependencies
10
set(RAYLIB_VERSION 5.0)
11
find_package(raylib ${RAYLIB_VERSION} QUIET) # QUIET or REQUIRED
12
if (NOT raylib_FOUND) # If there's none, fetch and build raylib
13
include(FetchContent)
14
FetchContent_Declare(
15
raylib
16
DOWNLOAD_EXTRACT_TIMESTAMP OFF
17
URL https://github.com/raysan5/raylib/archive/refs/tags/${RAYLIB_VERSION}.tar.gz
18
)
19
FetchContent_GetProperties(raylib)
20
if (NOT raylib_POPULATED) # Have we downloaded raylib yet?
21
set(FETCHCONTENT_QUIET NO)
22
FetchContent_MakeAvailable(raylib)
23
set(BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) # don't build the supplied examples
24
endif()
25
endif()
26
27
FetchContent_Declare(
28
raylib-cpp
29
GIT_REPOSITORY https://github.com/RobLoach/raylib-cpp.git
30
GIT_TAG master # or specify a specific tag/commit if needed
31
)
32
FetchContent_MakeAvailable(raylib-cpp)
33
34
# Our Project
35
36
add_executable(${PROJECT_NAME} main.cc)
37
#set(raylib_VERBOSE 1)
38
target_link_libraries(${PROJECT_NAME} raylib)
39
target_include_directories(${PROJECT_NAME} PRIVATE ${raylib-cpp_SOURCE_DIR}/include)
40
41
# Web Configurations
42
if (${PLATFORM} STREQUAL "Web")
43
# Tell Emscripten to build an example.html file.
44
set_target_properties(${PROJECT_NAME} PROPERTIES SUFFIX ".html")
45
endif()
46
47
# Checks if OSX and links appropriate frameworks (Only required on MacOS)
48
if (APPLE)
49
target_link_libraries(${PROJECT_NAME} "-framework CoreVideo")
50
target_link_libraries(${PROJECT_NAME} "-framework IOKit")
51
target_link_libraries(${PROJECT_NAME} "-framework Cocoa")
52
target_link_libraries(${PROJECT_NAME} "-framework OpenGL")
53
target_link_libraries(${PROJECT_NAME} "-framework GLUT")
54
endif()
55
56
add_custom_target(app
57
COMMAND example
58
DEPENDS example
59
COMMENT "Running app"
60
)

Lines 27-31 fetch raylib-cpp the master branch of the GitHub repository (although you should probably specify a tag), and line 32 makes the library available. Line 39 allows you to #include <raylib-cpp.hpp> so you can use the C++ API. The last 5 lines just add a custom target called app so you can run cmake --build build --target app and have it automatically build and run your app. That’s just for convenience. Now you can put the following in your main.cc/main.cpp:

main.cc
1
#include <raylib-cpp.hpp>
2
3
int main() {
4
int screenWidth = 800;
5
int screenHeight = 450;
6
7
raylib::Window window(screenWidth, screenHeight, "raylib-cpp - basic window");
8
9
SetTargetFPS(60);
10
11
while (!window.ShouldClose()) {
12
window.BeginDrawing();
13
14
window.ClearBackground(raylib::Color::RayWhite());
15
16
raylib::DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY);
17
18
window.EndDrawing();
19
}
20
21
return 0;
22
}

When you run cmake -B build && cmake --build build --target app, you should see a window open up. I hope this post was helpful!


raylib
c++
gamedev