Simplify the game interface

This commit is contained in:
bloeys
2022-07-02 21:21:59 +04:00
parent 901d8e2b5e
commit e1bf0697fc
3 changed files with 33 additions and 42 deletions

View File

@ -3,6 +3,7 @@ package engine
import ( import (
"runtime" "runtime"
"github.com/bloeys/nmage/asserts"
"github.com/bloeys/nmage/input" "github.com/bloeys/nmage/input"
"github.com/bloeys/nmage/renderer" "github.com/bloeys/nmage/renderer"
"github.com/bloeys/nmage/timing" "github.com/bloeys/nmage/timing"
@ -11,6 +12,10 @@ import (
"github.com/veandco/go-sdl2/sdl" "github.com/veandco/go-sdl2/sdl"
) )
var (
isInited = false
)
type Window struct { type Window struct {
SDLWin *sdl.Window SDLWin *sdl.Window
GlCtx sdl.GLContext GlCtx sdl.GLContext
@ -96,6 +101,8 @@ func (w *Window) Destroy() error {
func Init() error { func Init() error {
isInited = true
runtime.LockOSThread() runtime.LockOSThread()
timing.Init() timing.Init()
err := initSDL() err := initSDL()
@ -139,6 +146,7 @@ func CreateOpenGLWindowCentered(title string, width, height int32, flags WindowF
func createWindow(title string, x, y, width, height int32, flags WindowFlags, rend renderer.Render) (*Window, error) { func createWindow(title string, x, y, width, height int32, flags WindowFlags, rend renderer.Render) (*Window, error) {
asserts.T(isInited, "engine.Init was not called!")
if x == -1 && y == -1 { if x == -1 && y == -1 {
x = sdl.WINDOWPOS_CENTERED x = sdl.WINDOWPOS_CENTERED
y = sdl.WINDOWPOS_CENTERED y = sdl.WINDOWPOS_CENTERED
@ -186,6 +194,8 @@ func initOpenGL() error {
} }
func SetVSync(enabled bool) { func SetVSync(enabled bool) {
asserts.T(isInited, "engine.Init was not called!")
if enabled { if enabled {
sdl.GLSetSwapInterval(1) sdl.GLSetSwapInterval(1)
} else { } else {

View File

@ -6,37 +6,32 @@ import (
"github.com/go-gl/gl/v4.1-core/gl" "github.com/go-gl/gl/v4.1-core/gl"
) )
var (
isRunning = false
)
type Game interface { type Game interface {
Init() Init()
Start()
FrameStart()
Update() Update()
Render() Render()
FrameEnd() FrameEnd()
ShouldRun() bool
GetWindow() *Window DeInit()
GetImGUI() nmageimgui.ImguiInfo
Deinit()
} }
func Run(g Game) { func Run(g Game, w *Window, ui nmageimgui.ImguiInfo) {
w := g.GetWindow()
ui := g.GetImGUI()
isRunning = true
g.Init() g.Init()
//Simulate an imgui frame during init so any imgui calls are allowed within init //Simulate an imgui frame during init so any imgui calls are allowed within init
tempWidth, tempHeight := w.SDLWin.GetSize() tempWidth, tempHeight := w.SDLWin.GetSize()
tempFBWidth, tempFBHeight := w.SDLWin.GLGetDrawableSize() tempFBWidth, tempFBHeight := w.SDLWin.GLGetDrawableSize()
ui.FrameStart(float32(tempWidth), float32(tempHeight)) ui.FrameStart(float32(tempWidth), float32(tempHeight))
g.Start()
ui.Render(float32(tempWidth), float32(tempHeight), tempFBWidth, tempFBHeight) ui.Render(float32(tempWidth), float32(tempHeight), tempFBWidth, tempFBHeight)
for g.ShouldRun() { for isRunning {
//PERF: Cache these //PERF: Cache these
width, height := w.SDLWin.GetSize() width, height := w.SDLWin.GetSize()
@ -46,8 +41,6 @@ func Run(g Game) {
w.handleInputs() w.handleInputs()
ui.FrameStart(float32(width), float32(height)) ui.FrameStart(float32(width), float32(height))
g.FrameStart()
g.Update() g.Update()
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT) gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
@ -60,5 +53,9 @@ func Run(g Game) {
timing.FrameEnded() timing.FrameEnded()
} }
g.Deinit() g.DeInit()
}
func Quit() {
isRunning = false
} }

34
main.go
View File

@ -22,6 +22,11 @@ import (
// need to rebind the texutre if the texture (or any material values) change between draw calls // need to rebind the texutre if the texture (or any material values) change between draw calls
// DT handling for when it is zero is wrong! (Gives 1000 DT) // DT handling for when it is zero is wrong! (Gives 1000 DT)
//nMage TODO:
// * Allow texture loading without cache
// * Move SetAttribute away from material struct
// * Create VAO struct independent from VBO to support multi-VBO use cases (e.g. instancing)
//TODO: Tasks: //TODO: Tasks:
// Build simple game // Build simple game
// Integrate physx // Integrate physx
@ -39,7 +44,6 @@ import (
// Material system editor with fields automatically extracted from the shader // Material system editor with fields automatically extracted from the shader
var ( var (
isRunning bool = true
window *engine.Window window *engine.Window
simpleMat *materials.Material simpleMat *materials.Material
@ -57,7 +61,6 @@ var (
type OurGame struct { type OurGame struct {
Win *engine.Window Win *engine.Window
ImGUIInfo nmageimgui.ImguiInfo ImGUIInfo nmageimgui.ImguiInfo
Quitting bool
} }
func (g *OurGame) Init() { func (g *OurGame) Init() {
@ -102,16 +105,10 @@ func (g *OurGame) Init() {
simpleMat.SetUnifVec3("lightColor1", lightColor1) simpleMat.SetUnifVec3("lightColor1", lightColor1)
} }
func (g *OurGame) Start() {
}
func (g *OurGame) FrameStart() {
}
func (g *OurGame) Update() { func (g *OurGame) Update() {
if input.IsQuitClicked() { if input.IsQuitClicked() {
g.Quitting = true engine.Quit()
} }
winWidth, winHeight := g.Win.SDLWin.GetSize() winWidth, winHeight := g.Win.SDLWin.GetSize()
@ -167,25 +164,13 @@ func (g *OurGame) Render() {
tempModelMat.Translate(gglm.NewVec3(float32(rowSize), -1, 0)) tempModelMat.Translate(gglm.NewVec3(float32(rowSize), -1, 0))
} }
g.GetWindow().SDLWin.SetTitle(fmt.Sprint("nMage (", timing.GetAvgFPS(), " fps)")) g.Win.SDLWin.SetTitle(fmt.Sprint("nMage (", timing.GetAvgFPS(), " fps)"))
} }
func (g *OurGame) FrameEnd() { func (g *OurGame) FrameEnd() {
} }
func (g *OurGame) ShouldRun() bool { func (g *OurGame) DeInit() {
return !g.Quitting
}
func (g *OurGame) GetWindow() *engine.Window {
return g.Win
}
func (g *OurGame) GetImGUI() nmageimgui.ImguiInfo {
return g.ImGUIInfo
}
func (g *OurGame) Deinit() {
g.Win.Destroy() g.Win.Destroy()
} }
@ -211,8 +196,7 @@ func main() {
ImGUIInfo: nmageimgui.NewImGUI(), ImGUIInfo: nmageimgui.NewImGUI(),
} }
engine.Run(game) engine.Run(game, window, game.ImGUIInfo)
return
} }
func updateViewMat() { func updateViewMat() {