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"
)
type Device interface {
Initializing() <-chan chan func(TextureFactory)
TextureFactory() TextureFactory
Drawing() <-chan chan func(Context)
}
type Rect struct {
X int
Y int

View File

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

View File

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

View File

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