examples/moire: Make the window resizable

This commit is contained in:
Hajime Hoshi 2019-12-23 04:43:59 +09:00
parent 1d9c00688c
commit f8d47e5c6a

View File

@ -33,18 +33,18 @@ const (
) )
var ( var (
dots []uint8 dots []byte
dotsWidth int dotsWidth int
dotsHeight int dotsHeight int
) )
func getDots(width, height int) []uint8 { func getDots(width, height int) []byte {
if dotsWidth == width && dotsHeight == height { if dotsWidth == width && dotsHeight == height {
return dots return dots
} }
dotsWidth = width dotsWidth = width
dotsHeight = height dotsHeight = height
dots = make([]uint8, width*height*4) dots = make([]byte, width*height*4)
for j := 0; j < height; j++ { for j := 0; j < height; j++ {
for i := 0; i < width; i++ { for i := 0; i < width; i++ {
if (i+j)%2 == 0 { if (i+j)%2 == 0 {
@ -58,27 +58,34 @@ func getDots(width, height int) []uint8 {
return dots return dots
} }
func update(screen *ebiten.Image) error { type game struct {
screenScale := ebiten.ScreenScale() scale float64
}
func (g *game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
}
func (g *game) Update(screen *ebiten.Image) error {
fullscreen := ebiten.IsFullscreen() fullscreen := ebiten.IsFullscreen()
if inpututil.IsKeyJustPressed(ebiten.KeyS) { if inpututil.IsKeyJustPressed(ebiten.KeyS) {
switch screenScale { switch g.scale {
case 1: case 1:
screenScale = 1.5 g.scale = 1.5
case 1.5: case 1.5:
screenScale = 2 g.scale = 2
case 2: case 2:
screenScale = 1 g.scale = 1
default: default:
panic("not reached") panic("not reached")
} }
ebiten.SetWindowSize(int(screenWidth*g.scale), int(screenHeight*g.scale))
} }
if inpututil.IsKeyJustPressed(ebiten.KeyF) { if inpututil.IsKeyJustPressed(ebiten.KeyF) {
fullscreen = !fullscreen fullscreen = !fullscreen
ebiten.SetFullscreen(fullscreen)
} }
ebiten.SetScreenScale(screenScale)
ebiten.SetFullscreen(fullscreen)
if ebiten.IsDrawingSkipped() { if ebiten.IsDrawingSkipped() {
return nil return nil
@ -89,7 +96,13 @@ func update(screen *ebiten.Image) error {
} }
func main() { func main() {
if err := ebiten.Run(update, screenWidth, screenHeight, initScreenScale, "Moire (Ebiten Demo)"); err != nil { g := &game{
scale: initScreenScale,
}
ebiten.SetWindowSize(screenWidth*initScreenScale, screenHeight*initScreenScale)
ebiten.SetWindowTitle("Moire (Ebiten Demo)")
ebiten.SetWindowResizable(true)
if err := ebiten.RunGame(g); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }