diff --git a/core/Engine.go b/core/Engine.go index f6c309c7..e8efe2d9 100644 --- a/core/Engine.go +++ b/core/Engine.go @@ -7,6 +7,7 @@ import ( "runtime" "strings" "sync" + "time" "github.com/OpenDiablo2/OpenDiablo2/mpq" @@ -37,6 +38,7 @@ type Engine struct { SoundManager *sound.Manager // The sound manager 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 + lastTime float64 // Last time we updated the scene } // CreateEngine creates and instance of the OpenDiablo2 engine @@ -180,7 +182,12 @@ func (v *Engine) Update() { 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() } diff --git a/main.go b/main.go index 3a7c0b6d..f0490f82 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "image" "log" + "strconv" "github.com/hajimehoshi/ebiten/ebitenutil" @@ -52,6 +53,6 @@ func update(screen *ebiten.Image) error { return nil } 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 } diff --git a/scenes/MapEngineTest.go b/scenes/MapEngineTest.go index 5c851f05..4aee7c98 100644 --- a/scenes/MapEngineTest.go +++ b/scenes/MapEngineTest.go @@ -91,6 +91,13 @@ func (v *MapEngineTest) Render(screen *ebiten.Image) { } 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) { v.mapEngine.OffsetY -= tickTime * 800 }