serafim/ffi-sdl

PHP语言的SDL FFI绑定

2.0.0 2023-08-12 20:16 UTC

This package is auto-updated.

Last update: 2024-09-12 22:55:31 UTC


README

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

SDL FFI绑定用于PHP语言。

要求

  • PHP ^8.1
  • ext-ffi
  • Windows, Linux, BSD或MacOS
    • Android, iOS或其他平台目前尚不支持
  • SDL >= 2.0

安装

库可作为composer仓库使用,您可以在项目的根目录中使用以下命令进行安装。

$ composer require serafim/ffi-sdl

附加依赖

  • 基于Debian的Linux:sudo apt install libsdl2-2.0-0 -y
  • MacOS:brew install sdl2
  • Windows:可以从这里下载

扩展

文档

库API完全支持并重复了C语言中的类似功能。

  • SDL2官方文档

  • API尚未完全文档化,可能存在一些问题。

  • 已删除低级和内联函数(如SDL_mallocSDL_memcpy)。

初始化

要显式指定库路径,您可以将library参数添加到Serafim\SDL\SDL构造函数中。

默认情况下,库将尝试自动解析二进制文件的路径。

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

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

您可以显式指定用于编译头文件的平台(操作系统)。

默认情况下,库将尝试自动解析平台。

// Use Linux as compile-aware platform
$sdl = new Serafim\SDL\SDL(platform: Serafim\SDL\Platform::LINUX);

// Detect platform automatically
$sdl = new Serafim\SDL\SDL(platform: null);

您还可以显式指定库版本。根据此版本,SDL将提供相应的功能。

默认情况下,库将尝试自动解析SDL版本。

// Use v2.28.2 from string
$sdl = new Serafim\SDL\SDL(version: '2.28.2');

// Use v2.24.1 from predefined versions constant
$sdl = new Serafim\SDL\SDL(version: \Serafim\SDL\Version::V2_24_1);

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

为了加快头文件编译器,您可以使用任何PSR-16兼容的缓存驱动器。

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

此外,您还可以显式控制其他预处理器指令。

$pre = new \FFI\Preprocessor\Preprocessor();
$pre->setLogger(new ExampleLogger());
$pre->define('__ANDROID__', '1');

$sdl = new Serafim\SDL\SDL(pre: $pre);
$jni = $sdl->SDL_AndroidGetJNIEnv();

示例

use Serafim\SDL\SDL;
use Serafim\SDL\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('SDL_Event');
$running = true;

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

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