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

View File

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

View File

@ -4,6 +4,7 @@ import (
"github.com/go-gl/gl"
"github.com/hajimehoshi/ebiten/graphics"
"github.com/hajimehoshi/ebiten/graphics/matrix"
"github.com/hajimehoshi/ebiten/ui"
"sync"
)
@ -59,11 +60,13 @@ func (c *Context) Dispose() {
}
// 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.Clear()
draw(c)
if err := drawer.Draw(c); err != nil {
return err
}
c.SetOffscreen(c.defaultId)
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())
flush()
return nil
}
func (c *Context) Clear() {

View File

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

View File

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

View File

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

View File

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