Vsync and FPS display toggles. (#124)

They now toggle more reliably as well as work globally.
This commit is contained in:
Ziemas 2019-11-10 00:56:45 +01:00 committed by Tim Sarbin
parent ebff54cfba
commit 4aba58eb28
3 changed files with 15 additions and 9 deletions

View File

@ -5,6 +5,7 @@ import (
"math"
"path"
"runtime"
"strconv"
"strings"
"sync"
"time"
@ -21,6 +22,8 @@ import (
"github.com/OpenDiablo2/OpenDiablo2/ui"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
"github.com/hajimehoshi/ebiten/inpututil"
)
// Engine is the core OpenDiablo2 engine
@ -39,6 +42,7 @@ type Engine struct {
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
showFPS bool
}
// CreateEngine creates and instance of the OpenDiablo2 engine
@ -173,6 +177,14 @@ func (v *Engine) Update() {
v.fullscreenKey = false
}
if inpututil.IsKeyJustPressed(ebiten.KeyF6) {
v.showFPS = !v.showFPS
}
if inpututil.IsKeyJustPressed(ebiten.KeyF8) {
ebiten.SetVsyncEnabled(!ebiten.IsVsyncEnabled())
}
v.updateScene()
if v.CurrentScene == nil {
log.Fatal("no scene loaded")
@ -203,6 +215,9 @@ func (v *Engine) Draw(screen *ebiten.Image) {
v.CurrentScene.Render(screen)
v.UIManager.Draw(screen)
}
if v.showFPS == true {
ebitenutil.DebugPrintAt(screen, "vsync:"+strconv.FormatBool(ebiten.IsVsyncEnabled())+"\nFPS:"+strconv.Itoa(int(ebiten.CurrentFPS())), 5, 565)
}
}
// SetNextScene tells the engine what scene to load on the next update cycle

View File

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

View File

@ -91,13 +91,6 @@ 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
}