• Stars
    star
    358
  • Rank 115,988 (Top 3 %)
  • Language
    C++
  • License
    zlib License
  • Created over 5 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

C++ Object Oriented Wrapper for raylib

raylib-cpp Logo

raylib-cpp Targeting raylib 4.5 Tests License

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 paradigm.

Example

#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.

Features

There are a few conventions that raylib-cpp takes on when adopting raylib. See the raylib-cpp documentation for details on the entire API.

Constructors

Object constructors load raylib objects.

// raylib
Texture2D texture = LoadTexture("texture.png");

// raylib-cpp
raylib::Texture2D texture("texture.png");

Object Methods

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);

Method Names

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);

Optional Parameters

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.

Object Destructors

Objects will attempt to unload their respective raylib resources on destruction. This means that there is 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.

Property Get/Set

Properties can be assigned through getter and setter methods. However, you still have access to the internal properties.

// 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;

Function Overloading

Many similar raylib method names have different name suffixes based on what arguments they take. With raylib-cpp, these cases use function overloading 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 V in method name.
position.DrawPixel(color); // Alternatively

Method Chaining

When there's a method that doesn't return anything, in most cases it'll 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);

Operators

There are operator overloads implemented 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.

Vector-Based Returns

When there is a function that would return a pointer-array, there is a wrapper that allows returning a vector of objects instead.

// raylib
FilePathList files = LoadDirectoryFiles(".");
TraceLog(LOG_INFO, "Count: %i", files.count);
for (int i = 0; i < files.count; i++) {
    TraceLog(LOG_INFO, "File: %s", files.paths[i]);
}
UnloadDirectoryFiles(files);

// raylib-cpp
std::vector<std::string> files = raylib::GetDirectoryFiles(".");
TraceLog(LOG_INFO, "Count: %i", files.size());
for (auto& file : files) {
    TraceLog(LOG_INFO, "File: %s", file.c_str());
}

String Functions

Many of the raylib functions have std::string-related overrides to allow calling them directly with std::strings and avoid 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);

Exceptions

When loading an asset fails, raylib will log a warning, but provide no other feedback for you to act upon. raylib-cpp will throw a RaylibException runtime exception, allowing to provide a safe method for catching failed loads.

// raylib
Texture texture = LoadTexture("FileNotFound.png");
if (texture.width == 0) {
    TraceLog(LOG_ERROR, "Texture failed to load!");
}

// raylib-cpp
try {
    raylib::Texture texture("FileNotFound.png");
}
catch (raylib::RaylibException& error) {
    TraceLog(LOG_ERROR, "Texture failed to load!");
}

RayMath

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);

Getting Started

raylib-cpp is a header-only library. This means in order to use it, you must link your project to raylib, and then include raylib-cpp.hpp.

  1. Set up a raylib project using the build and install instructions
  2. Ensure .cpp files are compiled with C++
  3. Download raylib-cpp
  4. Include include/raylib-cpp.hpp
    #include "path/to/raylib-cpp.hpp"

Starter Projects

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.

Applications

Development

The following are some tools in order to build and contribute to raylib-cpp...

Compiling

Since raylib-cpp is a header only library, the build process is the same as raylib's, except you will use C++ to compile instead of C. The following are some specific instructions on local development.

Desktop

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
mkdir build
cd build
cmake ..
make
make test
./examples/core_basic_window

Web

Use emscripten to build and test core_basic_window_web.cpp.

mkdir build
cd build
emcmake cmake .. -DPLATFORM=Web -DCMAKE_BUILD_TYPE=Release -DCMAKE_EXE_LINKER_FLAGS="-s USE_GLFW=3"
emmake make

See core_basic_window_web.html for an example HTML canvas you can you.

Documentation

To build the document with Doxygen, use...

git submodule update --init
doxygen projects/Doxygen/Doxyfile

To publish the documentation to GitHub Pages, use...

npm run deploy

Coding Standards

This uses cpplint to adopt coding standards.

cpplint --recursive include

Defines

  • RAYLIB_CPP_NO_MATH - When set, will skip adding the raymath.h integrations

License

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.

More Repositories

1

component-installer

Install Web Components through Composer
JavaScript
280
star
2

node-raylib

Node.js bindings for Raylib
JavaScript
229
star
3

docker-composer

🙆‍♀️ Docker container to install and run Composer.
Makefile
104
star
4

raylib-lua-sol

A simple and easy-to-use Lua library to enjoy videogames programming
C++
77
star
5

jquery-once

💲 Act on jQuery elements only once.
JavaScript
61
star
6

raylib-nuklear

Nuklear immediate mode GUI for raylib
C
56
star
7

Dockie

🐳 Collection of Modern Development Environments for Docker
Shell
50
star
8

base16-geany

🔮 Base16 Scheme for Geany
Makefile
40
star
9

lutris-kodi-addon

Lutris Kodi Addon
Python
38
star
10

libretro-dolphin-launcher

Launch Dolphin from RetroArch/libretro
C
37
star
11

raylib-aseprite

Load Aseprite files for animated sprites in raylib.
C
37
star
12

docker-compose-drupal

DEPRECATED: Use Docker4Drupal
31
star
13

raylib-duktape

C++
30
star
14

docker-pa11y

pa11y Docker Container
Dockerfile
27
star
15

libretro-dats

Build some of the libretro-database DATs
JavaScript
24
star
16

raylib-physfs

Integrate PhysFS with raylib to load images, audio, and fonts, from .zip files.
C
24
star
17

generator-docpad

✏️ Yeoman generator for DocPad
JavaScript
21
star
18

raylib-tmx

Load Tiled .tmx files for tile maps in raylib, using the TMX C Loader.
C
20
star
19

raylib-libretro

👾 libretro frontend using raylib.
C
19
star
20

drush-docker

🐳 Docker Container to run Drush
Dockerfile
19
star
21

libretro-thumbnails-check

Checks consistency of libretro-thumbnails
JavaScript
19
star
22

raylib-app

Application wrapper for raylib
C
15
star
23

retroarch-theme-materialdesign

Material Design theme for RetroArch
PHP
14
star
24

Bunnymark-TIC80

Bunnymark ported to a few languages in TIC-80 for performance testing.
Lua
13
star
25

docpad-plugin-grunt

🐗 Run Grunt.js tasks when building with DocPad
CoffeeScript
13
star
26

Awesome-ChaiScript

A curated list of ChaiScript Modules and Utilities.
12
star
27

docker-lamp

DEPRECATED: See Forge instead
PHP
12
star
28

ecwolf

Git mirror of ECWolf
C++
12
star
29

libretro-database-scummvm

ScummVM.dat file to run ScummVM games in RetroArch
JavaScript
12
star
30

sdl2_gfx

SDL2_gfx Mirror, likely out of date
HTML
11
star
31

docker-google-appengine

Google App Engine Docker container
Shell
10
star
32

xor-crypt

🔣 Simple JavaScript XOR string encryption library
HTML
10
star
33

composer.deb

Build a Debian package to install and use Composer.
Makefile
10
star
34

ChaiLove-Launcher

🚀 Launch applications through ChaiLove
9
star
35

raylib-wasm

WIP
C
9
star
36

ChaiLove-FloppyBird

🐦 Flappy Bird in ChaiLove
9
star
37

raylib_functions_parser

Fork of raysan5's raylib.h parser to output JSON
C
9
star
38

docpad-plugin-webpack

✏️ Bundle CommonJs/AMD Modules using webpack
CoffeeScript
8
star
39

nuklear_sdl_12

SDL 1.2 support for Nuklear
C
8
star
40

raylib-tileson

Use Tiled maps in raylib, through Tileson.
C++
7
star
41

DangerousDave-TIC80

Dangerous Dave in TIC-80
7
star
42

raylib-api

Exports of the raylib API from raylib_parser
Lua
7
star
43

awesome-sdl

Curated list of awesome libraries and bindings for SDL.
7
star
44

net.lutris.Lutris.old

Lutris for Flatpak
6
star
45

raylib-chaiscript

ChaiScript bindings for raylib, a simple and easy-to-use library to learn videogames programming.
C++
6
star
46

wren-bind

C
5
star
47

tic80-js

Bundle your TIC-80 JavaScript ES6 Games
JavaScript
5
star
48

datfile

ClrMamePro DAT file parser for Node.js
JavaScript
5
star
49

love-lutris

Lutris game launcher for Love2D or RetroArch/libretro
Lua
5
star
50

httpserver

PHP 5.4 HTTP Server wrapper.
PHP
4
star
51

wren-assert

Minimalistic assertion library for unit testing in Wren.
Makefile
4
star
52

asdf-emsdk

Emscripten SDK plugin for asdf version manager
Shell
4
star
53

ubuntu-mate

GRUB2 Theme for Ubuntu MATE
Shell
4
star
54

raylib-meson

raylib meson build system.
Meson
4
star
55

scriptpacker

Package multiple Wren, Squirrel or ChaiScript files into one, through JavaScript.
JavaScript
4
star
56

pntr

Image manipulation library for C.
C
4
star
57

docker-clean

🐳 Kill and delete all Docker containers and images
Shell
4
star
58

docker-fig-php

A port of the Fig Quick Start example to PHP and MySQL
PHP
4
star
59

libretrojs

C++
3
star
60

wren-path

Work with file and directory paths in Wren.
3
star
61

SDL_physfs

PhysFS virtual file system support for SDL.
C
3
star
62

Fission

A C++-ish wrapper for nuklear
C++
3
star
63

awesome-dats

A curated list of awesome DAT files.
JavaScript
3
star
64

raylib-assert

Minimalistic assertion library for raylib.
C
3
star
65

libretro-database-dos

DOS.dat for libretro-database
JavaScript
3
star
66

slides

Presentation slides for various conferences
HTML
3
star
67

ChaiScript_Extras_Box2D

ChaiScript Box2D Bindings
C++
3
star
68

language-drupal

Drupal syntax highlighting and snippets for Atom.io
CoffeeScript
3
star
69

bowersatis

Satis repository to index Bower packages.
PHP
3
star
70

libretro-rimage

Use pntr instead
C
3
star
71

Sokoban

Sokoban for Dinothawr
3
star
72

component-installer-example

An example of using Component Installer.
HTML
3
star
73

raylib-umka

Umka language bindings for raylib.
C
3
star
74

dotfiles

📂 My Dotfiles
Shell
3
star
75

sdl2js

SDL2.js - Experimental Node.js bindings to SDL2.
JavaScript
2
star
76

rimage

Use raylib's Image module by itself.
C
2
star
77

CMakePacks

CMake definition file for Duktape, raylib, and Umka.
C
2
star
78

raylib-assetsys

Use the file system abstraction library, assetsys.h, to load raylib assets from .zip files.
C
2
star
79

raylib-squirrel

C++
2
star
80

assetic-bundle

Sculpin bundle to add support for Assetic?
PHP
2
star
81

ChaiScript.js

JavaScript port of ChaiScript
HTML
2
star
82

metalsmith-jstransformer-layouts

Deprecated with Metalsmith JSTransformer
JavaScript
2
star
83

docker-drupal8

DEPRECATED: See /robloach/forge instead.
Shell
2
star
84

raylib-imagedraw

Extra Image manipulation methods for raylib.
C
2
star
85

Threes-TIC80

Threes in TIC-80
JavaScript
2
star
86

libretro-database-tic80

TIC-80 database for libretro
JavaScript
2
star
87

reveal-giphy

Rapid Giphy Reveal.js presentations
JavaScript
2
star
88

require-one

🎣 Require the first found module from an array.
JavaScript
2
star
89

raygui-container

Adds state to raygui, allowing for gamepad/keyboard-only support.
C
2
star
90

node-sdl2-pong

Pong, using Node.js and SDL2
JavaScript
1
star
91

node-sdl2-gfx

SDL2_gfx bindings for Node.js
JavaScript
1
star
92

libretro-fbg

C
1
star
93

pntr_app

Application wrapper for pntr.
C
1
star
94

YoxViewOld

A GIT branch from YoxView.
JavaScript
1
star
95

node-physfs

PhysicsFS (physfs) bindings for Node.js
JavaScript
1
star
96

docker-docpad

DEPRECATED: See docpad/dockerfile instead
Shell
1
star
97

ChaiLove-JSTest

🎮 Simple ChaiLove Joystick test application
1
star
98

jquery.ui.widget

jQuery UI Widget
JavaScript
1
star
99

wasm4-as

Drop-in replacement to WASM-4's wasm4.ts to make using AssemblyScript with WASM-4 easy
TypeScript
1
star
100

metalsmith-json-feed

Experimental use of a JSON Feed in Metalsmith
JavaScript
1
star