examples: Add error checks

This commit is contained in:
Hajime Hoshi 2016-08-03 01:42:46 +09:00
parent 8e58f3ce0a
commit 8f1b7a2d0c
19 changed files with 131 additions and 44 deletions

View File

@ -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{

View File

@ -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)

View File

@ -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
}

View File

@ -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

View File

@ -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

View File

@ -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
}

View File

@ -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()
}

View File

@ -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

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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)

View File

@ -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
}

View File

@ -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
}

View File

@ -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

View File

@ -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
}

View File

@ -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
}