2018-12-27 21:36:34 +00:00
|
|
|
package platform
|
|
|
|
|
|
|
|
import (
|
2018-12-27 21:59:35 +00:00
|
|
|
"errors"
|
2018-12-27 22:35:06 +00:00
|
|
|
"runtime"
|
|
|
|
"time"
|
2018-12-27 21:59:35 +00:00
|
|
|
|
2018-12-28 02:36:28 +00:00
|
|
|
"github.com/FooSoft/lazarus/platform/imgui_backend"
|
2018-12-27 21:36:34 +00:00
|
|
|
"github.com/go-gl/gl/v2.1/gl"
|
|
|
|
"github.com/veandco/go-sdl2/sdl"
|
|
|
|
)
|
|
|
|
|
2018-12-28 02:36:28 +00:00
|
|
|
var (
|
|
|
|
ErrAlreadyInit = errors.New("platform is already initialized")
|
|
|
|
ErrWasNotInit = errors.New("platform was not initialized")
|
|
|
|
)
|
|
|
|
|
2018-12-28 03:28:33 +00:00
|
|
|
var state struct {
|
|
|
|
isInit bool
|
2018-12-29 19:52:30 +00:00
|
|
|
windows []*Window
|
2018-12-28 17:26:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Scene interface {
|
2018-12-29 19:52:30 +00:00
|
|
|
Init(window *Window) error
|
|
|
|
Advance(window *Window) error
|
|
|
|
Shutdown(window *Window) error
|
2018-12-28 03:28:33 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 21:59:35 +00:00
|
|
|
func Init() error {
|
2018-12-28 03:28:33 +00:00
|
|
|
if state.isInit {
|
2018-12-28 02:36:28 +00:00
|
|
|
return ErrAlreadyInit
|
2018-12-27 21:59:35 +00:00
|
|
|
}
|
|
|
|
|
2018-12-27 22:35:06 +00:00
|
|
|
runtime.LockOSThread()
|
|
|
|
|
2018-12-27 21:59:35 +00:00
|
|
|
if err := sdl.Init(sdl.INIT_VIDEO); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-27 21:36:34 +00:00
|
|
|
|
2018-12-27 21:59:35 +00:00
|
|
|
if err := gl.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-12-27 21:36:34 +00:00
|
|
|
|
2018-12-28 02:36:28 +00:00
|
|
|
if err := imgui_backend.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-28 03:28:33 +00:00
|
|
|
state.isInit = true
|
2018-12-27 21:59:35 +00:00
|
|
|
return nil
|
2018-12-27 21:36:34 +00:00
|
|
|
}
|
|
|
|
|
2018-12-28 02:36:28 +00:00
|
|
|
func Shutdown() error {
|
2018-12-28 03:28:33 +00:00
|
|
|
if !state.isInit {
|
2018-12-28 02:36:28 +00:00
|
|
|
return ErrWasNotInit
|
|
|
|
}
|
|
|
|
|
2018-12-28 03:28:33 +00:00
|
|
|
for _, w := range state.windows {
|
2018-12-28 02:36:28 +00:00
|
|
|
if err := w.Destroy(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := imgui_backend.Shutdown(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-28 03:28:33 +00:00
|
|
|
state.windows = nil
|
|
|
|
state.isInit = false
|
2018-12-28 02:36:28 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-29 21:24:55 +00:00
|
|
|
func CreateWindow(title string, width, height int, scene Scene) (*Window, error) {
|
|
|
|
if !state.isInit {
|
|
|
|
return nil, ErrWasNotInit
|
|
|
|
}
|
|
|
|
|
|
|
|
window, err := newWindow(title, width, height, scene)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
state.windows = append(state.windows, window)
|
|
|
|
|
|
|
|
return window, err
|
|
|
|
}
|
|
|
|
|
2018-12-27 22:35:06 +00:00
|
|
|
func ProcessEvents() error {
|
2018-12-28 03:28:33 +00:00
|
|
|
if !state.isInit {
|
2018-12-28 02:36:28 +00:00
|
|
|
return ErrWasNotInit
|
2018-12-28 02:28:03 +00:00
|
|
|
}
|
2018-12-27 22:35:06 +00:00
|
|
|
|
2018-12-28 17:26:05 +00:00
|
|
|
for running := true; running; {
|
|
|
|
advanceWindows()
|
|
|
|
|
2018-12-27 22:35:06 +00:00
|
|
|
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
|
2018-12-29 19:52:30 +00:00
|
|
|
if !processEvent(event) {
|
2018-12-28 17:26:05 +00:00
|
|
|
running = false
|
2018-12-27 22:35:06 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
<-time.After(time.Millisecond * 25)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-28 17:26:05 +00:00
|
|
|
func advanceWindows() {
|
|
|
|
for _, window := range state.windows {
|
|
|
|
window.advance()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-29 19:52:30 +00:00
|
|
|
func processEvent(event sdl.Event) bool {
|
|
|
|
handled, _ := imgui_backend.ProcessEvent(event)
|
|
|
|
if handled {
|
|
|
|
return true
|
2018-12-28 17:26:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch event.(type) {
|
|
|
|
case *sdl.QuitEvent:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|