ui: Implement TPS/SetTPS

Fixes #605
Fixes #321
This commit is contained in:
Hajime Hoshi 2018-07-17 01:42:19 +09:00
parent 1626e40822
commit 3a8ca5ad73
4 changed files with 54 additions and 5 deletions

View File

@ -87,6 +87,7 @@ func update(screen *ebiten.Image) error {
runnableInBackground := ebiten.IsRunnableInBackground() runnableInBackground := ebiten.IsRunnableInBackground()
cursorVisible := ebiten.IsCursorVisible() cursorVisible := ebiten.IsCursorVisible()
vsyncEnabled := ebiten.IsVsyncEnabled() vsyncEnabled := ebiten.IsVsyncEnabled()
tps := ebiten.TPS()
if inpututil.IsKeyJustPressed(ebiten.KeyUp) { if inpututil.IsKeyJustPressed(ebiten.KeyUp) {
screenHeight += d screenHeight += d
@ -130,6 +131,20 @@ func update(screen *ebiten.Image) error {
if inpututil.IsKeyJustPressed(ebiten.KeyV) { if inpututil.IsKeyJustPressed(ebiten.KeyV) {
vsyncEnabled = !vsyncEnabled vsyncEnabled = !vsyncEnabled
} }
if inpututil.IsKeyJustPressed(ebiten.KeyT) {
switch tps {
case -1:
tps = 30
case 30:
tps = 60
case 60:
tps = 120
case 120:
tps = -1
default:
panic("not reached")
}
}
ebiten.SetScreenSize(screenWidth, screenHeight) ebiten.SetScreenSize(screenWidth, screenHeight)
ebiten.SetScreenScale(screenScale) ebiten.SetScreenScale(screenScale)
@ -137,6 +152,7 @@ func update(screen *ebiten.Image) error {
ebiten.SetRunnableInBackground(runnableInBackground) ebiten.SetRunnableInBackground(runnableInBackground)
ebiten.SetCursorVisible(cursorVisible) ebiten.SetCursorVisible(cursorVisible)
ebiten.SetVsyncEnabled(vsyncEnabled) ebiten.SetVsyncEnabled(vsyncEnabled)
ebiten.SetTPS(tps)
if inpututil.IsKeyJustPressed(ebiten.KeyI) { if inpututil.IsKeyJustPressed(ebiten.KeyI) {
ebiten.SetWindowIcon([]image.Image{createRandomIconImage()}) ebiten.SetWindowIcon([]image.Image{createRandomIconImage()})
@ -167,8 +183,10 @@ Press C key to switch the cursor visibility
Press I key to change the window icon Press I key to change the window icon
Press V key to switch vsync Press V key to switch vsync
Press Q key to quit Press Q key to quit
Press T key to switch TPS (ticks per second)
Cursor: (%d, %d) Cursor: (%d, %d)
FPS: %0.2f`, x, y, ebiten.CurrentFPS()) FPS: %0.2f
TPS: %d`, x, y, ebiten.CurrentFPS(), ebiten.TPS())
ebitenutil.DebugPrint(screen, msg) ebitenutil.DebugPrint(screen, msg)
return nil return nil
} }

View File

@ -82,7 +82,13 @@ func (c *graphicsContext) initializeIfNeeded() error {
} }
func (c *graphicsContext) Update(afterFrameUpdate func()) error { func (c *graphicsContext) Update(afterFrameUpdate func()) error {
updateCount := clock.Update(defaultTPS) tps := int(TPS())
count := clock.Update(tps)
updateCount := 1
if tps >= 0 {
updateCount = count
}
if err := c.initializeIfNeeded(); err != nil { if err := c.initializeIfNeeded(); err != nil {
return err return err

View File

@ -110,8 +110,7 @@ func updateFPS(now int64) {
// Update updates the inner clock state and returns an integer value // Update updates the inner clock state and returns an integer value
// indicating how many game frames the game should update based on given tps. // indicating how many game frames the game should update based on given tps.
// tps represents TPS (ticks per second). // tps represents TPS (ticks per second).
// If tps is 0, Update always returns 0. // If tps <= 0, Update always returns 0.
// If tps is negative, Update panics.
// //
// Update is expected to be called per frame. // Update is expected to be called per frame.
func Update(tps int) int { func Update(tps int) int {
@ -127,5 +126,8 @@ func Update(tps int) int {
n := now() n := now()
updateFPS(n) updateFPS(n)
return calcCountFromTPS(int64(tps), n) if tps > 0 {
return calcCountFromTPS(int64(tps), n)
}
return 0
} }

23
run.go
View File

@ -49,8 +49,13 @@ func CurrentFPS() float64 {
var ( var (
isDrawingSkipped = int32(0) isDrawingSkipped = int32(0)
currentTPS = int32(0)
) )
func init() {
atomic.StoreInt32(&currentTPS, defaultTPS)
}
func setDrawingSkipped(skipped bool) { func setDrawingSkipped(skipped bool) {
v := int32(0) v := int32(0)
if skipped { if skipped {
@ -552,3 +557,21 @@ func IsVsyncEnabled() bool {
func SetVsyncEnabled(enabled bool) { func SetVsyncEnabled(enabled bool) {
ui.SetVsyncEnabled(enabled) ui.SetVsyncEnabled(enabled)
} }
// TPS returns the current TPS.
//
// TPS is concurrent-safe.
func TPS() int {
return int(atomic.LoadInt32(&currentTPS))
}
// SetTPS sets the TPS (ticks per frame), that represents how many updating function is called
// per second.
// The initial value is 60.
//
// If the given tps is negative, TPS is unfixed and the game is updated per frame.
//
// SetTPS is concurrent-safe.
func SetTPS(tps int) {
atomic.StoreInt32(&currentTPS, int32(tps))
}