From 8f1b7a2d0c8c6754d842d53cb99ec300d26095f7 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Wed, 3 Aug 2016 01:42:46 +0900 Subject: [PATCH] examples: Add error checks --- examples/alphablending/main.go | 4 ++- examples/audio/main.go | 25 ++++++++++++---- examples/blocks/blocks/game.go | 10 +++++-- examples/blocks/blocks/gamepadscene.go | 4 ++- examples/blocks/blocks/gamescene.go | 16 ++++++++--- examples/blocks/blocks/scenemanager.go | 8 ++++-- examples/blocks/main.go | 4 ++- examples/font/main.go | 4 ++- examples/gamepad/main.go | 4 ++- examples/keyboard/main.go | 8 ++++-- examples/life/main.go | 4 ++- examples/mosaic/main.go | 8 ++++-- examples/noise/main.go | 8 ++++-- examples/paint/main.go | 4 ++- examples/pcm/main.go | 8 ++++-- examples/perspective/main.go | 4 ++- examples/piano/main.go | 40 +++++++++++++++++++------- examples/sinewave/main.go | 4 ++- examples/windowsize/main.go | 8 ++++-- 19 files changed, 131 insertions(+), 44 deletions(-) diff --git a/examples/alphablending/main.go b/examples/alphablending/main.go index 13d6e4aa0..6f310efe5 100644 --- a/examples/alphablending/main.go +++ b/examples/alphablending/main.go @@ -64,7 +64,9 @@ func update(screen *ebiten.Image) error { diff = float64(480-count) * 0.2 } - screen.Fill(color.NRGBA{0x00, 0x00, 0x80, 0xff}) + if err := screen.Fill(color.NRGBA{0x00, 0x00, 0x80, 0xff}); err != nil { + return err + } op := &ebiten.DrawImageOptions{} op.ColorM.Scale(1.0, 1.0, 1.0, 0.5) op.ImageParts = &imageParts{ diff --git a/examples/audio/main.go b/examples/audio/main.go index b7441c6f5..d6ee03b18 100644 --- a/examples/audio/main.go +++ b/examples/audio/main.go @@ -44,13 +44,17 @@ func init() { if err != nil { log.Fatal(err) } - playerBarImage.Fill(&color.RGBA{0x80, 0x80, 0x80, 0xff}) + if err := playerBarImage.Fill(&color.RGBA{0x80, 0x80, 0x80, 0xff}); err != nil { + log.Fatal(err) + } playerCurrentImage, err = ebiten.NewImage(4, 10, ebiten.FilterNearest) if err != nil { log.Fatal(err) } - playerCurrentImage.Fill(&color.RGBA{0xff, 0xff, 0xff, 0xff}) + if err := playerCurrentImage.Fill(&color.RGBA{0xff, 0xff, 0xff, 0xff}); err != nil { + log.Fatal(err) + } } type Player struct { @@ -193,7 +197,9 @@ func update(screen *ebiten.Image) error { op := &ebiten.DrawImageOptions{} x, y, w, h := playerBarRect() op.GeoM.Translate(float64(x), float64(y)) - screen.DrawImage(playerBarImage, op) + if err := screen.DrawImage(playerBarImage, op); err != nil { + return err + } currentTimeStr := "00:00" if musicPlayer != nil { c := musicPlayer.audioPlayer.Current() @@ -209,7 +215,9 @@ func update(screen *ebiten.Image) error { cy := y - (ch-h)/2 op := &ebiten.DrawImageOptions{} op.GeoM.Translate(float64(cx), float64(cy)) - screen.DrawImage(playerCurrentImage, op) + if err := screen.DrawImage(playerCurrentImage, op); err != nil { + return err + } } msg := fmt.Sprintf(`FPS: %0.2f @@ -220,7 +228,9 @@ Press Z or X to change volume of the music if musicPlayer == nil { msg += "\nNow Loading..." } - ebitenutil.DebugPrint(screen, msg) + if err := ebitenutil.DebugPrint(screen, msg); err != nil { + return err + } if err := audioContext.Update(); err != nil { return err } @@ -273,7 +283,10 @@ func main() { } close(musicCh) // TODO: Is this goroutine-safe? - p.Play() + if err := p.Play(); err != nil { + log.Fatal(err) + return + } }() if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Audio (Ebiten Demo)"); err != nil { log.Fatal(err) diff --git a/examples/blocks/blocks/game.go b/examples/blocks/blocks/game.go index 53046b7e8..b5ae0b453 100644 --- a/examples/blocks/blocks/game.go +++ b/examples/blocks/blocks/game.go @@ -42,12 +42,16 @@ func NewGame() *Game { func (game *Game) Update(r *ebiten.Image) error { game.input.Update() - game.sceneManager.Update(&GameState{ + if err := game.sceneManager.Update(&GameState{ SceneManager: game.sceneManager, Input: &game.input, - }) + }); err != nil { + return err + } if !ebiten.IsRunningSlowly() { - game.sceneManager.Draw(r) + if err := game.sceneManager.Draw(r); err != nil { + return err + } } return nil } diff --git a/examples/blocks/blocks/gamepadscene.go b/examples/blocks/blocks/gamepadscene.go index 4ae37554b..e2b259f17 100644 --- a/examples/blocks/blocks/gamepadscene.go +++ b/examples/blocks/blocks/gamepadscene.go @@ -76,7 +76,9 @@ func (s *GamepadScene) Update(state *GameState) error { } func (s *GamepadScene) Draw(screen *ebiten.Image) error { - screen.Fill(color.Black) + if err := screen.Fill(color.Black); err != nil { + return err + } if s.buttonStates == nil { return nil diff --git a/examples/blocks/blocks/gamescene.go b/examples/blocks/blocks/gamescene.go index 56fa481fa..4612b197c 100644 --- a/examples/blocks/blocks/gamescene.go +++ b/examples/blocks/blocks/gamescene.go @@ -74,7 +74,9 @@ func init() { if err != nil { panic(err) } - imageEmpty.Fill(color.White) + if err := imageEmpty.Fill(color.White); err != nil { + panic(err) + } // Background imageGameBG, _, err = ebitenutil.NewImageFromFile("_resources/images/gophers.jpg", ebiten.FilterLinear) if err != nil { @@ -121,7 +123,9 @@ func init() { if err != nil { panic(err) } - imageGameover.Fill(color.NRGBA{0x00, 0x00, 0x00, 0x80}) + if err := imageGameover.Fill(color.NRGBA{0x00, 0x00, 0x00, 0x80}); err != nil { + panic(err) + } y = (ScreenHeight - blockHeight) / 2 if err := drawTextWithShadowCenter(imageGameover, "GAME OVER", 0, y, 1, color.White, ScreenWidth); err != nil { panic(err) @@ -248,7 +252,9 @@ func (s *GameScene) addScore(lines int) { } func (s *GameScene) Update(state *GameState) error { - s.field.Update() + if err := s.field.Update(); err != nil { + return err + } if s.gameover { // TODO: Gamepad key? @@ -392,7 +398,9 @@ func (s *GameScene) Draw(r *ebiten.Image) error { } if s.gameover { - r.DrawImage(imageGameover, nil) + if err := r.DrawImage(imageGameover, nil); err != nil { + return err + } } return nil diff --git a/examples/blocks/blocks/scenemanager.go b/examples/blocks/blocks/scenemanager.go index 93c06ff18..82d5bdd93 100644 --- a/examples/blocks/blocks/scenemanager.go +++ b/examples/blocks/blocks/scenemanager.go @@ -72,12 +72,16 @@ func (s *SceneManager) Draw(r *ebiten.Image) error { if s.transitionCount == -1 { return s.current.Draw(r) } - transitionFrom.Clear() + if err := transitionFrom.Clear(); err != nil { + return err + } if err := s.current.Draw(transitionFrom); err != nil { return err } - transitionTo.Clear() + if err := transitionTo.Clear(); err != nil { + return err + } if err := s.next.Draw(transitionTo); err != nil { return err } diff --git a/examples/blocks/main.go b/examples/blocks/main.go index cf88456bf..fa659a031 100644 --- a/examples/blocks/main.go +++ b/examples/blocks/main.go @@ -33,7 +33,9 @@ func main() { if err != nil { log.Fatal(err) } - pprof.StartCPUProfile(f) + if err := pprof.StartCPUProfile(f); err != nil { + log.Fatal(err) + } defer pprof.StopCPUProfile() } diff --git a/examples/font/main.go b/examples/font/main.go index 4df44032a..4d16bf4cb 100644 --- a/examples/font/main.go +++ b/examples/font/main.go @@ -52,7 +52,9 @@ func parseFont() error { if err != nil { return err } - defer f.Close() + defer func() { + _ = f.Close() + }() b, err := ioutil.ReadAll(f) if err != nil { return err diff --git a/examples/gamepad/main.go b/examples/gamepad/main.go index e0ef60ba5..a0904aa24 100644 --- a/examples/gamepad/main.go +++ b/examples/gamepad/main.go @@ -54,7 +54,9 @@ func update(screen *ebiten.Image) error { Pressed Buttons: {{.Buttons}}` str = strings.Replace(str, "{{.Axes}}", strings.Join(axes, "\n "), -1) str = strings.Replace(str, "{{.Buttons}}", strings.Join(pressedButtons, ", "), -1) - ebitenutil.DebugPrint(screen, str) + if err := ebitenutil.DebugPrint(screen, str); err != nil { + return err + } return nil } diff --git a/examples/keyboard/main.go b/examples/keyboard/main.go index 9bfedb222..56c1577d0 100644 --- a/examples/keyboard/main.go +++ b/examples/keyboard/main.go @@ -84,7 +84,9 @@ func update(screen *ebiten.Image) error { op := &ebiten.DrawImageOptions{} op.GeoM.Translate(offsetX, offsetY) op.ColorM.Scale(0.5, 0.5, 0.5, 1) - screen.DrawImage(keyboardImage, op) + if err := screen.DrawImage(keyboardImage, op); err != nil { + return err + } pressed := []string{} for i := 0; i <= 9; i++ { @@ -112,7 +114,9 @@ func update(screen *ebiten.Image) error { ImageParts: pressedKeysParts(pressed), } op.GeoM.Translate(offsetX, offsetY) - screen.DrawImage(keyboardImage, op) + if err := screen.DrawImage(keyboardImage, op); err != nil { + return err + } return nil } diff --git a/examples/life/main.go b/examples/life/main.go index 007435e22..29553e50a 100644 --- a/examples/life/main.go +++ b/examples/life/main.go @@ -179,7 +179,9 @@ var ( func update(screen *ebiten.Image) error { world.Progress() world.DrawImage(noiseImage) - screen.ReplacePixels(noiseImage.Pix) + if err := screen.ReplacePixels(noiseImage.Pix); err != nil { + return err + } return nil } diff --git a/examples/mosaic/main.go b/examples/mosaic/main.go index 77a31b534..9ee648f54 100644 --- a/examples/mosaic/main.go +++ b/examples/mosaic/main.go @@ -37,10 +37,14 @@ var ( func update(screen *ebiten.Image) error { op := &ebiten.DrawImageOptions{} op.GeoM.Scale(1.0/mosaicRatio, 1.0/mosaicRatio) - gophersRenderTarget.DrawImage(gophersImage, op) + if err := gophersRenderTarget.DrawImage(gophersImage, op); err != nil { + return err + } op = &ebiten.DrawImageOptions{} op.GeoM.Scale(mosaicRatio, mosaicRatio) - screen.DrawImage(gophersRenderTarget, op) + if err := screen.DrawImage(gophersRenderTarget, op); err != nil { + return err + } return nil } diff --git a/examples/noise/main.go b/examples/noise/main.go index 2b1a5ab27..f356e83c2 100644 --- a/examples/noise/main.go +++ b/examples/noise/main.go @@ -56,8 +56,12 @@ func update(screen *ebiten.Image) error { noiseImage.Pix[4*i+2] = uint8(x >> 8) noiseImage.Pix[4*i+3] = 0xff } - screen.ReplacePixels(noiseImage.Pix) - ebitenutil.DebugPrint(screen, fmt.Sprintf("FPS: %f", ebiten.CurrentFPS())) + if err := screen.ReplacePixels(noiseImage.Pix); err != nil { + return err + } + if err := ebitenutil.DebugPrint(screen, fmt.Sprintf("FPS: %f", ebiten.CurrentFPS())); err != nil { + return err + } return nil } diff --git a/examples/paint/main.go b/examples/paint/main.go index a2eecce81..cb78d949c 100644 --- a/examples/paint/main.go +++ b/examples/paint/main.go @@ -105,7 +105,9 @@ func main() { if err != nil { log.Fatal(err) } - canvasImage.Fill(color.White) + if err := canvasImage.Fill(color.White); err != nil { + log.Fatal(err) + } if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Paint (Ebiten Demo)"); err != nil { log.Fatal(err) diff --git a/examples/pcm/main.go b/examples/pcm/main.go index afdda5f66..cca4b17b7 100644 --- a/examples/pcm/main.go +++ b/examples/pcm/main.go @@ -129,7 +129,9 @@ func addNote() error { if err != nil { return err } - p.Play() + if err := p.Play(); err != nil { + return err + } return nil } @@ -142,7 +144,9 @@ func update(screen *ebiten.Image) error { return err } } - ebitenutil.DebugPrint(screen, fmt.Sprintf("FPS: %0.2f", ebiten.CurrentFPS())) + if err := ebitenutil.DebugPrint(screen, fmt.Sprintf("FPS: %0.2f", ebiten.CurrentFPS())); err != nil { + return err + } if err := audioContext.Update(); err != nil { return err } diff --git a/examples/perspective/main.go b/examples/perspective/main.go index e78cc1ec9..5a332e185 100644 --- a/examples/perspective/main.go +++ b/examples/perspective/main.go @@ -60,7 +60,9 @@ func update(screen *ebiten.Image) error { maxWidth := float64(w) + float64(h)*0.75 op.GeoM.Translate(-maxWidth/2, -float64(h)/2) op.GeoM.Translate(screenWidth/2, screenHeight/2) - screen.DrawImage(gophersImage, op) + if err := screen.DrawImage(gophersImage, op); err != nil { + return err + } return nil } diff --git a/examples/piano/main.go b/examples/piano/main.go index 9cf4c9aa6..e86a11cf5 100644 --- a/examples/piano/main.go +++ b/examples/piano/main.go @@ -89,7 +89,9 @@ func addNote(freq float64, vol float64) error { if err != nil { return err } - p.Play() + if err := p.Play(); err != nil { + return err + } return nil } length := len(pcm) * baseFreq / f @@ -110,7 +112,9 @@ func addNote(freq float64, vol float64) error { if err != nil { return err } - p.Play() + if err := p.Play(); err != nil { + return err + } return nil } @@ -161,7 +165,9 @@ func init() { if err != nil { panic(err) } - imageEmpty.Fill(color.White) + if err := imageEmpty.Fill(color.White); err != nil { + panic(err) + } imagePiano, err = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterNearest) if err != nil { panic(err) @@ -177,8 +183,12 @@ func init() { op.GeoM.Scale(float64(width-1)/float64(w), float64(height)/float64(h)) op.GeoM.Translate(float64(x), float64(y)) op.ColorM.Scale(1, 1, 1, 1) - imagePiano.DrawImage(imageEmpty, op) - common.ArcadeFont.DrawText(imagePiano, k, x+8, y+height-16, 1, color.Black) + if err := imagePiano.DrawImage(imageEmpty, op); err != nil { + panic(err) + } + if err := common.ArcadeFont.DrawText(imagePiano, k, x+8, y+height-16, 1, color.Black); err != nil { + panic(err) + } } blackKeys := []string{"Q", "W", "", "R", "T", "", "U", "I", "O"} @@ -193,8 +203,12 @@ func init() { op.GeoM.Scale(float64(width-1)/float64(w), float64(height)/float64(h)) op.GeoM.Translate(float64(x), float64(y)) op.ColorM.Scale(0, 0, 0, 1) - imagePiano.DrawImage(imageEmpty, op) - common.ArcadeFont.DrawText(imagePiano, k, x+8, y+height-16, 1, color.White) + if err := imagePiano.DrawImage(imageEmpty, op); err != nil { + panic(err) + } + if err := common.ArcadeFont.DrawText(imagePiano, k, x+8, y+height-16, 1, color.White); err != nil { + panic(err) + } } } @@ -209,10 +223,16 @@ func update(screen *ebiten.Image) error { } } - screen.Fill(color.RGBA{0x80, 0x80, 0xc0, 0xff}) - screen.DrawImage(imagePiano, nil) + if err := screen.Fill(color.RGBA{0x80, 0x80, 0xc0, 0xff}); err != nil { + return err + } + if err := screen.DrawImage(imagePiano, nil); err != nil { + return err + } - ebitenutil.DebugPrint(screen, fmt.Sprintf("FPS: %0.2f", ebiten.CurrentFPS())) + if err := ebitenutil.DebugPrint(screen, fmt.Sprintf("FPS: %0.2f", ebiten.CurrentFPS())); err != nil { + return err + } if err := audioContext.Update(); err != nil { return err diff --git a/examples/sinewave/main.go b/examples/sinewave/main.go index 1bd049267..05f29d1b5 100644 --- a/examples/sinewave/main.go +++ b/examples/sinewave/main.go @@ -98,7 +98,9 @@ func update(screen *ebiten.Image) error { } } msg := fmt.Sprintf("FPS: %0.2f\nThis is an example using infinite audio stream.", ebiten.CurrentFPS()) - ebitenutil.DebugPrint(screen, msg) + if err := ebitenutil.DebugPrint(screen, msg); err != nil { + return err + } if err := audioContext.Update(); err != nil { return err } diff --git a/examples/windowsize/main.go b/examples/windowsize/main.go index bcd9d6f6f..0ca9f4a9b 100644 --- a/examples/windowsize/main.go +++ b/examples/windowsize/main.go @@ -84,7 +84,9 @@ func update(screen *ebiten.Image) error { ebiten.SetScreenSize(screenWidth, screenHeight) ebiten.SetScreenScale(screenScale) - screen.Fill(color.RGBA{0x80, 0x80, 0xc0, 0xff}) + if err := screen.Fill(color.RGBA{0x80, 0x80, 0xc0, 0xff}); err != nil { + return err + } w, h := gophersImage.Size() w2, h2 := screen.Size() op := &ebiten.DrawImageOptions{} @@ -98,7 +100,9 @@ func update(screen *ebiten.Image) error { Press S key to change the window scale Cursor: (%d, %d) FPS: %0.2f`, x, y, ebiten.CurrentFPS()) - ebitenutil.DebugPrint(screen, msg) + if err := ebitenutil.DebugPrint(screen, msg); err != nil { + return err + } return nil }