change platform to be a singleton
This commit is contained in:
parent
c045e345d2
commit
0089093c7f
@ -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
|
||||
|
||||
type platform struct {
|
||||
windows []Window
|
||||
}
|
||||
|
||||
func New() (*Platform, error) {
|
||||
if !globalPlatformInit {
|
||||
if err := sdl.Init(sdl.INIT_VIDEO); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := gl.Init(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
globalPlatformInit = true
|
||||
func Init() error {
|
||||
if platformIsInit {
|
||||
return errors.New("platform is already initialized")
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
if err := sdl.Init(sdl.INIT_VIDEO); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := gl.Init(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user