0
votes

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.

1
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.keltar
Thanks. I'll try that once i get to the lesson that shows me how. I'm still only on lesson 4.Ralph
@keltar: That worked. If you change your comment into an answer, I'll accept it.Ralph

1 Answers

1
votes

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.