SerafimArts / ffi-sdl-ttf

SDL2 TTF FFI bindings for the PHP language
MIT License
2 stars 1 forks source link

Passing incompatible argument 2 of C function 'SDL_CreateTextureFromSurface', expecting 'struct SDL_Surface*', found 'struct SDL_Surface*' #2

Open he426100 opened 10 months ago

he426100 commented 10 months ago

define('ROOT_DIR', DIR . '/..');

require ROOT_DIR . '/vendor/autoload.php';

use Serafim\SDL\SDL; use Serafim\SDL\TTF\TTF; use Serafim\SDL\Event\Type;

$sdl = new SDL(library: ROOT_DIR . '/lib/SDL2.dll'); $ttf = new TTF(sdl: $sdl, library: ROOT_DIR . '/lib/SDL2_ttf.dll');

$sdl->SDL_Init(SDL::SDL_INIT_EVERYTHING); $ttf->TTF_Init();

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

$renderer = $sdl->SDL_CreateRenderer($window, -1, 0); if (!$renderer) { throw new \Exception("Can't create renderer!"); }

$font = $ttf->TTF_OpenFont(ROOT_DIR . '/font/arial.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!', FFI::addr($color)); $texture = $sdl->SDL_CreateTextureFromSurface($renderer, $surface);

$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_FreeSurface($surface); $sdl->SDL_DestroyTexture($texture); $sdl->SDL_DestroyWindow($window); $ttf->TTF_Quit(); $sdl->SDL_Quit();


- error

Hinting: 0 Kerning: 1 Style: normal

Fatal error: Uncaught FFI\Exception: Passing incompatible argument 2 of C function 'SDL_CreateTextureFromSurface', expecting 'struct SDL_Surface', found 'struct SDL_Surface' in E:\Git\ffi-sdl-test\vendor\ffi\proxy\src\ProxyAwareTrait.php:21 Stack trace:

0 E:\Git\ffi-sdl-test\vendor\ffi\proxy\src\ProxyAwareTrait.php(21): FFI->SDL_CreateTextureFromSurface(Object(FFI\CData:struct SDL_Renderer), Object(FFI\CData:struct SDL_Surface))

1 E:\Git\ffi-sdl-test\src\ttf.php(54): FFI\Proxy\Proxy->__call('SDL_CreateTextu...', Array)

2 {main}

thrown in E:\Git\ffi-sdl-test\vendor\ffi\proxy\src\ProxyAwareTrait.php on line 21

SerafimArts commented 10 months ago

This is strange, since the result of the method is converted to an SDL-compatible type https://github.com/SerafimArts/ffi-sdl-ttf/blob/master/src/Marshaller.php#L83

I can not solve the problem right away, I'll try to figure it out a little later. Thank you very much for the example code to reproduce the error.

SerafimArts commented 10 months ago

Ah, I think I understand! In return expressions i'll should do $this->sdl->cast() instead of $this->cast(). Whoops)

he426100 commented 10 months ago

Solved, thank you very much!

SerafimArts commented 10 months ago

Hmm... it seems this still needs to be fixed by the library so that all return types are compatible with SDL (instead of TTF). Is not it so?

he426100 commented 10 months ago

I'm sorry, I don't understand these, just modify as you said, the problem solved

SerafimArts commented 10 months ago

Kk. I will re-open the issue so as not to lose it and correct this behavior on the part of the TTF library in next minor patch.