You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
2.2 KiB
PHP

<?php
namespace Cijber\GraphicsToolkit;
use FFI;
class Utils {
static $ffi;
private static ?FFI\CData $null;
static function ffi(): FFI {
if (static::$ffi === null) {
static::$ffi = FFI::cdef("extern size_t malloc (size_t);");
}
return static::$ffi;
}
static function arrayOf($type, $items): FFI\CData {
$ffi = static::ffi();
$items = array_values($items);
if (is_string($type)) {
$type = $ffi->type($type);
}
$arrType = FFI::arrayType($type, [count($items)]);
$arr = $ffi->new($arrType);
for ($i = 0; $i < count($items); $i++) {
$arr[$i] = $items[$i];
}
return $arr;
}
public static function string(string $input, bool $owned = true): FFI\CData {
$ffi = static::ffi();
$data = $ffi->new('char[' . strlen($input) . ']', $owned);
FFI::memcpy($data, $input, strlen($input));
return $data;
}
public static function constString(string $input): FFI\CData {
$ffi = static::ffi();
$data = $ffi->new('char[' . strlen($input) . ']', false);
for ($i = 0; $i < strlen($input); $i++) {
$data[$i] = $input[$i];
}
return $ffi->cast('const char*', $data);
}
public static function sizeOf($type): int {
$ffi = static::ffi();
if (is_string($type)) {
$type = $ffi->type($type);
}
return FFI::sizeof($type);
}
public static function ptr(int $offset): FFI\CData {
$val = GL::$ffi->new("long long", false);
$val->cdata = $offset;
return GL::$ffi->cast('void*', $val);
}
public static function null(): FFI\CData {
if ( ! isset(static::$null)) {
static::$null = self::ptr(0);
}
return static::$null;
}
public static function pointer(&$data = null, $type = null): FFI\CData {
if ($type === null) {
$type = "void*";
}
$data = self::$ffi->new($type);
return FFI::addr($data);
}
public static function stringBuffer(&$infoLog, int $bufSize = 4096) {
return $infoLog = self::$ffi->new('char[' . $bufSize . ']');
}
}