How can i read the RGB values of a pixel from a specified x y position on sdl_surface with Pascal SDL2? I tried finding a solution to this already and found nothing that worked.
I tried
function get_pixel32(surface: psdl_surface; location: vector2): uInt32;
var pixels: ^uInt32;
begin
if sdl_mustLock(surface) then sdl_lockSurface(surface);
pixels^:= uInt32(surface^.pixels);
get_pixel32:= pixels[(location.y * surface^.w) + location.x];
sdl_unlockSurface(surface);
end;
begin
pD:= get_pixel32(surface1, vector2.new(1, 1));
sdl_getRGBA(pD, surface1^.format, @r, @g, @b, @a);
end.
but that returned me random colors in a non random pattern (black, random dark color, random bright color, random dark color, black etc...) when i looped through 32 pixels on the X coordinate in the surface.
Values of local variables are random (garbage) and should be initialized. pixels
variable is a pointer. In the assignment pixels^:= uInt32(surface^.pixels);
you do not initialize the variable, but write data to some random memory location.
The correct initialization is
pixels := PuInt32(surface^.pixels);