PHP code example of serafim / ffi-sdl-ttf

1. Go to this page and download the library: Download serafim/ffi-sdl-ttf library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

serafim / ffi-sdl-ttf example snippets


$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);

// 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);

// 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);

$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();