From 6e584eb642574c31dc071b8fbd2c6db938b04c90 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Tue, 25 Jun 2013 10:27:32 +0900 Subject: [PATCH] Add ebiten.InputState --- ebiten.go | 13 ++++-- example/game/blank/blank.go | 7 ++- example/game/monochrome/monochrome.go | 3 +- example/game/rects/rects.go | 3 +- example/game/rotating/rotating.go | 3 +- example/game/sprites/sprites.go | 3 +- example/glut/main.go | 63 +++++++++++++++++++++++++-- 7 files changed, 80 insertions(+), 15 deletions(-) diff --git a/ebiten.go b/ebiten.go index adad7318f..4ec1ac14b 100644 --- a/ebiten.go +++ b/ebiten.go @@ -16,7 +16,7 @@ type Game interface { ScreenHeight() int Fps() int Init(tf graphics.TextureFactory) - Update() + Update(input InputState) Draw(g graphics.GraphicsContext, offscreen graphics.Texture) } @@ -24,7 +24,13 @@ type UI interface { Run(device graphics.Device) } -func OpenGLRun(game Game, ui UI, screenScale int) { +type InputState struct { + IsTapped bool + X int + Y int +} + +func OpenGLRun(game Game, ui UI, screenScale int, input chan InputState) { ch := make(chan bool, 1) graphicsDevice := opengl.NewDevice( game.ScreenWidth(), game.ScreenHeight(), screenScale, @@ -40,7 +46,8 @@ func OpenGLRun(game Game, ui UI, screenScale int) { for { <-tick ticket := <-ch - game.Update() + inputState := <-input + game.Update(inputState) ch <- ticket } }() diff --git a/example/game/blank/blank.go b/example/game/blank/blank.go index 136e5a539..ed9ae85c6 100644 --- a/example/game/blank/blank.go +++ b/example/game/blank/blank.go @@ -1,6 +1,7 @@ package blank import ( + "github.com/hajimehoshi/go.ebiten" "github.com/hajimehoshi/go.ebiten/graphics" ) @@ -8,7 +9,7 @@ type Blank struct { } func New() *Blank { - return &Blank{} + return &Blank{} } func (game *Blank) ScreenWidth() int { @@ -26,10 +27,8 @@ func (game *Blank) Fps() int { func (game *Blank) Init(tf graphics.TextureFactory) { } -func (game *Blank) Update() { +func (game *Blank) Update(input ebiten.InputState) { } func (game *Blank) Draw(g graphics.GraphicsContext, offscreen graphics.Texture) { } - - diff --git a/example/game/monochrome/monochrome.go b/example/game/monochrome/monochrome.go index d9821f757..92db81ea4 100644 --- a/example/game/monochrome/monochrome.go +++ b/example/game/monochrome/monochrome.go @@ -1,6 +1,7 @@ package monochrome import ( + "github.com/hajimehoshi/go.ebiten" "github.com/hajimehoshi/go.ebiten/graphics" "github.com/hajimehoshi/go.ebiten/graphics/matrix" "image" @@ -94,7 +95,7 @@ func (game *Monochrome) update() { } } -func (game *Monochrome) Update() { +func (game *Monochrome) Update(input ebiten.InputState) { game.ch <- true <-game.ch } diff --git a/example/game/rects/rects.go b/example/game/rects/rects.go index 250038fc7..575cf3d66 100644 --- a/example/game/rects/rects.go +++ b/example/game/rects/rects.go @@ -1,6 +1,7 @@ package rects import ( + "github.com/hajimehoshi/go.ebiten" "github.com/hajimehoshi/go.ebiten/graphics" "github.com/hajimehoshi/go.ebiten/graphics/matrix" "image/color" @@ -32,7 +33,7 @@ func (game *Rects) Init(tf graphics.TextureFactory) { game.rectsTexture = tf.NewTexture(game.ScreenWidth(), game.ScreenHeight()) } -func (game *Rects) Update() { +func (game *Rects) Update(input ebiten.InputState) { } func (game *Rects) Draw(g graphics.GraphicsContext, offscreen graphics.Texture) { diff --git a/example/game/rotating/rotating.go b/example/game/rotating/rotating.go index e0e481c4d..8f5eff339 100644 --- a/example/game/rotating/rotating.go +++ b/example/game/rotating/rotating.go @@ -1,6 +1,7 @@ package rotating import ( + "github.com/hajimehoshi/go.ebiten" "github.com/hajimehoshi/go.ebiten/graphics" "github.com/hajimehoshi/go.ebiten/graphics/matrix" "image" @@ -47,7 +48,7 @@ func (game *Rotating) Init(tf graphics.TextureFactory) { } } -func (game *Rotating) Update() { +func (game *Rotating) Update(input ebiten.InputState) { game.x++ } diff --git a/example/game/sprites/sprites.go b/example/game/sprites/sprites.go index 7d270ae0c..aeb4135d9 100644 --- a/example/game/sprites/sprites.go +++ b/example/game/sprites/sprites.go @@ -1,6 +1,7 @@ package sprites import ( + "github.com/hajimehoshi/go.ebiten" "github.com/hajimehoshi/go.ebiten/graphics" "github.com/hajimehoshi/go.ebiten/graphics/matrix" "image" @@ -104,7 +105,7 @@ func (game *Sprites) Init(tf graphics.TextureFactory) { } } -func (game *Sprites) Update() { +func (game *Sprites) Update(input ebiten.InputState) { for _, sprite := range game.sprites { sprite.Update() } diff --git a/example/glut/main.go b/example/glut/main.go index 8f1194bde..6d0d60df2 100644 --- a/example/glut/main.go +++ b/example/glut/main.go @@ -29,8 +29,23 @@ import ( "unsafe" ) +type GlutInputEventState int + +const ( + GlutInputEventStateUp GlutInputEventState = iota + GlutInputEventStateDown +) + +type GlutInputEvent struct { + State GlutInputEventState + X int + Y int +} + type GlutUI struct { - device graphics.Device + screenScale int + device graphics.Device + glutInputEventCh chan GlutInputEvent } var currentUI *GlutUI @@ -42,8 +57,21 @@ func display() { } //export mouse -func mouse(button, state, x, y C.int) { - +func mouse(button, glutState, x, y C.int) { + var state GlutInputEventState + switch glutState { + case C.GLUT_UP: + state = GlutInputEventStateUp + case C.GLUT_DOWN: + state = GlutInputEventStateDown + default: + panic("invalid glutState") + } + currentUI.glutInputEventCh <- GlutInputEvent{ + State: state, + X: int(x), + Y: int(y), + } } //export idle @@ -52,6 +80,9 @@ func idle() { } func (ui *GlutUI) Init(screenWidth, screenHeight, screenScale int) { + ui.screenScale = screenScale + ui.glutInputEventCh = make(chan GlutInputEvent, 10) + cargs := []*C.char{} for _, arg := range os.Args { cargs = append(cargs, C.CString(arg)) @@ -109,5 +140,29 @@ func main() { currentUI = &GlutUI{} currentUI.Init(gm.ScreenWidth(), gm.ScreenHeight(), screenScale) - ebiten.OpenGLRun(gm, currentUI, screenScale) + input := make(chan ebiten.InputState) + go func() { + ch := currentUI.glutInputEventCh + var inputState ebiten.InputState + for { + select { + case event := <-ch: + switch event.State { + case GlutInputEventStateUp: + inputState.IsTapped = false + inputState.X = 0 + inputState.Y = 0 + case GlutInputEventStateDown: + inputState.IsTapped = true + inputState.X = event.X + inputState.Y = event.Y + } + default: + // do nothing + } + input <- inputState + } + }() + + ebiten.OpenGLRun(gm, currentUI, screenScale, input) }