added a size change event handler

This commit is contained in:
Jack Wakefield 2018-05-16 00:11:31 +01:00
parent fec30a0848
commit 27f755f171
2 changed files with 20 additions and 0 deletions

View file

@ -27,6 +27,10 @@ func main() {
rg.AddColorStop(1, "#00ff00")
rg.AddColorStop(0.5, "#0000ff")
wnd.SizeChange = func(w, h int) {
cv.SetSize(w, h)
}
wnd.MainLoop(func() {
w, h := float64(cv.Width()), float64(cv.Height())

View file

@ -19,6 +19,7 @@ import (
// functions can be set for callbacks
type Window struct {
Window *sdl.Window
WindowID uint32
GLContext sdl.GLContext
frameTimes [10]time.Time
frameIndex int
@ -33,6 +34,7 @@ type Window struct {
KeyDown func(scancode int, rn rune, name string)
KeyUp func(scancode int, rn rune, name string)
KeyChar func(rn rune)
SizeChange func(w, h int)
}
// CreateWindow creates a window using SDL and initializes the OpenGL context
@ -65,6 +67,10 @@ func CreateWindow(w, h int, title string) (*Window, *canvas.Canvas, error) {
return nil, nil, fmt.Errorf("Error creating window: %v", err)
}
}
windowID, err := window.GetID()
if err != nil {
return nil, nil, fmt.Errorf("Error getting window ID: %v", err)
}
// create GL context
glContext, err := window.GLCreateContext()
@ -89,6 +95,7 @@ func CreateWindow(w, h int, title string) (*Window, *canvas.Canvas, error) {
cv := canvas.New(0, 0, w, h)
wnd := &Window{
Window: window,
WindowID: windowID,
GLContext: glContext,
events: make([]sdl.Event, 0, 100),
}
@ -163,6 +170,15 @@ func (wnd *Window) StartFrame() error {
wnd.KeyChar(rn)
handled = true
}
case *sdl.WindowEvent:
if e.WindowID == wnd.WindowID {
if e.Event == sdl.WINDOWEVENT_SIZE_CHANGED {
if wnd.SizeChange != nil {
wnd.SizeChange(int(e.Data1), int(e.Data2))
handled = true
}
}
}
}
if !handled && wnd.Event != nil {