PascalGameDevelopment / SDL2-for-Pascal

Unit files for building Free Pascal and Delphi applications using the SDL2 library
https://pascalgamedevelopment.github.io/SDL2-for-Pascal/
Mozilla Public License 2.0
103 stars 20 forks source link

SDL_LockTexture problem #145

Closed MKGilby closed 3 months ago

MKGilby commented 3 months ago

Hello folks!

I have a problem using SDL_LockTexture.

If I try to use it as it is declared in sdlrenderer.inc, it generates an Access Violation:

function SDL_LockTexture(texture: PSDL_Texture; const rect: PSDL_Rect; pixels: PPointer; pitch: pcint): cint; cdecl;
     external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_LockTexture' {$ENDIF} {$ENDIF};

But If I change the declaration to the following (and change call parameters accordingly), it works flawlessly:

function SDL_LockTexture(texture: PSDL_Texture; const rect: PSDL_Rect; out pixels: Pointer; out pitch: cint): cint; cdecl;
     external SDL_LibName {$IFDEF DELPHI} {$IFDEF MACOS} name '_SDL_LockTexture' {$ENDIF} {$ENDIF};

Am I doing something wrong?

Attached a simple Lazarus test project. SimpleStreamingTextureTest.zip

Thanks, Gilby

MKGilby commented 3 months ago

I got the solution, need a new(p) and a new(pitch) before updatetexture (and disposes too, and one more ^ after p in move).

suve commented 3 months ago

Yes, the pointers passed to SDL_LockTexture need to point to valid memory. So you need to either create pointer vars and call new/dispose, or you can create non-pointer vars and pass their addresses using the @ operator.