Refactoring

This commit is contained in:
Hajime Hoshi 2013-12-11 22:31:13 +09:00
parent f4e59293fb
commit 82e8849e01
4 changed files with 42 additions and 38 deletions

View File

@ -2,7 +2,6 @@ package main
import ( import (
"github.com/hajimehoshi/go-ebiten/graphics" "github.com/hajimehoshi/go-ebiten/graphics"
"github.com/hajimehoshi/go-ebiten/ui"
"github.com/hajimehoshi/go-ebiten/ui/cocoa" "github.com/hajimehoshi/go-ebiten/ui/cocoa"
"image" "image"
_ "image/png" _ "image/png"
@ -36,15 +35,12 @@ func main() {
const fps = 60 const fps = 60
const title = "Ebiten Demo" const title = "Ebiten Demo"
type UI interface { ui := cocoa.UI()
ui.UI textureFactory := cocoa.TextureFactory()
graphics.TextureFactory window := ui.CreateWindow(screenWidth, screenHeight, screenScale, title)
}
var u UI = cocoa.NewUI()
window := u.CreateWindow(screenWidth, screenHeight, screenScale, title)
textureCreated := u.TextureCreated() textureCreated := textureFactory.TextureCreated()
renderTargetCreated := u.RenderTargetCreated() renderTargetCreated := textureFactory.RenderTargetCreated()
for tag, path := range TexturePaths { for tag, path := range TexturePaths {
tag := tag tag := tag
@ -54,7 +50,7 @@ func main() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
u.CreateTexture(tag, img) textureFactory.CreateTexture(tag, img)
}() }()
} }
@ -62,7 +58,7 @@ func main() {
tag := tag tag := tag
size := size size := size
go func() { go func() {
u.CreateRenderTarget(tag, size.Width, size.Height) textureFactory.CreateRenderTarget(tag, size.Width, size.Height)
}() }()
} }
@ -103,7 +99,7 @@ func main() {
}() }()
for { for {
u.PollEvents() ui.PollEvents()
select { select {
default: default:
window.Draw(func(actualCanvas graphics.Canvas) { window.Draw(func(actualCanvas graphics.Canvas) {

View File

@ -50,6 +50,6 @@ func (t *textureFactory) useContext(f func()) {
<-t.funcsDone <-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) return runWindow(ui, width, height, scale, title, t.sharedContext)
} }

View File

@ -14,40 +14,55 @@ import (
"image" "image"
) )
type UI struct { type cocoaUI struct {
textureFactory *textureFactory textureFactory *textureFactory
graphicsDevice *opengl.Device graphicsDevice *opengl.Device
} }
var currentUI *UI var currentUI *cocoaUI
func NewUI() *UI { func getCurrentUI() *cocoaUI {
if currentUI != nil { if currentUI != nil {
panic("UI can't be duplicated.") return currentUI
} }
u := &UI{}
currentUI = &cocoaUI{}
C.StartApplication() C.StartApplication()
u.textureFactory = runTextureFactory() currentUI.textureFactory = runTextureFactory()
u.textureFactory.useContext(func() { currentUI.textureFactory.useContext(func() {
u.graphicsDevice = opengl.NewDevice() currentUI.graphicsDevice = opengl.NewDevice()
}) })
currentUI = u return currentUI
return u
} }
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) return u.textureFactory.createWindow(u, width, height, scale, title)
} }
func (u *UI) PollEvents() { func (u *cocoaUI) PollEvents() {
C.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() { go func() {
var id graphics.TextureId var id graphics.TextureId
var err error 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() { go func() {
var id graphics.RenderTargetId var id graphics.RenderTargetId
var err error var err error
@ -78,11 +93,3 @@ func (u *UI) CreateRenderTarget(tag interface{}, width, height int) {
u.textureFactory.notifyRenderTargetCreated(e) 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()
}

View File

@ -21,7 +21,7 @@ import (
) )
type Window struct { type Window struct {
ui *UI ui *cocoaUI
screenWidth int screenWidth int
screenHeight int screenHeight int
screenScale int screenScale int
@ -35,7 +35,7 @@ type Window struct {
var windows = map[unsafe.Pointer]*Window{} 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{ w := &Window{
ui: ui, ui: ui,
screenWidth: width, screenWidth: width,
@ -134,4 +134,5 @@ func ebiten_WindowClosed(nativeWindow unsafe.Pointer) {
w := windows[nativeWindow] w := windows[nativeWindow]
w.closed = true w.closed = true
w.notifyWindowClosed(ui.WindowClosedEvent{}) w.notifyWindowClosed(ui.WindowClosedEvent{})
delete(windows, nativeWindow)
} }