1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-11-19 19:06:45 -05:00

Use delta time for scene updates. (#123)

* Use delta time when updating scenes.
Delta time is in seconds and capped to 1/10th.
* Show FPS and VSYNC status in the bottom left corner.

Fixes #123
This commit is contained in:
Ziemas 2019-11-09 22:17:01 +01:00 committed by Tim Sarbin
parent c51e464676
commit ebff54cfba
3 changed files with 17 additions and 2 deletions

View File

@ -7,6 +7,7 @@ import (
"runtime" "runtime"
"strings" "strings"
"sync" "sync"
"time"
"github.com/OpenDiablo2/OpenDiablo2/mpq" "github.com/OpenDiablo2/OpenDiablo2/mpq"
@ -37,6 +38,7 @@ type Engine struct {
SoundManager *sound.Manager // The sound manager SoundManager *sound.Manager // The sound manager
nextScene scenes.Scene // The next scene to be loaded at the end of the game loop nextScene scenes.Scene // The next scene to be loaded at the end of the game loop
fullscreenKey bool // When true, the fullscreen toggle is still being pressed fullscreenKey bool // When true, the fullscreen toggle is still being pressed
lastTime float64 // Last time we updated the scene
} }
// CreateEngine creates and instance of the OpenDiablo2 engine // CreateEngine creates and instance of the OpenDiablo2 engine
@ -180,7 +182,12 @@ func (v *Engine) Update() {
return return
} }
v.CurrentScene.Update(float64(1) / ebiten.CurrentTPS()) currentTime := float64(time.Now().UnixNano()) / float64(time.Second)
deltaTime := math.Min((currentTime - v.lastTime), 0.1)
v.lastTime = currentTime
v.CurrentScene.Update(deltaTime)
v.UIManager.Update() v.UIManager.Update()
} }

View File

@ -3,6 +3,7 @@ package main
import ( import (
"image" "image"
"log" "log"
"strconv"
"github.com/hajimehoshi/ebiten/ebitenutil" "github.com/hajimehoshi/ebiten/ebitenutil"
@ -52,6 +53,6 @@ func update(screen *ebiten.Image) error {
return nil return nil
} }
d2Engine.Draw(screen) d2Engine.Draw(screen)
//ebitenutil.DebugPrint(screen, "FPS:"+strconv.Itoa(int(ebiten.CurrentFPS()))) ebitenutil.DebugPrintAt(screen, "vsync:"+strconv.FormatBool(ebiten.IsVsyncEnabled())+"\nFPS:"+strconv.Itoa(int(ebiten.CurrentFPS())), 5, 565)
return nil return nil
} }

View File

@ -91,6 +91,13 @@ func (v *MapEngineTest) Render(screen *ebiten.Image) {
} }
func (v *MapEngineTest) Update(tickTime float64) { func (v *MapEngineTest) Update(tickTime float64) {
if v.uiManager.KeyPressed(ebiten.KeyF8) {
if ebiten.IsVsyncEnabled() == true {
ebiten.SetVsyncEnabled(false)
} else {
ebiten.SetVsyncEnabled(true)
}
}
if v.uiManager.KeyPressed(ebiten.KeyDown) { if v.uiManager.KeyPressed(ebiten.KeyDown) {
v.mapEngine.OffsetY -= tickTime * 800 v.mapEngine.OffsetY -= tickTime * 800
} }