Skip to main content

Python CFFI bindings for Raylib

Project description

Python Bindings for Raylib 3.7

New CFFI API static bindings. Faster, fewer bugs and easier to maintain than ctypes.

Advert

RetroWar: 8-bit Party Battle is out now. Defeat up to 15 of your friends in a tournament of 80s-inspired retro mini games.

Install

Option 1: Install from Pypi (easiest but may not be up to date or be available for your platform)

We distribute a statically linked binary Raylib library, install from Pypi.

pip3 install raylib==3.5.0

Some platforms that should be available:

Windows 10 (64 bit): Python 3.6 - 3.8

MacOS: Python 3.6 - 3.8

Linux (Ubuntu 16.04+): Python 3.6 - 3.9

If yours isn't available then pip should attempt to build from source, so you will need to have raylib development libs installed.

Option 2: Build from source

If you're using a platform we don't have binary builds for yet then you can either use the dynamic binding with your own dll or else you will have to build from source. If you do build on a new platform please submit your binaries as a PR.

These instructions have been tested on Ubuntu 20.10 and 16.04. Mac should be very similar. Windows is probably different.

Clone this repo including submodules so you get correct version of Raylib.

git clone --recurse-submodules https://github.com/electronstudio/raylib-python-cffi

Build and install Raylib from the raylib-c directory.

cd raylib-python-cffi/raylib-c
mkdir build
cd build
ccmake -DWITH_PIC=on -DBUILD_SHARED_LIBS=on -DCMAKE_BUILD_TYPE=Release ..
sudo make install

Make a patched version of raylib header. (Not necessary if you've already got raylib_modifed.h from repo and haven't changed anything.)

cd ../../raylib
cp raylib.h raylib_modified.h
patch  -p0 <raylib_modified.h.patch

Build

pip3 install cffi
cd ..
rm -rf build raylib/static/_raylib_cffi.*
python3 raylib/static/build.py

To update the Linux dynamic libs (names will be different on other platfroms):

rm raylib/dynamic/*.so*
cp -P /usr/local/lib/libraylib.so* raylib/dynamic/

Distributing

To build a binary wheel distribution:

pip3 install wheel
python3 setup.py bdist_wheel

and install it:

pip3 install dist/raylib*.whl

To build a complete set of libs for Python 3.6, 3.7, 3.8 and 3.9:

./raylib/static/build_multi.sh

(NOTE pypi wont accept Linux packages unless they are built --plat-name manylinux2014_x86_64 so on linux please run ./raylib/static/build_multi_linux.sh )

(TODO move the dynamic libs into a separate package rather than include them with every one.)

Raspberry Pi

The integrated GPU hardware in a Raspberry Pi ("VideoCore") is rather idiosyncratic, resulting in a complex set of software options. Probably the most interesting two options for Raylib applications are:

  1. Use the Broadcom proprietary Open GL ES 2.0 drivers, installed by Raspbian into /opt/vc. These are 32-bit only, and currently X11 doesn't use these for its acceleration, so this is most suitable for driving the entire HDMI output from one application with minimal overhead (no X11).

  2. Use the more recent open-source vc4-fkms-v3d kernel driver. This can run in either 32-bit or 64-bit, and X11 can use these, so using X11 is probably the more common choice here.

With option 2, the regular linux install instructions above should probably work as-is.

For option 1, then also follow the above instructions, but with these modifications:

  • With cmake, use cmake -DWITH_PIC=on -DSTATIC=on -DSHARED=on -DPLATFORM='Raspberry Pi' ..

Use

raylib.static

Goal is make usage as similar to the original C as CFFI will allow. There are a few differences you can see in the examples. See test_static.py and examples/*.py for how to use.

from raylib.static import *

InitWindow(800, 450, b"Hello Raylib")
SetTargetFPS(60)

camera = ffi.new("struct Camera3D *", [[18.0, 16.0, 18.0], [0.0, 0.0, 0.0], [0.0, 1.0, 0.0], 45.0, 0])
SetCameraMode(camera[0], CAMERA_ORBITAL)

while not WindowShouldClose():
    UpdateCamera(camera)
    BeginDrawing()
    ClearBackground(RAYWHITE)
    BeginMode3D(camera[0])
    DrawGrid(20, 1.0)
    EndMode3D()
    DrawText(b"Hellow World", 190, 200, 20, VIOLET)
    EndDrawing()
CloseWindow()

raylib.pyray

Wrapper around the static bindings. Makes the names snakecase and converts strings to bytes automatically. See test_pyray.py.

from raylib.pyray import PyRay
from raylib.colors import *

pyray = PyRay()

pyray.init_window(800, 450, "Hello Pyray")
pyray.set_target_fps(60)

camera = pyray.Camera3D([18.0, 16.0, 18.0], [0.0, 0.0, 0.0], [0.0, 1.0, 0.0], 45.0, 0)
pyray.set_camera_mode(camera, pyray.CAMERA_ORBITAL)

while not pyray.window_should_close():
    pyray.update_camera(camera)
    pyray.begin_drawing()
    pyray.clear_background(RAYWHITE)
    pyray.begin_mode_3d(camera)
    pyray.draw_grid(20, 1.0)
    pyray.end_mode_3d()
    pyray.draw_text("Hello world", 190, 200, 20, VIOLET)
    pyray.end_drawing()
pyray.close_window()

raylib.dynamic

In addition to the API static bindings we have CFFI ABI dynamic bindings in order to avoid the need to compile a C extension module. There have been some weird failures with dynamic bindings and ctypes bindings before and often the failures are silent so you dont even know. Also the static bindings should be faster. Therefore I recommend the static ones...

BUT the dynamic bindings have the big advantage that you don't need to compile anything to install. You just need a Raylib DLL, which we supply for Windows/Mac/Linux.

Currently the DLL is being removed from the pypi packages but still available in the git repo. Could split into its own pypi package if anyone wants it.

See test_dynamic.py for how to use.

richlib

A simplified API for Raylib for use in education and to enable beginners to create 3d games

HELP WANTED

  • converting more examples from C to python
  • testing and building on more platforms
  • sorting out binary wheel distribution for Mac/Win and compile-from-source distributtion for Linux

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

raylib-3.7.0-cp39-cp39-win_amd64.whl (1.3 MB view details)

Uploaded CPython 3.9Windows x86-64

raylib-3.7.0-cp39-cp39-manylinux2014_x86_64.whl (3.0 MB view details)

Uploaded CPython 3.9

raylib-3.7.0-cp38-cp38-win_amd64.whl (1.3 MB view details)

Uploaded CPython 3.8Windows x86-64

raylib-3.7.0-cp38-cp38-manylinux2014_x86_64.whl (3.0 MB view details)

Uploaded CPython 3.8

raylib-3.7.0-cp37-cp37m-win_amd64.whl (1.3 MB view details)

Uploaded CPython 3.7mWindows x86-64

raylib-3.7.0-cp37-cp37m-manylinux2014_x86_64.whl (3.0 MB view details)

Uploaded CPython 3.7m

raylib-3.7.0-cp36-cp36m-manylinux2014_x86_64.whl (3.0 MB view details)

Uploaded CPython 3.6m

File details

Details for the file raylib-3.7.0-cp39-cp39-win_amd64.whl.

File metadata

  • Download URL: raylib-3.7.0-cp39-cp39-win_amd64.whl
  • Upload date:
  • Size: 1.3 MB
  • Tags: CPython 3.9, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

File hashes

Hashes for raylib-3.7.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 45eb4b661c53dabbd1c5f43c3edf91f19adbccea1732b51fc3e43dc81321fffc
MD5 b7e150c27cf9793dd002ea6d25581174
BLAKE2b-256 e71f4db36d1c381df9fa483738a9c722e605f7fa3c850260297e76ef04b687ca

See more details on using hashes here.

File details

Details for the file raylib-3.7.0-cp39-cp39-manylinux2014_x86_64.whl.

File metadata

  • Download URL: raylib-3.7.0-cp39-cp39-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 3.0 MB
  • Tags: CPython 3.9
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

File hashes

Hashes for raylib-3.7.0-cp39-cp39-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6094f069818f23901cf3af767f2342d5409bdd3c8798507ee5c2091d05a12e66
MD5 741b290a3b7690470a483497673dea93
BLAKE2b-256 3b8458c205b83fa8e15885618581b834ccf1b323e85049e62ebd0e9125fed8d0

See more details on using hashes here.

File details

Details for the file raylib-3.7.0-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: raylib-3.7.0-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 1.3 MB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

File hashes

Hashes for raylib-3.7.0-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 2d8f89fa1c92f392ccf3490d67ab4d3c399bd3b864b21cfea52a74a8fc2ec5f0
MD5 c16ba307a08c03cb248bcfb6a58dc682
BLAKE2b-256 4266878cedc840da746c26756078354e046b43aa0c40815b6dced8ea301db834

See more details on using hashes here.

File details

Details for the file raylib-3.7.0-cp38-cp38-manylinux2014_x86_64.whl.

File metadata

  • Download URL: raylib-3.7.0-cp38-cp38-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 3.0 MB
  • Tags: CPython 3.8
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

File hashes

Hashes for raylib-3.7.0-cp38-cp38-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 707f1ac5705d0f07f36402dcf1653db8333420994830b184ea05a3e78aad717e
MD5 94f85df48000952149993dc508b5bd15
BLAKE2b-256 2cce37cfad5c107c6380b6d0efe9b2edf326ba5e376e24eab16c13d5ead10362

See more details on using hashes here.

File details

Details for the file raylib-3.7.0-cp37-cp37m-win_amd64.whl.

File metadata

  • Download URL: raylib-3.7.0-cp37-cp37m-win_amd64.whl
  • Upload date:
  • Size: 1.3 MB
  • Tags: CPython 3.7m, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

File hashes

Hashes for raylib-3.7.0-cp37-cp37m-win_amd64.whl
Algorithm Hash digest
SHA256 3b84dd5eb9fca697950054ca4790e8656d9031547f7796a68f556e2bdac30761
MD5 9f0f8b67d46f62edeffa2a6bc6051aae
BLAKE2b-256 0026424388888088b6a9e1251dcfab9d9e328b5cb1e457439ebd1697a3d93c4c

See more details on using hashes here.

File details

Details for the file raylib-3.7.0-cp37-cp37m-manylinux2014_x86_64.whl.

File metadata

  • Download URL: raylib-3.7.0-cp37-cp37m-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 3.0 MB
  • Tags: CPython 3.7m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

File hashes

Hashes for raylib-3.7.0-cp37-cp37m-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 15c9f6892100b60c086b751f45bbf363eef9911de94a6e776a4b3b67f5f48f30
MD5 621b4b1981789eb9b8013cbca6ba7bbb
BLAKE2b-256 6b1e20bf72896af8c6a449489947fadcf5d07acffb13245f12606ca1b2ee780c

See more details on using hashes here.

File details

Details for the file raylib-3.7.0-cp36-cp36m-manylinux2014_x86_64.whl.

File metadata

  • Download URL: raylib-3.7.0-cp36-cp36m-manylinux2014_x86_64.whl
  • Upload date:
  • Size: 3.0 MB
  • Tags: CPython 3.6m
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.9.5

File hashes

Hashes for raylib-3.7.0-cp36-cp36m-manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 480561051385b55e27351547044711e422cc31b00ee9c2f84856edf5e3d39e3d
MD5 fa85fa641dd1903b9a2430af154842c0
BLAKE2b-256 598171ec553910b07cf3674876bd4f6ab3f5a5692a34aa49766ac107454f78d6

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page