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() {
|
||||
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()
|
||||
}
|
||||
|
||||
|
54
uicontext.go
54
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
|
||||
|
Loading…
Reference in New Issue
Block a user