diff --git a/internal/ui/context.go b/internal/ui/context.go index 85e77fc13..985d4a71f 100644 --- a/internal/ui/context.go +++ b/internal/ui/context.go @@ -16,8 +16,6 @@ package ui import ( "math" - "sync" - "sync/atomic" "github.com/hajimehoshi/ebiten/v2/internal/atlas" "github.com/hajimehoshi/ebiten/v2/internal/buffered" @@ -235,81 +233,3 @@ func (c *context) adjustPosition(x, y float64, deviceScaleFactor float64) (float } return (x*deviceScaleFactor - ox) / s, (y*deviceScaleFactor - oy) / s } - -var theGlobalState = globalState{ - isScreenClearedEveryFrame_: 1, -} - -// globalState represents a global state in this package. -// This is available even before the game loop starts. -type globalState struct { - err_ error - errM sync.Mutex - - fpsMode_ int32 - isScreenClearedEveryFrame_ int32 - graphicsLibrary_ int32 -} - -func (g *globalState) error() error { - g.errM.Lock() - defer g.errM.Unlock() - return g.err_ -} - -func (g *globalState) setError(err error) { - g.errM.Lock() - defer g.errM.Unlock() - if g.err_ == nil { - g.err_ = err - } -} - -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 { - return atomic.LoadInt32(&g.isScreenClearedEveryFrame_) != 0 -} - -func (g *globalState) setScreenClearedEveryFrame(cleared bool) { - v := int32(0) - if cleared { - v = 1 - } - atomic.StoreInt32(&g.isScreenClearedEveryFrame_, v) -} - -func (g *globalState) setGraphicsLibrary(library GraphicsLibrary) { - atomic.StoreInt32(&g.graphicsLibrary_, int32(library)) -} - -func (g *globalState) graphicsLibrary() GraphicsLibrary { - return GraphicsLibrary(atomic.LoadInt32(&g.graphicsLibrary_)) -} - -func FPSMode() FPSModeType { - return theGlobalState.fpsMode() -} - -func SetFPSMode(fpsMode FPSModeType) { - theGlobalState.setFPSMode(fpsMode) - theUI.SetFPSMode(fpsMode) -} - -func IsScreenClearedEveryFrame() bool { - return theGlobalState.isScreenClearedEveryFrame() -} - -func SetScreenClearedEveryFrame(cleared bool) { - theGlobalState.setScreenClearedEveryFrame(cleared) -} - -func GetGraphicsLibrary() GraphicsLibrary { - return theGlobalState.graphicsLibrary() -} diff --git a/internal/ui/globalstate.go b/internal/ui/globalstate.go new file mode 100644 index 000000000..0b1f5c216 --- /dev/null +++ b/internal/ui/globalstate.go @@ -0,0 +1,98 @@ +// Copyright 2022 The Ebiten 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. + +package ui + +import ( + "sync" + "sync/atomic" +) + +var theGlobalState = globalState{ + isScreenClearedEveryFrame_: 1, +} + +// globalState represents a global state in this package. +// This is available even before the game loop starts. +type globalState struct { + err_ error + errM sync.Mutex + + fpsMode_ int32 + isScreenClearedEveryFrame_ int32 + graphicsLibrary_ int32 +} + +func (g *globalState) error() error { + g.errM.Lock() + defer g.errM.Unlock() + return g.err_ +} + +func (g *globalState) setError(err error) { + g.errM.Lock() + defer g.errM.Unlock() + if g.err_ == nil { + g.err_ = err + } +} + +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 { + return atomic.LoadInt32(&g.isScreenClearedEveryFrame_) != 0 +} + +func (g *globalState) setScreenClearedEveryFrame(cleared bool) { + v := int32(0) + if cleared { + v = 1 + } + atomic.StoreInt32(&g.isScreenClearedEveryFrame_, v) +} + +func (g *globalState) setGraphicsLibrary(library GraphicsLibrary) { + atomic.StoreInt32(&g.graphicsLibrary_, int32(library)) +} + +func (g *globalState) graphicsLibrary() GraphicsLibrary { + return GraphicsLibrary(atomic.LoadInt32(&g.graphicsLibrary_)) +} + +func FPSMode() FPSModeType { + return theGlobalState.fpsMode() +} + +func SetFPSMode(fpsMode FPSModeType) { + theGlobalState.setFPSMode(fpsMode) + theUI.SetFPSMode(fpsMode) +} + +func IsScreenClearedEveryFrame() bool { + return theGlobalState.isScreenClearedEveryFrame() +} + +func SetScreenClearedEveryFrame(cleared bool) { + theGlobalState.setScreenClearedEveryFrame(cleared) +} + +func GetGraphicsLibrary() GraphicsLibrary { + return theGlobalState.graphicsLibrary() +}