change platform to be a singleton
This commit is contained in:
parent
c045e345d2
commit
0089093c7f
@ -1,38 +1,52 @@
|
|||||||
package platform
|
package platform
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
|
|
||||||
|
"github.com/FooSoft/imgui-go"
|
||||||
"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"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Platform interface {
|
var (
|
||||||
CreateWindow(title string, width, height int) (Window, error)
|
platformIsInit bool
|
||||||
Destroy() error
|
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 {
|
if err := sdl.Init(sdl.INIT_VIDEO); err != nil {
|
||||||
return nil, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := gl.Init(); err != nil {
|
if err := gl.Init(); err != nil {
|
||||||
return nil, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
globalPlatformInit = true
|
platformImguiContext = imgui.CreateContext(nil)
|
||||||
}
|
return nil
|
||||||
|
|
||||||
return nil, 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)
|
window, err := newWindow(title, width, height)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -40,14 +54,3 @@ func (p *platform) CreateWindow(title string, width, height int) (Window, error)
|
|||||||
|
|
||||||
return window, err
|
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