ui: Remove running member

This commit is contained in:
Hajime Hoshi 2016-03-26 17:59:36 +09:00
parent 30d4437e97
commit 5a8f5ffe87

22
run.go
View File

@ -21,7 +21,6 @@ import (
) )
type runContext struct { type runContext struct {
running bool
fps float64 fps float64
newScreenWidth int newScreenWidth int
newScreenHeight int newScreenHeight int
@ -32,19 +31,22 @@ type runContext struct {
var currentRunContext = &runContext{} var currentRunContext = &runContext{}
func (c *runContext) CurrentFPS() float64 { func (c *runContext) CurrentFPS() float64 {
if c == nil {
// TODO: Should panic here?
return 0
}
return c.fps return c.fps
} }
func (c *runContext) IsRunningSlowly() bool { func (c *runContext) IsRunningSlowly() bool {
if c == nil {
// TODO: Should panic here?
return false
}
return c.isRunningSlowly return c.isRunningSlowly
} }
func (c *runContext) Run(f func(*Image) error, width, height, scale int, title string) error { func (c *runContext) Run(f func(*Image) error, width, height, scale int, title string) error {
c.running = true
defer func() {
c.running = false
}()
if err := ui.CurrentUI().Start(width, height, scale, title); err != nil { if err := ui.CurrentUI().Start(width, height, scale, title); err != nil {
return err return err
} }
@ -124,7 +126,7 @@ func (c *runContext) Run(f func(*Image) error, width, height, scale int, title s
} }
func (c *runContext) SetScreenSize(width, height int) { func (c *runContext) SetScreenSize(width, height int) {
if !c.running { if c == nil {
panic("ebiten: SetScreenSize must be called during Run") panic("ebiten: SetScreenSize must be called during Run")
} }
if width <= 0 || height <= 0 { if width <= 0 || height <= 0 {
@ -135,7 +137,7 @@ func (c *runContext) SetScreenSize(width, height int) {
} }
func (c *runContext) SetScreenScale(scale int) { func (c *runContext) SetScreenScale(scale int) {
if !c.running { if c == nil {
panic("ebiten: SetScreenScale must be called during Run") panic("ebiten: SetScreenScale must be called during Run")
} }
if scale <= 0 { if scale <= 0 {
@ -175,6 +177,10 @@ func IsRunningSlowly() bool {
// even if a rendering frame is skipped. // even if a rendering frame is skipped.
// f is not called when the screen is not shown. // f is not called when the screen is not shown.
func Run(f func(*Image) error, width, height, scale int, title string) error { func Run(f func(*Image) error, width, height, scale int, title string) error {
currentRunContext = &runContext{}
defer func() {
currentRunContext = nil
}()
return currentRunContext.Run(f, width, height, scale, title) return currentRunContext.Run(f, width, height, scale, title)
} }