Files
nmage/engine/game.go
2022-02-27 11:21:15 +04:00

65 lines
1.2 KiB
Go
Executable File

package engine
import (
"github.com/bloeys/nmage/timing"
nmageimgui "github.com/bloeys/nmage/ui/imgui"
"github.com/go-gl/gl/v4.1-core/gl"
)
type Game interface {
Init()
Start()
FrameStart()
Update()
Render()
FrameEnd()
ShouldRun() bool
GetWindow() *Window
GetImGUI() nmageimgui.ImguiInfo
Deinit()
}
func Run(g Game) {
w := g.GetWindow()
ui := g.GetImGUI()
g.Init()
//Simulate an imgui frame during init so any imgui calls are allowed within init
tempWidth, tempHeight := w.SDLWin.GetSize()
tempFBWidth, tempFBHeight := w.SDLWin.GLGetDrawableSize()
ui.FrameStart(float32(tempWidth), float32(tempHeight))
g.Start()
ui.Render(float32(tempWidth), float32(tempHeight), tempFBWidth, tempFBHeight)
for g.ShouldRun() {
//PERF: Cache these
width, height := w.SDLWin.GetSize()
fbWidth, fbHeight := w.SDLWin.GLGetDrawableSize()
timing.FrameStarted()
w.handleInputs()
ui.FrameStart(float32(width), float32(height))
g.FrameStart()
g.Update()
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
g.Render()
ui.Render(float32(width), float32(height), fbWidth, fbHeight)
w.SDLWin.GLSwap()
g.FrameEnd()
w.Rend.FrameEnd()
timing.FrameEnded()
}
g.Deinit()
}