ebiten: refactoring: reduce call of ui.Get()

This commit is contained in:
Hajime Hoshi 2022-02-13 19:40:03 +09:00
parent 6a8e45e6c1
commit 31aa01131d
2 changed files with 5 additions and 7 deletions

View File

@ -27,7 +27,7 @@ import (
const DefaultTPS = 60 const DefaultTPS = 60
type Context interface { type Context interface {
UpdateOffscreen(outsideWidth, outsideHeight float64) (int, int) UpdateOffscreen(outsideWidth, outsideHeight float64, deviceScaleFactor float64) (int, int)
UpdateFrame(updateCount int, screenScale float64, offsetX, offsetY float64) error UpdateFrame(updateCount int, screenScale float64, offsetX, offsetY float64) error
} }
@ -56,7 +56,7 @@ func (c *contextImpl) forceUpdateFrame(deviceScaleFactor float64) error {
} }
func (c *contextImpl) updateFrameImpl(updateCount int, deviceScaleFactor float64) error { func (c *contextImpl) updateFrameImpl(updateCount int, deviceScaleFactor float64) error {
ow, oh := c.context.UpdateOffscreen(c.outsideWidth, c.outsideHeight) ow, oh := c.context.UpdateOffscreen(c.outsideWidth, c.outsideHeight, deviceScaleFactor)
c.offscreenWidth = ow c.offscreenWidth = ow
c.offscreenHeight = oh c.offscreenHeight = oh

View File

@ -42,15 +42,13 @@ func (c *uiContext) set(game Game) {
c.game = game c.game = game
} }
func (c *uiContext) UpdateOffscreen(outsideWidth, outsideHeight float64) (int, int) { func (c *uiContext) UpdateOffscreen(outsideWidth, outsideHeight float64, deviceScaleFactor float64) (int, int) {
d := ui.Get().DeviceScaleFactor()
sw, sh := int(outsideWidth*d), int(outsideHeight*d)
ow, oh := c.game.Layout(int(outsideWidth), int(outsideHeight)) ow, oh := c.game.Layout(int(outsideWidth), int(outsideHeight))
if ow <= 0 || oh <= 0 { if ow <= 0 || oh <= 0 {
panic("ebiten: Layout must return positive numbers") panic("ebiten: Layout must return positive numbers")
} }
sw, sh := int(outsideWidth*deviceScaleFactor), int(outsideHeight*deviceScaleFactor)
if c.screen != nil { if c.screen != nil {
if w, h := c.screen.Size(); w != sw || h != sh { if w, h := c.screen.Size(); w != sw || h != sh {
c.screen.Dispose() c.screen.Dispose()
@ -58,7 +56,7 @@ func (c *uiContext) UpdateOffscreen(outsideWidth, outsideHeight float64) (int, i
} }
} }
if c.screen == nil { if c.screen == nil {
c.screen = newScreenFramebufferImage(int(outsideWidth*d), int(outsideHeight*d)) c.screen = newScreenFramebufferImage(sw, sh)
} }
if c.offscreen != nil { if c.offscreen != nil {