mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-24 18:02:02 +01:00
Add ebiten.InputState
This commit is contained in:
parent
fe11e23286
commit
6e584eb642
13
ebiten.go
13
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
|
||||
}
|
||||
}()
|
||||
|
@ -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) {
|
||||
}
|
||||
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -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++
|
||||
}
|
||||
|
||||
|
@ -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()
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user