Simplify Run and Remove Game

This commit is contained in:
Hajime Hoshi 2014-12-17 17:44:55 +09:00
parent 1c460d80c3
commit a95ed2cd2a
5 changed files with 11 additions and 54 deletions

View File

@ -70,7 +70,7 @@ func (game *Game) isInitialized() bool {
return true
}
func (game *Game) Update() error {
func (game *Game) Update(g ebiten.GraphicsContext) error {
game.once.Do(func() {
game.textures = NewTextures()
for name, path := range texturePaths {
@ -88,13 +88,6 @@ func (game *Game) Update() error {
SceneManager: game.sceneManager,
Input: game.input,
})
return nil
}
func (game *Game) Draw(g ebiten.GraphicsContext) error {
if !game.isInitialized() {
return nil
}
game.sceneManager.Draw(g, game.textures)
return nil
}

View File

@ -44,7 +44,7 @@ func main() {
}
game := blocks.NewGame()
if err := ebiten.Run(game, blocks.ScreenWidth, blocks.ScreenHeight, 2, "Blocks (Ebiten Demo)", 60); err != nil {
if err := ebiten.Run(game.Update, blocks.ScreenWidth, blocks.ScreenHeight, 2, "Blocks (Ebiten Demo)"); err != nil {
log.Fatal(err)
}
}

View File

@ -33,7 +33,7 @@ type Game struct {
gophersTexture ebiten.TextureID
}
func (g *Game) Update() error {
func (g *Game) Update(gr ebiten.GraphicsContext) error {
if g.gophersTexture.IsNil() {
file, err := os.Open("images/gophers.jpg")
if err != nil {
@ -50,10 +50,6 @@ func (g *Game) Update() error {
}
g.gophersTexture = id
}
return nil
}
func (g *Game) Draw(gr ebiten.GraphicsContext) error {
if g.gophersTexture.IsNil() {
return nil
}
@ -62,7 +58,8 @@ func (g *Game) Draw(gr ebiten.GraphicsContext) error {
}
func main() {
if err := ebiten.Run(new(Game), screenWidth, screenHeight, 2, "Image (Ebiten Demo)", 60); err != nil {
g := new(Game)
if err := ebiten.Run(g.Update, screenWidth, screenHeight, 2, "Image (Ebiten Demo)"); err != nil {
log.Fatal(err)
}
}

View File

@ -23,7 +23,6 @@ import (
"image/color"
"log"
"math"
"runtime"
)
const (
@ -37,14 +36,10 @@ type Game struct {
canvasRenderTarget ebiten.RenderTargetID
}
func (g *Game) Update() error {
func (g *Game) Update(gr ebiten.GraphicsContext) error {
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
g.count++
}
return nil
}
func (g *Game) Draw(gr ebiten.GraphicsContext) error {
if g.brushRenderTarget.IsNil() {
var err error
g.brushRenderTarget, err = ebiten.NewRenderTargetID(1, 1, ebiten.FilterNearest)
@ -84,13 +79,9 @@ func (g *Game) Draw(gr ebiten.GraphicsContext) error {
return nil
}
func init() {
runtime.LockOSThread()
}
func main() {
game := new(Game)
if err := ebiten.Run(game, screenWidth, screenHeight, 2, "Paint (Ebiten Demo)", 60); err != nil {
g := new(Game)
if err := ebiten.Run(g.Update, screenWidth, screenHeight, 2, "Paint (Ebiten Demo)"); err != nil {
log.Fatal(err)
}
}

28
run.go
View File

@ -17,19 +17,9 @@ limitations under the License.
package ebiten
import (
"os"
"os/signal"
"runtime"
"syscall"
"time"
)
// A Game is the interface that represents a game.
type Game interface {
Update() error
Draw(gr GraphicsContext) error
}
var currentUI *ui
func init() {
@ -38,7 +28,7 @@ func init() {
// Run runs the game.
// This function must be called from the main thread.
func Run(game Game, width, height, scale int, title string, fps int) error {
func Run(f func(GraphicsContext) error, width, height, scale int, title string) error {
ui, err := newUI(width, height, scale, title)
if err != nil {
return err
@ -50,27 +40,13 @@ func Run(game Game, width, height, scale int, title string, fps int) error {
currentUI = nil
}()
frameTime := time.Duration(int64(time.Second) / int64(fps))
tick := time.Tick(frameTime)
sigterm := make(chan os.Signal, 1)
signal.Notify(sigterm, os.Interrupt, syscall.SIGTERM)
for {
ui.doEvents()
if ui.isClosed() {
return nil
}
select {
default:
if err := ui.draw(game.Draw); err != nil {
if err := ui.draw(f); err != nil {
return err
}
case <-tick:
if err := game.Update(); err != nil {
return err
}
case <-sigterm:
return nil
}
}
}