raylib-cpp is a C++ wrapper library for raylib, a simple and easy-to-use library to enjoy videogames programming. This C++ header provides object-oriented wrappers around raylib's struct interfaces. raylib-cpp is not required to use raylib in C++, but the classes do bring using the raylib API more inline with C++'s language pardigm.
#include "raylib-cpp.hpp"
int main() {
int screenWidth = 800;
int screenHeight = 450;
raylib::Window window(screenWidth, screenHeight, "raylib-cpp - basic window");
raylib::Texture logo("raylib_logo.png");
SetTargetFPS(60);
while (!window.ShouldClose())
{
BeginDrawing();
window.ClearBackground(RAYWHITE);
DrawText("Congrats! You created your first window!", 190, 200, 20, LIGHTGRAY);
// Object methods.
logo.Draw(
screenWidth / 2 - logo.GetWidth() / 2,
screenHeight / 2 - logo.GetHeight() / 2);
EndDrawing();
}
// UnloadTexture() and CloseWindow() are called automatically.
return 0;
}
See the examples folder for more of the raylib examples that have been ported over to raylib-cpp.
There are a few conventions that raylib-cpp takes on when adopting raylib. See the raylib-cpp documentation for details on the entire API.
Object constructors load raylib objects.
// raylib
Texture2D texture = LoadTexture("texture.png");
// raylib-cpp
raylib::Texture2D texture("texture.png");
When a raylib method has an object as one of its arguments, you can call the method on the object itself.
// raylib
Vector2 position(50, 50);
DrawPixelV(position, PURPLE);
// raylib-cpp
raylib::Vector2 position(50, 50);
position.DrawPixel(PURPLE);
If a method's name contains an object's name, it is removed from its name to shorten the method name.
// raylib
DrawTexture(texture, 50, 50, WHITE);
// raylib-cpp
texture.Draw(50, 50, WHITE);
Many methods have optional parameters with sane defaults.
// raylib
DrawTexture(texture, 50, 50, WHITE);
// raylib-cpp
texture.Draw(50, 50); // WHITE is provided as the default tint.
Objects will attempt to unload their respective raylib resources on destruction. This means no need to call Unload or Close methods. This applies to the window, textures, images, sounds, etc.
// raylib
InitWindow(640, 480, "Hello World");
CloseWindow();
// raylib-cpp
raylib::Window window(640, 480, "Hello World");
// window.Close() will be called automatically when the object is destructed.
Properties can be assigned through getter and setter methods. You still have access to the internal properties, however.
// raylib
Vector2 position;
position.x = 50;
position.y = 100;
// raylib-cpp
raylib::Vector2 position;
position.SetX(50);
position.SetY(100);
// ... or
position.x = 50;
position.y = 100;
Many similar raylib method names have different name suffixes based on what arguments they take. With raylib-cpp, these cases use method overriding to allow using the same method names.
// raylib
Color color = GRAY;
DrawPixel(50, 50, color);
Vector2 position = {50.0f, 50.0f};
DrawPixelV(position, color); // Extra V in method name.
// raylib-cpp
raylib::Color color = raylib::Color::Gray();
color.DrawPixel(50, 50);
Vector2 position(50.0f, 50.0f);
color.DrawPixel(position); // No more V in method name.
position.DrawPixel(color); // Alternatively
When there's a method that doesn't return anything, it'll instead return the object itself, allowing method chaining.
// raylib
Image cat = ImageLoad("cat.png");
ImageCrop(&cat, (Rectangle){ 100, 10, 280, 380 });
ImageFlipHorizontal(&cat);
ImageResize(&cat, 150, 200);
// raylib-cpp
raylib::Image cat("cat.png");
cat.Crop(100, 10, 280, 380)
.FlipHorizontal()
.Resize(150, 200);
There are operator overrides for objects.
// raylib
Vector2 position = {50, 50};
Vector2 speed = {10, 10};
position.x += speed.x;
position.y += speed.y;
// raylib-cpp
raylib::Vector2 position(50, 50);
raylib::Vector2 speed(10, 10);
position += speed; // Addition assignment operator override.
When there is a function that would return a pointer-array, there is a wrapper that allows returning a vector of objects instead.
// raylib
int count;
char** files = GetDirectoryFiles(".", &count);
printf("Count: %i\n", count);
for (int i = 0; i < count; i++) {
printf("File: %s\n", files[i]);
}
ClearDirectoryFiles();
// raylib-cpp
std::vector<std::string> files = raylib::GetDirectoryFiles(".");
std::cout << "Count: " << files.size() << std::endl;
for (auto& file : files) {
std::cout << "File: " << file << std::endl;
}
Many of the raylib functions have std::string
-related overrides to allow calling them directly with std::string
s to save having to use the .c_str()
method.
// raylib
const char* url = "https://raylib.com";
OpenURL(url);
// raylib-cpp
std::string url = "https://raylib.com";
raylib::OpenURL(url);
The raymath methods are included.
// raylib
Vector2 direction = {50, 50};
Vector2 newDirection = Vector2Rotate(direction, 30);
// raylib-cpp
raylib::Vector2 direction(50, 50);
raylib::Vector2 newDirection = direction.Rotate(30);
raylib-cpp is a header-only library. This means in order to use it, you must link your project to raylib, and then include include/raylib-cpp.hpp
.
- Set up a raylib project using the build and install instructions
- Ensure C++ files are compiled with C++
- Download raylib-cpp
- Include
include/raylib-cpp.hpp
#include "path/to/raylib-cpp.hpp"
The projects directory includes some starter templates...
If there's a project template you would like to see added, feel free to make an issue and we can add it in.
The following are some tools in order to build and contribute to raylib-cpp...
raylib-cpp uses CMake as a primary target for development. To build it, and run the tests or examples, use...
git clone https://github.com/RobLoach/raylib-cpp.git
cd raylib-cpp
git submodule update --init
mkdir build
cd build
cmake ..
make
make test
./examples/core_basic_window
To build the document with Doxygen, use...
doxygen projects/Doxygen/Doxyfile
This uses cpplint to adopt coding standards.
cpplint --recursive include
RAYLIB_CPP_NO_MATH
- When set, will skip adding theraymath.h
integrations
raylib-cpp is licensed under an unmodified zlib/libpng license, which is an OSI-certified, BSD-like license that allows static linking with closed source software. Check LICENSE for further details.