mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
Refactoring
This commit is contained in:
parent
738c06c28d
commit
172dd4bca4
@ -43,7 +43,7 @@ func (context *Context) Dispose() {
|
|||||||
context.ids.DeleteRenderTarget(context.screenId)
|
context.ids.DeleteRenderTarget(context.screenId)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (context *Context) update(draw func(graphics.Context)) {
|
func (context *Context) Update(draw func(graphics.Context)) {
|
||||||
context.ResetOffscreen()
|
context.ResetOffscreen()
|
||||||
context.Clear()
|
context.Clear()
|
||||||
|
|
||||||
|
@ -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)
|
|
||||||
}
|
|
29
graphics/opengl/shared_context.go
Normal file
29
graphics/opengl/shared_context.go
Normal 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)
|
||||||
|
}
|
@ -25,17 +25,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type GameWindow struct {
|
type GameWindow struct {
|
||||||
graphicsDevice *opengl.Device
|
screenWidth int
|
||||||
screenWidth int
|
screenHeight int
|
||||||
screenHeight int
|
screenScale int
|
||||||
screenScale int
|
title string
|
||||||
title string
|
native *C.EbitenGameWindow
|
||||||
native *C.EbitenGameWindow
|
pressedKeys map[ui.Key]struct{}
|
||||||
pressedKeys map[ui.Key]struct{}
|
funcs chan func(*opengl.Context)
|
||||||
funcs chan func(*opengl.Context)
|
funcsDone chan struct{}
|
||||||
funcsDone chan struct{}
|
closed chan struct{}
|
||||||
closed chan struct{}
|
events chan interface{}
|
||||||
events chan interface{}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var windows = map[*C.EbitenGameWindow]*GameWindow{}
|
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)
|
cTitle := C.CString(w.title)
|
||||||
defer C.free(unsafe.Pointer(cTitle))
|
defer C.free(unsafe.Pointer(cTitle))
|
||||||
|
|
||||||
@ -61,30 +60,28 @@ func (w *GameWindow) run(graphicsDevice *opengl.Device, sharedGLContext *C.NSOpe
|
|||||||
go func() {
|
go func() {
|
||||||
runtime.LockOSThread()
|
runtime.LockOSThread()
|
||||||
glContext := C.CreateGLContext(sharedGLContext)
|
glContext := C.CreateGLContext(sharedGLContext)
|
||||||
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),
|
||||||
cTitle,
|
cTitle,
|
||||||
glContext)
|
glContext)
|
||||||
windows[w.native] = w
|
windows[w.native] = w
|
||||||
close(ch)
|
close(ch)
|
||||||
w.loop(glContext)
|
|
||||||
}()
|
|
||||||
<-ch
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w *GameWindow) loop(glContext *C.NSOpenGLContext) {
|
C.UseGLContext(glContext)
|
||||||
C.UseGLContext(glContext)
|
context := graphicsSharedContext.CreateContext(
|
||||||
context := w.graphicsDevice.CreateContext(
|
w.screenWidth, w.screenHeight, w.screenScale)
|
||||||
w.screenWidth, w.screenHeight, w.screenScale)
|
C.UnuseGLContext()
|
||||||
C.UnuseGLContext()
|
|
||||||
|
w.loop(context, glContext)
|
||||||
|
|
||||||
defer func() {
|
|
||||||
C.UseGLContext(glContext)
|
C.UseGLContext(glContext)
|
||||||
context.Dispose()
|
context.Dispose()
|
||||||
C.UnuseGLContext()
|
C.UnuseGLContext()
|
||||||
}()
|
}()
|
||||||
|
<-ch
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w *GameWindow) loop(context *opengl.Context, glContext *C.NSOpenGLContext) {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-w.closed:
|
case <-w.closed:
|
||||||
@ -100,7 +97,7 @@ func (w *GameWindow) loop(glContext *C.NSOpenGLContext) {
|
|||||||
|
|
||||||
func (w *GameWindow) Draw(f func(graphics.Context)) {
|
func (w *GameWindow) Draw(f func(graphics.Context)) {
|
||||||
w.useGLContext(func(context *opengl.Context) {
|
w.useGLContext(func(context *opengl.Context) {
|
||||||
w.graphicsDevice.Update(context, f)
|
context.Update(f)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,7 +16,7 @@ import (
|
|||||||
|
|
||||||
type sharedContext struct {
|
type sharedContext struct {
|
||||||
inited chan struct{}
|
inited chan struct{}
|
||||||
graphicsDevice *opengl.Device
|
graphicsSharedContext *opengl.SharedContext
|
||||||
events chan interface{}
|
events chan interface{}
|
||||||
funcs chan func()
|
funcs chan func()
|
||||||
funcsDone chan struct{}
|
funcsDone chan struct{}
|
||||||
@ -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.graphicsDevice = opengl.NewDevice()
|
t.graphicsSharedContext = opengl.NewSharedContext()
|
||||||
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.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 id graphics.TextureId
|
||||||
var err error
|
var err error
|
||||||
t.useGLContext(func() {
|
t.useGLContext(func() {
|
||||||
id, err = t.graphicsDevice.CreateTexture(img, filter)
|
id, err = t.graphicsSharedContext.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.graphicsDevice.CreateRenderTarget(width, height)
|
id, err = t.graphicsSharedContext.CreateRenderTarget(width, height)
|
||||||
})
|
})
|
||||||
if t.events == nil {
|
if t.events == nil {
|
||||||
return
|
return
|
||||||
|
Loading…
Reference in New Issue
Block a user