douxihui8270
2018-03-28 18:51
浏览 127
已采纳

第一次运行后,go-sdl2不清除窗口表面

I just started going through the SDL tutorial lessons at http://lazyfoo.net/tutorials/SDL/index.php and am working on the first lesson (Lesson 01). I am hand-translating the C/C++ code to Go using the github.com/veandco/go-sdl2 library.

This is my code:

package main

import (
    "github.com/veandco/go-sdl2/sdl"
)

const screenWidth, screenHeight = 800, 600

func main() {
    defer sdl.Quit()
    if err := sdl.Init(sdl.INIT_VIDEO); err != nil {
        panic(err)
    }

    window, err := sdl.CreateWindow("SDL Tutorial",
        int32(sdl.WINDOWPOS_UNDEFINED), int32(sdl.WINDOWPOS_UNDEFINED),
        screenWidth, screenHeight, uint32(sdl.WINDOW_SHOWN))
    if err != nil {
        panic(err)
    }
    defer window.Destroy()

    screenSurface, err := window.GetSurface()
    if err != nil {
        panic(err)
    }

    if err = screenSurface.FillRect(nil,
        sdl.MapRGB(screenSurface.Format, 0xff, 0xff, 0xff)); err != nil {
        panic(err)
    }

    if err = window.UpdateSurface(); err != nil {
        panic(err)
    }

    sdl.Delay(2000)
}

The first time it ran, it drew a blank (white background) window, but every time after that, it has drawn a transparent window, showing the desktop windows that are running behind it.

I'm not sure what I am doing wrong.

I am running Go 1.10 on the KDE window manager on Ubuntu 17.10, if it makes any difference.

UPDATE

The problem is not occurring when I run XFCE instead of KDE.

图片转代码服务由CSDN问答提供 功能建议

我刚刚在 http://lazyfoo.net/tutorials/SDL/index.php ,并且正在第一堂课(第01课)。 我正在使用 github.com/veandco/go-sdl2 库将C / C ++代码手动转换为Go。

这是我的代码:

 包main 
 
import(
“ github.com/veandco/go-sdl2/sdl"
)

const screenWidth,screenHeight = 800,600 
  
func main(){
如果错误,则延迟sdl.Quit()
:= sdl.Init(sdl.INIT_VIDEO);  err!= nil {
 panic(err)
} 
 
 window,err:= sdl.CreateWindow(“ SDL Tutorial”,
 int32(sdl.WINDOWPOS_UNDEFINED),int32(sdl.WINDOWPOS_UNDEFINED),
  screenWidth,screenHeight,uint32(sdl.WINDOW_SHOWN))
如果err!= nil {
 panic(err)
} 
延迟window.Destroy()
 
 screenSurface,err:= window.GetSurface()  
 if err!= nil {
 panic(err)
} 
 
 if err = screenSurface.FillRect(nil,
 sdl.MapRGB(screenSurface.Format,0xff,0xff,0xff));  err!= nil {
 panic(err)
} 
 
如果err = window.UpdateSurface();  err!= nil {
 panic(err)
} 
 
 sdl.Delay(2000)
} 
   
 
 

第一次运行, 它绘制了一个空白的窗口(白色背景),但是之后每次绘制一个透明窗口,显示在其后面运行的桌面窗口。

我不确定 我做错了。

我在Ubuntu 17.10的KDE窗口管理器上运行Go 1.10(如果有任何区别)。

UPDATE

当我运行XFCE而不是KDE时,不会出现问题

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongxing2302 2018-03-29 13:59
    已采纳

    Add an event loop and redraw either unconditionally or when asked to by window manager (SDL_WINDOWEVENT). You can't just draw once and expect things to stay on screen, or even ever be visible (e.g. your draw happened before window manager was ready to show your window), especially with compositing window managers.

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题