1 回答
TA贡献1878条经验 获得超4个赞
间距参数期望表面的宽度 * sizeof(int32) 尽我所能,此代码有效并在每个循环中打印随机像素:
func main() {
window, err := sdl.CreateWindow("test", sdl.WINDOWPOS_UNDEFINED,
sdl.WINDOWPOS_UNDEFINED, 800, 600, sdl.WINDOW_SHOWN)
if err != nil {
log.Fatal(err)
}
defer window.Destroy()
renderer, err := sdl.CreateRenderer(window, -1, 0)
if err != nil {
log.Fatal(err)
}
defer renderer.Destroy()
texture, err := renderer.CreateTexture(sdl.PIXELFORMAT_ARGB8888,
sdl.TEXTUREACCESS_STATIC, 800, 600)
if err != nil {
log.Fatal(err)
}
defer texture.Destroy()
pixels := make([]uint32, 800*600)
L:
for {
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
if _, ok := event.(*sdl.QuitEvent); ok {
break L
}
}
for i := range pixels {
pixels[i] = 0x00777777 + uint32(rand.Intn(0x00AAAAAA))
}
//4 == sizeof(int32), unsafe.Sizeof(pixels) returns the actual size of
//the slice header which is 24
texture.Update(nil, unsafe.Pointer(&pixels[0]), 800*4)
window.UpdateSurface()
renderer.Clear()
renderer.Copy(texture, nil, nil)
renderer.Present()
}
sdl.Delay(1000)
sdl.Quit()
}
//编辑它应该unsafe.Pointer(&pixels[0])不是unsafe.Pointer(&pixels)。
- 1 回答
- 0 关注
- 186 浏览
添加回答
举报