ebiten: add FloatLayouter

Closes #2285
This commit is contained in:
Hajime Hoshi 2022-11-09 01:49:51 +09:00
parent 0d6b42fedd
commit 8567c3c654
3 changed files with 25 additions and 5 deletions

View File

@ -90,12 +90,18 @@ func createRandomIconImage() image.Image {
type game struct {
count int
width int
height int
width float64
height float64
transparent bool
}
func (g *game) Layout(outsideWidth, outsideHeight int) (int, int) {
// As game implements the interface FloatLayouter, Layout is never called and LayoutF is called instead.
// However, game has to implement Layout to satisfy the interface Game.
panic("windowsize: Layout must not be called")
}
func (g *game) LayoutF(outsideWidth, outsideHeight float64) (float64, float64) {
if *flagAutoAdjusting {
g.width, g.height = outsideWidth, outsideHeight
return outsideWidth, outsideHeight
@ -106,14 +112,14 @@ func (g *game) Layout(outsideWidth, outsideHeight int) (int, int) {
func (g *game) Update() error {
var (
screenWidth int
screenHeight int
screenWidth float64
screenHeight float64
screenScale float64
)
screenWidth = g.width
screenHeight = g.height
if ww, wh := ebiten.WindowSize(); ww > 0 && wh > 0 {
screenScale = math.Min(float64(ww)/float64(g.width), float64(wh)/float64(g.height))
screenScale = math.Min(float64(ww)/g.width, float64(wh)/g.height)
} else {
// ebiten.WindowSize can return (0, 0) on browsers or mobiles.
screenScale = 1

View File

@ -126,6 +126,10 @@ func (g *gameForUI) NewScreenImage(width, height int) *ui.Image {
}
func (g *gameForUI) Layout(outsideWidth, outsideHeight float64) (float64, float64) {
if l, ok := g.game.(FloatLayouter); ok {
return l.LayoutF(outsideWidth, outsideHeight)
}
// Even if the original value is less than 1, the value must be a positive integer (#2340).
// This is for a simple implementation of Layout, which returns the argument values without modifications.
// TODO: Remove this hack when Game.Layout takes floats instead of integers.

10
run.go
View File

@ -79,9 +79,19 @@ type Game interface {
//
// You can return a fixed screen size if you don't care, or you can also return a calculated screen size
// adjusted with the given outside size.
//
// If the game implements the interface FloatLayouter, Layout is never called and LayoutF is called instead.
Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int)
}
// FloatLayouter is an interface for the float version of Game.Layout.
type FloatLayouter interface {
// LayoutF is the float version of Game.Layout.
//
// If the game implements this interface, Layout is never called and LayoutF is called instead.
LayoutF(outsideWidth, outsideHeight float64) (screenWidth, screenHeight float64)
}
// FinalScreen represents the final screen image.
// FinalScreen implements a part of Image functions.
type FinalScreen interface {