go-sdl2-examples/examples/events/WaitEvent.go

66 lines
1.7 KiB
Go

// This program creates an SDL window and renderer and takes in events using sdl.WaitEvent
package main
import (
"fmt"
"os"
"github.com/veandco/go-sdl2/sdl"
)
var winTitle string = "Go-SDL2 TestWaitEvent"
var winWidth, winHeight int32 = 800, 600
func run() int {
var window *sdl.Window
var renderer *sdl.Renderer
var event sdl.Event
var running bool
var err error
sdl.Init(sdl.INIT_EVERYTHING)
defer sdl.Quit()
window, err = sdl.CreateWindow(winTitle, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED,
winWidth, winHeight, sdl.WINDOW_SHOWN)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create window: %s\n", err)
return 1
}
defer window.Destroy()
renderer, err = sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create renderer: %s\n", err)
return 2
}
defer renderer.Destroy()
running = true
for running {
event = sdl.WaitEvent() // wait here until an event is in the event queue
switch t := event.(type) {
case sdl.QuitEvent:
running = false
case sdl.MouseMotionEvent:
fmt.Printf("[%d ms] MouseMotion\ttype:%d\tid:%d\tx:%d\ty:%d\txrel:%d\tyrel:%d\n",
t.Timestamp, t.Type, t.Which, t.X, t.Y, t.XRel, t.YRel)
case sdl.MouseButtonEvent:
fmt.Printf("[%d ms] MouseButton\ttype:%d\tid:%d\tx:%d\ty:%d\tbutton:%d\tstate:%d\n",
t.Timestamp, t.Type, t.Which, t.X, t.Y, t.Button, t.State)
case sdl.MouseWheelEvent:
fmt.Printf("[%d ms] MouseWheel\ttype:%d\tid:%d\tx:%d\ty:%d\n",
t.Timestamp, t.Type, t.Which, t.X, t.Y)
case sdl.KeyboardEvent:
fmt.Printf("[%d ms] Keyboard\ttype:%d\tsym:%c\tmodifiers:%d\tstate:%d\trepeat:%d\n",
t.Timestamp, t.Type, t.Keysym.Sym, t.Keysym.Mod, t.State, t.Repeat)
}
}
return 0
}
func main() {
os.Exit(run())
}