我在 golang 中使用 sdl 并想从 window 获取像素数据,但这不起作用
I am using sdl in golang and want to get pixel data from window, but that doesn't work
我想使用 surface.At()
获取像素数据
package main
import (
"fmt"
"github.com/veandco/go-sdl2/sdl"
)
func main() {
var (
winTitle string = "Surface At"
winWidth, winHeight int32 = 1200, 720
window *sdl.Window
renderer *sdl.Renderer
surface *sdl.Surface
)
window, _ = sdl.CreateWindow(winTitle, sdl.WINDOWPOS_CENTERED, sdl.WINDOWPOS_CENTERED, winWidth, winHeight, sdl.WINDOW_SHOWN)
renderer, _ = sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED)
renderer.SetDrawColor(35, 80, 0, 180)
renderer.Clear()
renderer.Present()
surface, _ = window.GetSurface()
for x := 0; x < int(winWidth); x++ {
for y := 0; y < int(winHeight); y++ {
color := surface.At(x, y)
fmt.Println(color, "color", x, "x", y, "y")
}
}
sdl.Delay(1000)
sdl.Quit()
}
但是它没有返回每个像素的实际颜色 ({35, 80, 0, 180} color 0 x 0 y
),而是 returns {0 0 0 255} color 0 x 0 y
一直到最后。
如何解决这个问题?
或者也许有一些东西,我可以用它来代替 surface.At()?
我发现了问题所在,我实际上必须对表面进行更改。这是代码:
package main
import (
"fmt"
"github.com/veandco/go-sdl2/sdl"
)
func main() {
var (
winTitle string = "Surface At"
winWidth, winHeight int32 = 1200, 720
window *sdl.Window
surface *sdl.Surface
)
window, _ = sdl.CreateWindow(winTitle, sdl.WINDOWPOS_CENTERED, sdl.WINDOWPOS_CENTERED, winWidth, winHeight, sdl.WINDOW_SHOWN)
surface, _ = window.GetSurface()
surface.FillRect(&sdl.Rect{50, 50, 600, 300}, 39700) //Change color from x - {50; 600}, y - {50; 300}
color1 := surface.At(100, 100)
color2 := surface.At(5, 5)
fmt.Printf("%v color at %d x, %d y\n", color1, 100, 100)
fmt.Printf("%v color at %d x, %d y\n", color2, 5, 5)
sdl.Delay(4000)
sdl.Quit()
}
输出为:
{20 155 0 255} color at 100 x, 100 y
{0 0 0 255} color at 5 x, 5 y
我想使用 surface.At()
获取像素数据package main
import (
"fmt"
"github.com/veandco/go-sdl2/sdl"
)
func main() {
var (
winTitle string = "Surface At"
winWidth, winHeight int32 = 1200, 720
window *sdl.Window
renderer *sdl.Renderer
surface *sdl.Surface
)
window, _ = sdl.CreateWindow(winTitle, sdl.WINDOWPOS_CENTERED, sdl.WINDOWPOS_CENTERED, winWidth, winHeight, sdl.WINDOW_SHOWN)
renderer, _ = sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED)
renderer.SetDrawColor(35, 80, 0, 180)
renderer.Clear()
renderer.Present()
surface, _ = window.GetSurface()
for x := 0; x < int(winWidth); x++ {
for y := 0; y < int(winHeight); y++ {
color := surface.At(x, y)
fmt.Println(color, "color", x, "x", y, "y")
}
}
sdl.Delay(1000)
sdl.Quit()
}
但是它没有返回每个像素的实际颜色 ({35, 80, 0, 180} color 0 x 0 y
),而是 returns {0 0 0 255} color 0 x 0 y
一直到最后。
如何解决这个问题? 或者也许有一些东西,我可以用它来代替 surface.At()?
我发现了问题所在,我实际上必须对表面进行更改。这是代码:
package main
import (
"fmt"
"github.com/veandco/go-sdl2/sdl"
)
func main() {
var (
winTitle string = "Surface At"
winWidth, winHeight int32 = 1200, 720
window *sdl.Window
surface *sdl.Surface
)
window, _ = sdl.CreateWindow(winTitle, sdl.WINDOWPOS_CENTERED, sdl.WINDOWPOS_CENTERED, winWidth, winHeight, sdl.WINDOW_SHOWN)
surface, _ = window.GetSurface()
surface.FillRect(&sdl.Rect{50, 50, 600, 300}, 39700) //Change color from x - {50; 600}, y - {50; 300}
color1 := surface.At(100, 100)
color2 := surface.At(5, 5)
fmt.Printf("%v color at %d x, %d y\n", color1, 100, 100)
fmt.Printf("%v color at %d x, %d y\n", color2, 5, 5)
sdl.Delay(4000)
sdl.Quit()
}
输出为:
{20 155 0 255} color at 100 x, 100 y
{0 0 0 255} color at 5 x, 5 y