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)
C.glFlush()
context.SetOffscreen(context.mainId)
context.Clear()

View File

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

View File

@ -17,7 +17,7 @@ type RenderTargetCreatedEvent struct {
}
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)
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)
defer C.free(unsafe.Pointer(cTitle))
ch := make(chan struct{})
go func() {
runtime.LockOSThread()
glContext := C.CreateGLContext(sharedContext)
glContext := C.CreateGLContext(sharedGLContext)
w.graphicsDevice = graphicsDevice
w.native = C.CreateGameWindow(C.size_t(w.screenWidth*w.screenScale),
C.size_t(w.screenHeight*w.screenScale),

View File

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

View File

@ -14,7 +14,7 @@ import (
)
type cocoaUI struct {
textureFactory *textureFactory
sharedContext *sharedContext
}
var currentUI *cocoaUI
@ -25,7 +25,7 @@ func getCurrentUI() *cocoaUI {
}
currentUI = &cocoaUI{}
currentUI.textureFactory = newTextureFactory()
currentUI.sharedContext = newSharedContext()
return currentUI
}
@ -35,11 +35,11 @@ func UI() ui.UI {
}
func TextureFactory() graphics.TextureFactory {
return getCurrentUI().textureFactory
return getCurrentUI().sharedContext
}
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() {
@ -48,8 +48,16 @@ func (u *cocoaUI) PollEvents() {
func (u *cocoaUI) RunMainLoop() {
C.StartApplication()
currentUI.textureFactory.run()
currentUI.sharedContext.run()
// TODO: Enable the loop
//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)
}*/