serafim/ffi-sdl

PHP FFI to SDL2 bindings

1.0.4 2020-06-18 14:11 UTC

This package is auto-updated.

Last update: 2022-04-21 21:33:09 UTC


README

This is a SDL bindings for PHP

Requirements

  • PHP ^8.1
  • 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

  • composer require --dev phing/phing
  • vendor/bin/phing serafim/ffi-sdl:download -f vendor/serafim/ffi-sdl/build.xml

Extensions

Documentation

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

Usage

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

$sdl = new SDL2(init: Init::VIDEO);

$window = $sdl->SDL_CreateWindow( 
    'An SDL2 window',
    SDL2::SDL_WINDOWPOS_UNDEFINED,
    SDL2::SDL_WINDOWPOS_UNDEFINED, 
    640,
    480,
    SDL2::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(SDL2::addr($event));
    if ($event->type === Type::SDL_QUIT) {
        $running = false;
    }
}

$sdl->SDL_DestroyWindow($window);

Remarks

This API not included and is only available through direct FFI calls:

  • From SDL_assert.h: Not required, as assertion is built into PHP.
    • SDL_ReportAssertion
    • SDL_SetAssertionHandler
    • SDL_GetDefaultAssertionHandler
    • SDL_GetAssertionHandler
    • SDL_GetAssertionReport
    • SDL_ResetAssertionReport
  • From SDL_atomic.h: Working with pointers is available only when working at a low level.
    • SDL_AtomicCASPtr
    • SDL_AtomicSetPtr
    • SDL_AtomicGetPtr