From ea842495cf170008db76fe7e36f2f1b17bec031f Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Fri, 27 Oct 2023 11:01:01 +0900 Subject: [PATCH] internal/ui: refactoring --- internal/ui/input_js.go | 2 +- internal/ui/run_glfw_notsinglethread.go | 4 ++-- internal/ui/run_glfw_singlethread.go | 4 ++-- internal/ui/ui.go | 22 ++++++++++++++++++++++ internal/ui/ui_glfw.go | 23 ----------------------- internal/ui/ui_js.go | 6 ++++-- internal/ui/ui_mobile.go | 7 +++++-- internal/ui/ui_nintendosdk.go | 12 ++++++++---- internal/ui/ui_playstation5.go | 3 +++ 9 files changed, 47 insertions(+), 36 deletions(-) diff --git a/internal/ui/input_js.go b/internal/ui/input_js.go index d1514d8f0..8bc0620cc 100644 --- a/internal/ui/input_js.go +++ b/internal/ui/input_js.go @@ -200,7 +200,7 @@ func init() { } func (u *UserInterface) KeyName(key Key) string { - if !u.running { + if !u.isRunning() { return "" } diff --git a/internal/ui/run_glfw_notsinglethread.go b/internal/ui/run_glfw_notsinglethread.go index ea18337c0..7718e6ce5 100644 --- a/internal/ui/run_glfw_notsinglethread.go +++ b/internal/ui/run_glfw_notsinglethread.go @@ -26,8 +26,6 @@ import ( ) func (u *UserInterface) Run(game Game, options *RunOptions) error { - u.context = newContext(game) - u.mainThread = thread.NewOSThread() u.renderThread = thread.NewOSThread() graphicscommand.SetRenderThread(u.renderThread) @@ -38,6 +36,8 @@ func (u *UserInterface) Run(game Game, options *RunOptions) error { u.setRunning(true) defer u.setRunning(false) + u.context = newContext(game) + if err := u.initOnMainThread(options); err != nil { return err } diff --git a/internal/ui/run_glfw_singlethread.go b/internal/ui/run_glfw_singlethread.go index a55ca2022..2c3860307 100644 --- a/internal/ui/run_glfw_singlethread.go +++ b/internal/ui/run_glfw_singlethread.go @@ -22,8 +22,6 @@ import ( ) func (u *UserInterface) Run(game Game, options *RunOptions) error { - u.context = newContext(game) - // Initialize the main thread first so the thread is available at u.run (#809). u.mainThread = thread.NewNoopThread() u.renderThread = thread.NewNoopThread() @@ -32,6 +30,8 @@ func (u *UserInterface) Run(game Game, options *RunOptions) error { u.setRunning(true) defer u.setRunning(false) + u.context = newContext(game) + if err := u.initOnMainThread(options); err != nil { return err } diff --git a/internal/ui/ui.go b/internal/ui/ui.go index 8b3ee1abe..e39f0d6c5 100644 --- a/internal/ui/ui.go +++ b/internal/ui/ui.go @@ -74,6 +74,8 @@ type UserInterface struct { isScreenClearedEveryFrame int32 graphicsLibrary int32 + running int32 + terminated int32 whiteImage *Image @@ -176,3 +178,23 @@ func (u *UserInterface) setGraphicsLibrary(library GraphicsLibrary) { func (u *UserInterface) GraphicsLibrary() GraphicsLibrary { return GraphicsLibrary(atomic.LoadInt32(&u.graphicsLibrary)) } + +func (u *UserInterface) isRunning() bool { + return atomic.LoadInt32(&u.running) != 0 && !u.isTerminated() +} + +func (u *UserInterface) setRunning(running bool) { + if running { + atomic.StoreInt32(&u.running, 1) + } else { + atomic.StoreInt32(&u.running, 0) + } +} + +func (u *UserInterface) isTerminated() bool { + return atomic.LoadInt32(&u.terminated) != 0 +} + +func (u *UserInterface) setTerminated() { + atomic.StoreInt32(&u.terminated, 1) +} diff --git a/internal/ui/ui_glfw.go b/internal/ui/ui_glfw.go index d4e54d92f..d255e11f7 100644 --- a/internal/ui/ui_glfw.go +++ b/internal/ui/ui_glfw.go @@ -24,7 +24,6 @@ import ( "os" "runtime" "sync" - "sync/atomic" "time" "github.com/hajimehoshi/ebiten/v2/internal/file" @@ -62,8 +61,6 @@ type userInterfaceImpl struct { maxWindowWidthInDIP int maxWindowHeightInDIP int - running uint32 - terminated uint32 runnableOnUnfocused bool fpsMode FPSModeType iconImages []image.Image @@ -287,26 +284,6 @@ func (u *UserInterface) Monitor() *Monitor { return monitor } -func (u *UserInterface) isRunning() bool { - return atomic.LoadUint32(&u.running) != 0 && !u.isTerminated() -} - -func (u *UserInterface) isTerminated() bool { - return atomic.LoadUint32(&u.terminated) != 0 -} - -func (u *UserInterface) setRunning(running bool) { - if running { - atomic.StoreUint32(&u.running, 1) - } else { - atomic.StoreUint32(&u.running, 0) - } -} - -func (u *UserInterface) setTerminated() { - atomic.StoreUint32(&u.terminated, 1) -} - // setWindowMonitor must be called on the main thread. func (u *UserInterface) setWindowMonitor(monitor *Monitor) error { if microsoftgdk.IsXbox() { diff --git a/internal/ui/ui_js.go b/internal/ui/ui_js.go index 678bbbe25..383534eb3 100644 --- a/internal/ui/ui_js.go +++ b/internal/ui/ui_js.go @@ -91,7 +91,6 @@ type userInterfaceImpl struct { runnableOnUnfocused bool fpsMode FPSModeType renderingScheduled bool - running bool cursorMode CursorMode cursorPrevMode CursorMode captureCursorLater bool @@ -744,6 +743,9 @@ func (u *UserInterface) forceUpdateOnMinimumFPSMode() { } func (u *UserInterface) Run(game Game, options *RunOptions) error { + u.setRunning(true) + defer u.setRunning(false) + if !options.InitUnfocused && window.Truthy() { // Do not focus the canvas when the current document is in an iframe. // Otherwise, the parent page tries to focus the iframe on every loading, which is annoying (#1373). @@ -752,7 +754,7 @@ func (u *UserInterface) Run(game Game, options *RunOptions) error { canvas.Call("focus") } } - u.running = true + g, lib, err := newGraphicsDriver(&graphicsDriverCreatorImpl{ canvas: canvas, }, options.GraphicsLibrary) diff --git a/internal/ui/ui_mobile.go b/internal/ui/ui_mobile.go index e234e631b..c25d8f45f 100644 --- a/internal/ui/ui_mobile.go +++ b/internal/ui/ui_mobile.go @@ -269,8 +269,6 @@ func (u *UserInterface) run(game Game, mainloop bool, options *RunOptions) (err } }() - u.context = newContext(game) - var mgl gl.Context if mainloop { // When gomobile-build is used, GL functions must be called via @@ -281,6 +279,11 @@ func (u *UserInterface) run(game Game, mainloop bool, options *RunOptions) (err graphicscommand.SetRenderThread(u.renderThread) } + u.setRunning(true) + defer u.setRunning(false) + + u.context = newContext(game) + g, lib, err := newGraphicsDriver(&graphicsDriverCreatorImpl{ gomobileContext: mgl, }, options.GraphicsLibrary) diff --git a/internal/ui/ui_nintendosdk.go b/internal/ui/ui_nintendosdk.go index 8677ecf83..008f5c706 100644 --- a/internal/ui/ui_nintendosdk.go +++ b/internal/ui/ui_nintendosdk.go @@ -84,7 +84,15 @@ func (u *UserInterface) init() error { } func (u *UserInterface) Run(game Game, options *RunOptions) error { + u.mainThread = thread.NewOSThread() + u.renderThread = thread.NewOSThread() + graphicscommand.SetRenderThread(u.renderThread) + + u.setRunning(true) + defer u.setRunning(false) + u.context = newContext(game) + g, lib, err := newGraphicsDriver(&graphicsDriverCreatorImpl{}, options.GraphicsLibrary) if err != nil { return err @@ -99,10 +107,6 @@ func (u *UserInterface) Run(game Game, options *RunOptions) error { initializeProfiler() - u.mainThread = thread.NewOSThread() - u.renderThread = thread.NewOSThread() - graphicscommand.SetRenderThread(u.renderThread) - ctx, cancel := stdcontext.WithCancel(stdcontext.Background()) defer cancel() diff --git a/internal/ui/ui_playstation5.go b/internal/ui/ui_playstation5.go index 4926b8912..e8dd04e76 100644 --- a/internal/ui/ui_playstation5.go +++ b/internal/ui/ui_playstation5.go @@ -64,6 +64,9 @@ func (u *UserInterface) init() error { } func (u *UserInterface) Run(game Game, options *RunOptions) error { + u.setRunning(true) + defer u.setRunning(false) + // TODO: Implement this. return nil }