mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 18:58:54 +01:00
Add error checks
This commit is contained in:
parent
6968e898c3
commit
30b521f3b8
@ -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
|
||||
|
@ -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 {
|
||||
|
@ -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 {
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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()
|
||||
|
@ -90,7 +90,6 @@ func (u *userInterface) main(ch <-chan error) error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *userInterface) runOnMainThread(f func() error) error {
|
||||
|
Loading…
Reference in New Issue
Block a user