go-sdl2: Window Doesn't Respond Sometimes

I buillt a small 2D game. However, the window becomes non-responsive to any events. The strange part is it works sometimes and doesn’t most of the time. I am on a OSX Yoesmite and using go-1.7.

Below is the main function

func main() {
	/* Initilize SDL with all its subsystems.
	create a Game instance if Intilization is a success */
	err := sdl.Init(sdl.INIT_EVERYTHING)
	if err != nil {
		panic(err)
	}

	g := new(Game)
	g.init("Flappy")
	g.drawTitle("Flappy Bird")
	g.clear()

	var event sdl.Event

	go g.play() //  Keep moving the background

	running := true
	fmt.Println("Waiting on events")
	for running {
		for event = sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
			switch event.(type) {
			case *sdl.QuitEvent:
				running = false
				break
			case *sdl.KeyDownEvent:
				g.handleKeyEvent(event.(*sdl.KeyDownEvent))
			}
		}
	}
	quito()
}

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 21 (9 by maintainers)

Most upvoted comments

Hi @Grandzam, sorry for late reply!

I experience the same result on my Windows 10 virtual machine. I find that locking the OS thread through runtime.LockOSThread() “fixes”. I’m guessing anything that does rendering needs to have it OS thread locked according to the SDL FAQ: https://wiki.libsdl.org/FAQDevelopment. Let me know if it solves your issue.