internal/hook: rename hooks -> hook

This commit is contained in:
Hajime Hoshi 2023-10-06 13:49:42 +09:00
parent 3d4dc239bc
commit 82f2319020
10 changed files with 36 additions and 37 deletions

View File

@ -42,7 +42,7 @@ import (
"time" "time"
"github.com/hajimehoshi/ebiten/v2/audio/internal/convert" "github.com/hajimehoshi/ebiten/v2/audio/internal/convert"
"github.com/hajimehoshi/ebiten/v2/internal/hooks" "github.com/hajimehoshi/ebiten/v2/internal/hook"
) )
const ( const (
@ -438,33 +438,33 @@ func (p *Player) SetBufferSize(bufferSize time.Duration) {
p.p.SetBufferSize(bufferSize) p.p.SetBufferSize(bufferSize)
} }
type hook interface { type hooker interface {
OnSuspendAudio(f func() error) OnSuspendAudio(f func() error)
OnResumeAudio(f func() error) OnResumeAudio(f func() error)
AppendHookOnBeforeUpdate(f func() error) AppendHookOnBeforeUpdate(f func() error)
} }
var hookForTesting hook var hookerForTesting hooker
func getHook() hook { func getHook() hooker {
if hookForTesting != nil { if hookerForTesting != nil {
return hookForTesting return hookerForTesting
} }
return &hookImpl{} return &hookerImpl{}
} }
type hookImpl struct{} type hookerImpl struct{}
func (h *hookImpl) OnSuspendAudio(f func() error) { func (h *hookerImpl) OnSuspendAudio(f func() error) {
hooks.OnSuspendAudio(f) hook.OnSuspendAudio(f)
} }
func (h *hookImpl) OnResumeAudio(f func() error) { func (h *hookerImpl) OnResumeAudio(f func() error) {
hooks.OnResumeAudio(f) hook.OnResumeAudio(f)
} }
func (h *hookImpl) AppendHookOnBeforeUpdate(f func() error) { func (h *hookerImpl) AppendHookOnBeforeUpdate(f func() error) {
hooks.AppendHookOnBeforeUpdate(f) hook.AppendHookOnBeforeUpdate(f)
} }
// Resample converts the sample rate of the given stream. // Resample converts the sample rate of the given stream.

View File

@ -127,11 +127,11 @@ func (h *dummyHook) AppendHookOnBeforeUpdate(f func() error) {
} }
func init() { func init() {
hookForTesting = &dummyHook{} hookerForTesting = &dummyHook{}
} }
func UpdateForTesting() error { func UpdateForTesting() error {
for _, f := range hookForTesting.(*dummyHook).updates { for _, f := range hookerForTesting.(*dummyHook).updates {
if err := f(); err != nil { if err := f(); err != nil {
return err return err
} }

View File

@ -59,7 +59,6 @@ func newPlayerFactory(sampleRate int) *playerFactory {
if driverForTesting != nil { if driverForTesting != nil {
f.context = driverForTesting f.context = driverForTesting
} }
// TODO: Consider the hooks.
return f return f
} }

View File

@ -20,7 +20,7 @@ import (
"sync" "sync"
"github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/internal/hooks" "github.com/hajimehoshi/ebiten/v2/internal/hook"
) )
type pos struct { type pos struct {
@ -80,7 +80,7 @@ var theInputState = &inputState{
} }
func init() { func init() {
hooks.AppendHookOnBeforeUpdate(func() error { hook.AppendHookOnBeforeUpdate(func() error {
theInputState.update() theInputState.update()
return nil return nil
}) })

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
package hooks package hook
import ( import (
"sync" "sync"
@ -20,10 +20,10 @@ import (
var m sync.Mutex var m sync.Mutex
var onBeforeUpdateHooks = []func() error{} var onBeforeUpdateHooks []func() error
// AppendHookOnBeforeUpdate appends a hook function that is run before the main update function // AppendHookOnBeforeUpdate appends a hook function that is run before the main update function
// every frame. // every tick.
func AppendHookOnBeforeUpdate(f func() error) { func AppendHookOnBeforeUpdate(f func() error) {
m.Lock() m.Lock()
onBeforeUpdateHooks = append(onBeforeUpdateHooks, f) onBeforeUpdateHooks = append(onBeforeUpdateHooks, f)

View File

@ -23,7 +23,7 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/clock" "github.com/hajimehoshi/ebiten/v2/internal/clock"
"github.com/hajimehoshi/ebiten/v2/internal/debug" "github.com/hajimehoshi/ebiten/v2/internal/debug"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver" "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
"github.com/hajimehoshi/ebiten/v2/internal/hooks" "github.com/hajimehoshi/ebiten/v2/internal/hook"
"github.com/hajimehoshi/ebiten/v2/internal/mipmap" "github.com/hajimehoshi/ebiten/v2/internal/mipmap"
) )
@ -133,7 +133,7 @@ func (c *context) updateFrameImpl(graphicsDriver graphicsdriver.Graphics, update
ui.readInputState(inputState) ui.readInputState(inputState)
}) })
if err := hooks.RunBeforeUpdateHooks(); err != nil { if err := hook.RunBeforeUpdateHooks(); err != nil {
return err return err
} }
if err := c.game.Update(); err != nil { if err := c.game.Update(); err != nil {

View File

@ -31,7 +31,7 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/gamepad" "github.com/hajimehoshi/ebiten/v2/internal/gamepad"
"github.com/hajimehoshi/ebiten/v2/internal/glfw" "github.com/hajimehoshi/ebiten/v2/internal/glfw"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver" "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
"github.com/hajimehoshi/ebiten/v2/internal/hooks" "github.com/hajimehoshi/ebiten/v2/internal/hook"
"github.com/hajimehoshi/ebiten/v2/internal/microsoftgdk" "github.com/hajimehoshi/ebiten/v2/internal/microsoftgdk"
"github.com/hajimehoshi/ebiten/v2/internal/thread" "github.com/hajimehoshi/ebiten/v2/internal/thread"
) )
@ -1380,7 +1380,7 @@ func (u *userInterfaceImpl) update() (float64, float64, error) {
return 0, 0, err return 0, 0, err
} }
for visible != 0 && !u.isRunnableOnUnfocused() && focused == 0 && !shouldClose { for visible != 0 && !u.isRunnableOnUnfocused() && focused == 0 && !shouldClose {
if err := hooks.SuspendAudio(); err != nil { if err := hook.SuspendAudio(); err != nil {
return 0, 0, err return 0, 0, err
} }
// Wait for an arbitrary period to avoid busy loop. // Wait for an arbitrary period to avoid busy loop.
@ -1389,7 +1389,7 @@ func (u *userInterfaceImpl) update() (float64, float64, error) {
return 0, 0, err return 0, 0, err
} }
} }
if err := hooks.ResumeAudio(); err != nil { if err := hook.ResumeAudio(); err != nil {
return 0, 0, err return 0, 0, err
} }

View File

@ -25,7 +25,7 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/gamepad" "github.com/hajimehoshi/ebiten/v2/internal/gamepad"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver" "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl" "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/opengl"
"github.com/hajimehoshi/ebiten/v2/internal/hooks" "github.com/hajimehoshi/ebiten/v2/internal/hook"
) )
type graphicsDriverCreatorImpl struct { type graphicsDriverCreatorImpl struct {
@ -338,9 +338,9 @@ func (u *userInterfaceImpl) update() error {
} }
if u.suspended() { if u.suspended() {
return hooks.SuspendAudio() return hook.SuspendAudio()
} }
if err := hooks.ResumeAudio(); err != nil { if err := hook.ResumeAudio(); err != nil {
return err return err
} }
return u.updateImpl(false) return u.updateImpl(false)
@ -465,12 +465,12 @@ func (u *userInterfaceImpl) loop(game Game) <-chan error {
select { select {
case <-t.C: case <-t.C:
if u.suspended() { if u.suspended() {
if err := hooks.SuspendAudio(); err != nil { if err := hook.SuspendAudio(); err != nil {
errCh <- err errCh <- err
return return
} }
} else { } else {
if err := hooks.ResumeAudio(); err != nil { if err := hook.ResumeAudio(); err != nil {
errCh <- err errCh <- err
return return
} }

View File

@ -36,7 +36,7 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/gamepad" "github.com/hajimehoshi/ebiten/v2/internal/gamepad"
"github.com/hajimehoshi/ebiten/v2/internal/graphicscommand" "github.com/hajimehoshi/ebiten/v2/internal/graphicscommand"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver" "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
"github.com/hajimehoshi/ebiten/v2/internal/hooks" "github.com/hajimehoshi/ebiten/v2/internal/hook"
"github.com/hajimehoshi/ebiten/v2/internal/restorable" "github.com/hajimehoshi/ebiten/v2/internal/restorable"
"github.com/hajimehoshi/ebiten/v2/internal/thread" "github.com/hajimehoshi/ebiten/v2/internal/thread"
) )
@ -231,9 +231,9 @@ func (u *userInterfaceImpl) SetForeground(foreground bool) error {
atomic.StoreInt32(&u.foreground, v) atomic.StoreInt32(&u.foreground, v)
if foreground { if foreground {
return hooks.ResumeAudio() return hook.ResumeAudio()
} else { } else {
return hooks.SuspendAudio() return hook.SuspendAudio()
} }
} }

View File

@ -26,7 +26,7 @@ import (
"golang.org/x/image/math/fixed" "golang.org/x/image/math/fixed"
"github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/internal/hooks" "github.com/hajimehoshi/ebiten/v2/internal/hook"
) )
var ( var (
@ -38,7 +38,7 @@ func now() int64 {
} }
func init() { func init() {
hooks.AppendHookOnBeforeUpdate(func() error { hook.AppendHookOnBeforeUpdate(func() error {
monotonicClock++ monotonicClock++
return nil return nil
}) })