mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-26 03:38:55 +01:00
Refactoring: lazy initializing the application
This commit is contained in:
parent
1f1c5cba95
commit
5d2e2476b6
@ -59,7 +59,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
//u.MainLoop()
|
u.RunMainLoop()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
u.PollEvents()
|
u.PollEvents()
|
||||||
|
@ -30,6 +30,7 @@ type GameWindow struct {
|
|||||||
screenWidth int
|
screenWidth int
|
||||||
screenHeight int
|
screenHeight int
|
||||||
screenScale int
|
screenScale int
|
||||||
|
title string
|
||||||
closed bool
|
closed bool
|
||||||
native *C.EbitenGameWindow
|
native *C.EbitenGameWindow
|
||||||
pressedKeys map[ui.Key]struct{}
|
pressedKeys map[ui.Key]struct{}
|
||||||
@ -41,27 +42,30 @@ type GameWindow struct {
|
|||||||
|
|
||||||
var windows = map[*C.EbitenGameWindow]*GameWindow{}
|
var windows = map[*C.EbitenGameWindow]*GameWindow{}
|
||||||
|
|
||||||
func runGameWindow(graphicsDevice *opengl.Device, width, height, scale int, title string, sharedContext *C.NSOpenGLContext) *GameWindow {
|
func newGameWindow(width, height, scale int, title string) *GameWindow {
|
||||||
w := &GameWindow{
|
return &GameWindow{
|
||||||
graphicsDevice: graphicsDevice,
|
screenWidth: width,
|
||||||
screenWidth: width,
|
screenHeight: height,
|
||||||
screenHeight: height,
|
screenScale: scale,
|
||||||
screenScale: scale,
|
title: title,
|
||||||
closed: false,
|
closed: false,
|
||||||
pressedKeys: map[ui.Key]struct{}{},
|
pressedKeys: map[ui.Key]struct{}{},
|
||||||
funcs: make(chan func()),
|
funcs: make(chan func()),
|
||||||
funcsDone: make(chan struct{}),
|
funcsDone: make(chan struct{}),
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
cTitle := C.CString(title)
|
func (w *GameWindow) run(graphicsDevice *opengl.Device, sharedContext *C.NSOpenGLContext) {
|
||||||
|
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(sharedContext)
|
||||||
w.native = C.CreateGameWindow(C.size_t(width*scale),
|
w.graphicsDevice = graphicsDevice
|
||||||
C.size_t(height*scale),
|
w.native = C.CreateGameWindow(C.size_t(w.screenWidth*w.screenScale),
|
||||||
|
C.size_t(w.screenHeight*w.screenScale),
|
||||||
cTitle,
|
cTitle,
|
||||||
glContext)
|
glContext)
|
||||||
windows[w.native] = w
|
windows[w.native] = w
|
||||||
@ -70,9 +74,9 @@ func runGameWindow(graphicsDevice *opengl.Device, width, height, scale int, titl
|
|||||||
}()
|
}()
|
||||||
<-ch
|
<-ch
|
||||||
w.useGLContext(func() {
|
w.useGLContext(func() {
|
||||||
w.context = w.graphicsDevice.CreateContext(width, height, scale)
|
w.context = w.graphicsDevice.CreateContext(
|
||||||
|
w.screenWidth, w.screenHeight, w.screenScale)
|
||||||
})
|
})
|
||||||
return w
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *GameWindow) loop() {
|
func (w *GameWindow) loop() {
|
||||||
|
@ -15,37 +15,45 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type textureFactory struct {
|
type textureFactory struct {
|
||||||
sharedContext *C.NSOpenGLContext
|
inited chan struct{}
|
||||||
graphicsDevice *opengl.Device
|
graphicsDevice *opengl.Device
|
||||||
events chan interface{}
|
events chan interface{}
|
||||||
funcs chan func()
|
funcs chan func()
|
||||||
funcsDone chan struct{}
|
funcsDone chan struct{}
|
||||||
|
gameWindows chan *GameWindow
|
||||||
}
|
}
|
||||||
|
|
||||||
func runTextureFactory() *textureFactory {
|
func newTextureFactory() *textureFactory {
|
||||||
t := &textureFactory{
|
return &textureFactory{
|
||||||
funcs: make(chan func()),
|
inited: make(chan struct{}),
|
||||||
funcsDone: make(chan struct{}),
|
funcs: make(chan func()),
|
||||||
|
funcsDone: make(chan struct{}),
|
||||||
|
gameWindows: make(chan *GameWindow),
|
||||||
}
|
}
|
||||||
ch := make(chan struct{})
|
}
|
||||||
|
|
||||||
|
func (t *textureFactory) run() {
|
||||||
|
var sharedContext *C.NSOpenGLContext
|
||||||
go func() {
|
go func() {
|
||||||
runtime.LockOSThread()
|
runtime.LockOSThread()
|
||||||
t.sharedContext = C.CreateGLContext(nil)
|
|
||||||
close(ch)
|
|
||||||
t.loop()
|
|
||||||
}()
|
|
||||||
<-ch
|
|
||||||
t.useGLContext(func() {
|
|
||||||
t.graphicsDevice = opengl.NewDevice()
|
t.graphicsDevice = opengl.NewDevice()
|
||||||
})
|
sharedContext = C.CreateGLContext(nil)
|
||||||
return t
|
close(t.inited)
|
||||||
|
t.loop(sharedContext)
|
||||||
|
}()
|
||||||
|
<-t.inited
|
||||||
|
go func() {
|
||||||
|
for w := range t.gameWindows {
|
||||||
|
w.run(t.graphicsDevice, sharedContext)
|
||||||
|
}
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *textureFactory) loop() {
|
func (t *textureFactory) loop(sharedContext *C.NSOpenGLContext) {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case f := <-t.funcs:
|
case f := <-t.funcs:
|
||||||
C.UseGLContext(t.sharedContext)
|
C.UseGLContext(sharedContext)
|
||||||
f()
|
f()
|
||||||
C.UnuseGLContext()
|
C.UnuseGLContext()
|
||||||
t.funcsDone <- struct{}{}
|
t.funcsDone <- struct{}{}
|
||||||
@ -59,7 +67,11 @@ func (t *textureFactory) useGLContext(f func()) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *textureFactory) createGameWindow(width, height, scale int, title string) *GameWindow {
|
func (t *textureFactory) createGameWindow(width, height, scale int, title string) *GameWindow {
|
||||||
return runGameWindow(t.graphicsDevice, width, height, scale, title, t.sharedContext)
|
w := newGameWindow(width, height, scale, title)
|
||||||
|
go func() {
|
||||||
|
t.gameWindows <- w
|
||||||
|
}()
|
||||||
|
return w
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *textureFactory) Events() <-chan interface{} {
|
func (t *textureFactory) Events() <-chan interface{} {
|
||||||
@ -72,6 +84,7 @@ func (t *textureFactory) Events() <-chan interface{} {
|
|||||||
|
|
||||||
func (t *textureFactory) CreateTexture(tag interface{}, img image.Image, filter graphics.Filter) {
|
func (t *textureFactory) CreateTexture(tag interface{}, img image.Image, filter graphics.Filter) {
|
||||||
go func() {
|
go func() {
|
||||||
|
<-t.inited
|
||||||
var id graphics.TextureId
|
var id graphics.TextureId
|
||||||
var err error
|
var err error
|
||||||
t.useGLContext(func() {
|
t.useGLContext(func() {
|
||||||
@ -91,6 +104,7 @@ func (t *textureFactory) CreateTexture(tag interface{}, img image.Image, filter
|
|||||||
|
|
||||||
func (t *textureFactory) CreateRenderTarget(tag interface{}, width, height int) {
|
func (t *textureFactory) CreateRenderTarget(tag interface{}, width, height int) {
|
||||||
go func() {
|
go func() {
|
||||||
|
<-t.inited
|
||||||
var id graphics.RenderTargetId
|
var id graphics.RenderTargetId
|
||||||
var err error
|
var err error
|
||||||
t.useGLContext(func() {
|
t.useGLContext(func() {
|
||||||
|
@ -25,10 +25,8 @@ func getCurrentUI() *cocoaUI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
currentUI = &cocoaUI{}
|
currentUI = &cocoaUI{}
|
||||||
|
currentUI.textureFactory = newTextureFactory()
|
||||||
|
|
||||||
C.StartApplication()
|
|
||||||
|
|
||||||
currentUI.textureFactory = runTextureFactory()
|
|
||||||
return currentUI
|
return currentUI
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,6 +46,10 @@ func (u *cocoaUI) PollEvents() {
|
|||||||
C.PollEvents()
|
C.PollEvents()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (u *cocoaUI) MainLoop() {
|
func (u *cocoaUI) RunMainLoop() {
|
||||||
C.Run()
|
C.StartApplication()
|
||||||
|
currentUI.textureFactory.run()
|
||||||
|
|
||||||
|
// TODO: Enable the loop
|
||||||
|
//C.Run()
|
||||||
}
|
}
|
||||||
|
2
ui/ui.go
2
ui/ui.go
@ -35,7 +35,7 @@ type WindowClosedEvent struct {
|
|||||||
type UI interface {
|
type UI interface {
|
||||||
PollEvents()
|
PollEvents()
|
||||||
CreateGameWindow(screenWidth, screenHeight, screenScale int, title string) GameWindow
|
CreateGameWindow(screenWidth, screenHeight, screenScale int, title string) GameWindow
|
||||||
MainLoop()
|
RunMainLoop()
|
||||||
}
|
}
|
||||||
|
|
||||||
type Window interface {
|
type Window interface {
|
||||||
|
Loading…
Reference in New Issue
Block a user