mirror of
https://github.com/bloeys/nmage.git
synced 2025-12-29 13:28:20 +00:00
Simplify the game interface
This commit is contained in:
@ -3,6 +3,7 @@ package engine
|
||||
import (
|
||||
"runtime"
|
||||
|
||||
"github.com/bloeys/nmage/asserts"
|
||||
"github.com/bloeys/nmage/input"
|
||||
"github.com/bloeys/nmage/renderer"
|
||||
"github.com/bloeys/nmage/timing"
|
||||
@ -11,6 +12,10 @@ import (
|
||||
"github.com/veandco/go-sdl2/sdl"
|
||||
)
|
||||
|
||||
var (
|
||||
isInited = false
|
||||
)
|
||||
|
||||
type Window struct {
|
||||
SDLWin *sdl.Window
|
||||
GlCtx sdl.GLContext
|
||||
@ -96,6 +101,8 @@ func (w *Window) Destroy() error {
|
||||
|
||||
func Init() error {
|
||||
|
||||
isInited = true
|
||||
|
||||
runtime.LockOSThread()
|
||||
timing.Init()
|
||||
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) {
|
||||
|
||||
asserts.T(isInited, "engine.Init was not called!")
|
||||
if x == -1 && y == -1 {
|
||||
x = sdl.WINDOWPOS_CENTERED
|
||||
y = sdl.WINDOWPOS_CENTERED
|
||||
@ -186,6 +194,8 @@ func initOpenGL() error {
|
||||
}
|
||||
|
||||
func SetVSync(enabled bool) {
|
||||
asserts.T(isInited, "engine.Init was not called!")
|
||||
|
||||
if enabled {
|
||||
sdl.GLSetSwapInterval(1)
|
||||
} else {
|
||||
|
||||
@ -6,37 +6,32 @@ import (
|
||||
"github.com/go-gl/gl/v4.1-core/gl"
|
||||
)
|
||||
|
||||
var (
|
||||
isRunning = false
|
||||
)
|
||||
|
||||
type Game interface {
|
||||
Init()
|
||||
Start()
|
||||
|
||||
FrameStart()
|
||||
Update()
|
||||
Render()
|
||||
FrameEnd()
|
||||
ShouldRun() bool
|
||||
|
||||
GetWindow() *Window
|
||||
GetImGUI() nmageimgui.ImguiInfo
|
||||
|
||||
Deinit()
|
||||
DeInit()
|
||||
}
|
||||
|
||||
func Run(g Game) {
|
||||
|
||||
w := g.GetWindow()
|
||||
ui := g.GetImGUI()
|
||||
func Run(g Game, w *Window, ui nmageimgui.ImguiInfo) {
|
||||
|
||||
isRunning = true
|
||||
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() {
|
||||
for isRunning {
|
||||
|
||||
//PERF: Cache these
|
||||
width, height := w.SDLWin.GetSize()
|
||||
@ -46,8 +41,6 @@ func Run(g Game) {
|
||||
w.handleInputs()
|
||||
ui.FrameStart(float32(width), float32(height))
|
||||
|
||||
g.FrameStart()
|
||||
|
||||
g.Update()
|
||||
|
||||
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
|
||||
@ -60,5 +53,9 @@ func Run(g Game) {
|
||||
timing.FrameEnded()
|
||||
}
|
||||
|
||||
g.Deinit()
|
||||
g.DeInit()
|
||||
}
|
||||
|
||||
func Quit() {
|
||||
isRunning = false
|
||||
}
|
||||
|
||||
34
main.go
34
main.go
@ -22,6 +22,11 @@ import (
|
||||
// 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)
|
||||
|
||||
//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:
|
||||
// Build simple game
|
||||
// Integrate physx
|
||||
@ -39,7 +44,6 @@ import (
|
||||
// Material system editor with fields automatically extracted from the shader
|
||||
|
||||
var (
|
||||
isRunning bool = true
|
||||
window *engine.Window
|
||||
|
||||
simpleMat *materials.Material
|
||||
@ -57,7 +61,6 @@ var (
|
||||
type OurGame struct {
|
||||
Win *engine.Window
|
||||
ImGUIInfo nmageimgui.ImguiInfo
|
||||
Quitting bool
|
||||
}
|
||||
|
||||
func (g *OurGame) Init() {
|
||||
@ -102,16 +105,10 @@ func (g *OurGame) Init() {
|
||||
simpleMat.SetUnifVec3("lightColor1", lightColor1)
|
||||
}
|
||||
|
||||
func (g *OurGame) Start() {
|
||||
}
|
||||
|
||||
func (g *OurGame) FrameStart() {
|
||||
}
|
||||
|
||||
func (g *OurGame) Update() {
|
||||
|
||||
if input.IsQuitClicked() {
|
||||
g.Quitting = true
|
||||
engine.Quit()
|
||||
}
|
||||
|
||||
winWidth, winHeight := g.Win.SDLWin.GetSize()
|
||||
@ -167,25 +164,13 @@ func (g *OurGame) Render() {
|
||||
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) ShouldRun() bool {
|
||||
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() {
|
||||
func (g *OurGame) DeInit() {
|
||||
g.Win.Destroy()
|
||||
}
|
||||
|
||||
@ -211,8 +196,7 @@ func main() {
|
||||
ImGUIInfo: nmageimgui.NewImGUI(),
|
||||
}
|
||||
|
||||
engine.Run(game)
|
||||
return
|
||||
engine.Run(game, window, game.ImGUIInfo)
|
||||
}
|
||||
|
||||
func updateViewMat() {
|
||||
|
||||
Reference in New Issue
Block a user