Merge pull request #3 from jackwakefield/master

Added a size change event handler
This commit is contained in:
Thomas Friedel 2018-05-17 15:06:26 +02:00 committed by GitHub
commit 4e1004ec57
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 0 deletions

View file

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

View file

@ -19,6 +19,7 @@ import (
// functions can be set for callbacks // functions can be set for callbacks
type Window struct { type Window struct {
Window *sdl.Window Window *sdl.Window
WindowID uint32
GLContext sdl.GLContext GLContext sdl.GLContext
frameTimes [10]time.Time frameTimes [10]time.Time
frameIndex int frameIndex int
@ -33,6 +34,7 @@ type Window struct {
KeyDown func(scancode int, rn rune, name string) KeyDown func(scancode int, rn rune, name string)
KeyUp func(scancode int, rn rune, name string) KeyUp func(scancode int, rn rune, name string)
KeyChar func(rn rune) KeyChar func(rn rune)
SizeChange func(w, h int)
} }
// CreateWindow creates a window using SDL and initializes the OpenGL context // 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) 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 // create GL context
glContext, err := window.GLCreateContext() 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) cv := canvas.New(0, 0, w, h)
wnd := &Window{ wnd := &Window{
Window: window, Window: window,
WindowID: windowID,
GLContext: glContext, GLContext: glContext,
events: make([]sdl.Event, 0, 100), events: make([]sdl.Event, 0, 100),
} }
@ -163,6 +170,15 @@ func (wnd *Window) StartFrame() error {
wnd.KeyChar(rn) wnd.KeyChar(rn)
handled = true 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 { if !handled && wnd.Event != nil {