serafim/ffi-sdl-mixer

SDL Mixer FFI bindings for the PHP language

2.0.1 2023-08-28 00:15 UTC

This package is auto-updated.

Last update: 2024-09-28 02:51:18 UTC


README

PHP 8.1+ SDL_mixer Latest Stable Version Latest Unstable Version Total Downloads License MIT

A SDL_ttf extension FFI bindings for the PHP language compatible with SDL FFI bindings for the PHP language.

Requirements

  • PHP ^8.1
  • ext-ffi
  • Windows, Linux, BSD or MacOS
    • Android, iOS or something else are not supported yet
  • SDL and SDL Mixer >= 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-mixer

Additional dependencies:

  • Debian-based Linux: sudo apt install libsdl2-mixer-2.0-0 -y
  • MacOS: brew install sdl2_mixer
  • Window: Can be downloaded from here

Documentation

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

Initialization

In the case that you need a specific SDL instance, it should be passed explicitly:

$sdl = new Serafim\SDL\SDL(version: '2.28.3');
$mixer = new Serafim\SDL\Mixer\Mixer(sdl: $sdl);

// If no argument is passed, the latest initialized
// version will be used.
$mixer = new Serafim\SDL\Mixer\Mixer(sdl: null);

! It is recommended to always specify the SDL dependency explicitly.

To specify a library pathname explicitly, you can add the library argument to the Serafim\SDL\Mixer\Mixer constructor.

By default, the library will try to resolve the binary's pathname automatically.

// Load library from pathname (it may be relative or part of system-dependent path)
$mixer = new Serafim\SDL\Mixer\Mixer(library: __DIR__ . '/path/to/library.so');

// Try to automatically resolve library's pathname
$mixer = new Serafim\SDL\Mixer\Mixer(library: null);

You can also specify the library version explicitly. Depending on this version, the corresponding functions of the SDL Image will be available.

By default, the library will try to resolve SDL Image version automatically.

// Use v2.0.5 from string
$mixer = new Serafim\SDL\Mixer\Mixer(version: '2.0.5');

// Use v2.6.3 from predefined versions constant
$mixer = new Serafim\SDL\Mixer\Mixer(version: \Serafim\SDL\Mixer\Version::V2_6_3);

// Use latest supported version
$mixer = new Serafim\SDL\Mixer\Mixer(version: \Serafim\SDL\Mixer\Version::LATEST);

To speed up the header compiler, you can use any PSR-16 compatible cache driver.

$mixer = new Serafim\SDL\Mixer\Mixer(cache: new Psr16Cache(...));

In addition, you can control other preprocessor directives explicitly:

$pre = new \FFI\Preprocessor\Preprocessor();
$pre->define('true', 'false'); // happy debugging!

$mixer = new Serafim\SDL\Mixer\Mixer(pre: $pre);

Example

use Serafim\SDL\SDL;
use Serafim\SDL\Mixer\Mixer;

$sdl = new SDL();
$mixer = new Mixer(sdl: $sdl);

$sdl->SDL_Init(\Serafim\SDL\InitFlags::SDL_INIT_EVERYTHING);
$mixer->Mix_Init(\Serafim\SDL\Mixer\InitFlags::MIX_INIT_MP3);

$mixer->Mix_OpenAudio(44100, SDL::AUDIO_S16SYS, 2, 2048);
$music = $mixer->Mix_LoadMUS(__DIR__ . '/example.mp3');
$mixer->Mix_PlayMusic($music, 0);

while (true) {
    usleep(1);
}

$mixer->Mix_Quit();
$sdl->SDL_Quit();