PHP code example of php-sdl / sdl3-ttf

1. Go to this page and download the library: Download php-sdl/sdl3-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/ */

    

php-sdl / sdl3-ttf example snippets




use Sdl3ttf\SDLTTF\SDLTTF;

// Initialise SDL3_ttf
SDLTTF::TTFInit();

// Open a font at 32pt
$font = SDLTTF::TTFOpenFont('/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf', 32.0);
printf("Font height: %d  ascent: %d  descent: %d\n",
    $font['height'], $font['ascent'], $font['descent']);

// Render white text to an SDL_Surface
$surface = SDLTTF::TTFRenderTextBlended(
    $font['ptr'],       // font pointer
    'Hello, SDL3_ttf!', // text
    255, 255, 255, 255  // RGBA
);
printf("Rendered surface: %dx%d (%d pixels)\n",
    $surface['w'], $surface['h'],
    count($surface['pixels']['data'])
);

// Clean up
SDLTTF::TTFCloseFont($font['ptr']);
SDLTTF::TTFQuit();

[
    'ptr'         => int,   // opaque TTF_Font pointer
    'size'        => int,   // resolved point size
    'style'       => int,   // TTF_STYLE_* bitmask
    'outline'     => int,   // outline thickness (0 = none)
    'hinting'     => int,   // TTF_HINTING_* value
    'height'      => int,   // max glyph height in pixels
    'ascent'      => int,   // pixels above baseline
    'descent'     => int,   // pixels below baseline (negative)
    'line_skip'   => int,   // recommended line spacing
    'fixed_width' => int,   // 1 if monospaced, 0 otherwise
]

[
    'ptr'      => int,   // SDL_Surface pointer
    'flags'    => int,   // surface flags
    'format'   => int,   // pixel format enum
    'w'        => int,   // width in pixels
    'h'        => int,   // height in pixels
    'pitch'    => int,   // bytes per row
    'pixels'   => [
        'ptr'  => int,   // raw pixel buffer pointer
        'data' => int[], // flat array of packed ARGB pixels (row-major)
    ],
    'refcount' => int,
]
bash
pie install php-sdl/sdl3
pie install php-sdl/sdl3-ttf
ini
; /etc/php/8.4/cli/conf.d/30-sdl3ttf.ini
extension=sdl3ttf.so
bash
php examples/proof_of_work.php