veandco / go-sdl2

SDL2 binding for Go
https://godoc.org/github.com/veandco/go-sdl2
BSD 3-Clause "New" or "Revised" License
2.21k stars 221 forks source link

about renderers ReadPixel usage #477

Closed sigzegv closed 3 years ago

sigzegv commented 3 years ago

How am I supposed to use renderer.ReadPIxels pixel parameter ?

I am doing this

renderer.SetRenderTarget(t)
var pixels unsafe.Pointer
renderer.ReadPixels(nil, sdl.PIXELFORMAT_ARGB8888, pixels, int(unsafe.Sizeof(uint32(1)))*int(width))
fmt.Printf("%#v\n", pixels)

but I just get (unsafe.Pointer)(nil) as response. So this mean my pixels array wasn't filled.

I would like to be able to load pixels data and then convert it to either []byte or *uint32.

veeableful commented 3 years ago

Hi @sigzegv, you can pass the address of the first item of an array such as unsafe.Pointer(&pixels[0]). So pixels can be declared like var pixels = make([]byte, width*height*channels).

sigzegv commented 3 years ago

Ty, this works well I am not familiar with unsafe.pointer type, so question now is, am I forced then to use the texture's Update method to reaffect the pixels, or is there a way then to redirect values to the texture directly ?

gen2brain commented 3 years ago

@sigzegv You have an example here https://play.golang.org/p/14JFZvpukGK, ReadPixels is a slow operation.

sigzegv commented 3 years ago

Do you think it's better (and quicker) to maintain a Surface (that will be updated some times) at same time and then push it to a new texture when needed to ?

gen2brain commented 3 years ago

Depends on what you want to do, check migration guide https://wiki.libsdl.org/MigrationGuide, and If your game wants to blit surfaces to the screen/If your game wants to do both, but yes, that is the recommended method for what you want.

sigzegv commented 3 years ago

thank you very much it's clear now.