lazarus/platform/platform.go

73 lines
1.0 KiB
Go
Raw Normal View History

package platform
import (
"errors"
2018-12-31 17:10:45 -08:00
"log"
2018-12-27 14:35:06 -08:00
"runtime"
2018-12-27 13:59:35 -08: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 13:40:02 -08:00
func Advance() (bool, error) {
if !platformState.isInit {
return false, ErrPlatformNotInit
}
2018-12-31 13:40:02 -08: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 13:40:02 -08:00
return false, err
}
}
}
run, err := windowAdvance()
if !run {
WindowDestroy()
2018-12-27 14:35:06 -08:00
}
return run, err
2018-12-27 14:35:06 -08:00
}