From 812a29bf07066064cb520c56119498747082b059 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 22 Dec 2019 18:50:41 +0900 Subject: [PATCH] ui: Refactoring: theUIContext is always non-nil --- run.go | 16 +++------------- uicontext.go | 54 ++++++++++++++++++++++++++++++++-------------------- 2 files changed, 36 insertions(+), 34 deletions(-) diff --git a/run.go b/run.go index 83c5f7131..013367d97 100644 --- a/run.go +++ b/run.go @@ -207,11 +207,11 @@ func RunGame(game Game) error { if uiDriver().CanHaveWindow() { fixWindowPosition(WindowSize()) } - return runGame(game, 1) + return runGame(game, 0) } func runGame(game Game, scale float64) error { - theUIContext = newUIContext(game, scale) + theUIContext.set(game, scale) if err := uiDriver().Run(theUIContext, graphicsDriver()); err != nil { if err == driver.RegularTermination { return nil @@ -232,8 +232,7 @@ func RunWithoutMainLoop(f func(*Image) error, width, height int, scale float64, width: width, height: height, } - - theUIContext = newUIContext(game, scale) + theUIContext.set(game, scale) return uiDriver().RunWithoutMainLoop(width, height, scale, title, theUIContext, graphicsDriver()) } @@ -252,9 +251,6 @@ func SetScreenSize(width, height int) { if width <= 0 || height <= 0 { panic("ebiten: width and height must be positive") } - if theUIContext == nil { - panic("ebiten: SetScreenSize can't be called before the main loop starts") - } theUIContext.SetScreenSize(width, height) } @@ -263,17 +259,11 @@ func SetScreenScale(scale float64) { if scale <= 0 { panic("ebiten: scale must be positive") } - if theUIContext == nil { - panic("ebiten: SetScreenScale can't be called before the main loop starts") - } theUIContext.setScaleForWindow(scale) } // ScreenScale is deprecated as of 1.11.0-alpha. Use WindowSize instead. func ScreenScale() float64 { - if theUIContext == nil { - panic("ebiten: ScreenScale can't be called before the main loop starts") - } return theUIContext.getScaleForWindow() } diff --git a/uicontext.go b/uicontext.go index a2e6d04ec..46f220d5f 100644 --- a/uicontext.go +++ b/uicontext.go @@ -51,17 +51,6 @@ func (d *defaultGame) Layout(outsideWidth, outsideHeight int) (screenWidth, scre return w, h } -func newUIContext(game Game, scaleForWindow float64) *uiContext { - u := &uiContext{ - game: game, - scaleForWindow: scaleForWindow, - } - if g, ok := game.(*defaultGame); ok { - g.context = u - } - return u -} - type uiContext struct { game Game offscreen *Image @@ -81,30 +70,49 @@ type uiContext struct { m sync.Mutex } -var theUIContext *uiContext +var theUIContext = &uiContext{} + +func (c *uiContext) set(game Game, scaleForWindow float64) { + c.m.Lock() + defer c.m.Unlock() + c.game = game + + if g, ok := game.(*defaultGame); ok { + c.scaleForWindow = scaleForWindow + g.context = c + } +} func (c *uiContext) setScaleForWindow(scale float64) { + c.m.Lock() + defer c.m.Unlock() + + if c.game == nil { + panic("ebiten: setScaleForWindow can be called only after the main loop starts") + } + g, ok := c.game.(*defaultGame) if !ok { panic("ebiten: setScaleForWindow can be called only when Run is used") } - c.m.Lock() - defer c.m.Unlock() - w, h := g.width, g.height c.scaleForWindow = scale uiDriver().SetWindowSize(int(float64(w)*scale), int(float64(h)*scale)) } func (c *uiContext) getScaleForWindow() float64 { + c.m.Lock() + defer c.m.Unlock() + + if c.game == nil { + panic("ebiten: getScaleForWindow can be called only after the main loop starts") + } + if _, ok := c.game.(*defaultGame); !ok { panic("ebiten: getScaleForWindow can be called only when Run is used") } - - c.m.Lock() s := c.scaleForWindow - c.m.Unlock() return s } @@ -113,14 +121,18 @@ func (c *uiContext) getScaleForWindow() float64 { // SetScreenSize is for backward compatibility. This is called from ebiten.SetScreenSize and // uidriver/mobile.UserInterface. func (c *uiContext) SetScreenSize(width, height int) { + c.m.Lock() + defer c.m.Unlock() + + if c.game == nil { + panic("ebiten: SetScreenSize can be called only after the main loop starts") + } + g, ok := c.game.(*defaultGame) if !ok { panic("ebiten: SetScreenSize can be called only when Run is used") } - c.m.Lock() - defer c.m.Unlock() - g.width = width g.height = height s := c.scaleForWindow