loop: Simplify logic

This commit is contained in:
Hajime Hoshi 2017-08-05 21:24:04 +09:00
parent 4ee0c9b482
commit b85f5edee0
2 changed files with 26 additions and 19 deletions

View File

@ -80,25 +80,24 @@ func (c *runContext) updateFPS(fps float64) {
c.m.Unlock()
}
type Runner interface {
Run(width, height int, scale float64, title string) error
}
func Run(runner Runner, width, height int, scale float64, title string) (err error) {
func Start() error {
if currentRunContext != nil {
return errors.New("loop: The game is already running")
}
currentRunContext = &runContext{}
currentRunContext.startRunning()
defer currentRunContext.endRunning()
n := now()
currentRunContext.lastUpdated = n
currentRunContext.lastFPSUpdated = n
close(contextInitCh)
return nil
}
return runner.Run(width, height, scale, title)
func End() {
currentRunContext.endRunning()
currentRunContext = nil
}
func (c *runContext) updateCount(now int64) int {

32
run.go
View File

@ -60,12 +60,8 @@ func IsRunningSlowly() bool {
var theGraphicsContext atomic.Value
type runner struct {
g *graphicsContext
}
func (r *runner) Run(width, height int, scale float64, title string) error {
if err := ui.Run(width, height, scale, title, &updater{r.g}); err != nil {
func run(width, height int, scale float64, title string, g *graphicsContext) error {
if err := ui.Run(width, height, scale, title, &updater{g}); err != nil {
if _, ok := err.(*ui.RegularTermination); ok {
return nil
}
@ -110,13 +106,19 @@ func (u *updater) Invalidate() {
func Run(f func(*Image) error, width, height int, scale float64, title string) error {
ch := make(chan error)
go func() {
defer close(ch)
g := newGraphicsContext(f)
theGraphicsContext.Store(g)
r := &runner{g}
if err := loop.Run(r, width, height, scale, title); err != nil {
if err := loop.Start(); err != nil {
ch <- err
return
}
close(ch)
if err := run(width, height, scale, title, g); err != nil {
ch <- err
return
}
loop.End()
}()
// TODO: Use context in Go 1.7?
if err := ui.RunMainThreadLoop(ch); err != nil {
@ -135,13 +137,19 @@ func Run(f func(*Image) error, width, height int, scale float64, title string) e
func RunWithoutMainLoop(f func(*Image) error, width, height int, scale float64, title string) <-chan error {
ch := make(chan error)
go func() {
defer close(ch)
g := newGraphicsContext(f)
theGraphicsContext.Store(g)
r := &runner{g}
if err := loop.Run(r, width, height, scale, title); err != nil {
if err := loop.Start(); err != nil {
ch <- err
return
}
close(ch)
if err := run(width, height, scale, title, g); err != nil {
ch <- err
return
}
loop.End()
}()
return ch
}