diff --git a/example/main.go b/example/main.go index 2a60cec1c..332a992ed 100644 --- a/example/main.go +++ b/example/main.go @@ -2,7 +2,6 @@ package main import ( "github.com/hajimehoshi/go-ebiten/graphics" - "github.com/hajimehoshi/go-ebiten/ui" "github.com/hajimehoshi/go-ebiten/ui/cocoa" "image" _ "image/png" @@ -36,15 +35,12 @@ func main() { const fps = 60 const title = "Ebiten Demo" - type UI interface { - ui.UI - graphics.TextureFactory - } - var u UI = cocoa.NewUI() - window := u.CreateWindow(screenWidth, screenHeight, screenScale, title) + ui := cocoa.UI() + textureFactory := cocoa.TextureFactory() + window := ui.CreateWindow(screenWidth, screenHeight, screenScale, title) - textureCreated := u.TextureCreated() - renderTargetCreated := u.RenderTargetCreated() + textureCreated := textureFactory.TextureCreated() + renderTargetCreated := textureFactory.RenderTargetCreated() for tag, path := range TexturePaths { tag := tag @@ -54,7 +50,7 @@ func main() { if err != nil { panic(err) } - u.CreateTexture(tag, img) + textureFactory.CreateTexture(tag, img) }() } @@ -62,7 +58,7 @@ func main() { tag := tag size := size go func() { - u.CreateRenderTarget(tag, size.Width, size.Height) + textureFactory.CreateRenderTarget(tag, size.Width, size.Height) }() } @@ -103,7 +99,7 @@ func main() { }() for { - u.PollEvents() + ui.PollEvents() select { default: window.Draw(func(actualCanvas graphics.Canvas) { diff --git a/ui/cocoa/texture_factory.go b/ui/cocoa/texture_factory.go index 7946e65d3..3f9c3f0ae 100644 --- a/ui/cocoa/texture_factory.go +++ b/ui/cocoa/texture_factory.go @@ -50,6 +50,6 @@ func (t *textureFactory) useContext(f func()) { <-t.funcsDone } -func (t *textureFactory) createWindow(ui *UI, width, height, scale int, title string) *Window { +func (t *textureFactory) createWindow(ui *cocoaUI, width, height, scale int, title string) *Window { return runWindow(ui, width, height, scale, title, t.sharedContext) } diff --git a/ui/cocoa/ui.go b/ui/cocoa/ui.go index 7437286af..9bfff9124 100644 --- a/ui/cocoa/ui.go +++ b/ui/cocoa/ui.go @@ -14,40 +14,55 @@ import ( "image" ) -type UI struct { +type cocoaUI struct { textureFactory *textureFactory graphicsDevice *opengl.Device } -var currentUI *UI +var currentUI *cocoaUI -func NewUI() *UI { +func getCurrentUI() *cocoaUI { if currentUI != nil { - panic("UI can't be duplicated.") + return currentUI } - u := &UI{} + + currentUI = &cocoaUI{} C.StartApplication() - u.textureFactory = runTextureFactory() - u.textureFactory.useContext(func() { - u.graphicsDevice = opengl.NewDevice() + currentUI.textureFactory = runTextureFactory() + currentUI.textureFactory.useContext(func() { + currentUI.graphicsDevice = opengl.NewDevice() }) - currentUI = u - - return u + return currentUI } -func (u *UI) CreateWindow(width, height, scale int, title string) ui.Window { +func UI() ui.UI { + return getCurrentUI() +} + +func TextureFactory() graphics.TextureFactory { + return getCurrentUI() +} + +func (u *cocoaUI) CreateWindow(width, height, scale int, title string) ui.Window { return u.textureFactory.createWindow(u, width, height, scale, title) } -func (u *UI) PollEvents() { +func (u *cocoaUI) PollEvents() { C.PollEvents() } -func (u *UI) CreateTexture(tag interface{}, img image.Image) { +func (u *cocoaUI) TextureCreated() <-chan graphics.TextureCreatedEvent { + return u.textureFactory.TextureCreated() +} + +func (u *cocoaUI) RenderTargetCreated() <-chan graphics.RenderTargetCreatedEvent { + return u.textureFactory.RenderTargetCreated() +} + +func (u *cocoaUI) CreateTexture(tag interface{}, img image.Image) { go func() { var id graphics.TextureId var err error @@ -63,7 +78,7 @@ func (u *UI) CreateTexture(tag interface{}, img image.Image) { }() } -func (u *UI) CreateRenderTarget(tag interface{}, width, height int) { +func (u *cocoaUI) CreateRenderTarget(tag interface{}, width, height int) { go func() { var id graphics.RenderTargetId var err error @@ -78,11 +93,3 @@ func (u *UI) CreateRenderTarget(tag interface{}, width, height int) { u.textureFactory.notifyRenderTargetCreated(e) }() } - -func (u *UI) TextureCreated() <-chan graphics.TextureCreatedEvent { - return u.textureFactory.TextureCreated() -} - -func (u *UI) RenderTargetCreated() <-chan graphics.RenderTargetCreatedEvent { - return u.textureFactory.RenderTargetCreated() -} diff --git a/ui/cocoa/window.go b/ui/cocoa/window.go index 2b049a17d..245e23d46 100644 --- a/ui/cocoa/window.go +++ b/ui/cocoa/window.go @@ -21,7 +21,7 @@ import ( ) type Window struct { - ui *UI + ui *cocoaUI screenWidth int screenHeight int screenScale int @@ -35,7 +35,7 @@ type Window struct { var windows = map[unsafe.Pointer]*Window{} -func runWindow(ui *UI, width, height, scale int, title string, sharedContext unsafe.Pointer) *Window { +func runWindow(ui *cocoaUI, width, height, scale int, title string, sharedContext unsafe.Pointer) *Window { w := &Window{ ui: ui, screenWidth: width, @@ -134,4 +134,5 @@ func ebiten_WindowClosed(nativeWindow unsafe.Pointer) { w := windows[nativeWindow] w.closed = true w.notifyWindowClosed(ui.WindowClosedEvent{}) + delete(windows, nativeWindow) }