change platform to be a singleton

This commit is contained in:
Alex Yatskov 2018-12-27 13:59:35 -08:00
parent c045e345d2
commit 0089093c7f

View File

@ -1,38 +1,52 @@
package platform
import (
"errors"
"github.com/FooSoft/imgui-go"
"github.com/go-gl/gl/v2.1/gl"
"github.com/veandco/go-sdl2/sdl"
)
type Platform interface {
CreateWindow(title string, width, height int) (Window, error)
Destroy() error
}
var (
platformIsInit bool
platformImguiContext *imgui.Context
platformWindows []Window
)
var globalPlatformInit bool
func Init() error {
if platformIsInit {
return errors.New("platform is already initialized")
}
type platform struct {
windows []Window
}
func New() (*Platform, error) {
if !globalPlatformInit {
if err := sdl.Init(sdl.INIT_VIDEO); err != nil {
return nil, err
return err
}
if err := gl.Init(); err != nil {
return nil, err
return err
}
globalPlatformInit = true
}
return nil, nil
platformImguiContext = imgui.CreateContext(nil)
return nil
}
func (p *platform) CreateWindow(title string, width, height int) (Window, error) {
func Shutdown() error {
if !platformIsInit {
return errors.New("platform is not yet initialized")
}
for _, w := range platformWindows {
if err := w.Destroy(); err != nil {
return err
}
}
platformWindows = nil
return nil
}
func CreateWindow(title string, width, height int) (Window, error) {
window, err := newWindow(title, width, height)
if err != nil {
return nil, err
@ -40,14 +54,3 @@ func (p *platform) CreateWindow(title string, width, height int) (Window, error)
return window, err
}
func (p *platform) Destroy() error {
for _, w := range p.windows {
if err := w.Destroy(); err != nil {
return err
}
}
p.windows = nil
return nil
}