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 ( import (
"github.com/hajimehoshi/go.ebiten/graphics" "github.com/hajimehoshi/go.ebiten/graphics"
"github.com/hajimehoshi/go.ebiten/graphics/opengl" "runtime"
"time" "time"
) )
@ -30,40 +30,34 @@ type InputState struct {
Y int Y int
} }
func mainLoop(game Game, input <-chan InputState, func mainLoop(game Game, input <-chan InputState, draw <-chan chan graphics.Drawable) {
deviceUpdate chan bool,
gameDraw chan func(graphics.GraphicsContext, graphics.Texture)) {
frameTime := time.Duration(int64(time.Second) / int64(game.Fps())) frameTime := time.Duration(int64(time.Second) / int64(game.Fps()))
updateTick := time.Tick(frameTime) update := time.Tick(frameTime)
for { for {
select { select {
case <-updateTick: case <-update:
inputState := <-input inputState := <-input
game.Update(inputState) game.Update(inputState)
case <-deviceUpdate: case gameDraw := <-draw:
ch := make(chan interface{}) gameDraw <- game
gameDraw <- func(g graphics.GraphicsContext, // TODO: wait!
offscreen graphics.Texture) {
game.Draw(g, offscreen)
close(ch)
}
<-ch
} }
} }
} }
func OpenGLRun(game Game, ui UI, screenScale int, input <-chan InputState) { func Run(game Game, ui UI,
deviceUpdate := make(chan bool) screenScale int,
gameDraw := make(chan func(graphics.GraphicsContext, graphics.Texture)) graphicsDevice graphics.Device,
input <-chan InputState) {
graphicsDevice := opengl.NewDevice( draw := graphicsDevice.Drawing()
game.ScreenWidth(), game.ScreenHeight(),
screenScale, deviceUpdate, gameDraw)
game.Init(graphicsDevice.TextureFactory()) go mainLoop(game, input, draw)
go mainLoop(game, input, deviceUpdate, gameDraw)
// UI should be executed on the main thread. // UI should be executed on the main thread.
ui.Run(graphicsDevice) 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/rotating"
"github.com/hajimehoshi/go.ebiten/example/game/sprites" "github.com/hajimehoshi/go.ebiten/example/game/sprites"
"github.com/hajimehoshi/go.ebiten/graphics" "github.com/hajimehoshi/go.ebiten/graphics"
"github.com/hajimehoshi/go.ebiten/graphics/opengl"
"os" "os"
"runtime" "runtime"
"unsafe" "unsafe"
@ -52,6 +53,7 @@ var currentUI *GlutUI
//export display //export display
func display() { func display() {
// TODO: Use channels?
currentUI.device.Update() currentUI.device.Update()
C.glutSwapBuffers() C.glutSwapBuffers()
} }
@ -79,9 +81,11 @@ func idle() {
C.glutPostRedisplay() C.glutPostRedisplay()
} }
func (ui *GlutUI) Init(screenWidth, screenHeight, screenScale int) { func NewGlutUI(screenWidth, screenHeight, screenScale int) *GlutUI{
ui.screenScale = screenScale ui := &GlutUI{
ui.glutInputEventCh = make(chan GlutInputEvent, 10) screenScale: screenScale,
glutInputEventCh: make(chan GlutInputEvent, 10),
}
cargs := []*C.char{} cargs := []*C.char{}
for _, arg := range os.Args { for _, arg := range os.Args {
@ -105,6 +109,8 @@ func (ui *GlutUI) Init(screenWidth, screenHeight, screenScale int) {
C.glutCreateWindow(title) C.glutCreateWindow(title)
C.setGlutFuncs() C.setGlutFuncs()
return ui
} }
func (ui *GlutUI) Run(device graphics.Device) { func (ui *GlutUI) Run(device graphics.Device) {
@ -137,8 +143,7 @@ func main() {
} }
screenScale := 2 screenScale := 2
currentUI = &GlutUI{} currentUI = NewGlutUI(gm.ScreenWidth(), gm.ScreenHeight(), screenScale)
currentUI.Init(gm.ScreenWidth(), gm.ScreenHeight(), screenScale)
input := make(chan ebiten.InputState) input := make(chan ebiten.InputState)
go func() { 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" "image/color"
) )
type Drawable interface {
Draw(g GraphicsContext, offscreen Texture)
}
type Device interface { type Device interface {
Update() Update()
TextureFactory() TextureFactory TextureFactory() TextureFactory
OffscreenTexture() Texture Drawing() <-chan chan Drawable
} }
type Rect struct { type Rect struct {

View File

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