Remove Canvas

This commit is contained in:
Hajime Hoshi 2014-12-10 10:42:47 +09:00
parent ed46099c1e
commit 00552d91c2
5 changed files with 25 additions and 21 deletions

View File

@ -44,9 +44,9 @@ func main() {
defer pprof.StopCPUProfile() defer pprof.StopCPUProfile()
} }
u := new(glfw.UI) ui := new(glfw.UI)
game := blocks.NewGame() game := blocks.NewGame()
if err := ebiten.Run(u, game, blocks.ScreenWidth, blocks.ScreenHeight, 2, "Blocks (Ebiten Demo)", 60); err != nil { if err := ebiten.Run(ui, game, blocks.ScreenWidth, blocks.ScreenHeight, 2, "Blocks (Ebiten Demo)", 60); err != nil {
log.Fatal(err) log.Fatal(err)
} }
} }

View File

@ -32,7 +32,7 @@ type canvas struct {
funcsDone chan struct{} funcsDone chan struct{}
} }
func (c *canvas) Draw(d ebiten.GraphicsContextDrawer) (err error) { func (c *canvas) draw(d ebiten.GraphicsContextDrawer) (err error) {
c.use(func() { c.use(func() {
c.context.PreUpdate() c.context.PreUpdate()
}) })
@ -46,7 +46,7 @@ func (c *canvas) Draw(d ebiten.GraphicsContextDrawer) (err error) {
return return
} }
func (c *canvas) IsClosed() bool { func (c *canvas) isClosed() bool {
return c.window.ShouldClose() return c.window.ShouldClose()
} }

View File

@ -34,14 +34,14 @@ type UI struct {
canvas *canvas canvas *canvas
} }
func (u *UI) Start(width, height, scale int, title string) (ebiten.Canvas, error) { func (u *UI) Start(width, height, scale int, title string) error {
if !glfw.Init() { if !glfw.Init() {
return nil, errors.New("glfw.Init() fails") return errors.New("glfw.Init() fails")
} }
glfw.WindowHint(glfw.Resizable, glfw.False) glfw.WindowHint(glfw.Resizable, glfw.False)
window, err := glfw.CreateWindow(width*scale, height*scale, title, nil, nil) window, err := glfw.CreateWindow(width*scale, height*scale, title, nil, nil)
if err != nil { if err != nil {
return nil, err return err
} }
c := &canvas{ c := &canvas{
@ -61,11 +61,11 @@ func (u *UI) Start(width, height, scale int, title string) (ebiten.Canvas, error
c.context, err = opengl.Initialize(width, height, realScale) c.context, err = opengl.Initialize(width, height, realScale)
}) })
if err != nil { if err != nil {
return nil, err return err
} }
u.canvas = c u.canvas = c
return c, nil return nil
} }
func (u *UI) DoEvents() { func (u *UI) DoEvents() {
@ -76,3 +76,11 @@ func (u *UI) DoEvents() {
func (u *UI) Terminate() { func (u *UI) Terminate() {
glfw.Terminate() glfw.Terminate()
} }
func (u *UI) Draw(drawer ebiten.GraphicsContextDrawer) error {
return u.canvas.draw(drawer)
}
func (u *UI) IsClosed() bool {
return u.canvas.isClosed()
}

11
run.go
View File

@ -32,8 +32,7 @@ type Game interface {
// calls ui.DoEvent(), game.Update() and game.Draw() at a regular interval, and finally // calls ui.DoEvent(), game.Update() and game.Draw() at a regular interval, and finally
// calls ui.Terminate(). // calls ui.Terminate().
func Run(ui UI, game Game, width, height, scale int, title string, fps int) error { func Run(ui UI, game Game, width, height, scale int, title string, fps int) error {
canvas, err := ui.Start(width, height, scale, title) if err := ui.Start(width, height, scale, title); err != nil {
if err != nil {
return err return err
} }
@ -45,18 +44,18 @@ func Run(ui UI, game Game, width, height, scale int, title string, fps int) erro
defer ui.Terminate() defer ui.Terminate()
for { for {
ui.DoEvents() ui.DoEvents()
if ui.IsClosed() {
return nil
}
select { select {
default: default:
if err := canvas.Draw(game); err != nil { if err := ui.Draw(game); err != nil {
return err return err
} }
case <-tick: case <-tick:
if err := game.Update(); err != nil { if err := game.Update(); err != nil {
return err return err
} }
if canvas.IsClosed() {
return nil
}
case <-sigterm: case <-sigterm:
return nil return nil
} }

9
ui.go
View File

@ -17,16 +17,13 @@ limitations under the License.
package ebiten package ebiten
type UI interface { type UI interface {
Start(widht, height, scale int, title string) (Canvas, error) Start(widht, height, scale int, title string) error
DoEvents() DoEvents()
Terminate() Terminate()
Draw(drawer GraphicsContextDrawer) error
IsClosed() bool
} }
type GraphicsContextDrawer interface { type GraphicsContextDrawer interface {
Draw(d GraphicsContext) error Draw(d GraphicsContext) error
} }
type Canvas interface {
Draw(drawer GraphicsContextDrawer) error
IsClosed() bool
}