diff --git a/examples/windowsize/main.go b/examples/windowsize/main.go index b50562c68..f03c75aa2 100644 --- a/examples/windowsize/main.go +++ b/examples/windowsize/main.go @@ -431,7 +431,6 @@ func main() { ebiten.SetWindowResizingMode(ebiten.WindowResizingModeEnabled) } - ebiten.SetInitFocused(*flagInitFocused) if !*flagInitFocused { ebiten.SetRunnableOnUnfocused(true) } @@ -464,6 +463,7 @@ func main() { default: log.Fatalf("unexpected graphics library: %s", *flagGraphicsLibrary) } + op.InitUnfocused = !*flagInitFocused const title = "Window Size (Ebitengine Demo)" ww := int(float64(g.width) * initScreenScale) diff --git a/internal/ui/ui.go b/internal/ui/ui.go index f41f4a09b..745c3ebd7 100644 --- a/internal/ui/ui.go +++ b/internal/ui/ui.go @@ -98,4 +98,5 @@ func (u *UserInterface) dumpImages(dir string) (string, error) { type RunOptions struct { GraphicsLibrary GraphicsLibrary + InitUnfocused bool } diff --git a/internal/ui/ui_glfw.go b/internal/ui/ui_glfw.go index 6b48e417f..6b4911fc0 100644 --- a/internal/ui/ui_glfw.go +++ b/internal/ui/ui_glfw.go @@ -93,7 +93,6 @@ type userInterfaceImpl struct { initWindowFloating bool initWindowMaximized bool initScreenTransparent bool - initFocused bool origWindowPosX int origWindowPosY int @@ -135,7 +134,6 @@ func init() { initWindowPositionYInDIP: invalidPos, initWindowWidthInDIP: 640, initWindowHeightInDIP: 480, - initFocused: true, fpsMode: FPSModeVsyncOn, origWindowPosX: invalidPos, origWindowPosY: invalidPos, @@ -490,27 +488,6 @@ func (u *userInterfaceImpl) isWindowBeingClosed() bool { return v } -func (u *userInterfaceImpl) isInitFocused() bool { - if microsoftgdk.IsXbox() { - return true - } - - u.m.RLock() - v := u.initFocused - u.m.RUnlock() - return v -} - -func (u *userInterfaceImpl) setInitFocused(focused bool) { - if microsoftgdk.IsXbox() { - return - } - - u.m.Lock() - u.initFocused = focused - u.m.Unlock() -} - func (u *userInterfaceImpl) ScreenSizeInFullscreen() (int, int) { if !u.isRunning() { return u.initFullscreenWidthInDIP, u.initFullscreenHeightInDIP @@ -933,9 +910,9 @@ func (u *userInterfaceImpl) init(options *RunOptions) error { } glfw.WindowHint(glfw.Floating, floating) - focused := glfw.False - if u.isInitFocused() { - focused = glfw.True + focused := glfw.True + if options.InitUnfocused { + focused = glfw.False } glfw.WindowHint(glfw.FocusOnShow, focused) @@ -1467,13 +1444,6 @@ func (u *userInterfaceImpl) resetForTick() { u.m.Unlock() } -func (u *userInterfaceImpl) SetInitFocused(focused bool) { - if u.isRunning() { - panic("ui: SetInitFocused must be called before the main loop") - } - u.setInitFocused(focused) -} - func (u *userInterfaceImpl) Input() *Input { return &u.input } diff --git a/internal/ui/ui_js.go b/internal/ui/ui_js.go index b8834de58..e327c0daf 100644 --- a/internal/ui/ui_js.go +++ b/internal/ui/ui_js.go @@ -77,7 +77,6 @@ type userInterfaceImpl struct { fpsMode FPSModeType renderingScheduled bool running bool - initFocused bool cursorMode CursorMode cursorPrevMode CursorMode cursorShape CursorShape @@ -96,7 +95,6 @@ type userInterfaceImpl struct { func init() { theUI.userInterfaceImpl = userInterfaceImpl{ runnableOnUnfocused: true, - initFocused: true, } theUI.input.ui = &theUI.userInterfaceImpl } @@ -638,7 +636,7 @@ func (u *userInterfaceImpl) forceUpdateOnMinimumFPSMode() { } func (u *userInterfaceImpl) Run(game Game, options *RunOptions) error { - if u.initFocused && window.Truthy() { + 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). isInIframe := !window.Get("location").Equal(window.Get("parent").Get("location")) @@ -689,13 +687,6 @@ func (u *userInterfaceImpl) resetForTick() { u.input.resetForTick() } -func (u *userInterfaceImpl) SetInitFocused(focused bool) { - if u.running { - panic("ui: SetInitFocused must be called before the main loop") - } - u.initFocused = focused -} - func (u *userInterfaceImpl) Input() *Input { return &u.input } diff --git a/internal/ui/ui_mobile.go b/internal/ui/ui_mobile.go index 953fe9b70..7fc266b1f 100644 --- a/internal/ui/ui_mobile.go +++ b/internal/ui/ui_mobile.go @@ -433,10 +433,6 @@ func (u *userInterfaceImpl) resetForTick() { u.input.resetForTick() } -func (u *userInterfaceImpl) SetInitFocused(focused bool) { - // Do nothing -} - func (u *userInterfaceImpl) Input() *Input { return &u.input } diff --git a/internal/ui/ui_nintendosdk.go b/internal/ui/ui_nintendosdk.go index c095ff418..208b5e902 100644 --- a/internal/ui/ui_nintendosdk.go +++ b/internal/ui/ui_nintendosdk.go @@ -133,9 +133,6 @@ func (*userInterfaceImpl) IsScreenTransparent() bool { func (*userInterfaceImpl) SetScreenTransparent(transparent bool) { } -func (*userInterfaceImpl) SetInitFocused(focused bool) { -} - func (*userInterfaceImpl) Input() *Input { return &theUI.input } diff --git a/run.go b/run.go index 7ac42eaa6..efd46dd96 100644 --- a/run.go +++ b/run.go @@ -232,8 +232,15 @@ func RunGame(game Game) error { // RungameOptions represents options for RunGameWithOptions. type RunGameOptions struct { // GraphicsLibrary is a graphics library Ebitengine will use. + // // The default (zero) value is GraphicsLibraryAuto, which lets Ebitengine choose the graphics library. GraphicsLibrary GraphicsLibrary + + // InitUnfocused represents whether the window is unfocused or not on launching. + // InitUnfocused is valid on desktops and browsers. + // + // The default (zero) value is false, which means that the window is focused. + InitUnfocused bool } // RunGameWithOptions starts the main loop and runs the game with the specified options. @@ -598,15 +605,26 @@ func SetScreenTransparent(transparent bool) { // SetInitFocused panics if this is called after the main loop. // // SetInitFocused is cuncurrent-safe. +// +// Deprecated: as of v2.5. Use RunGameWithOptions instead. func SetInitFocused(focused bool) { - ui.Get().SetInitFocused(focused) + if focused { + atomic.StoreInt32(&initUnfocused, 0) + } else { + atomic.StoreInt32(&initUnfocused, 1) + } } +var initUnfocused int32 = 0 + func toUIRunOptions(options *RunGameOptions) *ui.RunOptions { if options == nil { - return &ui.RunOptions{} + return &ui.RunOptions{ + InitUnfocused: atomic.LoadInt32(&initUnfocused) != 0, + } } return &ui.RunOptions{ GraphicsLibrary: ui.GraphicsLibrary(options.GraphicsLibrary), + InitUnfocused: options.InitUnfocused, } }