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

View File

@ -44,7 +44,7 @@ func main() {
} }
game := blocks.NewGame() 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) log.Fatal(err)
} }
} }

View File

@ -33,7 +33,7 @@ type Game struct {
gophersTexture ebiten.TextureID gophersTexture ebiten.TextureID
} }
func (g *Game) Update() error { func (g *Game) Update(gr ebiten.GraphicsContext) error {
if g.gophersTexture.IsNil() { if g.gophersTexture.IsNil() {
file, err := os.Open("images/gophers.jpg") file, err := os.Open("images/gophers.jpg")
if err != nil { if err != nil {
@ -50,10 +50,6 @@ func (g *Game) Update() error {
} }
g.gophersTexture = id g.gophersTexture = id
} }
return nil
}
func (g *Game) Draw(gr ebiten.GraphicsContext) error {
if g.gophersTexture.IsNil() { if g.gophersTexture.IsNil() {
return nil return nil
} }
@ -62,7 +58,8 @@ func (g *Game) Draw(gr ebiten.GraphicsContext) error {
} }
func main() { 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) log.Fatal(err)
} }
} }

View File

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

30
run.go
View File

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