mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
ui: Refactoring: theUIContext is always non-nil
This commit is contained in:
parent
4c6006343e
commit
812a29bf07
16
run.go
16
run.go
@ -207,11 +207,11 @@ func RunGame(game Game) error {
|
|||||||
if uiDriver().CanHaveWindow() {
|
if uiDriver().CanHaveWindow() {
|
||||||
fixWindowPosition(WindowSize())
|
fixWindowPosition(WindowSize())
|
||||||
}
|
}
|
||||||
return runGame(game, 1)
|
return runGame(game, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func runGame(game Game, scale float64) error {
|
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 := uiDriver().Run(theUIContext, graphicsDriver()); err != nil {
|
||||||
if err == driver.RegularTermination {
|
if err == driver.RegularTermination {
|
||||||
return nil
|
return nil
|
||||||
@ -232,8 +232,7 @@ func RunWithoutMainLoop(f func(*Image) error, width, height int, scale float64,
|
|||||||
width: width,
|
width: width,
|
||||||
height: height,
|
height: height,
|
||||||
}
|
}
|
||||||
|
theUIContext.set(game, scale)
|
||||||
theUIContext = newUIContext(game, scale)
|
|
||||||
return uiDriver().RunWithoutMainLoop(width, height, scale, title, theUIContext, graphicsDriver())
|
return uiDriver().RunWithoutMainLoop(width, height, scale, title, theUIContext, graphicsDriver())
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -252,9 +251,6 @@ func SetScreenSize(width, height int) {
|
|||||||
if width <= 0 || height <= 0 {
|
if width <= 0 || height <= 0 {
|
||||||
panic("ebiten: width and height must be positive")
|
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)
|
theUIContext.SetScreenSize(width, height)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -263,17 +259,11 @@ func SetScreenScale(scale float64) {
|
|||||||
if scale <= 0 {
|
if scale <= 0 {
|
||||||
panic("ebiten: scale must be positive")
|
panic("ebiten: scale must be positive")
|
||||||
}
|
}
|
||||||
if theUIContext == nil {
|
|
||||||
panic("ebiten: SetScreenScale can't be called before the main loop starts")
|
|
||||||
}
|
|
||||||
theUIContext.setScaleForWindow(scale)
|
theUIContext.setScaleForWindow(scale)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ScreenScale is deprecated as of 1.11.0-alpha. Use WindowSize instead.
|
// ScreenScale is deprecated as of 1.11.0-alpha. Use WindowSize instead.
|
||||||
func ScreenScale() float64 {
|
func ScreenScale() float64 {
|
||||||
if theUIContext == nil {
|
|
||||||
panic("ebiten: ScreenScale can't be called before the main loop starts")
|
|
||||||
}
|
|
||||||
return theUIContext.getScaleForWindow()
|
return theUIContext.getScaleForWindow()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
54
uicontext.go
54
uicontext.go
@ -51,17 +51,6 @@ func (d *defaultGame) Layout(outsideWidth, outsideHeight int) (screenWidth, scre
|
|||||||
return w, h
|
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 {
|
type uiContext struct {
|
||||||
game Game
|
game Game
|
||||||
offscreen *Image
|
offscreen *Image
|
||||||
@ -81,30 +70,49 @@ type uiContext struct {
|
|||||||
m sync.Mutex
|
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) {
|
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)
|
g, ok := c.game.(*defaultGame)
|
||||||
if !ok {
|
if !ok {
|
||||||
panic("ebiten: setScaleForWindow can be called only when Run is used")
|
panic("ebiten: setScaleForWindow can be called only when Run is used")
|
||||||
}
|
}
|
||||||
|
|
||||||
c.m.Lock()
|
|
||||||
defer c.m.Unlock()
|
|
||||||
|
|
||||||
w, h := g.width, g.height
|
w, h := g.width, g.height
|
||||||
c.scaleForWindow = scale
|
c.scaleForWindow = scale
|
||||||
uiDriver().SetWindowSize(int(float64(w)*scale), int(float64(h)*scale))
|
uiDriver().SetWindowSize(int(float64(w)*scale), int(float64(h)*scale))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *uiContext) getScaleForWindow() float64 {
|
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 {
|
if _, ok := c.game.(*defaultGame); !ok {
|
||||||
panic("ebiten: getScaleForWindow can be called only when Run is used")
|
panic("ebiten: getScaleForWindow can be called only when Run is used")
|
||||||
}
|
}
|
||||||
|
|
||||||
c.m.Lock()
|
|
||||||
s := c.scaleForWindow
|
s := c.scaleForWindow
|
||||||
c.m.Unlock()
|
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -113,14 +121,18 @@ func (c *uiContext) getScaleForWindow() float64 {
|
|||||||
// SetScreenSize is for backward compatibility. This is called from ebiten.SetScreenSize and
|
// SetScreenSize is for backward compatibility. This is called from ebiten.SetScreenSize and
|
||||||
// uidriver/mobile.UserInterface.
|
// uidriver/mobile.UserInterface.
|
||||||
func (c *uiContext) SetScreenSize(width, height int) {
|
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)
|
g, ok := c.game.(*defaultGame)
|
||||||
if !ok {
|
if !ok {
|
||||||
panic("ebiten: SetScreenSize can be called only when Run is used")
|
panic("ebiten: SetScreenSize can be called only when Run is used")
|
||||||
}
|
}
|
||||||
|
|
||||||
c.m.Lock()
|
|
||||||
defer c.m.Unlock()
|
|
||||||
|
|
||||||
g.width = width
|
g.width = width
|
||||||
g.height = height
|
g.height = height
|
||||||
s := c.scaleForWindow
|
s := c.scaleForWindow
|
||||||
|
Loading…
Reference in New Issue
Block a user