Add error checks

This commit is contained in:
Hajime Hoshi 2016-08-03 01:02:05 +09:00
parent 6968e898c3
commit 30b521f3b8
8 changed files with 35 additions and 17 deletions

View File

@ -34,7 +34,9 @@ func NewImageFromFile(path string, filter ebiten.Filter) (*ebiten.Image, image.I
if err != nil {
return nil, nil, err
}
defer file.Close()
defer func() {
_ = file.Close()
}()
img, _, err := image.Decode(file)
if err != nil {
return nil, nil, err

View File

@ -30,15 +30,17 @@ type Board struct {
tasks []task
}
func NewBoard(size int) *Board {
func NewBoard(size int) (*Board, error) {
b := &Board{
size: size,
tiles: map[*Tile]struct{}{},
}
for i := 0; i < 2; i++ {
addRandomTile(b.tiles, b.size)
if err := addRandomTile(b.tiles, b.size); err != nil {
return nil, err
}
}
return b
return b, nil
}
func (b *Board) tileAt(x, y int) *Tile {

View File

@ -37,11 +37,16 @@ type Game struct {
boardImage *ebiten.Image
}
func NewGame() *Game {
return &Game{
func NewGame() (*Game, error) {
g := &Game{
input: NewInput(),
board: NewBoard(boardSize),
}
var err error
g.board, err = NewBoard(boardSize)
if err != nil {
return nil, err
}
return g, nil
}
func (g *Game) Update() error {

View File

@ -346,6 +346,8 @@ func (t *Tile) Draw(boardImage *ebiten.Image) error {
h := common.ArcadeFont.TextHeight(str) * scale
x = x + (tileSize-w)/2
y = y + (tileSize-h)/2
common.ArcadeFont.DrawText(boardImage, str, x, y, scale, tileColor(v))
if err := common.ArcadeFont.DrawText(boardImage, str, x, y, scale, tileColor(v)); err != nil {
return err
}
return nil
}

View File

@ -22,7 +22,7 @@ import (
)
var (
game = twenty48.NewGame()
game *twenty48.Game
)
func update(screen *ebiten.Image) error {
@ -39,6 +39,11 @@ func update(screen *ebiten.Image) error {
}
func main() {
var err error
game, err = twenty48.NewGame()
if err != nil {
log.Fatal(err)
}
if err := ebiten.Run(update, twenty48.ScreenWidth, twenty48.ScreenHeight, 2, "2048 (Ebiten Demo)"); err != nil {
log.Fatal(err)
}

View File

@ -37,14 +37,20 @@ func update(screen *ebiten.Image) error {
const ox = 10
const oy = 10
screen.Fill(color.NRGBA{0x00, 0x40, 0x80, 0xff})
if err := screen.Fill(color.NRGBA{0x00, 0x40, 0x80, 0xff}); err != nil {
return err
}
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(ox, oy)
screen.DrawImage(ebitenImage, op)
if err := screen.DrawImage(ebitenImage, op); err != nil {
return err
}
op = &ebiten.DrawImageOptions{}
op.GeoM.Translate(ox+float64(w), oy)
op.CompositeMode = ebiten.CompositeModeLighter
screen.DrawImage(ebitenImage, op)
if err := screen.DrawImage(ebitenImage, op); err != nil {
return err
}
return nil
}

View File

@ -101,10 +101,7 @@ func Run(g GraphicsContext, width, height int, scale float64, title string, fps
}
// TODO: Use the error value
defer func() {
if err != nil {
return
}
err = ui.CurrentUI().Terminate()
_ = ui.CurrentUI().Terminate()
}()
n := now()

View File

@ -90,7 +90,6 @@ func (u *userInterface) main(ch <-chan error) error {
return err
}
}
return nil
}
func (u *userInterface) runOnMainThread(f func() error) error {