Skip to content

Working on Raspberry Pi

Coo Chew Games edited this page Sep 2, 2024 · 21 revisions

To build your raylib game for Raspberry Pi you just need to download raylib git repository (or get the current release from here). All required libraries come with the raylib, no additional dependencies are required. raylib also comes with ready-to-use makefiles to compile source code and examples.

WARNING: PLATFORM_RPI is deprecated (only works with Raspbian Buster). Use PLATFORM_DRM for latest RPI OS Bullseye.

BE AWARE: referring to this question, that a library compiled with PLATFORM=PLATFORM_DRM will not work on X Desktop and a library compiled for X Desktop will not run via CLI, relevantly.

Supported Devices and OS

raylib supports the following Raspberry Pi models:

  • Raspberry Pi Zero (all models)
  • Raspberry Pi 1 (all models)
  • Raspberry Pi 2 (all models)
  • Raspberry Pi 3 (all models)
  • Raspberry Pi 4 (all models)

raylib supports the platforms/OSs, desktop and native modes:

Supported OpenGL backends

  • OpenGL ES 2.0 in native mode (no X11 required) (PLATFORM_DRM or PLATFORM_RPI)
  • OpenGL 1.1 on X11 desktop mode (PLATFORM_DESKTOP)
  • OpenGL 2.1 on X11 desktop mode (PLATFORM_DESKTOP)

By default, raylib should be compiled with PLATFORM_DESKTOP on classic X11-based Linux desktop environment, using the provided OpenGL desktop drivers. However, it is also possible to compile in native mode not depending on any windowing system (no X11 required).

When compiling for native mode, PLATFORM_DRM or PLATFORM_RPI should be used, depending on the RPI OS version, check previous point for details.

Compiling raylib source code

Before you can use raylib in your project you will have to compile it, but this is quick and easy!

Just navigate to raylib\src\ directory and run one of the following options depending on your needs:

1. To compile on desktop mode (X11 window)

make PLATFORM=PLATFORM_DESKTOP GRAPHICS=GRAPHICS_API_OPENGL_21

Raylib examples use GLSL 3.3 shaders. Older Raspberry Pi's 0-3 only support upto GLSL 1.2. If you are on Raspberry Pi OS desktop on something lower than RPi4 you will see that shaders will default to a base shader and examples may not look as intended. To fix this, we need to tell Raylib to load GLSL 1.2 versions of the shader in the .c file of the example which will look something like this:

#if defined(PLATFORM_DESKTOP)
    #define GLSL_VERSION            120 // Change this back to 330 if compiling for a platform that supports GLSL3.3
#else   // PLATFORM_ANDROID, PLATFORM_WEB
    #define GLSL_VERSION            100
#endif

Also, ensure the correct versions of the shaders are available in the raylib/examples/resources/shaders/glsl120 folder. Troubleshooting is easier while running your example from a terminal as raylib will log the errors over there.

NOTE on compilation errors: To use raylib on the Raspberry Pi desktop, you need to had previously installed all desktop window-dev system libraries, if you just downloaded Raspberry Pi OS Desktop, maybe it comes with required libraries installed but if it complains on compilation, just make sure to install the following libraries:

sudo apt-get install --no-install-recommends raspberrypi-ui-mods lxterminal gvfs
sudo apt-get install libx11-dev libxcursor-dev libxinerama-dev libxrandr-dev libxi-dev libasound2-dev mesa-common-dev libgl1-mesa-dev

2. To compile in native mode (no X11)

On Raspberry Pi models 0 to 4 using the Raspbian OS:

Buster:
make PLATFORM=PLATFORM_RPI
Bullseye or newer:

To compile raylib in native mode on Bullseye or newer, you will need the install the DRM libraries:

sudo apt-get install libdrm-dev libegl1-mesa-dev libgles2-mesa-dev libgbm-dev

To build raylib:

make PLATFORM=PLATFORM_DRM
DietPi

raylib also works on the DietPi distribution, though the following libraries are required prior to building raylib:

sudo apt-get install libraspberrypi-dev raspberrypi-kernel-headers
sudo apt-get install build-essential

Example compilation scripts for building a raylib application

The following are example compilation commands that can be used to build a raylib application assuming the raylib repository has been cloned to a parent directory to the application code.

./raylib/src
./app

The example assumes the application is built from x.c in the app directory. The emphasis is which libraries to utilise when building on the earlier and later versions of Raspbian OS. The examples provided are derived from code written on a Pi 0.

Buster
cc -o x x.c -I../raylib/src -I/opt/vc/include -L../raylib/src -L/opt/vc/lib -lraylib -lm -lpthread -lbrcmGLESv2 -lbrcmEGL -lvcos -lvchiq_arm -lbcm_host
Bullseye
cc -o x x.c -I../raylib/src -I/opt/vc/include -L../raylib/src -L/opt/vc/lib -lraylib -lm -lpthread -lGLESv2 -lEGL -lvcos -lvchiq_arm -lgbm -ldrm

3. To compile on OPENBOX

WARNING: The program compiled as PLATFORM_DRM under openbox fails (More testing is required).

It is untested.

Documentations must be added.

Compiling raylib examples

Just move to folder raylib/examples/ and run the same make command you used to compile raylib

Rotate Screen

PLATFORM_DESKTOP

It's clear on desktop:

Settings -> Display -> Rotation
PLATFORM_DRM

In this case, it's not that simple. With regard to this discussion https://github.com/raysan5/raylib/issues/3958, I am forced to state here that it is not possible to rotate the image by 90° or 180° in hardware. And it has to be done programmatically!?

He still wants to investigate and supplement it..

Clone this wiki locally