diff --git a/example/blocks/blocks/game.go b/example/blocks/blocks/game.go index 6b31c7906..841c5982e 100644 --- a/example/blocks/blocks/game.go +++ b/example/blocks/blocks/game.go @@ -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 } diff --git a/example/blocks/main.go b/example/blocks/main.go index 78a2a8afd..4937804c0 100644 --- a/example/blocks/main.go +++ b/example/blocks/main.go @@ -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) } } diff --git a/example/image/main.go b/example/image/main.go index 6cb0ce62f..986be9f4d 100644 --- a/example/image/main.go +++ b/example/image/main.go @@ -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) } } diff --git a/example/paint/main.go b/example/paint/main.go index 354117e5e..fb5cf4929 100644 --- a/example/paint/main.go +++ b/example/paint/main.go @@ -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) } } diff --git a/run.go b/run.go index 096dc7f4b..533bc6e62 100644 --- a/run.go +++ b/run.go @@ -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 { - return err - } - case <-tick: - if err := game.Update(); err != nil { - return err - } - case <-sigterm: - return nil + if err := ui.draw(f); err != nil { + return err } } }