platform cleanup

This commit is contained in:
Alex Yatskov 2018-12-31 13:28:50 -08:00
parent 5eeb5b6b3e
commit 1c7fdda65f
2 changed files with 32 additions and 26 deletions

View File

@ -14,7 +14,7 @@ var (
ErrWasNotInit = errors.New("platform was not initialized") ErrWasNotInit = errors.New("platform was not initialized")
) )
var state struct { var singleton struct {
isInit bool isInit bool
windows []*Window windows []*Window
} }
@ -28,7 +28,7 @@ type Scene interface {
} }
func Init() error { func Init() error {
if state.isInit { if singleton.isInit {
return ErrAlreadyInit return ErrAlreadyInit
} }
@ -42,29 +42,29 @@ func Init() error {
return err return err
} }
state.isInit = true singleton.isInit = true
return nil return nil
} }
func Shutdown() error { func Shutdown() error {
if !state.isInit { if !singleton.isInit {
return ErrWasNotInit return ErrWasNotInit
} }
for _, w := range state.windows { for _, w := range singleton.windows {
if err := w.Destroy(); err != nil { if err := w.Destroy(); err != nil {
return err return err
} }
} }
state.windows = nil singleton.windows = nil
state.isInit = false singleton.isInit = false
return nil return nil
} }
func CreateWindow(title string, width, height int, scene Scene) (*Window, error) { func CreateWindow(title string, width, height int, scene Scene) (*Window, error) {
if !state.isInit { if !singleton.isInit {
return nil, ErrWasNotInit return nil, ErrWasNotInit
} }
@ -73,13 +73,13 @@ func CreateWindow(title string, width, height int, scene Scene) (*Window, error)
return nil, err return nil, err
} }
state.windows = append(state.windows, window) singleton.windows = append(singleton.windows, window)
return window, err return window, err
} }
func ProcessEvents() error { func ProcessEvents() error {
if !state.isInit { if !singleton.isInit {
return ErrWasNotInit return ErrWasNotInit
} }
@ -87,9 +87,11 @@ func ProcessEvents() error {
advanceWindows() advanceWindows()
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() { for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
if !processEvent(event) { switch event.(type) {
case *sdl.QuitEvent:
running = false running = false
break default:
processWindowEvents(event)
} }
} }
@ -100,23 +102,13 @@ func ProcessEvents() error {
} }
func advanceWindows() { func advanceWindows() {
for _, window := range state.windows { for _, window := range singleton.windows {
window.advance() window.advance()
} }
} }
func processEvent(event sdl.Event) bool { func processWindowEvents(event sdl.Event) {
for _, window := range state.windows { for _, window := range singleton.windows {
handled, _ := window.processEvent(event) window.processEvent(event)
if handled {
return true
}
}
switch event.(type) {
case *sdl.QuitEvent:
return false
default:
return true
} }
} }

View File

@ -1,6 +1,8 @@
package platform package platform
import ( import (
"errors"
"github.com/FooSoft/lazarus/math" "github.com/FooSoft/lazarus/math"
"github.com/FooSoft/lazarus/platform/imgui" "github.com/FooSoft/lazarus/platform/imgui"
"github.com/go-gl/gl/v2.1/gl" "github.com/go-gl/gl/v2.1/gl"
@ -75,6 +77,18 @@ func (w *Window) Destroy() error {
} }
w.sdlWindow = nil w.sdlWindow = nil
index := -1
for i, window := range singleton.windows {
if w == window {
index = i
}
}
if index < 0 {
return errors.New("platform does not contain window to destroy")
}
singleton.windows = append(singleton.windows[:index], singleton.windows[index+1:]...)
return nil return nil
} }