platform and imgui cleanup

This commit is contained in:
Alex Yatskov 2018-12-31 13:40:02 -08:00
parent 1c7fdda65f
commit e9f5793a5c
4 changed files with 56 additions and 44 deletions

View File

@ -15,7 +15,7 @@ type Context struct {
bufferSize math.Vec2i bufferSize math.Vec2i
} }
func CreateContext(displaySize, bufferSize math.Vec2i) (*Context, error) { func New(displaySize, bufferSize math.Vec2i) (*Context, error) {
singleton.refCount++ singleton.refCount++
if singleton.refCount == 1 { if singleton.refCount == 1 {
singleton.context = imgui.CreateContext(nil) singleton.context = imgui.CreateContext(nil)

View File

@ -3,7 +3,6 @@ package platform
import ( import (
"errors" "errors"
"runtime" "runtime"
"time"
"github.com/go-gl/gl/v2.1/gl" "github.com/go-gl/gl/v2.1/gl"
"github.com/veandco/go-sdl2/sdl" "github.com/veandco/go-sdl2/sdl"
@ -46,6 +45,27 @@ func Init() error {
return nil return nil
} }
func Advance() (bool, error) {
if !singleton.isInit {
return false, ErrWasNotInit
}
advanceWindows()
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
switch event.(type) {
case *sdl.QuitEvent:
return false, nil
default:
if err := processWindowEvents(event); err != nil {
return false, err
}
}
}
return true, nil
}
func Shutdown() error { func Shutdown() error {
if !singleton.isInit { if !singleton.isInit {
return ErrWasNotInit return ErrWasNotInit
@ -68,37 +88,28 @@ func CreateWindow(title string, width, height int, scene Scene) (*Window, error)
return nil, ErrWasNotInit return nil, ErrWasNotInit
} }
window, err := newWindow(title, width, height, scene) w, err := newWindow(title, width, height, scene)
if err != nil { if err != nil {
return nil, err return nil, err
} }
singleton.windows = append(singleton.windows, window) appendWindow(w)
return w, err
return window, err
} }
func ProcessEvents() error { func appendWindow(window *Window) {
if !singleton.isInit { singleton.windows = append(singleton.windows, window)
return ErrWasNotInit }
}
for running := true; running; { func removeWindow(window *Window) bool {
advanceWindows() for i, w := range singleton.windows {
if w == window {
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() { singleton.windows = append(singleton.windows[:i], singleton.windows[i+1:]...)
switch event.(type) { return true
case *sdl.QuitEvent:
running = false
default:
processWindowEvents(event)
}
} }
<-time.After(time.Millisecond * 25)
} }
return nil return false
} }
func advanceWindows() { func advanceWindows() {
@ -107,8 +118,12 @@ func advanceWindows() {
} }
} }
func processWindowEvents(event sdl.Event) { func processWindowEvents(event sdl.Event) error {
for _, window := range singleton.windows { for _, window := range singleton.windows {
window.processEvent(event) if _, err := window.processEvent(event); err != nil {
return err
}
} }
return nil
} }

View File

@ -1,8 +1,6 @@
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"
@ -45,7 +43,7 @@ func newWindow(title string, width, height int, scene Scene) (*Window, error) {
scene: scene, scene: scene,
} }
w.imguiContext, err = imgui_backend.CreateContext(w.DisplaySize(), w.BufferSize()) w.imguiContext, err = imgui_backend.New(w.DisplaySize(), w.BufferSize())
if err != nil { if err != nil {
w.Destroy() w.Destroy()
return nil, err return nil, err
@ -77,18 +75,7 @@ func (w *Window) Destroy() error {
} }
w.sdlWindow = nil w.sdlWindow = nil
index := -1 removeWindow(w)
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
} }

View File

@ -3,9 +3,9 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"log"
"os" "os"
"path/filepath" "path/filepath"
"time"
imgui "github.com/FooSoft/imgui-go" imgui "github.com/FooSoft/imgui-go"
"github.com/FooSoft/lazarus/formats/dat" "github.com/FooSoft/lazarus/formats/dat"
@ -152,11 +152,21 @@ func main() {
scene := &scene{sprite: sprite, palette: palette} scene := &scene{sprite: sprite, palette: palette}
window, err := platform.CreateWindow("Viewer", 1280, 720, scene) window, err := platform.CreateWindow("Viewer", 1280, 720, scene)
if err != nil { if err != nil {
log.Fatal(err) fmt.Fprintln(os.Stderr, err)
os.Exit(1)
} }
defer window.Destroy() defer window.Destroy()
if err := platform.ProcessEvents(); err != nil { for {
log.Fatal(err) run, err := platform.Advance()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
if !run {
break
}
<-time.After(time.Millisecond * 25)
} }
} }