mirror of
https://github.com/bloeys/nmage.git
synced 2025-12-29 13:28:20 +00:00
Timing package
This commit is contained in:
10
main.go
10
main.go
@ -11,15 +11,16 @@ import (
|
|||||||
"github.com/bloeys/nmage/input"
|
"github.com/bloeys/nmage/input"
|
||||||
"github.com/bloeys/nmage/logging"
|
"github.com/bloeys/nmage/logging"
|
||||||
"github.com/bloeys/nmage/shaders"
|
"github.com/bloeys/nmage/shaders"
|
||||||
|
"github.com/bloeys/nmage/timing"
|
||||||
"github.com/go-gl/gl/v4.1-core/gl"
|
"github.com/go-gl/gl/v4.1-core/gl"
|
||||||
"github.com/inkyblackness/imgui-go/v4"
|
"github.com/inkyblackness/imgui-go/v4"
|
||||||
"github.com/veandco/go-sdl2/sdl"
|
"github.com/veandco/go-sdl2/sdl"
|
||||||
)
|
)
|
||||||
|
|
||||||
//TODO:
|
//TODO:
|
||||||
//Timing and deltatime
|
|
||||||
//Make a window/engine class
|
//Make a window/engine class
|
||||||
//Mesh class
|
//Mesh class
|
||||||
|
//Object
|
||||||
//Abstract UI
|
//Abstract UI
|
||||||
//Textures
|
//Textures
|
||||||
//Proper Asset loading
|
//Proper Asset loading
|
||||||
@ -117,13 +118,18 @@ func main() {
|
|||||||
simpleShader.SetUnifVec3("lightColor1", &lightColor1)
|
simpleShader.SetUnifVec3("lightColor1", &lightColor1)
|
||||||
|
|
||||||
//Game loop
|
//Game loop
|
||||||
|
timing.Init()
|
||||||
for isRunning {
|
for isRunning {
|
||||||
|
|
||||||
|
timing.FrameStarted()
|
||||||
|
|
||||||
handleInputs()
|
handleInputs()
|
||||||
runGameLogic()
|
runGameLogic()
|
||||||
draw()
|
draw()
|
||||||
|
|
||||||
sdl.Delay(17)
|
timing.FrameEnded()
|
||||||
|
|
||||||
|
window.SetTitle(fmt.Sprintf("FPS: %.0f; Elapsed: %v", 1/timing.DT(), timing.ElapsedTime()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
31
timing/timing.go
Executable file
31
timing/timing.go
Executable file
@ -0,0 +1,31 @@
|
|||||||
|
package timing
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
|
var (
|
||||||
|
dt float32 = 0.01
|
||||||
|
frameStart time.Time
|
||||||
|
startTime time.Time
|
||||||
|
)
|
||||||
|
|
||||||
|
func Init() {
|
||||||
|
startTime = time.Now()
|
||||||
|
}
|
||||||
|
|
||||||
|
func FrameStarted() {
|
||||||
|
frameStart = time.Now()
|
||||||
|
}
|
||||||
|
|
||||||
|
func FrameEnded() {
|
||||||
|
dt = float32(time.Since(frameStart).Seconds())
|
||||||
|
}
|
||||||
|
|
||||||
|
//DT is frame deltatime in milliseconds
|
||||||
|
func DT() float32 {
|
||||||
|
return dt
|
||||||
|
}
|
||||||
|
|
||||||
|
//ElapsedTime is time since game start
|
||||||
|
func ElapsedTime() uint64 {
|
||||||
|
return uint64(time.Since(startTime).Seconds())
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user