platform and imgui cleanup
This commit is contained in:
parent
1c7fdda65f
commit
e9f5793a5c
@ -15,7 +15,7 @@ type Context struct {
|
||||
bufferSize math.Vec2i
|
||||
}
|
||||
|
||||
func CreateContext(displaySize, bufferSize math.Vec2i) (*Context, error) {
|
||||
func New(displaySize, bufferSize math.Vec2i) (*Context, error) {
|
||||
singleton.refCount++
|
||||
if singleton.refCount == 1 {
|
||||
singleton.context = imgui.CreateContext(nil)
|
||||
|
@ -3,7 +3,6 @@ package platform
|
||||
import (
|
||||
"errors"
|
||||
"runtime"
|
||||
"time"
|
||||
|
||||
"github.com/go-gl/gl/v2.1/gl"
|
||||
"github.com/veandco/go-sdl2/sdl"
|
||||
@ -46,6 +45,27 @@ func Init() error {
|
||||
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 {
|
||||
if !singleton.isInit {
|
||||
return ErrWasNotInit
|
||||
@ -68,37 +88,28 @@ func CreateWindow(title string, width, height int, scene Scene) (*Window, error)
|
||||
return nil, ErrWasNotInit
|
||||
}
|
||||
|
||||
window, err := newWindow(title, width, height, scene)
|
||||
w, err := newWindow(title, width, height, scene)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
appendWindow(w)
|
||||
return w, err
|
||||
}
|
||||
|
||||
func appendWindow(window *Window) {
|
||||
singleton.windows = append(singleton.windows, window)
|
||||
|
||||
return window, err
|
||||
}
|
||||
|
||||
func ProcessEvents() error {
|
||||
if !singleton.isInit {
|
||||
return ErrWasNotInit
|
||||
}
|
||||
|
||||
for running := true; running; {
|
||||
advanceWindows()
|
||||
|
||||
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
|
||||
switch event.(type) {
|
||||
case *sdl.QuitEvent:
|
||||
running = false
|
||||
default:
|
||||
processWindowEvents(event)
|
||||
func removeWindow(window *Window) bool {
|
||||
for i, w := range singleton.windows {
|
||||
if w == window {
|
||||
singleton.windows = append(singleton.windows[:i], singleton.windows[i+1:]...)
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
<-time.After(time.Millisecond * 25)
|
||||
}
|
||||
|
||||
return nil
|
||||
return false
|
||||
}
|
||||
|
||||
func advanceWindows() {
|
||||
@ -107,8 +118,12 @@ func advanceWindows() {
|
||||
}
|
||||
}
|
||||
|
||||
func processWindowEvents(event sdl.Event) {
|
||||
func processWindowEvents(event sdl.Event) error {
|
||||
for _, window := range singleton.windows {
|
||||
window.processEvent(event)
|
||||
if _, err := window.processEvent(event); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@ -1,8 +1,6 @@
|
||||
package platform
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/FooSoft/lazarus/math"
|
||||
"github.com/FooSoft/lazarus/platform/imgui"
|
||||
"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,
|
||||
}
|
||||
|
||||
w.imguiContext, err = imgui_backend.CreateContext(w.DisplaySize(), w.BufferSize())
|
||||
w.imguiContext, err = imgui_backend.New(w.DisplaySize(), w.BufferSize())
|
||||
if err != nil {
|
||||
w.Destroy()
|
||||
return nil, err
|
||||
@ -77,18 +75,7 @@ func (w *Window) Destroy() error {
|
||||
}
|
||||
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:]...)
|
||||
removeWindow(w)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -3,9 +3,9 @@ package main
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
imgui "github.com/FooSoft/imgui-go"
|
||||
"github.com/FooSoft/lazarus/formats/dat"
|
||||
@ -152,11 +152,21 @@ func main() {
|
||||
scene := &scene{sprite: sprite, palette: palette}
|
||||
window, err := platform.CreateWindow("Viewer", 1280, 720, scene)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
defer window.Destroy()
|
||||
|
||||
if err := platform.ProcessEvents(); err != nil {
|
||||
log.Fatal(err)
|
||||
for {
|
||||
run, err := platform.Advance()
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if !run {
|
||||
break
|
||||
}
|
||||
|
||||
<-time.After(time.Millisecond * 25)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user