lazarus/platform/platform.go

73 lines
1.0 KiB
Go
Raw Permalink Normal View History

package platform
import (
"errors"
2019-01-01 01:10:45 +00:00
"log"
2018-12-27 22:35:06 +00:00
"runtime"
2018-12-27 21:59:35 +00:00
"github.com/veandco/go-sdl2/sdl"
)
var (
ErrPlatformNotInit = errors.New("platform is not initialized")
)
var platformState struct {
isInit bool
}
func Initialize() error {
log.Println("platform init")
if platformState.isInit {
return nil
}
runtime.LockOSThread()
if err := sdl.Init(sdl.INIT_VIDEO); err != nil {
return err
}
platformState.isInit = true
return nil
}
func Shutdown() error {
log.Println("platform shutdown")
if !platformState.isInit {
return nil
}
if err := FileUnmountAll(); err != nil {
return err
}
return nil
}
2018-12-31 21:40:02 +00:00
func Advance() (bool, error) {
if !platformState.isInit {
return false, ErrPlatformNotInit
}
2018-12-31 21:40:02 +00:00
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
switch event.(type) {
case *sdl.QuitEvent:
return false, nil
default:
if _, err := windowProcessEvent(event); err != nil {
2018-12-31 21:40:02 +00:00
return false, err
}
}
}
run, err := windowAdvance()
if !run {
WindowDestroy()
2018-12-27 22:35:06 +00:00
}
return run, err
2018-12-27 22:35:06 +00:00
}