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 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 := &ebiten.DrawImageOptions{}
op.ColorM.Scale(1.0, 1.0, 1.0, 0.5) op.ColorM.Scale(1.0, 1.0, 1.0, 0.5)
op.ImageParts = &imageParts{ op.ImageParts = &imageParts{

View File

@ -44,13 +44,17 @@ func init() {
if err != nil { if err != nil {
log.Fatal(err) 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) playerCurrentImage, err = ebiten.NewImage(4, 10, ebiten.FilterNearest)
if err != nil { if err != nil {
log.Fatal(err) 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 { type Player struct {
@ -193,7 +197,9 @@ func update(screen *ebiten.Image) error {
op := &ebiten.DrawImageOptions{} op := &ebiten.DrawImageOptions{}
x, y, w, h := playerBarRect() x, y, w, h := playerBarRect()
op.GeoM.Translate(float64(x), float64(y)) op.GeoM.Translate(float64(x), float64(y))
screen.DrawImage(playerBarImage, op) if err := screen.DrawImage(playerBarImage, op); err != nil {
return err
}
currentTimeStr := "00:00" currentTimeStr := "00:00"
if musicPlayer != nil { if musicPlayer != nil {
c := musicPlayer.audioPlayer.Current() c := musicPlayer.audioPlayer.Current()
@ -209,7 +215,9 @@ func update(screen *ebiten.Image) error {
cy := y - (ch-h)/2 cy := y - (ch-h)/2
op := &ebiten.DrawImageOptions{} op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(cx), float64(cy)) 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 msg := fmt.Sprintf(`FPS: %0.2f
@ -220,7 +228,9 @@ Press Z or X to change volume of the music
if musicPlayer == nil { if musicPlayer == nil {
msg += "\nNow Loading..." msg += "\nNow Loading..."
} }
ebitenutil.DebugPrint(screen, msg) if err := ebitenutil.DebugPrint(screen, msg); err != nil {
return err
}
if err := audioContext.Update(); err != nil { if err := audioContext.Update(); err != nil {
return err return err
} }
@ -273,7 +283,10 @@ func main() {
} }
close(musicCh) close(musicCh)
// TODO: Is this goroutine-safe? // 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 { if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Audio (Ebiten Demo)"); err != nil {
log.Fatal(err) log.Fatal(err)

View File

@ -42,12 +42,16 @@ func NewGame() *Game {
func (game *Game) Update(r *ebiten.Image) error { func (game *Game) Update(r *ebiten.Image) error {
game.input.Update() game.input.Update()
game.sceneManager.Update(&GameState{ if err := game.sceneManager.Update(&GameState{
SceneManager: game.sceneManager, SceneManager: game.sceneManager,
Input: &game.input, Input: &game.input,
}) }); err != nil {
return err
}
if !ebiten.IsRunningSlowly() { if !ebiten.IsRunningSlowly() {
game.sceneManager.Draw(r) if err := game.sceneManager.Draw(r); err != nil {
return err
}
} }
return nil return nil
} }

View File

@ -76,7 +76,9 @@ func (s *GamepadScene) Update(state *GameState) error {
} }
func (s *GamepadScene) Draw(screen *ebiten.Image) 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 { if s.buttonStates == nil {
return nil return nil

View File

@ -74,7 +74,9 @@ func init() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
imageEmpty.Fill(color.White) if err := imageEmpty.Fill(color.White); err != nil {
panic(err)
}
// Background // Background
imageGameBG, _, err = ebitenutil.NewImageFromFile("_resources/images/gophers.jpg", ebiten.FilterLinear) imageGameBG, _, err = ebitenutil.NewImageFromFile("_resources/images/gophers.jpg", ebiten.FilterLinear)
if err != nil { if err != nil {
@ -121,7 +123,9 @@ func init() {
if err != nil { if err != nil {
panic(err) 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 y = (ScreenHeight - blockHeight) / 2
if err := drawTextWithShadowCenter(imageGameover, "GAME OVER", 0, y, 1, color.White, ScreenWidth); err != nil { if err := drawTextWithShadowCenter(imageGameover, "GAME OVER", 0, y, 1, color.White, ScreenWidth); err != nil {
panic(err) panic(err)
@ -248,7 +252,9 @@ func (s *GameScene) addScore(lines int) {
} }
func (s *GameScene) Update(state *GameState) error { func (s *GameScene) Update(state *GameState) error {
s.field.Update() if err := s.field.Update(); err != nil {
return err
}
if s.gameover { if s.gameover {
// TODO: Gamepad key? // TODO: Gamepad key?
@ -392,7 +398,9 @@ func (s *GameScene) Draw(r *ebiten.Image) error {
} }
if s.gameover { if s.gameover {
r.DrawImage(imageGameover, nil) if err := r.DrawImage(imageGameover, nil); err != nil {
return err
}
} }
return nil return nil

View File

@ -72,12 +72,16 @@ func (s *SceneManager) Draw(r *ebiten.Image) error {
if s.transitionCount == -1 { if s.transitionCount == -1 {
return s.current.Draw(r) return s.current.Draw(r)
} }
transitionFrom.Clear() if err := transitionFrom.Clear(); err != nil {
return err
}
if err := s.current.Draw(transitionFrom); err != nil { if err := s.current.Draw(transitionFrom); err != nil {
return err return err
} }
transitionTo.Clear() if err := transitionTo.Clear(); err != nil {
return err
}
if err := s.next.Draw(transitionTo); err != nil { if err := s.next.Draw(transitionTo); err != nil {
return err return err
} }

View File

@ -33,7 +33,9 @@ func main() {
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
pprof.StartCPUProfile(f) if err := pprof.StartCPUProfile(f); err != nil {
log.Fatal(err)
}
defer pprof.StopCPUProfile() defer pprof.StopCPUProfile()
} }

View File

@ -52,7 +52,9 @@ func parseFont() error {
if err != nil { if err != nil {
return err return err
} }
defer f.Close() defer func() {
_ = f.Close()
}()
b, err := ioutil.ReadAll(f) b, err := ioutil.ReadAll(f)
if err != nil { if err != nil {
return err return err

View File

@ -54,7 +54,9 @@ func update(screen *ebiten.Image) error {
Pressed Buttons: {{.Buttons}}` Pressed Buttons: {{.Buttons}}`
str = strings.Replace(str, "{{.Axes}}", strings.Join(axes, "\n "), -1) str = strings.Replace(str, "{{.Axes}}", strings.Join(axes, "\n "), -1)
str = strings.Replace(str, "{{.Buttons}}", strings.Join(pressedButtons, ", "), -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 return nil
} }

View File

@ -84,7 +84,9 @@ func update(screen *ebiten.Image) error {
op := &ebiten.DrawImageOptions{} op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(offsetX, offsetY) op.GeoM.Translate(offsetX, offsetY)
op.ColorM.Scale(0.5, 0.5, 0.5, 1) 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{} pressed := []string{}
for i := 0; i <= 9; i++ { for i := 0; i <= 9; i++ {
@ -112,7 +114,9 @@ func update(screen *ebiten.Image) error {
ImageParts: pressedKeysParts(pressed), ImageParts: pressedKeysParts(pressed),
} }
op.GeoM.Translate(offsetX, offsetY) op.GeoM.Translate(offsetX, offsetY)
screen.DrawImage(keyboardImage, op) if err := screen.DrawImage(keyboardImage, op); err != nil {
return err
}
return nil return nil
} }

View File

@ -179,7 +179,9 @@ var (
func update(screen *ebiten.Image) error { func update(screen *ebiten.Image) error {
world.Progress() world.Progress()
world.DrawImage(noiseImage) world.DrawImage(noiseImage)
screen.ReplacePixels(noiseImage.Pix) if err := screen.ReplacePixels(noiseImage.Pix); err != nil {
return err
}
return nil return nil
} }

View File

@ -37,10 +37,14 @@ var (
func update(screen *ebiten.Image) error { func update(screen *ebiten.Image) error {
op := &ebiten.DrawImageOptions{} op := &ebiten.DrawImageOptions{}
op.GeoM.Scale(1.0/mosaicRatio, 1.0/mosaicRatio) 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 = &ebiten.DrawImageOptions{}
op.GeoM.Scale(mosaicRatio, mosaicRatio) op.GeoM.Scale(mosaicRatio, mosaicRatio)
screen.DrawImage(gophersRenderTarget, op) if err := screen.DrawImage(gophersRenderTarget, op); err != nil {
return err
}
return nil 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+2] = uint8(x >> 8)
noiseImage.Pix[4*i+3] = 0xff noiseImage.Pix[4*i+3] = 0xff
} }
screen.ReplacePixels(noiseImage.Pix) if err := screen.ReplacePixels(noiseImage.Pix); err != nil {
ebitenutil.DebugPrint(screen, fmt.Sprintf("FPS: %f", ebiten.CurrentFPS())) return err
}
if err := ebitenutil.DebugPrint(screen, fmt.Sprintf("FPS: %f", ebiten.CurrentFPS())); err != nil {
return err
}
return nil return nil
} }

View File

@ -105,7 +105,9 @@ func main() {
if err != nil { if err != nil {
log.Fatal(err) 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 { if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Paint (Ebiten Demo)"); err != nil {
log.Fatal(err) log.Fatal(err)

View File

@ -129,7 +129,9 @@ func addNote() error {
if err != nil { if err != nil {
return err return err
} }
p.Play() if err := p.Play(); err != nil {
return err
}
return nil return nil
} }
@ -142,7 +144,9 @@ func update(screen *ebiten.Image) error {
return err 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 { if err := audioContext.Update(); err != nil {
return err return err
} }

View File

@ -60,7 +60,9 @@ func update(screen *ebiten.Image) error {
maxWidth := float64(w) + float64(h)*0.75 maxWidth := float64(w) + float64(h)*0.75
op.GeoM.Translate(-maxWidth/2, -float64(h)/2) op.GeoM.Translate(-maxWidth/2, -float64(h)/2)
op.GeoM.Translate(screenWidth/2, screenHeight/2) op.GeoM.Translate(screenWidth/2, screenHeight/2)
screen.DrawImage(gophersImage, op) if err := screen.DrawImage(gophersImage, op); err != nil {
return err
}
return nil return nil
} }

View File

@ -89,7 +89,9 @@ func addNote(freq float64, vol float64) error {
if err != nil { if err != nil {
return err return err
} }
p.Play() if err := p.Play(); err != nil {
return err
}
return nil return nil
} }
length := len(pcm) * baseFreq / f length := len(pcm) * baseFreq / f
@ -110,7 +112,9 @@ func addNote(freq float64, vol float64) error {
if err != nil { if err != nil {
return err return err
} }
p.Play() if err := p.Play(); err != nil {
return err
}
return nil return nil
} }
@ -161,7 +165,9 @@ func init() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
imageEmpty.Fill(color.White) if err := imageEmpty.Fill(color.White); err != nil {
panic(err)
}
imagePiano, err = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterNearest) imagePiano, err = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterNearest)
if err != nil { if err != nil {
panic(err) panic(err)
@ -177,8 +183,12 @@ func init() {
op.GeoM.Scale(float64(width-1)/float64(w), float64(height)/float64(h)) op.GeoM.Scale(float64(width-1)/float64(w), float64(height)/float64(h))
op.GeoM.Translate(float64(x), float64(y)) op.GeoM.Translate(float64(x), float64(y))
op.ColorM.Scale(1, 1, 1, 1) op.ColorM.Scale(1, 1, 1, 1)
imagePiano.DrawImage(imageEmpty, op) if err := imagePiano.DrawImage(imageEmpty, op); err != nil {
common.ArcadeFont.DrawText(imagePiano, k, x+8, y+height-16, 1, color.Black) 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"} 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.Scale(float64(width-1)/float64(w), float64(height)/float64(h))
op.GeoM.Translate(float64(x), float64(y)) op.GeoM.Translate(float64(x), float64(y))
op.ColorM.Scale(0, 0, 0, 1) op.ColorM.Scale(0, 0, 0, 1)
imagePiano.DrawImage(imageEmpty, op) if err := imagePiano.DrawImage(imageEmpty, op); err != nil {
common.ArcadeFont.DrawText(imagePiano, k, x+8, y+height-16, 1, color.White) 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}) if err := screen.Fill(color.RGBA{0x80, 0x80, 0xc0, 0xff}); err != nil {
screen.DrawImage(imagePiano, 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 { if err := audioContext.Update(); err != nil {
return err 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()) 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 { if err := audioContext.Update(); err != nil {
return err return err
} }

View File

@ -84,7 +84,9 @@ func update(screen *ebiten.Image) error {
ebiten.SetScreenSize(screenWidth, screenHeight) ebiten.SetScreenSize(screenWidth, screenHeight)
ebiten.SetScreenScale(screenScale) 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() w, h := gophersImage.Size()
w2, h2 := screen.Size() w2, h2 := screen.Size()
op := &ebiten.DrawImageOptions{} op := &ebiten.DrawImageOptions{}
@ -98,7 +100,9 @@ func update(screen *ebiten.Image) error {
Press S key to change the window scale Press S key to change the window scale
Cursor: (%d, %d) Cursor: (%d, %d)
FPS: %0.2f`, x, y, ebiten.CurrentFPS()) FPS: %0.2f`, x, y, ebiten.CurrentFPS())
ebitenutil.DebugPrint(screen, msg) if err := ebitenutil.DebugPrint(screen, msg); err != nil {
return err
}
return nil return nil
} }