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