Separate input and render goroutines
This commit is contained in:
parent
9c2dafd5fc
commit
f29c115db6
10
gfx/gfx.go
10
gfx/gfx.go
|
@ -17,9 +17,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type RenderState struct {
|
type RenderState struct {
|
||||||
Animating bool
|
Frames int
|
||||||
Frames int
|
Frame int
|
||||||
Frame int
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type context struct {
|
type context struct {
|
||||||
|
@ -87,9 +86,8 @@ func Render(rs RenderState, sOld, sNew game.State, w *pixelgl.Window, sb spriteB
|
||||||
renderSpawnPoints(sBatch, sNew.SpawnPoints, w.Bounds(), colors)
|
renderSpawnPoints(sBatch, sNew.SpawnPoints, w.Bounds(), colors)
|
||||||
sBatch.Draw(w)
|
sBatch.Draw(w)
|
||||||
|
|
||||||
rs.Frame++
|
if rs.Frame < rs.Frames {
|
||||||
if rs.Frame > rs.Frames {
|
rs.Frame++
|
||||||
rs.Animating = false
|
|
||||||
}
|
}
|
||||||
return rs
|
return rs
|
||||||
}
|
}
|
||||||
|
|
78
main.go
78
main.go
|
@ -29,8 +29,7 @@ func run() error {
|
||||||
s := game.NewState()
|
s := game.NewState()
|
||||||
|
|
||||||
rs := gfx.RenderState{
|
rs := gfx.RenderState{
|
||||||
Animating: true,
|
Frames: 15,
|
||||||
Frames: 15,
|
|
||||||
}
|
}
|
||||||
sb, err := gfx.NewSpriteBank()
|
sb, err := gfx.NewSpriteBank()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -39,20 +38,30 @@ func run() error {
|
||||||
sOld := s
|
sOld := s
|
||||||
turn := 1
|
turn := 1
|
||||||
|
|
||||||
var (
|
|
||||||
frames = 0
|
|
||||||
second = time.Tick(time.Second)
|
|
||||||
)
|
|
||||||
|
|
||||||
cmdC := make(chan []game.Command)
|
cmdC := make(chan []game.Command)
|
||||||
go func() { cmdC <- game.PollCommands(s) }()
|
go func() { cmdC <- game.PollCommands(s) }()
|
||||||
|
|
||||||
for !w.Closed() {
|
stateCA := make(chan game.State)
|
||||||
if rs.Animating {
|
stateCB := make(chan game.State)
|
||||||
rs = gfx.Render(rs, sOld, s, w, *sb)
|
|
||||||
if !rs.Animating {
|
go func(s game.State, sOld game.State, stateC <-chan game.State) {
|
||||||
sOld = s
|
var (
|
||||||
|
frames = 0
|
||||||
|
second = time.Tick(time.Second)
|
||||||
|
)
|
||||||
|
|
||||||
|
for !w.Closed() {
|
||||||
|
if rs.Frame == rs.Frames {
|
||||||
|
select {
|
||||||
|
case ss := <-stateCA:
|
||||||
|
sOld = s
|
||||||
|
s = ss
|
||||||
|
rs.Frame = 0
|
||||||
|
default:
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rs = gfx.Render(rs, sOld, s, w, *sb)
|
||||||
w.Update()
|
w.Update()
|
||||||
frames++
|
frames++
|
||||||
|
|
||||||
|
@ -62,28 +71,31 @@ func run() error {
|
||||||
frames = 0
|
frames = 0
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
switch {
|
|
||||||
case w.Pressed(pixelgl.KeyQ):
|
|
||||||
return nil
|
|
||||||
case w.Pressed(pixelgl.KeySpace) || true:
|
|
||||||
//log.Printf("TURN %d", turn)
|
|
||||||
rs.Animating = true
|
|
||||||
rs.Frame = 0
|
|
||||||
|
|
||||||
cmds := <-cmdC
|
|
||||||
s = game.UpdateState(s, sOld, cmds)
|
|
||||||
turn++
|
|
||||||
if s.GameOver {
|
|
||||||
s = game.NewState()
|
|
||||||
sOld = s
|
|
||||||
turn = 1
|
|
||||||
}
|
|
||||||
go func() { cmdC <- game.PollCommands(s) }()
|
|
||||||
}
|
|
||||||
|
|
||||||
w.UpdateInput()
|
|
||||||
}
|
}
|
||||||
|
}(s, sOld, stateCA)
|
||||||
|
|
||||||
|
for !w.Closed() {
|
||||||
|
switch {
|
||||||
|
case w.Pressed(pixelgl.KeyQ):
|
||||||
|
return nil
|
||||||
|
case w.Pressed(pixelgl.KeySpace):
|
||||||
|
cmds := <-cmdC
|
||||||
|
s = game.UpdateState(s, sOld, cmds)
|
||||||
|
turn++
|
||||||
|
if s.GameOver {
|
||||||
|
s = game.NewState()
|
||||||
|
sOld = s
|
||||||
|
turn = 1
|
||||||
|
}
|
||||||
|
go func() {
|
||||||
|
s := <-stateCB
|
||||||
|
cmdC <- game.PollCommands(s)
|
||||||
|
}()
|
||||||
|
stateCA <- s
|
||||||
|
stateCB <- s
|
||||||
|
}
|
||||||
|
|
||||||
|
w.UpdateInput()
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue