Implement GameContext.Terminate

This commit is contained in:
Hajime Hoshi 2013-07-17 01:42:53 +09:00
parent 70723e1211
commit d6cd54eba7
4 changed files with 73 additions and 0 deletions

View File

@ -35,6 +35,7 @@ type Game interface {
type GameContext interface {
InputState() InputState
Terminate()
}
type InputState struct {

View File

@ -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) {
}

View File

@ -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()
}

View File

@ -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
}