Extended Game interface, added more logging, removed unused test png.

This commit is contained in:
Marvin Blum
2016-05-06 13:50:48 +02:00
parent 2c63ebff2b
commit cd13241329
4 changed files with 56 additions and 10 deletions

39
game.go
View File

@@ -25,13 +25,19 @@ type RunOptions struct {
Width uint32 Width uint32
Height uint32 Height uint32
ClearColor Vec4 ClearColor Vec4
Resizable bool
SetViewportOnResize bool SetViewportOnResize bool
ResizeCallbackFunc ResizeCallback ResizeCallbackFunc ResizeCallback
ExitOnClose bool ExitOnClose bool
} }
// Main game object. // Main game object.
// Setup will be called before the main loop and after GL context has been created.
// Update will be called each frame. This can be used to switch scenes or end game on win state.
// For game logic, System should be used.
type Game interface { type Game interface {
Setup()
Update(float64)
} }
var ( var (
@@ -92,6 +98,10 @@ func Run(game Game, options *RunOptions) {
exitOnClose = options.ExitOnClose exitOnClose = options.ExitOnClose
} }
if options != nil && !options.Resizable {
glfw.WindowHint(glfw.Resizable, glfw.False)
}
wnd, err := glfw.CreateWindow(int(width), int(height), title, nil, nil) wnd, err := glfw.CreateWindow(int(width), int(height), title, nil, nil)
if err != nil { if err != nil {
@@ -106,6 +116,10 @@ func Run(game Game, options *RunOptions) {
SetViewport(0, 0, int32(width), int32(height)) SetViewport(0, 0, int32(width), int32(height))
} }
if activeScene != nil {
activeScene.Resize(width, height)
}
if options != nil && options.ResizeCallbackFunc != nil { if options != nil && options.ResizeCallbackFunc != nil {
options.ResizeCallbackFunc(width, height) options.ResizeCallbackFunc(width, height)
} }
@@ -128,6 +142,8 @@ func Run(game Game, options *RunOptions) {
clearColor = options.ClearColor clearColor = options.ClearColor
} }
game.Setup()
// start and loop // start and loop
log.Print("Starting main loop") log.Print("Starting main loop")
delta := time.Duration(0) delta := time.Duration(0)
@@ -148,8 +164,11 @@ func Run(game Game, options *RunOptions) {
if !math.IsInf(deltaSec, 0) && !math.IsInf(deltaSec, -1) { if !math.IsInf(deltaSec, 0) && !math.IsInf(deltaSec, -1) {
updateSystems(deltaSec) updateSystems(deltaSec)
game.Update(deltaSec)
} }
CheckGLError()
wnd.SwapBuffers() wnd.SwapBuffers()
delta = time.Since(start) delta = time.Since(start)
deltaSec = delta.Seconds() deltaSec = delta.Seconds()
@@ -220,11 +239,13 @@ func initGoga() {
} }
func cleanup() { func cleanup() {
// cleanup scenes // cleanup resources
log.Print("Cleaning up scenes") log.Print("Cleaning up resources")
for _, scene := range scenes { for _, res := range resources {
scene.Cleanup() if drop, ok := res.(Dropable); ok {
drop.Drop()
}
} }
// cleanup systems // cleanup systems
@@ -234,12 +255,10 @@ func cleanup() {
system.Cleanup() system.Cleanup()
} }
// cleanup resources // cleanup scenes
log.Print("Cleaning up resources") log.Print("Cleaning up scenes")
for _, res := range resources { for _, scene := range scenes {
if drop, ok := res.(Dropable); ok { scene.Cleanup()
drop.Drop()
}
} }
} }

10
res.go
View File

@@ -3,6 +3,7 @@ package goga
import ( import (
"errors" "errors"
"io/ioutil" "io/ioutil"
"log"
"path/filepath" "path/filepath"
"strings" "strings"
) )
@@ -44,6 +45,7 @@ func AddLoader(loader ResLoader) bool {
} }
resloader = append(resloader, loader) resloader = append(resloader, loader)
log.Print("Added loader for " + loader.Ext() + " files")
return true return true
} }
@@ -54,6 +56,7 @@ func RemoveLoader(loader ResLoader) bool {
for i, l := range resloader { for i, l := range resloader {
if l == loader { if l == loader {
resloader = append(resloader[:i], resloader[i+1:]...) resloader = append(resloader[:i], resloader[i+1:]...)
log.Print("Removed loader for " + loader.Ext() + " files")
return true return true
} }
} }
@@ -69,6 +72,7 @@ func RemoveLoaderByExt(ext string) bool {
for i, l := range resloader { for i, l := range resloader {
if strings.ToLower(l.Ext()) == ext { if strings.ToLower(l.Ext()) == ext {
resloader = append(resloader[:i], resloader[i+1:]...) resloader = append(resloader[:i], resloader[i+1:]...)
log.Print("Removed loader for " + ext + " files")
return true return true
} }
} }
@@ -79,6 +83,7 @@ func RemoveLoaderByExt(ext string) bool {
// Removes all loaders. // Removes all loaders.
func RemoveAllLoaders() { func RemoveAllLoaders() {
resloader = make([]ResLoader, 0) resloader = make([]ResLoader, 0)
log.Print("Cleared loaders")
} }
// Returns a loader by file extension. // Returns a loader by file extension.
@@ -130,6 +135,7 @@ func LoadRes(path string) (Res, error) {
} }
resources = append(resources, res) resources = append(resources, res)
log.Print("Loaded resource: " + res.GetName())
return res, nil return res, nil
} }
@@ -138,6 +144,7 @@ func LoadRes(path string) (Res, error) {
// If a loader is missing or fails to load the resource, an error will be returned. // If a loader is missing or fails to load the resource, an error will be returned.
// All resources will be kept until an error occures. // All resources will be kept until an error occures.
func LoadResFromFolder(path string) error { func LoadResFromFolder(path string) error {
log.Print("Loading resources from: " + path)
dir, err := ioutil.ReadDir(path) dir, err := ioutil.ReadDir(path)
if err != nil { if err != nil {
@@ -185,6 +192,7 @@ func RemoveResByName(name string) bool {
for i, r := range resources { for i, r := range resources {
if r.GetName() == name { if r.GetName() == name {
resources = append(resources[:i], resources[i+1:]...) resources = append(resources[:i], resources[i+1:]...)
log.Print("Removed resource: " + r.GetName())
return true return true
} }
} }
@@ -198,6 +206,7 @@ func RemoveResByPath(path string) bool {
for i, r := range resources { for i, r := range resources {
if r.GetPath() == path { if r.GetPath() == path {
resources = append(resources[:i], resources[i+1:]...) resources = append(resources[:i], resources[i+1:]...)
log.Print("Removed resource: " + r.GetName())
return true return true
} }
} }
@@ -208,4 +217,5 @@ func RemoveResByPath(path string) bool {
// Removes all resources. // Removes all resources.
func RemoveAllRes() { func RemoveAllRes() {
resources = make([]Res, 0) resources = make([]Res, 0)
log.Print("Cleared resources")
} }

View File

@@ -1,9 +1,16 @@
package goga package goga
import (
"log"
)
// A scene used to switch between game states. // A scene used to switch between game states.
// The Cleanup() method is called when a scene is removed // The Cleanup() method is called when a scene is removed
// or the program is stopped. It can be used to cleanup open resources // or the program is stopped. It can be used to cleanup open resources
// (like GL objects). // (like GL objects).
// On switch, Pause() and Resume() are called.
// The name returned by GetName() must be unique. A scene must only be
// registered once.
type Scene interface { type Scene interface {
Pause() Pause()
Resume() Resume()
@@ -19,6 +26,7 @@ var (
// Adds a scene to game. // Adds a scene to game.
// Returns false if the scene exists already. // Returns false if the scene exists already.
// The first scene added will be set active.
func AddScene(scene Scene) bool { func AddScene(scene Scene) bool {
for _, s := range scenes { for _, s := range scenes {
if s == scene { if s == scene {
@@ -27,6 +35,12 @@ func AddScene(scene Scene) bool {
} }
scenes = append(scenes, scene) scenes = append(scenes, scene)
log.Print("Added scene: " + scene.GetName())
if activeScene == nil {
activeScene = scene
log.Print("Active scene: " + scene.GetName())
}
return true return true
} }
@@ -38,6 +52,7 @@ func RemoveScene(scene Scene) bool {
if s == scene { if s == scene {
s.Cleanup() s.Cleanup()
scenes = append(scenes[:i], scenes[i+1:]...) scenes = append(scenes[:i], scenes[i+1:]...)
log.Print("Removed scene: " + scene.GetName())
return true return true
} }
} }
@@ -52,6 +67,7 @@ func RemoveAllScenes() {
} }
scenes = make([]Scene, 0) scenes = make([]Scene, 0)
log.Print("Cleared scenes")
} }
// Finds and returns a scene by name, or nil if not found. // Finds and returns a scene by name, or nil if not found.
@@ -73,6 +89,7 @@ func SwitchScene(scene Scene) {
} }
activeScene = scene activeScene = scene
log.Print("Active scene: " + scene.GetName())
for _, s := range scenes { for _, s := range scenes {
if s == activeScene { if s == activeScene {

Binary file not shown.

Before

Width:  |  Height:  |  Size: 40 KiB