Added GL initializsation, added clear buffer and clear color options.

This commit is contained in:
Marvin Blum
2016-05-06 12:27:33 +02:00
parent 788399df73
commit c85c5dedb1
5 changed files with 106 additions and 7 deletions

View File

@@ -2,3 +2,4 @@
* cleanup resources * cleanup resources
* more logging * more logging
* limit FPS

88
game.go
View File

@@ -5,6 +5,7 @@ import (
"github.com/go-gl/glfw/v3.1/glfw" "github.com/go-gl/glfw/v3.1/glfw"
"log" "log"
"math" "math"
"runtime"
"time" "time"
) )
@@ -20,6 +21,7 @@ type RunOptions struct {
Title string Title string
Width uint32 Width uint32
Height uint32 Height uint32
ClearColor Vec4
ExitOnClose bool ExitOnClose bool
} }
@@ -29,13 +31,28 @@ type Game interface {
var ( 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. // Creates a new window with given options and starts the game.
// The game struct must implement the Game interface. // The game struct must implement the Game interface.
// If options is nil, the default options will be used. // If options is nil, the default options will be used.
// This function will panic on error. // This function will panic on error.
func Run(game Game, options *RunOptions) { 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 // init glfw
log.Print("Initializing GLFW") log.Print("Initializing GLFW")
@@ -52,9 +69,15 @@ func Run(game Game, options *RunOptions) {
title := default_title title := default_title
exitOnClose := default_exit_on_close exitOnClose := default_exit_on_close
if options != nil { if options != nil && options.Width > 0 {
width = options.Width width = options.Width
}
if options != nil && options.Height > 0 {
height = options.Height height = options.Height
}
if options != nil {
title = options.Title title = options.Title
exitOnClose = options.ExitOnClose exitOnClose = options.ExitOnClose
} }
@@ -65,11 +88,22 @@ func Run(game Game, options *RunOptions) {
panic("Error creating GLFW window: " + err.Error()) panic("Error creating GLFW window: " + err.Error())
} }
wnd.MakeContextCurrent()
// init go-game // init go-game
log.Print("Initializing goga") log.Print("Initializing goga")
wnd.MakeContextCurrent()
initGoga() 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 // start and loop
log.Print("Starting main loop") log.Print("Starting main loop")
delta := time.Duration(0) delta := time.Duration(0)
@@ -82,6 +116,12 @@ func Run(game Game, options *RunOptions) {
} }
start := time.Now() 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) { if !math.IsInf(deltaSec, 0) && !math.IsInf(deltaSec, -1) {
updateSystems(deltaSec) updateSystems(deltaSec)
@@ -90,7 +130,6 @@ func Run(game Game, options *RunOptions) {
delta = time.Since(start) delta = time.Since(start)
deltaSec = delta.Seconds() deltaSec = delta.Seconds()
wnd.SwapBuffers() wnd.SwapBuffers()
glfw.PollEvents()
} }
} }
@@ -100,7 +139,47 @@ func Stop() {
running = false 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() { func initGoga() {
ClearColorBuffer(true)
AddLoader(&PngLoader{gl.LINEAR, false}) AddLoader(&PngLoader{gl.LINEAR, false})
} }
@@ -118,4 +197,7 @@ func cleanup() {
for _, system := range systems { for _, system := range systems {
system.Cleanup() system.Cleanup()
} }
// cleanup resources
log.Print("TODO: cleanup resources")
} }

15
gl_util.go Normal file
View File

@@ -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)
}
}

1
tex.go
View File

@@ -10,6 +10,7 @@ type Tex struct {
name string name string
path string path string
ext string ext string
id uint32 id uint32
target uint32 target uint32
activeTexture uint32 activeTexture uint32