internal/ui: bug fix: disable global functions after the game termination

Closes #2743
This commit is contained in:
Hajime Hoshi 2023-09-02 15:24:59 +09:00
parent 6e968558b1
commit f30a58a393
4 changed files with 238 additions and 9 deletions

View File

@ -0,0 +1,47 @@
// Copyright 2023 The Ebitengine Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build ignore
package main
import (
"time"
"github.com/hajimehoshi/ebiten/v2"
)
type Game struct{}
func (g *Game) Update() error {
return ebiten.Termination
}
func (g *Game) Draw(screen *ebiten.Image) {}
func (g *Game) Layout(width, height int) (int, int) {
return width, height
}
func main() {
go func() {
for {
ebiten.SetCursorMode(ebiten.CursorModeHidden)
time.Sleep(time.Millisecond)
}
}()
if err := ebiten.RunGame(&Game{}); err != nil {
panic(err)
}
}

View File

@ -93,6 +93,9 @@ func (u *userInterfaceImpl) keyName(key Key) string {
var name string
u.mainThread.Call(func() {
if u.isTerminated() {
return
}
name = glfw.GetKeyName(gk, 0)
})
return name

View File

@ -62,6 +62,7 @@ type userInterfaceImpl struct {
maxWindowHeightInDIP int
running uint32
terminated uint32
runnableOnUnfocused bool
fpsMode FPSModeType
iconImages []image.Image
@ -259,6 +260,9 @@ func (u *userInterfaceImpl) Monitor() *Monitor {
}
var monitor *Monitor
u.mainThread.Call(func() {
if u.isTerminated() {
return
}
glfwMonitor := u.currentMonitor()
if glfwMonitor == nil {
return
@ -322,7 +326,11 @@ func getMonitorFromPosition(wx, wy int) *Monitor {
}
func (u *userInterfaceImpl) isRunning() bool {
return atomic.LoadUint32(&u.running) != 0
return atomic.LoadUint32(&u.running) != 0 && !u.isTerminated()
}
func (u *userInterfaceImpl) isTerminated() bool {
return atomic.LoadUint32(&u.terminated) != 0
}
func (u *userInterfaceImpl) setRunning(running bool) {
@ -333,6 +341,10 @@ func (u *userInterfaceImpl) setRunning(running bool) {
}
}
func (u *userInterfaceImpl) setTerminated() {
atomic.StoreUint32(&u.terminated, 1)
}
// setWindowMonitor must be called on the main thread.
func (u *userInterfaceImpl) setWindowMonitor(monitor int) {
if microsoftgdk.IsXbox() {
@ -628,12 +640,18 @@ func (u *userInterfaceImpl) setWindowClosingHandled(handled bool) {
}
func (u *userInterfaceImpl) ScreenSizeInFullscreen() (int, int) {
if u.isTerminated() {
return 0, 0
}
if !u.isRunning() {
return u.initFullscreenWidthInDIP, u.initFullscreenHeightInDIP
}
var w, h int
u.mainThread.Call(func() {
if u.isTerminated() {
return
}
m := u.currentMonitor()
if m == nil {
return
@ -658,11 +676,17 @@ func (u *userInterfaceImpl) IsFullscreen() bool {
return false
}
if u.isTerminated() {
return false
}
if !u.isRunning() {
return u.isInitFullscreen()
}
b := false
var b bool
u.mainThread.Call(func() {
if u.isTerminated() {
return
}
b = u.isFullscreen()
})
return b
@ -673,12 +697,18 @@ func (u *userInterfaceImpl) SetFullscreen(fullscreen bool) {
return
}
if u.isTerminated() {
return
}
if !u.isRunning() {
u.setInitFullscreen(fullscreen)
return
}
u.mainThread.Call(func() {
if u.isTerminated() {
return
}
if u.isFullscreen() == fullscreen {
return
}
@ -693,6 +723,9 @@ func (u *userInterfaceImpl) IsFocused() bool {
var focused bool
u.mainThread.Call(func() {
if u.isTerminated() {
return
}
focused = u.window.GetAttrib(glfw.Focused) == glfw.True
})
return focused
@ -707,6 +740,9 @@ func (u *userInterfaceImpl) IsRunnableOnUnfocused() bool {
}
func (u *userInterfaceImpl) SetFPSMode(mode FPSModeType) {
if u.isTerminated() {
return
}
if !u.isRunning() {
u.m.Lock()
u.fpsMode = mode
@ -714,6 +750,9 @@ func (u *userInterfaceImpl) SetFPSMode(mode FPSModeType) {
return
}
u.mainThread.Call(func() {
if u.isTerminated() {
return
}
if !u.fpsModeInited {
u.fpsMode = mode
return
@ -732,12 +771,18 @@ func (u *userInterfaceImpl) ScheduleFrame() {
}
func (u *userInterfaceImpl) CursorMode() CursorMode {
if u.isTerminated() {
return 0
}
if !u.isRunning() {
return u.getInitCursorMode()
}
var mode int
u.mainThread.Call(func() {
if u.isTerminated() {
return
}
mode = u.window.GetInputMode(glfw.CursorMode)
})
@ -756,11 +801,17 @@ func (u *userInterfaceImpl) CursorMode() CursorMode {
}
func (u *userInterfaceImpl) SetCursorMode(mode CursorMode) {
if u.isTerminated() {
return
}
if !u.isRunning() {
u.setInitCursorMode(mode)
return
}
u.mainThread.Call(func() {
if u.isTerminated() {
return
}
u.window.SetInputMode(glfw.CursorMode, driverCursorModeToGLFWCursorMode(mode))
if mode == CursorModeVisible {
u.window.SetCursor(glfwSystemCursors[u.getCursorShape()])
@ -773,6 +824,10 @@ func (u *userInterfaceImpl) CursorShape() CursorShape {
}
func (u *userInterfaceImpl) SetCursorShape(shape CursorShape) {
if u.isTerminated() {
return
}
old := u.setCursorShape(shape)
if old == shape {
return
@ -781,17 +836,26 @@ func (u *userInterfaceImpl) SetCursorShape(shape CursorShape) {
return
}
u.mainThread.Call(func() {
if u.isTerminated() {
return
}
u.window.SetCursor(glfwSystemCursors[shape])
})
}
func (u *userInterfaceImpl) DeviceScaleFactor() float64 {
if u.isTerminated() {
return 0
}
if !u.isRunning() {
return u.initDeviceScaleFactor
}
f := 0.0
var f float64
u.mainThread.Call(func() {
if u.isTerminated() {
return
}
f = u.deviceScaleFactor(u.currentMonitor())
})
return f
@ -1210,6 +1274,7 @@ func (u *userInterfaceImpl) loopGame() error {
u.renderThread.Call(func() {})
u.mainThread.Call(func() {
glfw.Terminate()
u.setTerminated()
})
}()

View File

@ -28,28 +28,43 @@ type glfwWindow struct {
}
func (w *glfwWindow) IsDecorated() bool {
if w.ui.isTerminated() {
return false
}
if !w.ui.isRunning() {
return w.ui.isInitWindowDecorated()
}
v := false
var v bool
w.ui.mainThread.Call(func() {
if w.ui.isTerminated() {
return
}
v = w.ui.window.GetAttrib(glfw.Decorated) == glfw.True
})
return v
}
func (w *glfwWindow) SetDecorated(decorated bool) {
if w.ui.isTerminated() {
return
}
if !w.ui.isRunning() {
w.ui.setInitWindowDecorated(decorated)
return
}
w.ui.mainThread.Call(func() {
if w.ui.isTerminated() {
return
}
w.ui.setWindowDecorated(decorated)
})
}
func (w *glfwWindow) ResizingMode() WindowResizingMode {
if w.ui.isTerminated() {
return 0
}
if !w.ui.isRunning() {
w.ui.m.Lock()
mode := w.ui.windowResizingMode
@ -58,12 +73,18 @@ func (w *glfwWindow) ResizingMode() WindowResizingMode {
}
var mode WindowResizingMode
w.ui.mainThread.Call(func() {
if w.ui.isTerminated() {
return
}
mode = w.ui.windowResizingMode
})
return mode
}
func (w *glfwWindow) SetResizingMode(mode WindowResizingMode) {
if w.ui.isTerminated() {
return
}
if !w.ui.isRunning() {
w.ui.m.Lock()
w.ui.windowResizingMode = mode
@ -71,32 +92,50 @@ func (w *glfwWindow) SetResizingMode(mode WindowResizingMode) {
return
}
w.ui.mainThread.Call(func() {
if w.ui.isTerminated() {
return
}
w.ui.setWindowResizingMode(mode)
})
}
func (w *glfwWindow) IsFloating() bool {
if w.ui.isTerminated() {
return false
}
if !w.ui.isRunning() {
return w.ui.isInitWindowFloating()
}
var v bool
w.ui.mainThread.Call(func() {
if w.ui.isTerminated() {
return
}
v = w.ui.window.GetAttrib(glfw.Floating) == glfw.True
})
return v
}
func (w *glfwWindow) SetFloating(floating bool) {
if w.ui.isTerminated() {
return
}
if !w.ui.isRunning() {
w.ui.setInitWindowFloating(floating)
return
}
w.ui.mainThread.Call(func() {
if w.ui.isTerminated() {
return
}
w.ui.setWindowFloating(floating)
})
}
func (w *glfwWindow) IsMaximized() bool {
if w.ui.isTerminated() {
return false
}
if !w.ui.isRunning() {
return w.ui.isInitWindowMaximized()
}
@ -105,12 +144,19 @@ func (w *glfwWindow) IsMaximized() bool {
}
var v bool
w.ui.mainThread.Call(func() {
if w.ui.isTerminated() {
return
}
v = w.ui.isWindowMaximized()
})
return v
}
func (w *glfwWindow) Maximize() {
if w.ui.isTerminated() {
return
}
// Do not allow maximizing the window when the window is not resizable.
// On Windows, it is possible to restore the window from being maximized by mouse-dragging,
// and this can be an unexpected behavior (#1990).
@ -126,7 +172,12 @@ func (w *glfwWindow) Maximize() {
w.ui.setInitWindowMaximized(true)
return
}
w.ui.mainThread.Call(w.ui.maximizeWindow)
w.ui.mainThread.Call(func() {
if w.ui.isTerminated() {
return
}
w.ui.maximizeWindow()
})
}
func (w *glfwWindow) IsMinimized() bool {
@ -135,6 +186,9 @@ func (w *glfwWindow) IsMinimized() bool {
}
var v bool
w.ui.mainThread.Call(func() {
if w.ui.isTerminated() {
return
}
v = w.ui.window.GetAttrib(glfw.Iconified) == glfw.True
})
return v
@ -145,10 +199,18 @@ func (w *glfwWindow) Minimize() {
// Do nothing
return
}
w.ui.mainThread.Call(w.ui.iconifyWindow)
w.ui.mainThread.Call(func() {
if w.ui.isTerminated() {
return
}
w.ui.iconifyWindow()
})
}
func (w *glfwWindow) Restore() {
if w.ui.isTerminated() {
return
}
if !w.ui.isWindowMaximizable() {
return
}
@ -156,28 +218,45 @@ func (w *glfwWindow) Restore() {
// Do nothing
return
}
w.ui.mainThread.Call(w.ui.restoreWindow)
w.ui.mainThread.Call(func() {
if w.ui.isTerminated() {
return
}
w.ui.restoreWindow()
})
}
func (w *glfwWindow) SetMonitor(monitor *Monitor) {
if monitor == nil {
panic("ui: monitor cannot be nil at SetMonitor")
}
if w.ui.isTerminated() {
return
}
if !w.ui.isRunning() {
w.ui.setInitWindowMonitor(monitor.id)
return
}
w.ui.mainThread.Call(func() {
if w.ui.isTerminated() {
return
}
w.ui.setWindowMonitor(monitor.id)
})
}
func (w *glfwWindow) Position() (int, int) {
if w.ui.isTerminated() {
return 0, 0
}
if !w.ui.isRunning() {
panic("ui: WindowPosition can't be called before the main loop starts")
}
x, y := 0, 0
var x, y int
w.ui.mainThread.Call(func() {
if w.ui.isTerminated() {
return
}
var wx, wy int
if w.ui.isFullscreen() {
wx, wy = w.ui.origWindowPos()
@ -196,22 +275,34 @@ func (w *glfwWindow) Position() (int, int) {
}
func (w *glfwWindow) SetPosition(x, y int) {
if w.ui.isTerminated() {
return
}
if !w.ui.isRunning() {
w.ui.setInitWindowPositionInDIP(x, y)
return
}
w.ui.mainThread.Call(func() {
if w.ui.isTerminated() {
return
}
w.ui.setWindowPositionInDIP(x, y, w.ui.currentMonitor())
})
}
func (w *glfwWindow) Size() (int, int) {
if w.ui.isTerminated() {
return 0, 0
}
if !w.ui.isRunning() {
ww, wh := w.ui.getInitWindowSizeInDIP()
return w.ui.adjustWindowSizeBasedOnSizeLimitsInDIP(ww, wh)
}
var ww, wh int
w.ui.mainThread.Call(func() {
if w.ui.isTerminated() {
return
}
// Unlike origWindowPos, origWindow{Width,Height}InDPI are always updated via the callback.
ww = w.ui.origWindowWidthInDIP
wh = w.ui.origWindowHeightInDIP
@ -220,12 +311,18 @@ func (w *glfwWindow) Size() (int, int) {
}
func (w *glfwWindow) SetSize(width, height int) {
if w.ui.isTerminated() {
return
}
if !w.ui.isRunning() {
// If the window is initially maximized, the set size is ignored anyway.
w.ui.setInitWindowSizeInDIP(width, height)
return
}
w.ui.mainThread.Call(func() {
if w.ui.isTerminated() {
return
}
if w.ui.isWindowMaximized() && runtime.GOOS != "darwin" {
return
}
@ -238,6 +335,9 @@ func (w *glfwWindow) SizeLimits() (minw, minh, maxw, maxh int) {
}
func (w *glfwWindow) SetSizeLimits(minw, minh, maxw, maxh int) {
if w.ui.isTerminated() {
return
}
if !w.ui.setWindowSizeLimitsInDIP(minw, minh, maxw, maxh) {
return
}
@ -245,15 +345,26 @@ func (w *glfwWindow) SetSizeLimits(minw, minh, maxw, maxh int) {
return
}
w.ui.mainThread.Call(w.ui.updateWindowSizeLimits)
w.ui.mainThread.Call(func() {
if w.ui.isTerminated() {
return
}
w.ui.updateWindowSizeLimits()
})
}
func (w *glfwWindow) SetIcon(iconImages []image.Image) {
if w.ui.isTerminated() {
return
}
// The icons are actually set at (*UserInterface).loop.
w.ui.setIconImages(iconImages)
}
func (w *glfwWindow) SetTitle(title string) {
if w.ui.isTerminated() {
return
}
if !w.ui.isRunning() {
w.ui.m.Lock()
w.ui.title = title
@ -262,6 +373,9 @@ func (w *glfwWindow) SetTitle(title string) {
}
w.ui.title = title
w.ui.mainThread.Call(func() {
if w.ui.isTerminated() {
return
}
w.ui.setWindowTitle(title)
})
}