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:
parent
c51e464676
commit
ebff54cfba
@ -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()
|
||||
}
|
||||
|
||||
|
3
main.go
3
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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user