internal/ui: move screenScaleAndOffsets to the ebiten package

Updates #2046
This commit is contained in:
Hajime Hoshi 2022-10-14 01:58:03 +09:00
parent 03621e22c6
commit 25ae96db89
2 changed files with 23 additions and 19 deletions

View File

@ -136,7 +136,8 @@ func (g *gameForUI) DrawOffscreen() {
g.game.Draw(g.offscreen)
}
func (g *gameForUI) DrawScreen(scale, offsetX, offsetY float64) {
func (g *gameForUI) DrawScreen() {
scale, offsetX, offsetY := g.ScreenScaleAndOffsets()
switch {
case !isScreenFilterEnabled(), math.Floor(scale) == scale:
op := &DrawImageOptions{}
@ -158,3 +159,20 @@ func (g *gameForUI) DrawScreen(scale, offsetX, offsetY float64) {
g.screen.DrawRectShader(w, h, g.screenShader, op)
}
}
func (g *gameForUI) ScreenScaleAndOffsets() (scale, offsetX, offsetY float64) {
if g.screen == nil {
return
}
sw, sh := g.screen.Size()
ow, oh := g.offscreen.Size()
scaleX := float64(sw) / float64(ow)
scaleY := float64(sh) / float64(oh)
scale = math.Min(scaleX, scaleY)
width := float64(ow) * scale
height := float64(oh) * scale
offsetX = (float64(sw) - width) / 2
offsetY = (float64(sh) - height) / 2
return
}

View File

@ -39,7 +39,8 @@ type Game interface {
Layout(outsideWidth, outsideHeight int) (int, int)
Update() error
DrawOffscreen()
DrawScreen(scale, offsetX, offsteY float64)
DrawScreen()
ScreenScaleAndOffsets() (scale, offsetX, offsetY float64)
}
type context struct {
@ -169,7 +170,7 @@ func (c *context) drawGame(graphicsDriver graphicsdriver.Graphics) {
c.screen.clear()
}
c.game.DrawScreen(c.screenScaleAndOffsets())
c.game.DrawScreen()
}
func (c *context) layoutGame(outsideWidth, outsideHeight float64, deviceScaleFactor float64) (int, int) {
@ -215,7 +216,7 @@ func (c *context) layoutGame(outsideWidth, outsideHeight float64, deviceScaleFac
}
func (c *context) adjustPosition(x, y float64, deviceScaleFactor float64) (float64, float64) {
s, ox, oy := c.screenScaleAndOffsets()
s, ox, oy := c.game.ScreenScaleAndOffsets()
// The scale 0 indicates that the screen is not initialized yet.
// As any cursor values don't make sense, just return NaN.
if s == 0 {
@ -224,21 +225,6 @@ func (c *context) adjustPosition(x, y float64, deviceScaleFactor float64) (float
return (x*deviceScaleFactor - ox) / s, (y*deviceScaleFactor - oy) / s
}
func (c *context) screenScaleAndOffsets() (float64, float64, float64) {
if c.screen == nil {
return 0, 0, 0
}
scaleX := float64(c.screen.width) / float64(c.offscreen.width)
scaleY := float64(c.screen.height) / float64(c.offscreen.height)
scale := math.Min(scaleX, scaleY)
width := float64(c.offscreen.width) * scale
height := float64(c.offscreen.height) * scale
x := (float64(c.screen.width) - width) / 2
y := (float64(c.screen.height) - height) / 2
return scale, x, y
}
var theGlobalState = globalState{
isScreenClearedEveryFrame_: 1,
}