Add syncer

This commit is contained in:
Hajime Hoshi 2014-12-14 21:04:26 +09:00
parent 3190609f07
commit 97857715fa
2 changed files with 30 additions and 18 deletions

View File

@ -16,65 +16,70 @@ limitations under the License.
package ebiten package ebiten
type syncer interface {
Sync(f func())
}
type syncGraphicsContext struct { type syncGraphicsContext struct {
ui *ui syncer syncer
innerGraphicsContext GraphicsContext
} }
var _ GraphicsContext = new(syncGraphicsContext) var _ GraphicsContext = new(syncGraphicsContext)
func (c *syncGraphicsContext) Clear() { func (c *syncGraphicsContext) Clear() {
c.ui.use(func() { c.syncer.Sync(func() {
c.ui.graphicsContext.Clear() c.innerGraphicsContext.Clear()
}) })
} }
func (c *syncGraphicsContext) Fill(r, g, b uint8) { func (c *syncGraphicsContext) Fill(r, g, b uint8) {
c.ui.use(func() { c.syncer.Sync(func() {
c.ui.graphicsContext.Fill(r, g, b) c.innerGraphicsContext.Fill(r, g, b)
}) })
} }
func (c *syncGraphicsContext) Texture(id TextureID) (d Drawer) { func (c *syncGraphicsContext) Texture(id TextureID) (d Drawer) {
c.ui.use(func() { c.syncer.Sync(func() {
d = &drawer{ d = &drawer{
ui: c.ui, syncer: c.syncer,
innerDrawer: c.ui.graphicsContext.Texture(id), innerDrawer: c.innerGraphicsContext.Texture(id),
} }
}) })
return return
} }
func (c *syncGraphicsContext) RenderTarget(id RenderTargetID) (d Drawer) { func (c *syncGraphicsContext) RenderTarget(id RenderTargetID) (d Drawer) {
c.ui.use(func() { c.syncer.Sync(func() {
d = &drawer{ d = &drawer{
ui: c.ui, syncer: c.syncer,
innerDrawer: c.ui.graphicsContext.RenderTarget(id), innerDrawer: c.innerGraphicsContext.RenderTarget(id),
} }
}) })
return return
} }
func (c *syncGraphicsContext) PopRenderTarget() { func (c *syncGraphicsContext) PopRenderTarget() {
c.ui.use(func() { c.syncer.Sync(func() {
c.ui.graphicsContext.PopRenderTarget() c.innerGraphicsContext.PopRenderTarget()
}) })
} }
func (c *syncGraphicsContext) PushRenderTarget(id RenderTargetID) { func (c *syncGraphicsContext) PushRenderTarget(id RenderTargetID) {
c.ui.use(func() { c.syncer.Sync(func() {
c.ui.graphicsContext.PushRenderTarget(id) c.innerGraphicsContext.PushRenderTarget(id)
}) })
} }
type drawer struct { type drawer struct {
ui *ui syncer syncer
innerDrawer Drawer innerDrawer Drawer
} }
var _ Drawer = new(drawer) var _ Drawer = new(drawer)
func (d *drawer) Draw(parts []TexturePart, geo GeometryMatrix, color ColorMatrix) { func (d *drawer) Draw(parts []TexturePart, geo GeometryMatrix, color ColorMatrix) {
d.ui.use(func() { d.syncer.Sync(func() {
d.innerDrawer.Draw(parts, geo, color) d.innerDrawer.Draw(parts, geo, color)
}) })
} }

9
ui.go
View File

@ -87,11 +87,18 @@ func (u *ui) drawGame(game Game) error {
return u.draw(game) return u.draw(game)
} }
func (u *ui) Sync(f func()) {
u.use(f)
}
func (u *ui) draw(game Game) (err error) { func (u *ui) draw(game Game) (err error) {
u.use(func() { u.use(func() {
u.graphicsContext.preUpdate() u.graphicsContext.preUpdate()
}) })
if err = game.Draw(&syncGraphicsContext{u}); err != nil { if err = game.Draw(&syncGraphicsContext{
syncer: u,
innerGraphicsContext: u.graphicsContext,
}); err != nil {
return return
} }
u.use(func() { u.use(func() {