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.
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
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:
When you run cmake -B build && cmake --build build --target app, you should see a window open up. I hope this post was helpful!