Refactoring

This commit is contained in:
Hajime Hoshi 2014-01-11 11:42:23 +09:00
parent 738c06c28d
commit 172dd4bca4
5 changed files with 56 additions and 67 deletions

View File

@ -43,7 +43,7 @@ func (context *Context) Dispose() {
context.ids.DeleteRenderTarget(context.screenId)
}
func (context *Context) update(draw func(graphics.Context)) {
func (context *Context) Update(draw func(graphics.Context)) {
context.ResetOffscreen()
context.Clear()

View File

@ -1,37 +0,0 @@
package opengl
import (
"github.com/hajimehoshi/go-ebiten/graphics"
"image"
)
type Device struct {
ids *ids
}
func NewDevice() *Device {
device := &Device{
ids: newIds(),
}
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) {
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

@ -0,0 +1,29 @@
package opengl
import (
"github.com/hajimehoshi/go-ebiten/graphics"
"image"
)
type SharedContext struct {
ids *ids
}
func NewSharedContext() *SharedContext {
device := &SharedContext{
ids: newIds(),
}
return device
}
func (s *SharedContext) CreateContext(screenWidth, screenHeight, screenScale int) *Context {
return newContext(s.ids, screenWidth, screenHeight, screenScale)
}
func (s *SharedContext) CreateRenderTarget(width, height int) (graphics.RenderTargetId, error) {
return s.ids.CreateRenderTarget(width, height, graphics.FilterLinear)
}
func (s *SharedContext) CreateTexture(img image.Image, filter graphics.Filter) (graphics.TextureId, error) {
return s.ids.CreateTexture(img, filter)
}

View File

@ -25,17 +25,16 @@ import (
)
type GameWindow struct {
graphicsDevice *opengl.Device
screenWidth int
screenHeight int
screenScale int
title string
native *C.EbitenGameWindow
pressedKeys map[ui.Key]struct{}
funcs chan func(*opengl.Context)
funcsDone chan struct{}
closed chan struct{}
events chan interface{}
screenWidth int
screenHeight int
screenScale int
title string
native *C.EbitenGameWindow
pressedKeys map[ui.Key]struct{}
funcs chan func(*opengl.Context)
funcsDone chan struct{}
closed chan struct{}
events chan interface{}
}
var windows = map[*C.EbitenGameWindow]*GameWindow{}
@ -53,7 +52,7 @@ func newGameWindow(width, height, scale int, title string) *GameWindow {
}
}
func (w *GameWindow) run(graphicsDevice *opengl.Device, sharedGLContext *C.NSOpenGLContext) {
func (w *GameWindow) run(graphicsSharedContext *opengl.SharedContext, sharedGLContext *C.NSOpenGLContext) {
cTitle := C.CString(w.title)
defer C.free(unsafe.Pointer(cTitle))
@ -61,30 +60,28 @@ func (w *GameWindow) run(graphicsDevice *opengl.Device, sharedGLContext *C.NSOpe
go func() {
runtime.LockOSThread()
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),
cTitle,
glContext)
windows[w.native] = w
close(ch)
w.loop(glContext)
}()
<-ch
}
func (w *GameWindow) loop(glContext *C.NSOpenGLContext) {
C.UseGLContext(glContext)
context := w.graphicsDevice.CreateContext(
w.screenWidth, w.screenHeight, w.screenScale)
C.UnuseGLContext()
C.UseGLContext(glContext)
context := graphicsSharedContext.CreateContext(
w.screenWidth, w.screenHeight, w.screenScale)
C.UnuseGLContext()
w.loop(context, glContext)
defer func() {
C.UseGLContext(glContext)
context.Dispose()
C.UnuseGLContext()
}()
<-ch
}
func (w *GameWindow) loop(context *opengl.Context, glContext *C.NSOpenGLContext) {
for {
select {
case <-w.closed:
@ -100,7 +97,7 @@ func (w *GameWindow) loop(glContext *C.NSOpenGLContext) {
func (w *GameWindow) Draw(f func(graphics.Context)) {
w.useGLContext(func(context *opengl.Context) {
w.graphicsDevice.Update(context, f)
context.Update(f)
})
}

View File

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