Change APIs to return errors

This commit is contained in:
Hajime Hoshi 2014-12-07 04:14:35 +09:00
parent 53cb2fbce8
commit b27ba795df
7 changed files with 43 additions and 29 deletions

View File

@ -54,7 +54,7 @@ func (game *Game) isInitialized() bool {
var once sync.Once var once sync.Once
func (game *Game) Update() { func (game *Game) Update() error {
once.Do(func() { once.Do(func() {
for name, path := range texturePaths { for name, path := range texturePaths {
game.textures.RequestTexture(name, path) game.textures.RequestTexture(name, path)
@ -64,18 +64,20 @@ func (game *Game) Update() {
} }
}) })
if !game.isInitialized() { if !game.isInitialized() {
return return nil
} }
game.input.Update() game.input.Update()
game.sceneManager.Update(&GameState{ game.sceneManager.Update(&GameState{
SceneManager: game.sceneManager, SceneManager: game.sceneManager,
Input: game.input, Input: game.input,
}) })
return nil
} }
func (game *Game) Draw(context graphics.Context) { func (game *Game) Draw(context graphics.Context) error {
if !game.isInitialized() { if !game.isInitialized() {
return return nil
} }
game.sceneManager.Draw(context, game.textures) game.sceneManager.Draw(context, game.textures)
return nil
} }

View File

@ -18,16 +18,10 @@ type TexturePart struct {
} }
type Drawer interface { type Drawer interface {
Draw(parts []TexturePart, Draw(parts []TexturePart, geometryMatrix matrix.Geometry, colorMatrix matrix.Color)
geometryMatrix matrix.Geometry,
colorMatrix matrix.Color)
} }
func DrawWhole( func DrawWhole(drawer Drawer, width, height int, geo matrix.Geometry, color matrix.Color) {
drawer Drawer,
width, height int,
geo matrix.Geometry,
color matrix.Color) {
parts := []TexturePart{ parts := []TexturePart{
{0, 0, Rect{0, 0, width, height}}, {0, 0, Rect{0, 0, width, height}},
} }

View File

@ -4,6 +4,7 @@ import (
"github.com/go-gl/gl" "github.com/go-gl/gl"
"github.com/hajimehoshi/ebiten/graphics" "github.com/hajimehoshi/ebiten/graphics"
"github.com/hajimehoshi/ebiten/graphics/matrix" "github.com/hajimehoshi/ebiten/graphics/matrix"
"github.com/hajimehoshi/ebiten/ui"
"sync" "sync"
) )
@ -59,11 +60,13 @@ func (c *Context) Dispose() {
} }
// TODO: This interface is confusing: Can we change this? // TODO: This interface is confusing: Can we change this?
func (c *Context) Update(draw func(graphics.Context)) { func (c *Context) Update(drawer ui.Drawer) error {
c.ResetOffscreen() c.ResetOffscreen()
c.Clear() c.Clear()
draw(c) if err := drawer.Draw(c); err != nil {
return err
}
c.SetOffscreen(c.defaultId) c.SetOffscreen(c.defaultId)
c.Clear() c.Clear()
@ -74,6 +77,7 @@ func (c *Context) Update(draw func(graphics.Context)) {
graphics.DrawWhole(c.RenderTarget(c.screenId), c.screenWidth, c.screenHeight, geo, matrix.ColorI()) graphics.DrawWhole(c.RenderTarget(c.screenId), c.screenWidth, c.screenHeight, geo, matrix.ColorI())
flush() flush()
return nil
} }
func (c *Context) Clear() { func (c *Context) Clear() {

View File

@ -43,11 +43,12 @@ func NewCanvas(width, height, scale int, title string) *Canvas {
return canvas return canvas
} }
func (c *Canvas) Draw(f func(graphics.Context)) { func (c *Canvas) Draw(d ui.Drawer) (err error) {
c.use(func() { c.use(func() {
c.context.Update(f) err = c.context.Update(d)
c.window.SwapBuffers() c.window.SwapBuffers()
}) })
return
} }
func (c *Canvas) IsClosed() bool { func (c *Canvas) IsClosed() bool {

View File

@ -1,6 +1,7 @@
package glfw package glfw
import ( import (
"errors"
glfw "github.com/go-gl/glfw3" glfw "github.com/go-gl/glfw3"
"github.com/hajimehoshi/ebiten/ui" "github.com/hajimehoshi/ebiten/ui"
"log" "log"
@ -16,13 +17,14 @@ type UI struct {
canvas *Canvas canvas *Canvas
} }
func (u *UI) Start(width, height, scale int, title string) ui.Canvas { func (u *UI) Start(width, height, scale int, title string) (ui.Canvas, error) {
if !glfw.Init() { if !glfw.Init() {
panic("glfw.Init() fails") // TODO: Use glfw error
return nil, errors.New("glfw.Init() fails")
} }
glfw.WindowHint(glfw.Resizable, glfw.False) glfw.WindowHint(glfw.Resizable, glfw.False)
u.canvas = NewCanvas(width, height, scale, title) u.canvas = NewCanvas(width, height, scale, title)
return u.canvas return u.canvas, nil
} }
func (u *UI) DoEvents() { func (u *UI) DoEvents() {

View File

@ -9,12 +9,15 @@ import (
) )
type Game interface { type Game interface {
Draw(context graphics.Context) Update() error
Update() Draw(context graphics.Context) error
} }
func Run(u UI, game Game, width, height, scale int, title string, fps int) { func Run(u UI, game Game, width, height, scale int, title string, fps int) error {
canvas := u.Start(width, height, scale, title) canvas, err := u.Start(width, height, scale, title)
if err != nil {
return err
}
frameTime := time.Duration(int64(time.Second) / int64(fps)) frameTime := time.Duration(int64(time.Second) / int64(fps))
tick := time.Tick(frameTime) tick := time.Tick(frameTime)
@ -26,14 +29,18 @@ func Run(u UI, game Game, width, height, scale int, title string, fps int) {
u.DoEvents() u.DoEvents()
select { select {
default: default:
canvas.Draw(game.Draw) if err := canvas.Draw(game); err != nil {
return err
}
case <-tick: case <-tick:
game.Update() if err := game.Update(); err != nil {
return err
}
if canvas.IsClosed() { if canvas.IsClosed() {
return return nil
} }
case <-sigterm: case <-sigterm:
return return nil
} }
} }
} }

View File

@ -5,12 +5,16 @@ import (
) )
type UI interface { type UI interface {
Start(widht, height, scale int, title string) Canvas Start(widht, height, scale int, title string) (Canvas, error)
DoEvents() DoEvents()
Terminate() Terminate()
} }
type Drawer interface {
Draw(c graphics.Context) error
}
type Canvas interface { type Canvas interface {
Draw(func(graphics.Context)) Draw(drawer Drawer) error
IsClosed() bool IsClosed() bool
} }