internal/ui: refactoring: reduce members from globalState

This commit is contained in:
Hajime Hoshi 2022-07-12 12:34:50 +09:00
parent 08e54ae1a6
commit c5e952d196
2 changed files with 27 additions and 25 deletions

View File

@ -20,7 +20,15 @@ import (
"time"
)
const (
DefaultTPS = 60
SyncWithFPS = -1
)
var (
// tps represents TPS (ticks per second).
tps = DefaultTPS
lastNow int64
// lastSystemTime is the last system time in the previous Update.
@ -127,16 +135,14 @@ func updateFPSAndTPS(now int64, count int) {
tpsCount = 0
}
const SyncWithFPS = -1
// Update updates the inner clock state and returns an integer value
// indicating how many times the game should update based on given tps.
// tps represents TPS (ticks per second).
// indicating how many times the game should update based on the current tps.
//
// If tps is SyncWithFPS, Update always returns 1.
// If tps <= 0 and not SyncWithFPS, Update always returns 0.
//
// Update is expected to be called per frame.
func Update(tps int) int {
func Update() int {
m.Lock()
defer m.Unlock()
@ -157,3 +163,15 @@ func Update(tps int) int {
return c
}
func SetTPS(newTPS int) {
m.Lock()
defer m.Unlock()
tps = newTPS
}
func TPS() int {
m.Lock()
defer m.Unlock()
return tps
}

View File

@ -30,7 +30,7 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/hooks"
)
const DefaultTPS = 60
const DefaultTPS = clock.DefaultTPS
type Game interface {
NewOffscreenImage(width, height int) *Image
@ -62,7 +62,7 @@ func newContext(game Game) *context {
func (c *context) updateFrame(graphicsDriver graphicsdriver.Graphics, outsideWidth, outsideHeight float64, deviceScaleFactor float64) error {
// TODO: If updateCount is 0 and vsync is disabled, swapping buffers can be skipped.
return c.updateFrameImpl(graphicsDriver, clock.Update(theGlobalState.tps()), outsideWidth, outsideHeight, deviceScaleFactor)
return c.updateFrameImpl(graphicsDriver, clock.Update(), outsideWidth, outsideHeight, deviceScaleFactor)
}
func (c *context) forceUpdateFrame(graphicsDriver graphicsdriver.Graphics, outsideWidth, outsideHeight float64, deviceScaleFactor float64) error {
@ -283,7 +283,6 @@ func (c *context) screenScaleAndOffsets() (float64, float64, float64) {
}
var theGlobalState = globalState{
tps_: DefaultTPS,
isScreenClearedEveryFrame_: 1,
screenFilterEnabled_: 1,
}
@ -295,7 +294,6 @@ type globalState struct {
errM sync.Mutex
fpsMode_ int32
tps_ int32
isScreenClearedEveryFrame_ int32
screenFilterEnabled_ int32
}
@ -322,20 +320,6 @@ func (g *globalState) setFPSMode(fpsMode FPSModeType) {
atomic.StoreInt32(&g.fpsMode_, int32(fpsMode))
}
func (g *globalState) tps() int {
if g.fpsMode() == FPSModeVsyncOffMinimum {
return clock.SyncWithFPS
}
return int(atomic.LoadInt32(&g.tps_))
}
func (g *globalState) setTPS(tps int) {
if tps < 0 && tps != clock.SyncWithFPS {
panic("ui: tps must be >= 0 or SyncWithFPS")
}
atomic.StoreInt32(&g.tps_, int32(tps))
}
func (g *globalState) isScreenClearedEveryFrame() bool {
return atomic.LoadInt32(&g.isScreenClearedEveryFrame_) != 0
}
@ -370,11 +354,11 @@ func SetFPSMode(fpsMode FPSModeType) {
}
func TPS() int {
return theGlobalState.tps()
return clock.TPS()
}
func SetTPS(tps int) {
theGlobalState.setTPS(tps)
clock.SetTPS(tps)
}
func IsScreenClearedEveryFrame() bool {