loop: Remove dependency on audio

This commit is contained in:
Hajime Hoshi 2017-07-14 01:38:22 +09:00
parent 5d1d0844e1
commit 3d8fc790b6
2 changed files with 26 additions and 6 deletions

View File

@ -37,6 +37,7 @@ import (
"github.com/hajimehoshi/oto" "github.com/hajimehoshi/oto"
"github.com/hajimehoshi/ebiten/internal/clock" "github.com/hajimehoshi/ebiten/internal/clock"
"github.com/hajimehoshi/ebiten/internal/loop"
) )
const FPS = 60 const FPS = 60
@ -224,8 +225,7 @@ func CurrentContext() *Context {
return c return c
} }
// Internal Only? func (c *Context) ping() {
func (c *Context) Ping() {
if c.initCh != nil { if c.initCh != nil {
close(c.initCh) close(c.initCh)
c.initCh = nil c.initCh = nil
@ -237,6 +237,8 @@ func (c *Context) Ping() {
} }
func (c *Context) loop() { func (c *Context) loop() {
loop.RegisterPing(c.ping)
// Initialize oto.Player lazily to enable calling NewContext in an 'init' function. // Initialize oto.Player lazily to enable calling NewContext in an 'init' function.
// Accessing oto.Player functions requires the environment to be already initialized, // Accessing oto.Player functions requires the environment to be already initialized,
// but if Ebiten is used for a shared library, the timing when init functions are called // but if Ebiten is used for a shared library, the timing when init functions are called

View File

@ -18,7 +18,6 @@ import (
"errors" "errors"
"time" "time"
"github.com/hajimehoshi/ebiten/audio"
"github.com/hajimehoshi/ebiten/internal/clock" "github.com/hajimehoshi/ebiten/internal/clock"
"github.com/hajimehoshi/ebiten/internal/sync" "github.com/hajimehoshi/ebiten/internal/sync"
"github.com/hajimehoshi/ebiten/internal/ui" "github.com/hajimehoshi/ebiten/internal/ui"
@ -38,10 +37,14 @@ type runContext struct {
lastUpdated int64 lastUpdated int64
lastFPSUpdated int64 lastFPSUpdated int64
lastClockFrame int64 lastClockFrame int64
ping func()
m sync.RWMutex m sync.RWMutex
} }
var currentRunContext *runContext var (
currentRunContext *runContext
contextInitCh = make(chan struct{})
)
func (c *runContext) startRunning() { func (c *runContext) startRunning() {
c.m.Lock() c.m.Lock()
@ -114,6 +117,8 @@ func Run(g GraphicsContext, width, height int, scale float64, title string, fps
currentRunContext.lastUpdated = n currentRunContext.lastUpdated = n
currentRunContext.lastFPSUpdated = n currentRunContext.lastFPSUpdated = n
close(contextInitCh)
lg := &loopGraphicsContext{currentRunContext, g} lg := &loopGraphicsContext{currentRunContext, g}
if err := ui.Run(width, height, scale, title, lg); err != nil { if err := ui.Run(width, height, scale, title, lg); err != nil {
if _, ok := err.(*ui.RegularTermination); ok { if _, ok := err.(*ui.RegularTermination); ok {
@ -166,12 +171,25 @@ func (c *runContext) updateCount(now int64) int {
return count return count
} }
func RegisterPing(ping func()) {
<-contextInitCh
currentRunContext.registerPing(ping)
}
func (c *runContext) registerPing(ping func()) {
c.m.Lock()
c.ping = ping
c.m.Unlock()
}
func (c *runContext) render(g GraphicsContext) error { func (c *runContext) render(g GraphicsContext) error {
n := now() n := now()
if audio.CurrentContext() != nil { c.m.Lock()
audio.CurrentContext().Ping() if c.ping != nil {
c.ping()
} }
c.m.Unlock()
count := c.updateCount(n) count := c.updateCount(n)
if err := g.UpdateAndDraw(count); err != nil { if err := g.UpdateAndDraw(count); err != nil {