ui: Use events in the game loop

This commit is contained in:
Hajime Hoshi 2016-05-18 01:30:27 +09:00
parent 49c156d2b5
commit 5c68ee4034
4 changed files with 70 additions and 47 deletions

21
internal/ui/event.go Normal file
View File

@ -0,0 +1,21 @@
// Copyright 2016 Hajime Hoshi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ui
type CloseEvent struct {
}
type RenderEvent struct {
}

View File

@ -196,7 +196,14 @@ func (u *UserInterface) pollEvents() error {
return currentInput.update(u.window, u.windowScale()) return currentInput.update(u.window, u.windowScale())
} }
func (u *UserInterface) Update() error { func (u *UserInterface) Update() (interface{}, error) {
shouldClose := false
u.runOnMainThread(func() {
shouldClose = u.window.ShouldClose()
})
if shouldClose {
return CloseEvent{}, nil
}
var ferr error var ferr error
u.runOnMainThread(func() { u.runOnMainThread(func() {
if err := u.pollEvents(); err != nil { if err := u.pollEvents(); err != nil {
@ -215,7 +222,10 @@ func (u *UserInterface) Update() error {
} }
} }
}) })
return ferr if ferr != nil {
return nil, ferr
}
return RenderEvent{}, nil
} }
func (u *UserInterface) Terminate() { func (u *UserInterface) Terminate() {
@ -226,14 +236,6 @@ func (u *UserInterface) Terminate() {
u.funcs = nil u.funcs = nil
} }
func (u *UserInterface) IsClosed() bool {
r := false
u.runOnMainThread(func() {
r = u.window.ShouldClose()
})
return r
}
func (u *UserInterface) SwapBuffers() { func (u *UserInterface) SwapBuffers() {
u.runOnMainThread(func() { u.runOnMainThread(func() {
u.swapBuffers() u.swapBuffers()

View File

@ -83,19 +83,15 @@ func vsync() {
<-ch <-ch
} }
func (u *UserInterface) Update() error { func (u *UserInterface) Update() (interface{}, error) {
currentInput.UpdateGamepads() currentInput.UpdateGamepads()
return nil return RenderEvent{}, nil
} }
func (u *UserInterface) Terminate() { func (u *UserInterface) Terminate() {
// Do nothing. // Do nothing.
} }
func (u *UserInterface) IsClosed() bool {
return false
}
func (u *UserInterface) SwapBuffers() { func (u *UserInterface) SwapBuffers() {
vsync() vsync()
for !shown() { for !shown() {

12
run.go
View File

@ -204,16 +204,17 @@ func run(f func(*Image) error, width, height, scale int, title string) error {
beforeForUpdate := n beforeForUpdate := n
beforeForFPS := n beforeForFPS := n
for { for {
// TODO: setSize should be called after swapping buffers?
if err := currentRunContext.updateScreenSize(graphicsContext); err != nil { if err := currentRunContext.updateScreenSize(graphicsContext); err != nil {
return err return err
} }
if err := ui.CurrentUI().Update(); err != nil { e, err := ui.CurrentUI().Update()
if err != nil {
return err return err
} }
if ui.CurrentUI().IsClosed() { switch e.(type) {
case ui.CloseEvent:
return nil return nil
} case ui.RenderEvent:
now := ui.Now() now := ui.Now()
// If beforeForUpdate is too old, we assume that screen is not shown. // If beforeForUpdate is too old, we assume that screen is not shown.
if int64(5*time.Second/FPS) < now-beforeForUpdate { if int64(5*time.Second/FPS) < now-beforeForUpdate {
@ -245,6 +246,9 @@ func run(f func(*Image) error, width, height, scale int, title string) error {
beforeForFPS = now beforeForFPS = now
frames = 0 frames = 0
} }
default:
panic("not reach")
}
} }
} }