ui: Protect defaultGame's memers with mutex

This commit is contained in:
Hajime Hoshi 2019-12-16 01:44:55 +09:00
parent 46a7ae6175
commit 97ddef0047

View File

@ -36,6 +36,7 @@ type defaultGame struct {
update func(screen *Image) error update func(screen *Image) error
width int width int
height int height int
context *uiContext
} }
func (d *defaultGame) Update(screen *Image) error { func (d *defaultGame) Update(screen *Image) error {
@ -44,19 +45,21 @@ func (d *defaultGame) Update(screen *Image) error {
func (d *defaultGame) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) { func (d *defaultGame) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
// Ignore the outside size. // Ignore the outside size.
return d.width, d.height d.context.m.Lock()
} w, h := d.width, d.height
d.context.m.Unlock()
func (d *defaultGame) setScreenSize(width, height int) { return w, h
d.width = width
d.height = height
} }
func newUIContext(game Game, width, height int, scaleForWindow float64) *uiContext { func newUIContext(game Game, width, height int, scaleForWindow float64) *uiContext {
return &uiContext{ u := &uiContext{
game: game, game: game,
scaleForWindow: scaleForWindow, scaleForWindow: scaleForWindow,
} }
if g, ok := game.(*defaultGame); ok {
g.context = u
}
return u
} }
type uiContext struct { type uiContext struct {
@ -82,17 +85,20 @@ func (c *uiContext) setScaleForWindow(scale float64) {
if !ok { if !ok {
panic("ebiten: setScaleForWindow must be called when Run is used") panic("ebiten: setScaleForWindow must be called when Run is used")
} }
w, h := g.width, g.height
c.m.Lock() c.m.Lock()
defer c.m.Unlock()
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))
c.m.Unlock()
} }
func (c *uiContext) getScaleForWindow() float64 { func (c *uiContext) getScaleForWindow() float64 {
if _, ok := c.game.(*defaultGame); !ok { if _, ok := c.game.(*defaultGame); !ok {
panic("ebiten: getScaleForWindow must be called when Run is used") panic("ebiten: getScaleForWindow must be called when Run is used")
} }
c.m.Lock() c.m.Lock()
s := c.scaleForWindow s := c.scaleForWindow
c.m.Unlock() c.m.Unlock()
@ -100,14 +106,16 @@ func (c *uiContext) getScaleForWindow() float64 {
} }
func (c *uiContext) SetScreenSize(width, height int) { func (c *uiContext) SetScreenSize(width, height int) {
c.m.Lock()
defer c.m.Unlock()
g, ok := c.game.(*defaultGame) g, ok := c.game.(*defaultGame)
if !ok { if !ok {
panic("ebiten: SetScreenSize must be called when Run is used") panic("ebiten: SetScreenSize must be called when Run is used")
} }
g.setScreenSize(width, height)
c.m.Lock()
defer c.m.Unlock()
g.width = width
g.height = height
s := c.scaleForWindow s := c.scaleForWindow
uiDriver().SetWindowSize(int(float64(width)*s), int(float64(height)*s)) uiDriver().SetWindowSize(int(float64(width)*s), int(float64(height)*s))
} }