Rename textureFactory -> sharedContext

This commit is contained in:
Hajime Hoshi 2014-01-11 10:34:38 +09:00
parent 96c53234e5
commit 41eb300065
6 changed files with 38 additions and 33 deletions

View File

@ -55,7 +55,6 @@ func (context *Context) update(draw func(graphics.Context)) {
draw(context) draw(context)
C.glFlush()
context.SetOffscreen(context.mainId) context.SetOffscreen(context.mainId)
context.Clear() context.Clear()

View File

@ -16,22 +16,22 @@ func NewDevice() *Device {
return device return device
} }
// called from window
func (d *Device) CreateContext(screenWidth, screenHeight, screenScale int) *Context { func (d *Device) CreateContext(screenWidth, screenHeight, screenScale int) *Context {
return newContext(d.ids, screenWidth, screenHeight, screenScale) return newContext(d.ids, screenWidth, screenHeight, screenScale)
} }
// called from window
func (d *Device) Update(context *Context, draw func(graphics.Context)) { func (d *Device) Update(context *Context, draw func(graphics.Context)) {
context.update(draw) context.update(draw)
} }
// called from ui
func (d *Device) CreateRenderTarget(width, height int) (graphics.RenderTargetId, error) { func (d *Device) CreateRenderTarget(width, height int) (graphics.RenderTargetId, error) {
renderTargetId, err := d.ids.CreateRenderTarget(width, height, graphics.FilterLinear) return d.ids.CreateRenderTarget(width, height, graphics.FilterLinear)
if err != nil {
return 0, err
}
return renderTargetId, nil
} }
// called from ui
func (d *Device) CreateTexture(img image.Image, filter graphics.Filter) (graphics.TextureId, error) { func (d *Device) CreateTexture(img image.Image, filter graphics.Filter) (graphics.TextureId, error) {
return d.ids.CreateTexture(img, filter) return d.ids.CreateTexture(img, filter)
} }

View File

@ -17,7 +17,7 @@ type RenderTargetCreatedEvent struct {
} }
type TextureFactory interface { type TextureFactory interface {
CreateRenderTarget(tag interface{}, width, height int) CreateRenderTarget(tag interface{}, width, height int) // TODO: Add filter
CreateTexture(tag interface{}, img image.Image, filter Filter) CreateTexture(tag interface{}, img image.Image, filter Filter)
Events() <-chan interface{} Events() <-chan interface{}
} }

View File

@ -53,14 +53,14 @@ func newGameWindow(width, height, scale int, title string) *GameWindow {
} }
} }
func (w *GameWindow) run(graphicsDevice *opengl.Device, sharedContext *C.NSOpenGLContext) { func (w *GameWindow) run(graphicsDevice *opengl.Device, sharedGLContext *C.NSOpenGLContext) {
cTitle := C.CString(w.title) cTitle := C.CString(w.title)
defer C.free(unsafe.Pointer(cTitle)) defer C.free(unsafe.Pointer(cTitle))
ch := make(chan struct{}) ch := make(chan struct{})
go func() { go func() {
runtime.LockOSThread() runtime.LockOSThread()
glContext := C.CreateGLContext(sharedContext) glContext := C.CreateGLContext(sharedGLContext)
w.graphicsDevice = graphicsDevice w.graphicsDevice = graphicsDevice
w.native = C.CreateGameWindow(C.size_t(w.screenWidth*w.screenScale), w.native = C.CreateGameWindow(C.size_t(w.screenWidth*w.screenScale),
C.size_t(w.screenHeight*w.screenScale), C.size_t(w.screenHeight*w.screenScale),

View File

@ -14,7 +14,7 @@ import (
"runtime" "runtime"
) )
type textureFactory struct { type sharedContext struct {
inited chan struct{} inited chan struct{}
graphicsDevice *opengl.Device graphicsDevice *opengl.Device
events chan interface{} events chan interface{}
@ -23,8 +23,8 @@ type textureFactory struct {
gameWindows chan *GameWindow gameWindows chan *GameWindow
} }
func newTextureFactory() *textureFactory { func newSharedContext() *sharedContext {
return &textureFactory{ return &sharedContext{
inited: make(chan struct{}), inited: make(chan struct{}),
funcs: make(chan func()), funcs: make(chan func()),
funcsDone: make(chan struct{}), funcsDone: make(chan struct{}),
@ -32,28 +32,28 @@ func newTextureFactory() *textureFactory {
} }
} }
func (t *textureFactory) run() { func (t *sharedContext) run() {
var sharedContext *C.NSOpenGLContext var sharedGLContext *C.NSOpenGLContext
go func() { go func() {
runtime.LockOSThread() runtime.LockOSThread()
t.graphicsDevice = opengl.NewDevice() t.graphicsDevice = opengl.NewDevice()
sharedContext = C.CreateGLContext(nil) sharedGLContext = C.CreateGLContext(nil)
close(t.inited) close(t.inited)
t.loop(sharedContext) t.loop(sharedGLContext)
}() }()
<-t.inited <-t.inited
go func() { go func() {
for w := range t.gameWindows { for w := range t.gameWindows {
w.run(t.graphicsDevice, sharedContext) w.run(t.graphicsDevice, sharedGLContext)
} }
}() }()
} }
func (t *textureFactory) loop(sharedContext *C.NSOpenGLContext) { func (t *sharedContext) loop(sharedGLContext *C.NSOpenGLContext) {
for { for {
select { select {
case f := <-t.funcs: case f := <-t.funcs:
C.UseGLContext(sharedContext) C.UseGLContext(sharedGLContext)
f() f()
C.UnuseGLContext() C.UnuseGLContext()
t.funcsDone <- struct{}{} t.funcsDone <- struct{}{}
@ -61,12 +61,12 @@ func (t *textureFactory) loop(sharedContext *C.NSOpenGLContext) {
} }
} }
func (t *textureFactory) useGLContext(f func()) { func (t *sharedContext) useGLContext(f func()) {
t.funcs <- f t.funcs <- f
<-t.funcsDone <-t.funcsDone
} }
func (t *textureFactory) createGameWindow(width, height, scale int, title string) *GameWindow { func (t *sharedContext) createGameWindow(width, height, scale int, title string) *GameWindow {
w := newGameWindow(width, height, scale, title) w := newGameWindow(width, height, scale, title)
go func() { go func() {
t.gameWindows <- w t.gameWindows <- w
@ -74,7 +74,7 @@ func (t *textureFactory) createGameWindow(width, height, scale int, title string
return w return w
} }
func (t *textureFactory) Events() <-chan interface{} { func (t *sharedContext) Events() <-chan interface{} {
if t.events != nil { if t.events != nil {
return t.events return t.events
} }
@ -82,7 +82,7 @@ func (t *textureFactory) Events() <-chan interface{} {
return t.events return t.events
} }
func (t *textureFactory) CreateTexture(tag interface{}, img image.Image, filter graphics.Filter) { func (t *sharedContext) CreateTexture(tag interface{}, img image.Image, filter graphics.Filter) {
go func() { go func() {
<-t.inited <-t.inited
var id graphics.TextureId var id graphics.TextureId
@ -93,16 +93,15 @@ func (t *textureFactory) CreateTexture(tag interface{}, img image.Image, filter
if t.events == nil { if t.events == nil {
return return
} }
e := graphics.TextureCreatedEvent{ t.events <- graphics.TextureCreatedEvent{
Tag: tag, Tag: tag,
Id: id, Id: id,
Error: err, Error: err,
} }
t.events <- e
}() }()
} }
func (t *textureFactory) CreateRenderTarget(tag interface{}, width, height int) { func (t *sharedContext) CreateRenderTarget(tag interface{}, width, height int) {
go func() { go func() {
<-t.inited <-t.inited
var id graphics.RenderTargetId var id graphics.RenderTargetId
@ -113,11 +112,10 @@ func (t *textureFactory) CreateRenderTarget(tag interface{}, width, height int)
if t.events == nil { if t.events == nil {
return return
} }
e := graphics.RenderTargetCreatedEvent{ t.events <- graphics.RenderTargetCreatedEvent{
Tag: tag, Tag: tag,
Id: id, Id: id,
Error: err, Error: err,
} }
t.events <- e
}() }()
} }

View File

@ -14,7 +14,7 @@ import (
) )
type cocoaUI struct { type cocoaUI struct {
textureFactory *textureFactory sharedContext *sharedContext
} }
var currentUI *cocoaUI var currentUI *cocoaUI
@ -25,7 +25,7 @@ func getCurrentUI() *cocoaUI {
} }
currentUI = &cocoaUI{} currentUI = &cocoaUI{}
currentUI.textureFactory = newTextureFactory() currentUI.sharedContext = newSharedContext()
return currentUI return currentUI
} }
@ -35,11 +35,11 @@ func UI() ui.UI {
} }
func TextureFactory() graphics.TextureFactory { func TextureFactory() graphics.TextureFactory {
return getCurrentUI().textureFactory return getCurrentUI().sharedContext
} }
func (u *cocoaUI) CreateGameWindow(width, height, scale int, title string) ui.GameWindow { func (u *cocoaUI) CreateGameWindow(width, height, scale int, title string) ui.GameWindow {
return u.textureFactory.createGameWindow(width, height, scale, title) return u.sharedContext.createGameWindow(width, height, scale, title)
} }
func (u *cocoaUI) PollEvents() { func (u *cocoaUI) PollEvents() {
@ -48,8 +48,16 @@ func (u *cocoaUI) PollEvents() {
func (u *cocoaUI) RunMainLoop() { func (u *cocoaUI) RunMainLoop() {
C.StartApplication() C.StartApplication()
currentUI.textureFactory.run() currentUI.sharedContext.run()
// TODO: Enable the loop // TODO: Enable the loop
//C.Run() //C.Run()
} }
/*func (u *cocoaUI) CreateTexture(tag interface{}, img image.Image, filter graphics.Filter) {
t.sharedContext.CreateTexture(tag, img, filter)
}
func (u *cocoaUI) CreateRenderTarget(tag interface{}, width, height int) {
t.sharedContext.CreateRenderTarget(tag, width, height)
}*/