Added cleaning up GL resources, added resize callback.

This commit is contained in:
Marvin Blum
2016-05-06 13:07:41 +02:00
parent c85c5dedb1
commit 2c63ebff2b
2 changed files with 55 additions and 13 deletions

View File

@@ -1,5 +1,5 @@
# ToDo # ToDo
* cleanup resources * ~~cleanup resources~~
* more logging * more logging
* limit FPS * limit FPS

50
game.go
View File

@@ -16,12 +16,17 @@ const (
default_exit_on_close = true default_exit_on_close = true
) )
// If set in RunOptions, the function will be called on window resize.
type ResizeCallback func(width, height int)
// Run options allow to set some parameters on startup. // Run options allow to set some parameters on startup.
type RunOptions struct { type RunOptions struct {
Title string Title string
Width uint32 Width uint32
Height uint32 Height uint32
ClearColor Vec4 ClearColor Vec4
SetViewportOnResize bool
ResizeCallbackFunc ResizeCallback
ExitOnClose bool ExitOnClose bool
} }
@@ -33,6 +38,8 @@ var (
running = true running = true
clearColor = Vec4{} clearColor = Vec4{}
clearBuffer []uint32 clearBuffer []uint32
viewportWidth int
viewportHeight int
) )
func init() { func init() {
@@ -77,8 +84,11 @@ func Run(game Game, options *RunOptions) {
height = options.Height height = options.Height
} }
if options != nil { if options != nil && len(options.Title) > 0 {
title = options.Title title = options.Title
}
if options != nil {
exitOnClose = options.ExitOnClose exitOnClose = options.ExitOnClose
} }
@@ -88,6 +98,20 @@ func Run(game Game, options *RunOptions) {
panic("Error creating GLFW window: " + err.Error()) panic("Error creating GLFW window: " + err.Error())
} }
// window event handlers
wnd.SetSizeCallback(func(w *glfw.Window, width, height int) {
if options == nil {
SetViewport(0, 0, int32(width), int32(height))
} else if options != nil && options.SetViewportOnResize {
SetViewport(0, 0, int32(width), int32(height))
}
if options != nil && options.ResizeCallbackFunc != nil {
options.ResizeCallbackFunc(width, height)
}
})
// make GL context current
wnd.MakeContextCurrent() wnd.MakeContextCurrent()
// init go-game // init go-game
@@ -116,7 +140,6 @@ 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)) gl.ClearColor(float32(clearColor.X), float32(clearColor.Y), float32(clearColor.Z), float32(clearColor.W))
for _, buffer := range clearBuffer { for _, buffer := range clearBuffer {
@@ -127,9 +150,10 @@ func Run(game Game, options *RunOptions) {
updateSystems(deltaSec) updateSystems(deltaSec)
} }
wnd.SwapBuffers()
delta = time.Since(start) delta = time.Since(start)
deltaSec = delta.Seconds() deltaSec = delta.Seconds()
wnd.SwapBuffers() glfw.PollEvents()
} }
} }
@@ -161,6 +185,8 @@ func ClearDepthBuffer(do bool) {
// Sets GL viewport. // Sets GL viewport.
func SetViewport(x, y, width, height int32) { func SetViewport(x, y, width, height int32) {
viewportWidth = int(width)
viewportHeight = int(height)
gl.Viewport(x, y, width, height) gl.Viewport(x, y, width, height)
} }
@@ -169,6 +195,16 @@ func SetClearColor(r, g, b, a float64) {
clearColor = Vec4{r, g, b, a} clearColor = Vec4{r, g, b, a}
} }
// Returns width of viewport.
func GetWidth() int {
return viewportWidth
}
// Returns height of viewport.
func GetHeight() int {
return viewportHeight
}
func removeClearBuffer(buffer uint32) { func removeClearBuffer(buffer uint32) {
for i, buffer := range clearBuffer { for i, buffer := range clearBuffer {
if buffer == buffer { if buffer == buffer {
@@ -199,5 +235,11 @@ func cleanup() {
} }
// cleanup resources // cleanup resources
log.Print("TODO: cleanup resources") log.Print("Cleaning up resources")
for _, res := range resources {
if drop, ok := res.(Dropable); ok {
drop.Drop()
}
}
} }