Skip to content

SDL2 FFI bindings for the PHP language

License

Notifications You must be signed in to change notification settings

SerafimArts/ffi-sdl

Repository files navigation

FFI SDL Bindings

This is a SDL bindings for PHP

Requirements

  • PHP >= 7.4
  • ext-ffi
  • Windows, Linux or MacOS
    • Android, iOS, BSD or something else are not supported yet
  • SDL >= 2.0

Installation

Library is available as composer repository and can be installed using the following command in a root of your project.

$ composer require serafim/ffi-sdl

Linux

  • sudo apt install libsdl2-2.0-0 -y

MacOS

  • brew install sdl2

Windows

  • SDL (2.0.12) binaries are already bundled

Extensions

Documentation

The library API completely supports and repeats the analogue in the C language.

Notes

  • API not yet fully documented and may not work in places.
  • Low level system functions (such as SDL_malloc or SDL_memcpy) have been removed.

Example

use Serafim\SDL\SDL;
use Serafim\SDL\Event;
use Serafim\SDL\Kernel\Event\Type;

$sdl = new SDL();

$sdl->SDL_Init(SDL::SDL_INIT_VIDEO);

$window = $sdl->SDL_CreateWindow( 
    'An SDL2 window',
    SDL::SDL_WINDOWPOS_UNDEFINED,
    SDL::SDL_WINDOWPOS_UNDEFINED, 
    640,
    480,
    SDL::SDL_WINDOW_OPENGL
);

if ($window === null) {
    throw new \Exception(sprintf('Could not create window: %s', $sdl->SDL_GetError()));
}

$event = $sdl->new(Event::class);
$running = true;

while ($running) {
    $sdl->SDL_PollEvent(SDL::addr($event));
    if ($event->type === Type::SDL_QUIT) {
        $running = false;
    }
}

$sdl->SDL_DestroyWindow($window);
$sdl->SDL_Quit();