From bab3288a378ba89165c3a0413b8eed198c729b03 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 30 May 2020 21:14:10 +0900 Subject: [PATCH] examples: Use RunGame Updates #1111 --- examples/set/main.go | 25 ++++++++++++++++--------- examples/shapes/main.go | 28 +++++++++++++++++----------- examples/sinewave/main.go | 30 +++++++++++++++++++----------- 3 files changed, 52 insertions(+), 31 deletions(-) diff --git a/examples/set/main.go b/examples/set/main.go index 9b75ec13d..3206cf296 100644 --- a/examples/set/main.go +++ b/examples/set/main.go @@ -42,7 +42,10 @@ func init() { offscreen, _ = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterDefault) } -func update(screen *ebiten.Image) error { +type Game struct { +} + +func (g *Game) Update(screen *ebiten.Image) error { w, h := offscreen.Size() x := rand.Intn(w) y := rand.Intn(h) @@ -53,18 +56,22 @@ func update(screen *ebiten.Image) error { byte(0xff), } offscreen.Set(x, y, c) - - if ebiten.IsDrawingSkipped() { - return nil - } - - screen.DrawImage(offscreen, nil) - ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f\nFPS: %0.2f", ebiten.CurrentTPS(), ebiten.CurrentFPS())) return nil } +func (g *Game) Draw(screen *ebiten.Image) { + screen.DrawImage(offscreen, nil) + ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f\nFPS: %0.2f", ebiten.CurrentTPS(), ebiten.CurrentFPS())) +} + +func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) { + return screenWidth, screenHeight +} + func main() { - if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Test"); err != nil { + ebiten.SetWindowSize(screenWidth*2, screenHeight*2) + ebiten.SetWindowTitle("Set (Ebiten Demo)") + if err := ebiten.RunGame(&Game{}); err != nil { log.Fatal(err) } } diff --git a/examples/shapes/main.go b/examples/shapes/main.go index 4621d71eb..3b72673c8 100644 --- a/examples/shapes/main.go +++ b/examples/shapes/main.go @@ -39,7 +39,9 @@ func init() { emptyImage.Fill(color.White) } -var count = 0 +type Game struct { + count int +} func line(x0, y0, x1, y1 float32, clr color.RGBA) ([]ebiten.Vertex, []uint16) { const width = 1 @@ -152,15 +154,14 @@ func rect(x, y, w, h float32, clr color.RGBA) ([]ebiten.Vertex, []uint16) { }, []uint16{0, 1, 2, 1, 2, 3} } -func update(screen *ebiten.Image) error { - count++ - count %= 240 +func (g *Game) Update(screen *ebiten.Image) error { + g.count++ + g.count %= 240 + return nil +} - if ebiten.IsDrawingSkipped() { - return nil - } - - cf := float64(count) +func (g *Game) Draw(screen *ebiten.Image) { + cf := float64(g.count) v, i := line(100, 100, 300, 100, color.RGBA{0xff, 0xff, 0xff, 0xff}) screen.DrawTriangles(v, i, emptyImage, nil) v, i = line(50, 150, 50, 350, color.RGBA{0xff, 0xff, 0x00, 0xff}) @@ -174,11 +175,16 @@ func update(screen *ebiten.Image) error { screen.DrawTriangles(v, i, emptyImage, nil) ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f", ebiten.CurrentTPS())) - return nil +} + +func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) { + return screenWidth, screenHeight } func main() { - if err := ebiten.Run(update, screenWidth, screenHeight, 1, "Shapes (Ebiten Demo)"); err != nil { + ebiten.SetWindowSize(screenWidth, screenHeight) + ebiten.SetWindowTitle("Shapes (Ebiten Demo)") + if err := ebiten.RunGame(&Game{}); err != nil { log.Fatal(err) } } diff --git a/examples/sinewave/main.go b/examples/sinewave/main.go index 50de5a273..d1d7abeed 100644 --- a/examples/sinewave/main.go +++ b/examples/sinewave/main.go @@ -93,29 +93,37 @@ func (s *stream) Close() error { return nil } -var player *audio.Player +type Game struct { + player *audio.Player +} -func update(screen *ebiten.Image) error { - if player == nil { +func (g *Game) Update(screen *ebiten.Image) error { + if g.player == nil { // Pass the (infinite) stream to audio.NewPlayer. // After calling Play, the stream never ends as long as the player object lives. var err error - player, err = audio.NewPlayer(audioContext, &stream{}) + g.player, err = audio.NewPlayer(audioContext, &stream{}) if err != nil { return err } - player.Play() + g.player.Play() } - if ebiten.IsDrawingSkipped() { - return nil - } - msg := fmt.Sprintf("TPS: %0.2f\nThis is an example using infinite audio stream.", ebiten.CurrentTPS()) - ebitenutil.DebugPrint(screen, msg) return nil } +func (g *Game) Draw(screen *ebiten.Image) { + msg := fmt.Sprintf("TPS: %0.2f\nThis is an example using infinite audio stream.", ebiten.CurrentTPS()) + ebitenutil.DebugPrint(screen, msg) +} + +func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) { + return screenWidth, screenHeight +} + func main() { - if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Sine Wave (Ebiten Demo)"); err != nil { + ebiten.SetWindowSize(screenWidth*2, screenHeight*2) + ebiten.SetWindowTitle("Sine Wave (Ebiten Demo)") + if err := ebiten.RunGame(&Game{}); err != nil { log.Fatal(err) } }