Add Device.Update

This commit is contained in:
Hajime Hoshi 2013-10-06 01:47:19 +09:00
parent d6cd54eba7
commit df84d26636
4 changed files with 38 additions and 47 deletions

View File

@ -26,12 +26,6 @@ import (
"image/color" "image/color"
) )
type Device interface {
Initializing() <-chan chan func(TextureFactory)
TextureFactory() TextureFactory
Drawing() <-chan chan func(Context)
}
type Rect struct { type Rect struct {
X int X int
Y int Y int

View File

@ -37,6 +37,8 @@ import (
type Context struct { type Context struct {
screen *Texture screen *Texture
screenWidth int
screenHeight int
screenScale int screenScale int
textures map[graphics.TextureID]*Texture textures map[graphics.TextureID]*Texture
currentOffscreen *Texture currentOffscreen *Texture
@ -48,23 +50,28 @@ type Context struct {
// This method should be called on the UI thread. // This method should be called on the UI thread.
func newContext(screenWidth, screenHeight, screenScale int) *Context { func newContext(screenWidth, screenHeight, screenScale int) *Context {
context := &Context{ context := &Context{
screenWidth: screenWidth,
screenHeight: screenHeight,
screenScale: screenScale, screenScale: screenScale,
textures: map[graphics.TextureID]*Texture{}, textures: map[graphics.TextureID]*Texture{},
} }
return context
}
func (context *Context) Init() {
// main framebuffer should be created sooner than any other framebuffers! // main framebuffer should be created sooner than any other framebuffers!
mainFramebuffer := C.GLint(0) mainFramebuffer := C.GLint(0)
C.glGetIntegerv(C.GL_FRAMEBUFFER_BINDING, &mainFramebuffer) C.glGetIntegerv(C.GL_FRAMEBUFFER_BINDING, &mainFramebuffer)
context.mainFramebufferTexture = newVirtualTexture( context.mainFramebufferTexture = newVirtualTexture(
screenWidth*screenScale, context.screenWidth*context.screenScale,
screenHeight*screenScale) context.screenHeight*context.screenScale)
context.mainFramebufferTexture.framebuffer = C.GLuint(mainFramebuffer) context.mainFramebufferTexture.framebuffer = C.GLuint(mainFramebuffer)
initializeShaders() initializeShaders()
context.screen = context.NewTexture(screenWidth, screenHeight).(*Texture) context.screen =
context.NewTexture(context.screenWidth, context.screenHeight).(*Texture)
return context
} }
func (context *Context) Screen() graphics.Texture { func (context *Context) Screen() graphics.Texture {

View File

@ -35,29 +35,22 @@ type Device struct {
screenScale int screenScale int
context *Context context *Context
drawing chan chan func(graphics.Context) drawing chan chan func(graphics.Context)
updating chan chan func()
} }
func NewDevice(screenWidth, screenHeight, screenScale int, updating chan chan func()) *Device { func NewDevice(screenWidth, screenHeight, screenScale int) *Device {
context := newContext(screenWidth, screenHeight, screenScale) graphicsContext := newContext(screenWidth, screenHeight, screenScale)
device := &Device{ device := &Device{
screenScale: screenScale, screenScale: screenScale,
context: graphicsContext,
drawing: make(chan chan func(graphics.Context)), drawing: make(chan chan func(graphics.Context)),
context: context,
updating: updating,
} }
go func() {
for {
ch := <-device.updating
ch <- device.Update
}
}()
return device return device
} }
func (device *Device) Init() {
device.context.Init()
}
func (device *Device) Drawing() <-chan chan func(graphics.Context) { func (device *Device) Drawing() <-chan chan func(graphics.Context) {
return device.drawing return device.drawing
} }

View File

@ -57,17 +57,14 @@ type glutInputEvent struct {
type GlutUI struct { type GlutUI struct {
screenScale int screenScale int
glutInputting chan glutInputEvent glutInputting chan glutInputEvent
updating chan chan func() graphicsDevice *opengl.Device
} }
var currentUI *GlutUI var currentUI *GlutUI
//export display //export display
func display() { func display() {
ch := make(chan func()) currentUI.graphicsDevice.Update()
currentUI.updating <- ch
f := <-ch
f()
C.glutSwapBuffers() C.glutSwapBuffers()
} }
@ -97,9 +94,11 @@ func idle() {
} }
func new(screenWidth, screenHeight, screenScale int, title string) *GlutUI { func new(screenWidth, screenHeight, screenScale int, title string) *GlutUI {
graphicsDevice := opengl.NewDevice(
screenWidth, screenHeight, screenScale)
ui := &GlutUI{ ui := &GlutUI{
glutInputting: make(chan glutInputEvent, 10), glutInputting: make(chan glutInputEvent, 10),
updating: make(chan chan func()), graphicsDevice: graphicsDevice,
} }
cargs := []*C.char{} cargs := []*C.char{}
@ -125,6 +124,8 @@ func new(screenWidth, screenHeight, screenScale int, title string) *GlutUI {
C.setGlutFuncs() C.setGlutFuncs()
graphicsDevice.Init()
return ui return ui
} }
@ -135,12 +136,8 @@ func Run(game ebiten.Game, screenScale int, title string) {
ui := new(screenWidth, screenHeight, screenScale, title) ui := new(screenWidth, screenHeight, screenScale, title)
currentUI = ui currentUI = ui
graphicsDevice := opengl.NewDevice( game.Init(ui.graphicsDevice.TextureFactory())
screenWidth, screenHeight, screenScale, draw := ui.graphicsDevice.Drawing()
ui.updating)
game.Init(graphicsDevice.TextureFactory())
draw := graphicsDevice.Drawing()
input := make(chan ebiten.InputState) input := make(chan ebiten.InputState)
go func() { go func() {