loop: Remove dependency on ui

This commit is contained in:
Hajime Hoshi 2017-08-05 20:43:49 +09:00
parent 56a17a7f79
commit eef8289854
2 changed files with 45 additions and 35 deletions

View File

@ -20,7 +20,6 @@ import (
"github.com/hajimehoshi/ebiten/internal/clock"
"github.com/hajimehoshi/ebiten/internal/sync"
"github.com/hajimehoshi/ebiten/internal/ui"
)
func CurrentFPS() float64 {
@ -81,30 +80,11 @@ func (c *runContext) updateFPS(fps float64) {
c.m.Unlock()
}
type GraphicsContext interface {
SetSize(width, height int, scale float64)
Update(updateCount int) error
Invalidate()
type Runner interface {
Run(width, height int, scale float64, title string) error
}
type loopGraphicsContext struct {
runContext *runContext
graphicsContext GraphicsContext
}
func (g *loopGraphicsContext) SetSize(width, height int, scale float64) {
g.graphicsContext.SetSize(width, height, scale)
}
func (g *loopGraphicsContext) Update() error {
return g.runContext.render(g.graphicsContext)
}
func (g *loopGraphicsContext) Invalidate() {
g.graphicsContext.Invalidate()
}
func Run(g GraphicsContext, width, height int, scale float64, title string) (err error) {
func Run(runner Runner, width, height int, scale float64, title string) (err error) {
if currentRunContext != nil {
return errors.New("loop: The game is already running")
}
@ -118,14 +98,7 @@ func Run(g GraphicsContext, width, height int, scale float64, title string) (err
close(contextInitCh)
lg := &loopGraphicsContext{currentRunContext, g}
if err := ui.Run(width, height, scale, title, lg); err != nil {
if _, ok := err.(*ui.RegularTermination); ok {
return nil
}
return err
}
return nil
return runner.Run(width, height, scale, title)
}
func (c *runContext) updateCount(now int64) int {
@ -187,7 +160,16 @@ func (c *runContext) registerPing(ping func()) {
c.m.Unlock()
}
func (c *runContext) render(g GraphicsContext) error {
type Updater interface {
Update(updateCount int) error
}
func Update(u Updater) error {
<-contextInitCh
return currentRunContext.update(u)
}
func (c *runContext) update(u Updater) error {
n := now()
c.m.Lock()
@ -197,7 +179,7 @@ func (c *runContext) render(g GraphicsContext) error {
c.m.Unlock()
count := c.updateCount(n)
if err := g.Update(count); err != nil {
if err := u.Update(count); err != nil {
return err
}
c.framesForFPS++

32
run.go
View File

@ -60,6 +60,32 @@ 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, r); err != nil {
if _, ok := err.(*ui.RegularTermination); ok {
return nil
}
return err
}
return nil
}
func (r *runner) SetSize(width, height int, scale float64) {
r.g.SetSize(width, height, scale)
}
func (r *runner) Update() error {
return loop.Update(r.g)
}
func (r *runner) Invalidate() {
r.g.Invalidate()
}
// Run runs the game.
// f is a function which is called at every frame.
// The argument (*Image) is the render target that represents the screen.
@ -82,7 +108,8 @@ func Run(f func(*Image) error, width, height int, scale float64, title string) e
go func() {
g := newGraphicsContext(f)
theGraphicsContext.Store(g)
if err := loop.Run(g, width, height, scale, title); err != nil {
r := &runner{g}
if err := loop.Run(r, width, height, scale, title); err != nil {
ch <- err
}
close(ch)
@ -106,7 +133,8 @@ func RunWithoutMainLoop(f func(*Image) error, width, height int, scale float64,
go func() {
g := newGraphicsContext(f)
theGraphicsContext.Store(g)
if err := loop.Run(g, width, height, scale, title); err != nil {
r := &runner{g}
if err := loop.Run(r, width, height, scale, title); err != nil {
ch <- err
}
close(ch)