diff --git a/ebiten.go b/ebiten.go index 3ab958fd4..40d4ea343 100644 --- a/ebiten.go +++ b/ebiten.go @@ -35,6 +35,7 @@ type Game interface { type GameContext interface { InputState() InputState + Terminate() } type InputState struct { diff --git a/example/game/terminate/terminate.go b/example/game/terminate/terminate.go new file mode 100644 index 000000000..94483f384 --- /dev/null +++ b/example/game/terminate/terminate.go @@ -0,0 +1,60 @@ +// Copyright 2013 Hajime Hoshi +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +package terminate + +import ( + "github.com/hajimehoshi/go.ebiten" + "github.com/hajimehoshi/go.ebiten/graphics" +) + +type Terminate struct { + life int +} + +func New() *Terminate { + return &Terminate{60} +} + +func (game *Terminate) ScreenWidth() int { + return 256 +} + +func (game *Terminate) ScreenHeight() int { + return 240 +} + +func (game *Terminate) Fps() int { + return 60 +} + +func (game *Terminate) Init(tf graphics.TextureFactory) { +} + +func (game *Terminate) Update(context ebiten.GameContext) { + game.life-- + if game.life <= 0 { + context.Terminate() + } +} + +func (game *Terminate) Draw(context graphics.Context) { +} + diff --git a/example/main.go b/example/main.go index 86f8170f5..bd039e61d 100644 --- a/example/main.go +++ b/example/main.go @@ -28,6 +28,7 @@ import ( "github.com/hajimehoshi/go.ebiten/example/game/rects" "github.com/hajimehoshi/go.ebiten/example/game/rotating" "github.com/hajimehoshi/go.ebiten/example/game/sprites" + "github.com/hajimehoshi/go.ebiten/example/game/terminate" "github.com/hajimehoshi/go.ebiten/ui/glut" "os" "runtime" @@ -55,6 +56,8 @@ func main() { game = rotating.New() case "sprites": game = sprites.New() + case "terminate": + game = terminate.New() default: game = rotating.New() } diff --git a/ui/glut/glut.go b/ui/glut/glut.go index 97ab5a5ed..ab5afbccc 100644 --- a/ui/glut/glut.go +++ b/ui/glut/glut.go @@ -188,7 +188,11 @@ func Run(game ebiten.Game, screenScale int, title string) { } <-ch } + if gameContext.terminated { + break + } } + os.Exit(0) }() C.glutMainLoop() @@ -196,8 +200,13 @@ func Run(game ebiten.Game, screenScale int, title string) { type GameContext struct { inputState ebiten.InputState + terminated bool } func (context *GameContext) InputState() ebiten.InputState { return context.inputState } + +func (context *GameContext) Terminate() { + context.terminated = true +}