我在 golang 中使用 sdl 并想从窗口中获取像素数据,但这不起作用

我想使用 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但它不是返回每个像素的实际颜色 ( ),而是一直返回{0 0 0 255} color 0 x 0 y到最后。


如何解决这个问题?或者也许有什么东西可以用来代替surface.At()?


烙印99
浏览 101回答 1
1回答

holdtom

我发现出了什么问题,我实际上不得不对表面进行更改。这是代码:package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "github.com/veandco/go-sdl2/sdl")func main() {&nbsp; &nbsp; var (&nbsp; &nbsp; &nbsp; &nbsp; winTitle string = "Surface At"&nbsp; &nbsp; &nbsp; &nbsp; winWidth, winHeight int32 = 1200, 720&nbsp; &nbsp; &nbsp; &nbsp; window *sdl.Window&nbsp; &nbsp; &nbsp; &nbsp; surface *sdl.Surface&nbsp; &nbsp; )&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; window, _ = sdl.CreateWindow(winTitle, sdl.WINDOWPOS_CENTERED, sdl.WINDOWPOS_CENTERED, winWidth, winHeight, sdl.WINDOW_SHOWN)&nbsp; &nbsp; surface, _ = window.GetSurface()&nbsp; &nbsp; surface.FillRect(&sdl.Rect{50, 50, 600, 300}, 39700) //Change color from x - {50; 600}, y - {50; 300}&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; color1 := surface.At(100, 100)&nbsp; &nbsp; color2 := surface.At(5, 5)&nbsp; &nbsp; fmt.Printf("%v color at %d x, %d y\n", color1, 100, 100)&nbsp; &nbsp; fmt.Printf("%v color at %d x, %d y\n", color2, 5, 5)&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; sdl.Delay(4000)&nbsp; &nbsp; sdl.Quit()}输出是:{20 155 0 255} 颜色在 100 x、100 y{0 0 0 255} 颜色在 5 x, 5 y
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go