mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 03:08:54 +01:00
Change Game.Update
This commit is contained in:
parent
30cdcfd574
commit
572f4057a8
@ -29,12 +29,12 @@ type Game interface {
|
||||
ScreenHeight() int
|
||||
Fps() int
|
||||
Init(tf graphics.TextureFactory)
|
||||
Update(inputState InputState)
|
||||
Draw(g graphics.Context)
|
||||
Update(context GameContext)
|
||||
Draw(context graphics.Context)
|
||||
}
|
||||
|
||||
type UI interface {
|
||||
Run()
|
||||
type GameContext interface {
|
||||
InputState() InputState
|
||||
}
|
||||
|
||||
type InputState struct {
|
||||
|
@ -47,7 +47,7 @@ func (game *Blank) Fps() int {
|
||||
func (game *Blank) Init(tf graphics.TextureFactory) {
|
||||
}
|
||||
|
||||
func (game *Blank) Update(inputState ebiten.InputState) {
|
||||
func (game *Blank) Update(context ebiten.GameContext) {
|
||||
}
|
||||
|
||||
func (game *Blank) Draw(context graphics.Context) {
|
||||
|
@ -67,8 +67,8 @@ func (game *Input) Init(tf graphics.TextureFactory) {
|
||||
}
|
||||
}
|
||||
|
||||
func (game *Input) Update(inputState ebiten.InputState) {
|
||||
game.inputState = inputState
|
||||
func (game *Input) Update(context ebiten.GameContext) {
|
||||
game.inputState = context.InputState()
|
||||
}
|
||||
|
||||
func (game *Input) Draw(g graphics.Context) {
|
||||
|
@ -115,7 +115,7 @@ func (game *Monochrome) update() {
|
||||
}
|
||||
}
|
||||
|
||||
func (game *Monochrome) Update(inputState ebiten.InputState) {
|
||||
func (game *Monochrome) Update(context ebiten.GameContext) {
|
||||
game.ch <- true
|
||||
<-game.ch
|
||||
}
|
||||
|
@ -53,7 +53,7 @@ func (game *Rects) Init(tf graphics.TextureFactory) {
|
||||
game.rectsTexture = tf.NewTexture(game.ScreenWidth(), game.ScreenHeight())
|
||||
}
|
||||
|
||||
func (game *Rects) Update(inputState ebiten.InputState) {
|
||||
func (game *Rects) Update(context ebiten.GameContext) {
|
||||
}
|
||||
|
||||
func (game *Rects) Draw(g graphics.Context) {
|
||||
|
@ -68,7 +68,7 @@ func (game *Rotating) Init(tf graphics.TextureFactory) {
|
||||
}
|
||||
}
|
||||
|
||||
func (game *Rotating) Update(inputState ebiten.InputState) {
|
||||
func (game *Rotating) Update(context ebiten.GameContext) {
|
||||
game.x++
|
||||
}
|
||||
|
||||
|
@ -125,7 +125,7 @@ func (game *Sprites) Init(tf graphics.TextureFactory) {
|
||||
}
|
||||
}
|
||||
|
||||
func (game *Sprites) Update(inputState ebiten.InputState) {
|
||||
func (game *Sprites) Update(context ebiten.GameContext) {
|
||||
for _, sprite := range game.sprites {
|
||||
sprite.Update()
|
||||
}
|
||||
|
@ -122,6 +122,14 @@ func (ui *GlutUI) Run() {
|
||||
C.glutMainLoop()
|
||||
}
|
||||
|
||||
type GameContext struct {
|
||||
inputState ebiten.InputState
|
||||
}
|
||||
|
||||
func (context *GameContext) InputState() ebiten.InputState {
|
||||
return context.inputState
|
||||
}
|
||||
|
||||
func main() {
|
||||
runtime.GOMAXPROCS(runtime.NumCPU())
|
||||
|
||||
@ -188,12 +196,14 @@ func main() {
|
||||
frameTime := time.Duration(
|
||||
int64(time.Second) / int64(game.Fps()))
|
||||
update := time.Tick(frameTime)
|
||||
inputState := ebiten.InputState{-1, -1}
|
||||
gameContext := &GameContext{
|
||||
inputState: ebiten.InputState{-1, -1},
|
||||
}
|
||||
for {
|
||||
select {
|
||||
case inputState = <-input:
|
||||
case gameContext.inputState = <-input:
|
||||
case <-update:
|
||||
game.Update(inputState)
|
||||
game.Update(gameContext)
|
||||
case drawing := <-draw:
|
||||
ch := make(chan interface{})
|
||||
drawing <- func(context graphics.Context) {
|
||||
|
Loading…
Reference in New Issue
Block a user