2018-01-17 19:51:32 -06:00
|
|
|
// This program creates an SDL window and renderer and pushes events with PushEvent to be read by PollEvents
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2018-08-08 18:59:37 -05:00
|
|
|
|
|
|
|
"github.com/veandco/go-sdl2/sdl"
|
2018-01-17 19:51:32 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
var winTitle string = "Go-SDL2 TestWaitEvent"
|
2018-08-08 18:59:37 -05:00
|
|
|
var winWidth, winHeight int32 = 800, 600
|
2018-01-17 19:51:32 -06:00
|
|
|
|
|
|
|
const pushTime uint32 = 1000 // number of milliseconds between event pushes
|
|
|
|
|
|
|
|
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
|
|
|
|
lastPushTime := sdl.GetTicks()
|
|
|
|
for running {
|
|
|
|
|
|
|
|
// Push a UserEvent every second
|
|
|
|
if lastPushTime+pushTime < sdl.GetTicks() {
|
|
|
|
lastPushTime = sdl.GetTicks()
|
2018-08-08 18:59:37 -05:00
|
|
|
id, err := window.GetID()
|
|
|
|
if err != nil {
|
|
|
|
return 3
|
|
|
|
}
|
|
|
|
pEvent := &sdl.UserEvent{sdl.USEREVENT, sdl.GetTicks(), id, 1331, nil, nil}
|
2018-01-17 19:51:32 -06:00
|
|
|
|
|
|
|
retVal, err := sdl.PushEvent(pEvent) // Here's where the event is actually pushed
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Failed to push event: %s\n", err)
|
2018-08-08 18:59:37 -05:00
|
|
|
return 4
|
2018-01-17 19:51:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if retVal {
|
|
|
|
fmt.Println("PushEvent returned success")
|
|
|
|
} else {
|
|
|
|
fmt.Println("PushEvent returned filtered")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for event = sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
|
|
|
|
switch t := event.(type) {
|
2023-11-30 00:59:58 -06:00
|
|
|
case sdl.QuitEvent:
|
2018-01-17 19:51:32 -06:00
|
|
|
running = false
|
2023-11-30 00:59:58 -06:00
|
|
|
case sdl.MouseMotionEvent:
|
2018-01-17 19:51:32 -06:00
|
|
|
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)
|
2023-11-30 00:59:58 -06:00
|
|
|
case sdl.MouseButtonEvent:
|
2018-01-17 19:51:32 -06:00
|
|
|
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)
|
2023-11-30 00:59:58 -06:00
|
|
|
case sdl.MouseWheelEvent:
|
2018-01-17 19:51:32 -06:00
|
|
|
fmt.Printf("[%d ms] MouseWheel\ttype:%d\tid:%d\tx:%d\ty:%d\n",
|
|
|
|
t.Timestamp, t.Type, t.Which, t.X, t.Y)
|
2023-11-30 00:59:58 -06:00
|
|
|
case sdl.KeyboardEvent:
|
2018-01-17 19:51:32 -06:00
|
|
|
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)
|
2023-11-30 00:59:58 -06:00
|
|
|
case sdl.UserEvent:
|
2018-01-17 19:51:32 -06:00
|
|
|
fmt.Printf("[%d ms] UserEvent\tcode:%d\n", t.Timestamp, t.Code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sdl.Delay(1000 / 30)
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
os.Exit(run())
|
|
|
|
}
|