Remove Device.Drawing

This commit is contained in:
Hajime Hoshi 2013-10-06 18:22:45 +09:00
parent df84d26636
commit a35c74790c
2 changed files with 20 additions and 26 deletions

View File

@ -32,17 +32,15 @@ import (
) )
type Device struct { type Device struct {
screenScale int screenScale int
context *Context context *Context
drawing chan chan func(graphics.Context)
} }
func NewDevice(screenWidth, screenHeight, screenScale int) *Device { func NewDevice(screenWidth, screenHeight, screenScale int) *Device {
graphicsContext := newContext(screenWidth, screenHeight, screenScale) graphicsContext := newContext(screenWidth, screenHeight, screenScale)
device := &Device{ device := &Device{
screenScale: screenScale, screenScale: screenScale,
context: graphicsContext, context: graphicsContext,
drawing: make(chan chan func(graphics.Context)),
} }
return device return device
} }
@ -51,11 +49,7 @@ func (device *Device) Init() {
device.context.Init() device.context.Init()
} }
func (device *Device) Drawing() <-chan chan func(graphics.Context) { func (device *Device) Update(draw func(graphics.Context)) {
return device.drawing
}
func (device *Device) Update() {
context := device.context context := device.context
C.glEnable(C.GL_TEXTURE_2D) C.glEnable(C.GL_TEXTURE_2D)
C.glTexParameteri(C.GL_TEXTURE_2D, C.GL_TEXTURE_MIN_FILTER, C.GL_NEAREST) C.glTexParameteri(C.GL_TEXTURE_2D, C.GL_TEXTURE_MIN_FILTER, C.GL_NEAREST)
@ -63,10 +57,7 @@ func (device *Device) Update() {
context.SetOffscreen(context.Screen().ID()) context.SetOffscreen(context.Screen().ID())
context.Clear() context.Clear()
ch := make(chan func(graphics.Context)) draw(context)
device.drawing <- ch
drawable := <-ch
drawable(context)
context.flush() context.flush()

View File

@ -58,13 +58,17 @@ type GlutUI struct {
screenScale int screenScale int
glutInputting chan glutInputEvent glutInputting chan glutInputEvent
graphicsDevice *opengl.Device graphicsDevice *opengl.Device
updating chan func(graphics.Context)
updated chan bool
} }
var currentUI *GlutUI var currentUI *GlutUI
//export display //export display
func display() { func display() {
currentUI.graphicsDevice.Update() draw := <-currentUI.updating
currentUI.graphicsDevice.Update(draw)
currentUI.updated <- true
C.glutSwapBuffers() C.glutSwapBuffers()
} }
@ -99,6 +103,8 @@ func new(screenWidth, screenHeight, screenScale int, title string) *GlutUI {
ui := &GlutUI{ ui := &GlutUI{
glutInputting: make(chan glutInputEvent, 10), glutInputting: make(chan glutInputEvent, 10),
graphicsDevice: graphicsDevice, graphicsDevice: graphicsDevice,
updating: make(chan func(graphics.Context)),
updated: make(chan bool),
} }
cargs := []*C.char{} cargs := []*C.char{}
@ -137,7 +143,6 @@ func Run(game ebiten.Game, screenScale int, title string) {
currentUI = ui currentUI = ui
game.Init(ui.graphicsDevice.TextureFactory()) game.Init(ui.graphicsDevice.TextureFactory())
draw := ui.graphicsDevice.Drawing()
input := make(chan ebiten.InputState) input := make(chan ebiten.InputState)
go func() { go func() {
@ -168,22 +173,20 @@ func Run(game ebiten.Game, screenScale int, title string) {
go func() { go func() {
frameTime := time.Duration( frameTime := time.Duration(
int64(time.Second) / int64(game.Fps())) int64(time.Second) / int64(game.Fps()))
update := time.Tick(frameTime) tick := time.Tick(frameTime)
gameContext := &GameContext{ gameContext := &GameContext{
inputState: ebiten.InputState{-1, -1}, inputState: ebiten.InputState{-1, -1},
} }
draw := func(context graphics.Context) {
game.Draw(context)
}
for { for {
select { select {
case gameContext.inputState = <-input: case gameContext.inputState = <-input:
case <-update: case <-tick:
game.Update(gameContext) game.Update(gameContext)
case drawing := <-draw: case ui.updating <- draw:
ch := make(chan interface{}) <-ui.updated
drawing <- func(context graphics.Context) {
game.Draw(context)
close(ch)
}
<-ch
} }
if gameContext.terminated { if gameContext.terminated {
break break