Refactoring

This commit is contained in:
Hajime Hoshi 2013-06-27 01:13:09 +09:00
parent a8f42d6497
commit 9dd6fff9ab
4 changed files with 48 additions and 40 deletions

View File

@ -2,7 +2,7 @@ package ebiten
import (
"github.com/hajimehoshi/go.ebiten/graphics"
"github.com/hajimehoshi/go.ebiten/graphics/opengl"
"runtime"
"time"
)
@ -30,40 +30,34 @@ type InputState struct {
Y int
}
func mainLoop(game Game, input <-chan InputState,
deviceUpdate chan bool,
gameDraw chan func(graphics.GraphicsContext, graphics.Texture)) {
func mainLoop(game Game, input <-chan InputState, draw <-chan chan graphics.Drawable) {
frameTime := time.Duration(int64(time.Second) / int64(game.Fps()))
updateTick := time.Tick(frameTime)
update := time.Tick(frameTime)
for {
select {
case <-updateTick:
case <-update:
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
case gameDraw := <-draw:
gameDraw <- game
// TODO: wait!
}
}
}
func OpenGLRun(game Game, ui UI, screenScale int, input <-chan InputState) {
deviceUpdate := make(chan bool)
gameDraw := make(chan func(graphics.GraphicsContext, graphics.Texture))
func Run(game Game, ui UI,
screenScale int,
graphicsDevice graphics.Device,
input <-chan InputState) {
graphicsDevice := opengl.NewDevice(
game.ScreenWidth(), game.ScreenHeight(),
screenScale, deviceUpdate, gameDraw)
draw := graphicsDevice.Drawing()
game.Init(graphicsDevice.TextureFactory())
go mainLoop(game, input, deviceUpdate, gameDraw)
go mainLoop(game, input, draw)
// UI should be executed on the main thread.
ui.Run(graphicsDevice)
}
func init() {
runtime.LockOSThread()
}

View File

@ -24,6 +24,7 @@ import (
"github.com/hajimehoshi/go.ebiten/example/game/rotating"
"github.com/hajimehoshi/go.ebiten/example/game/sprites"
"github.com/hajimehoshi/go.ebiten/graphics"
"github.com/hajimehoshi/go.ebiten/graphics/opengl"
"os"
"runtime"
"unsafe"
@ -52,6 +53,7 @@ var currentUI *GlutUI
//export display
func display() {
// TODO: Use channels?
currentUI.device.Update()
C.glutSwapBuffers()
}
@ -79,9 +81,11 @@ func idle() {
C.glutPostRedisplay()
}
func (ui *GlutUI) Init(screenWidth, screenHeight, screenScale int) {
ui.screenScale = screenScale
ui.glutInputEventCh = make(chan GlutInputEvent, 10)
func NewGlutUI(screenWidth, screenHeight, screenScale int) *GlutUI{
ui := &GlutUI{
screenScale: screenScale,
glutInputEventCh: make(chan GlutInputEvent, 10),
}
cargs := []*C.char{}
for _, arg := range os.Args {
@ -105,6 +109,8 @@ func (ui *GlutUI) Init(screenWidth, screenHeight, screenScale int) {
C.glutCreateWindow(title)
C.setGlutFuncs()
return ui
}
func (ui *GlutUI) Run(device graphics.Device) {
@ -137,8 +143,7 @@ func main() {
}
screenScale := 2
currentUI = &GlutUI{}
currentUI.Init(gm.ScreenWidth(), gm.ScreenHeight(), screenScale)
currentUI = NewGlutUI(gm.ScreenWidth(), gm.ScreenHeight(), screenScale)
input := make(chan ebiten.InputState)
go func() {
@ -164,5 +169,9 @@ func main() {
}
}()
ebiten.OpenGLRun(gm, currentUI, screenScale, input)
graphicsDevice := opengl.NewDevice(
gm.ScreenWidth(), gm.ScreenHeight(), screenScale)
gm.Init(graphicsDevice.TextureFactory())
ebiten.Run(gm, currentUI, screenScale, graphicsDevice, input)
}

View File

@ -6,10 +6,14 @@ import (
"image/color"
)
type Drawable interface {
Draw(g GraphicsContext, offscreen Texture)
}
type Device interface {
Update()
TextureFactory() TextureFactory
OffscreenTexture() Texture
Drawing() <-chan chan Drawable
}
type Rect struct {

View File

@ -16,13 +16,10 @@ type Device struct {
screenScale int
graphicsContext *GraphicsContext
offscreenTexture graphics.Texture
deviceUpdate chan<- bool
gameDraw <-chan func(graphics.GraphicsContext, graphics.Texture)
deviceUpdate chan chan graphics.Drawable
}
func NewDevice(screenWidth, screenHeight, screenScale int,
deviceUpdate chan<- bool,
gameDraw <-chan func(graphics.GraphicsContext, graphics.Texture)) *Device {
func NewDevice(screenWidth, screenHeight, screenScale int) *Device {
graphicsContext := newGraphicsContext(screenWidth, screenHeight, screenScale)
@ -30,8 +27,7 @@ func NewDevice(screenWidth, screenHeight, screenScale int,
screenWidth: screenWidth,
screenHeight: screenHeight,
screenScale: screenScale,
deviceUpdate: deviceUpdate,
gameDraw: gameDraw,
deviceUpdate: make(chan chan graphics.Drawable),
graphicsContext: graphicsContext,
}
device.offscreenTexture =
@ -39,6 +35,10 @@ func NewDevice(screenWidth, screenHeight, screenScale int,
return device
}
func (device *Device) Drawing() <-chan chan graphics.Drawable {
return device.deviceUpdate
}
func (device *Device) OffscreenTexture() graphics.Texture {
return device.offscreenTexture
}
@ -51,9 +51,10 @@ func (device *Device) Update() {
g.SetOffscreen(device.offscreenTexture.ID)
g.Clear()
device.deviceUpdate <- true
f := <-device.gameDraw
f(g, device.offscreenTexture)
ch := make(chan graphics.Drawable)
device.deviceUpdate <- ch
drawable := <-ch
drawable.Draw(g, device.offscreenTexture)
g.flush()