Refactoring

This commit is contained in:
Hajime Hoshi 2013-06-26 01:13:09 +09:00
parent 6b1e76b669
commit a8f42d6497
3 changed files with 32 additions and 85 deletions

View File

@ -30,34 +30,39 @@ type InputState struct {
Y int
}
func mainLoop(game Game, input <-chan InputState,
deviceUpdate chan bool,
gameDraw chan func(graphics.GraphicsContext, graphics.Texture)) {
frameTime := time.Duration(int64(time.Second) / int64(game.Fps()))
updateTick := time.Tick(frameTime)
for {
select {
case <-updateTick:
inputState := <-input
game.Update(inputState)
case <-deviceUpdate:
ch := make(chan interface{})
gameDraw <- func(g graphics.GraphicsContext,
offscreen graphics.Texture) {
game.Draw(g, offscreen)
close(ch)
}
<-ch
}
}
}
func OpenGLRun(game Game, ui UI, screenScale int, input <-chan InputState) {
deviceUpdate := make(chan bool)
commandSet := make(chan chan func(graphics.GraphicsContext))
gameDraw := make(chan func(graphics.GraphicsContext, graphics.Texture))
graphicsDevice := opengl.NewDevice(
game.ScreenWidth(), game.ScreenHeight(), screenScale, deviceUpdate, commandSet)
game.ScreenWidth(), game.ScreenHeight(),
screenScale, deviceUpdate, gameDraw)
game.Init(graphicsDevice.TextureFactory())
go func() {
frameTime := time.Duration(int64(time.Second) / int64(game.Fps()))
updateTick := time.Tick(frameTime)
for {
select {
case <-updateTick:
inputState := <-input
game.Update(inputState)
case <-deviceUpdate:
commands := make(chan func(graphics.GraphicsContext))
commandSet <- commands
g := graphics.NewAsyncGraphicsContext(commands)
// TODO: graphicsDevice is shared by multiple goroutines.
game.Draw(g, graphicsDevice.OffscreenTexture())
close(commands)
}
}
}()
go mainLoop(game, input, deviceUpdate, gameDraw)
// UI should be executed on the main thread.
ui.Run(graphicsDevice)

View File

@ -1,57 +0,0 @@
package graphics
import (
"github.com/hajimehoshi/go.ebiten/graphics/matrix"
"image/color"
)
type AsyncGraphicsContext struct {
ch chan<- func(GraphicsContext)
}
func NewAsyncGraphicsContext(ch chan<- func(GraphicsContext)) *AsyncGraphicsContext {
return &AsyncGraphicsContext{
ch: ch,
}
}
func (g *AsyncGraphicsContext) Clear() {
g.ch <- func(g2 GraphicsContext) {
g2.Clear()
}
}
func (g *AsyncGraphicsContext) Fill(clr color.Color) {
g.ch <- func(g2 GraphicsContext) {
g2.Fill(clr)
}
}
func (g *AsyncGraphicsContext) DrawRect(rect Rect, clr color.Color) {
g.ch <- func(g2 GraphicsContext) {
g2.DrawRect(rect, clr)
}
}
func (g *AsyncGraphicsContext) DrawTexture(textureID TextureID,
geometryMatrix matrix.Geometry,
colorMatrix matrix.Color) {
g.ch <- func(g2 GraphicsContext) {
g2.DrawTexture(textureID, geometryMatrix, colorMatrix)
}
}
func (g *AsyncGraphicsContext) DrawTextureParts(textureID TextureID,
locations []TexturePart,
geometryMatrix matrix.Geometry,
colorMatrix matrix.Color) {
g.ch <- func(g2 GraphicsContext) {
g2.DrawTextureParts(textureID, locations, geometryMatrix, colorMatrix)
}
}
func (g *AsyncGraphicsContext) SetOffscreen(textureID TextureID) {
g.ch <- func(g2 GraphicsContext) {
g2.SetOffscreen(textureID)
}
}

View File

@ -17,12 +17,13 @@ type Device struct {
graphicsContext *GraphicsContext
offscreenTexture graphics.Texture
deviceUpdate chan<- bool
commandSet <-chan chan func(graphics.GraphicsContext)
gameDraw <-chan func(graphics.GraphicsContext, graphics.Texture)
}
func NewDevice(screenWidth, screenHeight, screenScale int,
deviceUpdate chan<- bool,
commandSet <-chan chan func(graphics.GraphicsContext)) *Device {
gameDraw <-chan func(graphics.GraphicsContext, graphics.Texture)) *Device {
graphicsContext := newGraphicsContext(screenWidth, screenHeight, screenScale)
device := &Device{
@ -30,7 +31,7 @@ func NewDevice(screenWidth, screenHeight, screenScale int,
screenHeight: screenHeight,
screenScale: screenScale,
deviceUpdate: deviceUpdate,
commandSet: commandSet,
gameDraw: gameDraw,
graphicsContext: graphicsContext,
}
device.offscreenTexture =
@ -51,10 +52,8 @@ func (device *Device) Update() {
g.Clear()
device.deviceUpdate <- true
commands := <-device.commandSet
for command := range commands {
command(g)
}
f := <-device.gameDraw
f(g, device.offscreenTexture)
g.flush()