ui: Protect uiContext.scaleForWindow by mutex

This commit is contained in:
Hajime Hoshi 2019-12-15 19:53:33 +09:00
parent bda11b0e17
commit 12a13892cd
2 changed files with 10 additions and 11 deletions

4
run.go
View File

@ -225,8 +225,6 @@ func SetScreenSize(width, height int) {
panic("ebiten: SetScreenSize can't be called before the main loop starts") panic("ebiten: SetScreenSize can't be called before the main loop starts")
} }
theUIContext.SetScreenSize(width, height) theUIContext.SetScreenSize(width, height)
s := theUIContext.getScaleForWindow()
uiDriver().SetWindowSize(int(float64(width)*s), int(float64(height)*s))
} }
// SetScreenScale changes the scale of the screen on desktops. // SetScreenScale changes the scale of the screen on desktops.
@ -258,8 +256,6 @@ func SetScreenScale(scale float64) {
panic("ebiten: SetScreenScale can't be called before the main loop starts") panic("ebiten: SetScreenScale can't be called before the main loop starts")
} }
theUIContext.setScaleForWindow(scale) theUIContext.setScaleForWindow(scale)
w, h := theUIContext.size()
uiDriver().SetWindowSize(int(float64(w)*scale), int(float64(h)*scale))
} }
// ScreenScale returns the current screen scale. // ScreenScale returns the current screen scale.

View File

@ -80,26 +80,29 @@ func (c *uiContext) resolveSize() (int, int) {
return c.screenWidth, c.screenHeight return c.screenWidth, c.screenHeight
} }
func (c *uiContext) size() (int, int) {
return c.resolveSize()
}
func (c *uiContext) setScaleForWindow(scale float64) { func (c *uiContext) setScaleForWindow(scale float64) {
w, h := c.resolveSize()
c.m.Lock()
c.scaleForWindow = scale c.scaleForWindow = scale
uiDriver().SetWindowSize(int(float64(w)*scale), int(float64(h)*scale))
c.m.Unlock()
} }
func (c *uiContext) getScaleForWindow() float64 { func (c *uiContext) getScaleForWindow() float64 {
return c.scaleForWindow c.m.Lock()
s := c.scaleForWindow
c.m.Unlock()
return s
} }
func (c *uiContext) SetScreenSize(width, height int) { func (c *uiContext) SetScreenSize(width, height int) {
c.m.Lock() c.m.Lock()
defer c.m.Unlock() defer c.m.Unlock()
// TODO: Use the interface Game's Layout and then update screenWidth and screenHeight, then this function
// is no longer needed.
c.reqWidth = width c.reqWidth = width
c.reqHeight = height c.reqHeight = height
s := c.scaleForWindow
uiDriver().SetWindowSize(int(float64(width)*s), int(float64(height)*s))
} }
func (c *uiContext) Layout(outsideWidth, outsideHeight float64) { func (c *uiContext) Layout(outsideWidth, outsideHeight float64) {