Refactoring

This commit is contained in:
Hajime Hoshi 2014-01-11 18:04:15 +09:00
parent 11a9b77be3
commit ca4275d347
2 changed files with 18 additions and 12 deletions

View File

@ -9,10 +9,16 @@ type SharedContext struct {
ids *ids ids *ids
} }
func NewSharedContext() *SharedContext { var sharedContext *SharedContext = nil
return &SharedContext{
func Initialize() *SharedContext {
if sharedContext != nil {
panic("OpenGL is already initialized")
}
sharedContext = &SharedContext{
ids: newIds(), ids: newIds(),
} }
return sharedContext
} }
func (s *SharedContext) CreateContext(screenWidth, screenHeight, screenScale int) *Context { func (s *SharedContext) CreateContext(screenWidth, screenHeight, screenScale int) *Context {

View File

@ -15,12 +15,12 @@ import (
) )
type sharedContext struct { type sharedContext struct {
inited chan struct{} inited chan struct{}
graphicsSharedContext *opengl.SharedContext sharedContext *opengl.SharedContext
events chan interface{} events chan interface{}
funcs chan func() funcs chan func()
funcsDone chan struct{} funcsDone chan struct{}
gameWindows chan *GameWindow gameWindows chan *GameWindow
} }
func newSharedContext() *sharedContext { func newSharedContext() *sharedContext {
@ -36,7 +36,7 @@ func (t *sharedContext) run() {
var sharedGLContext *C.NSOpenGLContext var sharedGLContext *C.NSOpenGLContext
go func() { go func() {
runtime.LockOSThread() runtime.LockOSThread()
t.graphicsSharedContext = opengl.NewSharedContext() t.sharedContext = opengl.Initialize()
sharedGLContext = C.CreateGLContext(nil) sharedGLContext = C.CreateGLContext(nil)
close(t.inited) close(t.inited)
t.loop(sharedGLContext) t.loop(sharedGLContext)
@ -44,7 +44,7 @@ func (t *sharedContext) run() {
<-t.inited <-t.inited
go func() { go func() {
for w := range t.gameWindows { for w := range t.gameWindows {
w.run(t.graphicsSharedContext, sharedGLContext) w.run(t.sharedContext, sharedGLContext)
} }
}() }()
} }
@ -88,7 +88,7 @@ func (t *sharedContext) CreateTexture(tag interface{}, img image.Image, filter g
var id graphics.TextureId var id graphics.TextureId
var err error var err error
t.useGLContext(func() { t.useGLContext(func() {
id, err = t.graphicsSharedContext.CreateTexture(img, filter) id, err = t.sharedContext.CreateTexture(img, filter)
}) })
if t.events == nil { if t.events == nil {
return return
@ -107,7 +107,7 @@ func (t *sharedContext) CreateRenderTarget(tag interface{}, width, height int) {
var id graphics.RenderTargetId var id graphics.RenderTargetId
var err error var err error
t.useGLContext(func() { t.useGLContext(func() {
id, err = t.graphicsSharedContext.CreateRenderTarget(width, height) id, err = t.sharedContext.CreateRenderTarget(width, height)
}) })
if t.events == nil { if t.events == nil {
return return