From 571d67f9676e9a68243ef139798ef9a035552c38 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Fri, 2 Sep 2016 02:31:03 +0900 Subject: [PATCH] ui: Reduce methods in UserInterface --- internal/loop/run.go | 7 ++----- internal/ui/ui.go | 3 +-- internal/ui/ui_glfw.go | 6 +++--- internal/ui/ui_js.go | 6 +++--- internal/ui/ui_mobile.go | 6 +----- 5 files changed, 10 insertions(+), 18 deletions(-) diff --git a/internal/loop/run.go b/internal/loop/run.go index f92b1f8c7..cee358529 100644 --- a/internal/loop/run.go +++ b/internal/loop/run.go @@ -109,15 +109,12 @@ func Run(g GraphicsContext, width, height int, scale float64, title string, fps currentRunContext.startRunning() defer currentRunContext.endRunning() - if err := ui.CurrentUI().Start(width, height, scale, title); err != nil { - return err - } - n := now() currentRunContext.lastUpdated = n currentRunContext.lastFPSUpdated = n - if err := ui.CurrentUI().AnimationFrameLoop(&loopGraphicsContext{currentRunContext, g}); err != nil { + lg := &loopGraphicsContext{currentRunContext, g} + if err := ui.CurrentUI().Run(width, height, scale, title, lg); err != nil { if _, ok := err.(*ui.RegularTermination); ok { return nil } diff --git a/internal/ui/ui.go b/internal/ui/ui.go index cbbc64989..665cfaea9 100644 --- a/internal/ui/ui.go +++ b/internal/ui/ui.go @@ -20,8 +20,7 @@ type GraphicsContext interface { } type UserInterface interface { - Start(width, height int, scale float64, title string) error - AnimationFrameLoop(g GraphicsContext) error + Run(width, height int, scale float64, title string, g GraphicsContext) error ScreenScale() float64 SetScreenSize(width, height int) (bool, error) SetScreenScale(scale float64) (bool, error) diff --git a/internal/ui/ui_glfw.go b/internal/ui/ui_glfw.go index 96033fdb6..0db1f8390 100644 --- a/internal/ui/ui_glfw.go +++ b/internal/ui/ui_glfw.go @@ -147,7 +147,7 @@ func (u *userInterface) ScreenScale() float64 { return s } -func (u *userInterface) Start(width, height int, scale float64, title string) error { +func (u *userInterface) Run(width, height int, scale float64, title string, g GraphicsContext) error { // GLContext must be created before setting the screen size, which requires // swapping buffers. var err error @@ -176,7 +176,7 @@ func (u *userInterface) Start(width, height int, scale float64, title string) er }); err != nil { return err } - return nil + return u.loop(g) } func (u *userInterface) glfwSize() (int, int) { @@ -241,7 +241,7 @@ func (u *userInterface) update(g GraphicsContext) error { return nil } -func (u *userInterface) AnimationFrameLoop(g GraphicsContext) error { +func (u *userInterface) loop(g GraphicsContext) error { defer func() { _ = u.runOnMainThread(func() error { glfw.Terminate() diff --git a/internal/ui/ui_js.go b/internal/ui/ui_js.go index bb2fa42f9..d7256d4db 100644 --- a/internal/ui/ui_js.go +++ b/internal/ui/ui_js.go @@ -87,7 +87,7 @@ func (u *userInterface) update(g GraphicsContext) error { return nil } -func (u *userInterface) AnimationFrameLoop(g GraphicsContext) error { +func (u *userInterface) loop(g GraphicsContext) error { ch := make(chan error) var f func() f = func() { @@ -262,7 +262,7 @@ func Main(ch <-chan error) error { return <-ch } -func (u *userInterface) Start(width, height int, scale float64, title string) error { +func (u *userInterface) Run(width, height int, scale float64, title string, g GraphicsContext) error { doc := js.Global.Get("document") doc.Set("title", title) u.setScreenSize(width, height, scale) @@ -272,7 +272,7 @@ func (u *userInterface) Start(width, height int, scale float64, title string) er if err != nil { return err } - return nil + return u.loop(g) } func (u *userInterface) size() (width, height int) { diff --git a/internal/ui/ui_mobile.go b/internal/ui/ui_mobile.go index 6e8fe3928..e289a5e5a 100644 --- a/internal/ui/ui_mobile.go +++ b/internal/ui/ui_mobile.go @@ -64,7 +64,7 @@ func CurrentUI() UserInterface { return currentUI } -func (u *userInterface) Start(width, height int, scale float64, title string) error { +func (u *userInterface) Run(width, height int, scale float64, title string, g GraphicsContext) error { u.width = width u.height = height u.scale = scale @@ -74,10 +74,6 @@ func (u *userInterface) Start(width, height int, scale float64, title string) er if err != nil { return err } - return nil -} - -func (u *userInterface) AnimationFrameLoop(g GraphicsContext) error { for { if err := u.update(g); err != nil { return err