From c85c5dedb11d104e45b74e460ac48b1aa075b218 Mon Sep 17 00:00:00 2001 From: Marvin Blum Date: Fri, 6 May 2016 12:27:33 +0200 Subject: [PATCH] Added GL initializsation, added clear buffer and clear color options. --- ToDo.md | 1 + game.go | 90 +++++++++++++++++++++++++++++++++++++++-- gl_util.go | 15 +++++++ util.go => math_util.go | 0 tex.go | 7 ++-- 5 files changed, 106 insertions(+), 7 deletions(-) create mode 100644 gl_util.go rename util.go => math_util.go (100%) diff --git a/ToDo.md b/ToDo.md index 9db4b59..25311e6 100644 --- a/ToDo.md +++ b/ToDo.md @@ -2,3 +2,4 @@ * cleanup resources * more logging +* limit FPS diff --git a/game.go b/game.go index 7615ec3..e84d78e 100644 --- a/game.go +++ b/game.go @@ -5,6 +5,7 @@ import ( "github.com/go-gl/glfw/v3.1/glfw" "log" "math" + "runtime" "time" ) @@ -20,6 +21,7 @@ type RunOptions struct { Title string Width uint32 Height uint32 + ClearColor Vec4 ExitOnClose bool } @@ -28,14 +30,29 @@ type Game interface { } var ( - running = true + running = true + clearColor = Vec4{} + clearBuffer []uint32 ) +func init() { + // GL functions must be called from main thread. + log.Print("Locking OS thread") + runtime.LockOSThread() +} + // Creates a new window with given options and starts the game. // The game struct must implement the Game interface. // If options is nil, the default options will be used. // This function will panic on error. func Run(game Game, options *RunOptions) { + // init GL + log.Print("Initializing GL") + + if err := gl.Init(); err != nil { + panic("Error initializing GL: " + err.Error()) + } + // init glfw log.Print("Initializing GLFW") @@ -52,9 +69,15 @@ func Run(game Game, options *RunOptions) { title := default_title exitOnClose := default_exit_on_close - if options != nil { + if options != nil && options.Width > 0 { width = options.Width + } + + if options != nil && options.Height > 0 { height = options.Height + } + + if options != nil { title = options.Title exitOnClose = options.ExitOnClose } @@ -65,11 +88,22 @@ func Run(game Game, options *RunOptions) { panic("Error creating GLFW window: " + err.Error()) } + wnd.MakeContextCurrent() + // init go-game log.Print("Initializing goga") - wnd.MakeContextCurrent() initGoga() + if options != nil && options.Width > 0 && options.Height > 0 { + SetViewport(0, 0, int32(options.Width), int32(options.Height)) + } else { + SetViewport(0, 0, int32(default_width), int32(default_height)) + } + + if options != nil { + clearColor = options.ClearColor + } + // start and loop log.Print("Starting main loop") delta := time.Duration(0) @@ -82,6 +116,12 @@ func Run(game Game, options *RunOptions) { } start := time.Now() + glfw.PollEvents() + gl.ClearColor(float32(clearColor.X), float32(clearColor.Y), float32(clearColor.Z), float32(clearColor.W)) + + for _, buffer := range clearBuffer { + gl.Clear(buffer) + } if !math.IsInf(deltaSec, 0) && !math.IsInf(deltaSec, -1) { updateSystems(deltaSec) @@ -90,7 +130,6 @@ func Run(game Game, options *RunOptions) { delta = time.Since(start) deltaSec = delta.Seconds() wnd.SwapBuffers() - glfw.PollEvents() } } @@ -100,7 +139,47 @@ func Stop() { running = false } +// Adds color buffer to list of buffers to be cleared. +// If parameter is false, it will be removed. +func ClearColorBuffer(do bool) { + removeClearBuffer(gl.COLOR_BUFFER_BIT) + + if do { + clearBuffer = append(clearBuffer, gl.COLOR_BUFFER_BIT) + } +} + +// Adds depth buffer to list of buffers to be cleared. +// If parameter is false, it will be removed. +func ClearDepthBuffer(do bool) { + removeClearBuffer(gl.DEPTH_BUFFER_BIT) + + if do { + clearBuffer = append(clearBuffer, gl.DEPTH_BUFFER_BIT) + } +} + +// Sets GL viewport. +func SetViewport(x, y, width, height int32) { + gl.Viewport(x, y, width, height) +} + +// Sets GL clear color. +func SetClearColor(r, g, b, a float64) { + clearColor = Vec4{r, g, b, a} +} + +func removeClearBuffer(buffer uint32) { + for i, buffer := range clearBuffer { + if buffer == buffer { + clearBuffer = append(clearBuffer[:i], clearBuffer[i+1:]...) + return + } + } +} + func initGoga() { + ClearColorBuffer(true) AddLoader(&PngLoader{gl.LINEAR, false}) } @@ -118,4 +197,7 @@ func cleanup() { for _, system := range systems { system.Cleanup() } + + // cleanup resources + log.Print("TODO: cleanup resources") } diff --git a/gl_util.go b/gl_util.go new file mode 100644 index 0000000..86461b6 --- /dev/null +++ b/gl_util.go @@ -0,0 +1,15 @@ +package goga + +import ( + "github.com/go-gl/gl/v4.5-core/gl" + "log" +) + +// Checks for GL errors and prints to log if one occured. +func CheckGLError() { + error := gl.GetError() + + if error != 0 { + log.Print(error) + } +} diff --git a/util.go b/math_util.go similarity index 100% rename from util.go rename to math_util.go diff --git a/tex.go b/tex.go index 4e82f5c..dccefb5 100644 --- a/tex.go +++ b/tex.go @@ -7,9 +7,10 @@ import ( // Texture object. type Tex struct { - name string - path string - ext string + name string + path string + ext string + id uint32 target uint32 activeTexture uint32