internal/ui: refactoring: remove fpsMode from globalState

This commit is contained in:
Hajime Hoshi 2023-10-15 16:10:13 +09:00
parent beabe9c6ee
commit a89aaa0756
6 changed files with 37 additions and 38 deletions

View File

@ -31,7 +31,6 @@ type globalState struct {
err_ error err_ error
errM sync.Mutex errM sync.Mutex
fpsMode_ int32
isScreenClearedEveryFrame_ int32 isScreenClearedEveryFrame_ int32
graphicsLibrary_ int32 graphicsLibrary_ int32
} }
@ -50,14 +49,6 @@ func (g *globalState) setError(err error) {
} }
} }
func (g *globalState) fpsMode() FPSModeType {
return FPSModeType(atomic.LoadInt32(&g.fpsMode_))
}
func (g *globalState) setFPSMode(fpsMode FPSModeType) {
atomic.StoreInt32(&g.fpsMode_, int32(fpsMode))
}
func (g *globalState) isScreenClearedEveryFrame() bool { func (g *globalState) isScreenClearedEveryFrame() bool {
return atomic.LoadInt32(&g.isScreenClearedEveryFrame_) != 0 return atomic.LoadInt32(&g.isScreenClearedEveryFrame_) != 0
} }
@ -78,15 +69,6 @@ func (g *globalState) graphicsLibrary() GraphicsLibrary {
return GraphicsLibrary(atomic.LoadInt32(&g.graphicsLibrary_)) return GraphicsLibrary(atomic.LoadInt32(&g.graphicsLibrary_))
} }
func FPSMode() FPSModeType {
return theGlobalState.fpsMode()
}
func (u *UserInterface) SetFPSMode(fpsMode FPSModeType) {
theGlobalState.setFPSMode(fpsMode)
u.setFPSMode(fpsMode)
}
func IsScreenClearedEveryFrame() bool { func IsScreenClearedEveryFrame() bool {
return theGlobalState.isScreenClearedEveryFrame() return theGlobalState.isScreenClearedEveryFrame()
} }

View File

@ -138,7 +138,6 @@ func (u *UserInterface) init() error {
initWindowPositionYInDIP: invalidPos, initWindowPositionYInDIP: invalidPos,
initWindowWidthInDIP: 640, initWindowWidthInDIP: 640,
initWindowHeightInDIP: 480, initWindowHeightInDIP: 480,
fpsMode: FPSModeVsyncOn,
origWindowPosX: invalidPos, origWindowPosX: invalidPos,
origWindowPosY: invalidPos, origWindowPosY: invalidPos,
savedCursorX: math.NaN(), savedCursorX: math.NaN(),
@ -727,14 +726,20 @@ func (u *UserInterface) IsRunnableOnUnfocused() bool {
return u.isRunnableOnUnfocused() return u.isRunnableOnUnfocused()
} }
func (u *UserInterface) setFPSMode(mode FPSModeType) { func (u *UserInterface) FPSMode() FPSModeType {
u.m.Lock()
defer u.m.Unlock()
return u.fpsMode
}
func (u *UserInterface) SetFPSMode(mode FPSModeType) {
if u.isTerminated() { if u.isTerminated() {
return return
} }
if !u.isRunning() { if !u.isRunning() {
u.m.Lock() u.m.Lock()
defer u.m.Unlock()
u.fpsMode = mode u.fpsMode = mode
u.m.Unlock()
return return
} }
@ -746,7 +751,7 @@ func (u *UserInterface) setFPSMode(mode FPSModeType) {
u.fpsMode = mode u.fpsMode = mode
return return
} }
if err := u.setFPSModeImpl(mode); err != nil { if err := u.setFPSMode(mode); err != nil {
theGlobalState.setError(err) theGlobalState.setError(err)
return return
} }
@ -1290,8 +1295,8 @@ func (u *UserInterface) outsideSize() (float64, float64, error) {
return w, h, nil return w, h, nil
} }
// setFPSModeImpl must be called from the main thread. // setFPSMode must be called from the main thread.
func (u *UserInterface) setFPSModeImpl(fpsMode FPSModeType) error { func (u *UserInterface) setFPSMode(fpsMode FPSModeType) error {
needUpdate := u.fpsMode != fpsMode || !u.fpsModeInited needUpdate := u.fpsMode != fpsMode || !u.fpsModeInited
u.fpsMode = fpsMode u.fpsMode = fpsMode
u.fpsModeInited = true u.fpsModeInited = true
@ -1361,7 +1366,7 @@ func (u *UserInterface) update() (float64, float64, error) {
// Initialize vsync after SetMonitor is called. See the comment in updateVsync. // Initialize vsync after SetMonitor is called. See the comment in updateVsync.
// Calling this inside setWindowSize didn't work (#1363). // Calling this inside setWindowSize didn't work (#1363).
if !u.fpsModeInited { if !u.fpsModeInited {
if err := u.setFPSModeImpl(u.fpsMode); err != nil { if err := u.setFPSMode(u.fpsMode); err != nil {
return 0, 0, err return 0, 0, err
} }
} }

View File

@ -186,7 +186,11 @@ func (u *UserInterface) IsRunnableOnUnfocused() bool {
return u.runnableOnUnfocused return u.runnableOnUnfocused
} }
func (u *UserInterface) setFPSMode(mode FPSModeType) { func (u *UserInterface) FPSMode() FPSModeType {
return u.fpsMode
}
func (u *UserInterface) SetFPSMode(mode FPSModeType) {
u.fpsMode = mode u.fpsMode = mode
} }

View File

@ -119,7 +119,7 @@ type userInterfaceImpl struct {
inputState InputState inputState InputState
touches []TouchForInput touches []TouchForInput
fpsMode FPSModeType fpsMode int32
renderRequester RenderRequester renderRequester RenderRequester
renderThread *thread.OSThread renderThread *thread.OSThread
@ -399,16 +399,20 @@ func (u *UserInterface) SetRunnableOnUnfocused(runnableOnUnfocused bool) {
// Do nothing // Do nothing
} }
func (u *UserInterface) setFPSMode(mode FPSModeType) { func (u *UserInterface) FPSMode() FPSModeType {
u.fpsMode = mode return FPSModeType(atomic.LoadInt32(&u.fpsMode))
u.updateExplicitRenderingModeIfNeeded()
} }
func (u *UserInterface) updateExplicitRenderingModeIfNeeded() { func (u *UserInterface) SetFPSMode(mode FPSModeType) {
atomic.StoreInt32(&u.fpsMode, int32(mode))
u.updateExplicitRenderingModeIfNeeded(mode)
}
func (u *UserInterface) updateExplicitRenderingModeIfNeeded(fpsMode FPSModeType) {
if u.renderRequester == nil { if u.renderRequester == nil {
return return
} }
u.renderRequester.SetExplicitRenderingMode(u.fpsMode == FPSModeVsyncOffMinimum) u.renderRequester.SetExplicitRenderingMode(fpsMode == FPSModeVsyncOffMinimum)
} }
func (u *UserInterface) DeviceScaleFactor() float64 { func (u *UserInterface) DeviceScaleFactor() float64 {
@ -452,7 +456,7 @@ func (u *UserInterface) Monitor() *Monitor {
func (u *UserInterface) UpdateInput(keys map[Key]struct{}, runes []rune, touches []TouchForInput) { func (u *UserInterface) UpdateInput(keys map[Key]struct{}, runes []rune, touches []TouchForInput) {
u.updateInputStateFromOutside(keys, runes, touches) u.updateInputStateFromOutside(keys, runes, touches)
if u.fpsMode == FPSModeVsyncOffMinimum { if FPSModeType(atomic.LoadInt32(&u.fpsMode)) == FPSModeVsyncOffMinimum {
u.renderRequester.RequestRenderIfNeeded() u.renderRequester.RequestRenderIfNeeded()
} }
} }
@ -464,11 +468,11 @@ type RenderRequester interface {
func (u *UserInterface) SetRenderRequester(renderRequester RenderRequester) { func (u *UserInterface) SetRenderRequester(renderRequester RenderRequester) {
u.renderRequester = renderRequester u.renderRequester = renderRequester
u.updateExplicitRenderingModeIfNeeded() u.updateExplicitRenderingModeIfNeeded(FPSModeType(atomic.LoadInt32(&u.fpsMode)))
} }
func (u *UserInterface) ScheduleFrame() { func (u *UserInterface) ScheduleFrame() {
if u.renderRequester != nil && u.fpsMode == FPSModeVsyncOffMinimum { if u.renderRequester != nil && FPSModeType(atomic.LoadInt32(&u.fpsMode)) == FPSModeVsyncOffMinimum {
u.renderRequester.RequestRenderIfNeeded() u.renderRequester.RequestRenderIfNeeded()
} }
} }

View File

@ -182,7 +182,11 @@ func (*UserInterface) IsRunnableOnUnfocused() bool {
func (*UserInterface) SetRunnableOnUnfocused(runnableOnUnfocused bool) { func (*UserInterface) SetRunnableOnUnfocused(runnableOnUnfocused bool) {
} }
func (*UserInterface) setFPSMode(mode FPSModeType) { func (*UserInterface) FPSMode() FPSModeType {
return FPSModeVsyncOn
}
func (*UserInterface) SetFPSMode(mode FPSModeType) {
} }
func (*UserInterface) ScheduleFrame() { func (*UserInterface) ScheduleFrame() {

4
run.go
View File

@ -470,7 +470,7 @@ func DeviceScaleFactor() float64 {
// IsVsyncEnabled returns a boolean value indicating whether // IsVsyncEnabled returns a boolean value indicating whether
// the game uses the display's vsync. // the game uses the display's vsync.
func IsVsyncEnabled() bool { func IsVsyncEnabled() bool {
return ui.FPSMode() == ui.FPSModeVsyncOn return ui.Get().FPSMode() == ui.FPSModeVsyncOn
} }
// SetVsyncEnabled sets a boolean value indicating whether // SetVsyncEnabled sets a boolean value indicating whether
@ -526,7 +526,7 @@ const (
// //
// Deprecated: as of v2.5. Use SetVsyncEnabled instead. // Deprecated: as of v2.5. Use SetVsyncEnabled instead.
func FPSMode() FPSModeType { func FPSMode() FPSModeType {
return ui.FPSMode() return ui.Get().FPSMode()
} }
// SetFPSMode sets the FPS mode. // SetFPSMode sets the FPS mode.