serafim / ffi-sdl-ttf
为PHP语言提供的SDL TTF FFI绑定
2.0.2
2023-09-03 10:45 UTC
Requires
- php: ^8.1
- ext-ffi: *
- ext-mbstring: *
- ffi-headers/contracts: ^1.0
- ffi/env: ^1.0
- ffi/location: ^1.0
- ffi/preprocessor: ^0.2
- ffi/proxy: ^1.0
- ffi/work-directory: ^1.0
- psr/simple-cache: ^1.0|^2.0|^3.0
- serafim/ffi-sdl: ^2.0
Requires (Dev)
- ffi/ide-helper-generator: ^0.1|^1.0
- friendsofphp/php-cs-fixer: ^3.22
- phpunit/phpunit: ^10.3
- symfony/cache: ^5.4|^6.0
- vimeo/psalm: ^5.14
This package is auto-updated.
Last update: 2024-08-24 10:33:35 UTC
README
这是一个为PHP语言兼容的SDL FFI绑定的SDL_ttf扩展FFI绑定。
要求
- PHP ^8.1
- ext-ffi
- Windows, Linux, BSD或MacOS
- Android, iOS或其他尚未支持
- SDL和SDL TTF >= 2.0
安装
库可作为composer仓库提供,并在项目根目录中使用以下命令安装。
$ composer require serafim/ffi-sdl-ttf
其他依赖
- 基于Debian的Linux:
sudo apt install libsdl2-ttf-2.0-0 -y
- MacOS:
brew install sdl2_ttf
- Window: 可从此处下载
文档
库API完全支持并重复了C语言中的类似项。
初始化
如果需要特定的SDL实例,应明确传递。
$sdl = new Serafim\SDL\SDL(version: '2.28.3'); $ttf = new Serafim\SDL\TTF\TTF(sdl: $sdl); // If no argument is passed, the latest initialized // version will be used. $ttf = new Serafim\SDL\TTF\TTF(sdl: null);
! 建议始终明确指定SDL依赖。
要明确指定库路径,可以将library
参数添加到Serafim\SDL\TTF\TTF
构造函数中。
默认情况下,库将尝试自动解析二进制路径名。
// Load library from pathname (it may be relative or part of system-dependent path) $ttf = new Serafim\SDL\TTF\TTF(library: __DIR__ . '/path/to/library.so'); // Try to automatically resolve library's pathname $ttf = new Serafim\SDL\TTF\TTF(library: null);
您还可以明确指定库版本。根据此版本,SDL Image的相关功能将可用。
默认情况下,库将尝试自动解析SDL Image版本。
// Use v2.0.14 from string $ttf = new Serafim\SDL\TTF\TTF(version: '2.0.14'); // Use v2.20.2 from predefined versions constant $ttf = new Serafim\SDL\TTF\TTF(version: \Serafim\SDL\TTF\Version::V2_20_2); // Use latest supported version $ttf = new Serafim\SDL\TTF\TTF(version: \Serafim\SDL\TTF\Version::LATEST);
要加快头文件编译器,您可以使用任何PSR-16兼容的缓存驱动程序。
$ttf = new Serafim\SDL\TTF\TTF(cache: new Psr16Cache(...));
此外,您还可以明确控制其他预处理指令。
$pre = new \FFI\Preprocessor\Preprocessor(); $pre->define('true', 'false'); // happy debugging! $ttf = new Serafim\SDL\TTF\TTF(pre: $pre);
示例
use Serafim\SDL\SDL; use Serafim\SDL\TTF\TTF; $sdl = new SDL(); $ttf = new TTF(sdl: $sdl); $sdl->SDL_Init(SDL::SDL_INIT_EVERYTHING); $ttf->TTF_Init(); $font = $ttf->TTF_OpenFont(__DIR__ . '/path/to/font.ttf', 42); echo 'Hinting: ' . $ttf->TTF_GetFontHinting($font) . "\n"; echo 'Kerning: ' . $ttf->TTF_GetFontKerning($font) . "\n"; echo 'Style: ' . match($ttf->TTF_GetFontStyle($font)) { TTF::TTF_STYLE_NORMAL => 'normal', TTF::TTF_STYLE_BOLD => 'bold', TTF::TTF_STYLE_ITALIC => 'italic', TTF::TTF_STYLE_UNDERLINE => 'underline', TTF::TTF_STYLE_STRIKETHROUGH => 'strikethrough', } . "\n"; $color = $sdl->new('SDL_Color'); $color->r = 120; $color->g = 100; $color->b = 80; $surface = $ttf->TTF_RenderText_Solid($font, 'Hello World!', $color); $ttf->TTF_Quit(); $sdl->SDL_Quit();