mirror of
https://github.com/Kugelschieber/go-game.git
synced 2026-01-17 22:30:28 +00:00
Extended Game interface, added more logging, removed unused test png.
This commit is contained in:
39
game.go
39
game.go
@@ -25,13 +25,19 @@ type RunOptions struct {
|
||||
Width uint32
|
||||
Height uint32
|
||||
ClearColor Vec4
|
||||
Resizable bool
|
||||
SetViewportOnResize bool
|
||||
ResizeCallbackFunc ResizeCallback
|
||||
ExitOnClose bool
|
||||
}
|
||||
|
||||
// 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 {
|
||||
Setup()
|
||||
Update(float64)
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -92,6 +98,10 @@ func Run(game Game, options *RunOptions) {
|
||||
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)
|
||||
|
||||
if err != nil {
|
||||
@@ -106,6 +116,10 @@ func Run(game Game, options *RunOptions) {
|
||||
SetViewport(0, 0, int32(width), int32(height))
|
||||
}
|
||||
|
||||
if activeScene != nil {
|
||||
activeScene.Resize(width, height)
|
||||
}
|
||||
|
||||
if options != nil && options.ResizeCallbackFunc != nil {
|
||||
options.ResizeCallbackFunc(width, height)
|
||||
}
|
||||
@@ -128,6 +142,8 @@ func Run(game Game, options *RunOptions) {
|
||||
clearColor = options.ClearColor
|
||||
}
|
||||
|
||||
game.Setup()
|
||||
|
||||
// start and loop
|
||||
log.Print("Starting main loop")
|
||||
delta := time.Duration(0)
|
||||
@@ -148,8 +164,11 @@ func Run(game Game, options *RunOptions) {
|
||||
|
||||
if !math.IsInf(deltaSec, 0) && !math.IsInf(deltaSec, -1) {
|
||||
updateSystems(deltaSec)
|
||||
game.Update(deltaSec)
|
||||
}
|
||||
|
||||
CheckGLError()
|
||||
|
||||
wnd.SwapBuffers()
|
||||
delta = time.Since(start)
|
||||
deltaSec = delta.Seconds()
|
||||
@@ -220,11 +239,13 @@ func initGoga() {
|
||||
}
|
||||
|
||||
func cleanup() {
|
||||
// cleanup scenes
|
||||
log.Print("Cleaning up scenes")
|
||||
// cleanup resources
|
||||
log.Print("Cleaning up resources")
|
||||
|
||||
for _, scene := range scenes {
|
||||
scene.Cleanup()
|
||||
for _, res := range resources {
|
||||
if drop, ok := res.(Dropable); ok {
|
||||
drop.Drop()
|
||||
}
|
||||
}
|
||||
|
||||
// cleanup systems
|
||||
@@ -234,12 +255,10 @@ func cleanup() {
|
||||
system.Cleanup()
|
||||
}
|
||||
|
||||
// cleanup resources
|
||||
log.Print("Cleaning up resources")
|
||||
// cleanup scenes
|
||||
log.Print("Cleaning up scenes")
|
||||
|
||||
for _, res := range resources {
|
||||
if drop, ok := res.(Dropable); ok {
|
||||
drop.Drop()
|
||||
}
|
||||
for _, scene := range scenes {
|
||||
scene.Cleanup()
|
||||
}
|
||||
}
|
||||
|
||||
10
res.go
10
res.go
@@ -3,6 +3,7 @@ package goga
|
||||
import (
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
@@ -44,6 +45,7 @@ func AddLoader(loader ResLoader) bool {
|
||||
}
|
||||
|
||||
resloader = append(resloader, loader)
|
||||
log.Print("Added loader for " + loader.Ext() + " files")
|
||||
|
||||
return true
|
||||
}
|
||||
@@ -54,6 +56,7 @@ func RemoveLoader(loader ResLoader) bool {
|
||||
for i, l := range resloader {
|
||||
if l == loader {
|
||||
resloader = append(resloader[:i], resloader[i+1:]...)
|
||||
log.Print("Removed loader for " + loader.Ext() + " files")
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -69,6 +72,7 @@ func RemoveLoaderByExt(ext string) bool {
|
||||
for i, l := range resloader {
|
||||
if strings.ToLower(l.Ext()) == ext {
|
||||
resloader = append(resloader[:i], resloader[i+1:]...)
|
||||
log.Print("Removed loader for " + ext + " files")
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -79,6 +83,7 @@ func RemoveLoaderByExt(ext string) bool {
|
||||
// Removes all loaders.
|
||||
func RemoveAllLoaders() {
|
||||
resloader = make([]ResLoader, 0)
|
||||
log.Print("Cleared loaders")
|
||||
}
|
||||
|
||||
// Returns a loader by file extension.
|
||||
@@ -130,6 +135,7 @@ func LoadRes(path string) (Res, error) {
|
||||
}
|
||||
|
||||
resources = append(resources, res)
|
||||
log.Print("Loaded resource: " + res.GetName())
|
||||
|
||||
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.
|
||||
// All resources will be kept until an error occures.
|
||||
func LoadResFromFolder(path string) error {
|
||||
log.Print("Loading resources from: " + path)
|
||||
dir, err := ioutil.ReadDir(path)
|
||||
|
||||
if err != nil {
|
||||
@@ -185,6 +192,7 @@ func RemoveResByName(name string) bool {
|
||||
for i, r := range resources {
|
||||
if r.GetName() == name {
|
||||
resources = append(resources[:i], resources[i+1:]...)
|
||||
log.Print("Removed resource: " + r.GetName())
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -198,6 +206,7 @@ func RemoveResByPath(path string) bool {
|
||||
for i, r := range resources {
|
||||
if r.GetPath() == path {
|
||||
resources = append(resources[:i], resources[i+1:]...)
|
||||
log.Print("Removed resource: " + r.GetName())
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -208,4 +217,5 @@ func RemoveResByPath(path string) bool {
|
||||
// Removes all resources.
|
||||
func RemoveAllRes() {
|
||||
resources = make([]Res, 0)
|
||||
log.Print("Cleared resources")
|
||||
}
|
||||
|
||||
17
scene.go
17
scene.go
@@ -1,9 +1,16 @@
|
||||
package goga
|
||||
|
||||
import (
|
||||
"log"
|
||||
)
|
||||
|
||||
// A scene used to switch between game states.
|
||||
// The Cleanup() method is called when a scene is removed
|
||||
// or the program is stopped. It can be used to cleanup open resources
|
||||
// (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 {
|
||||
Pause()
|
||||
Resume()
|
||||
@@ -19,6 +26,7 @@ var (
|
||||
|
||||
// Adds a scene to game.
|
||||
// Returns false if the scene exists already.
|
||||
// The first scene added will be set active.
|
||||
func AddScene(scene Scene) bool {
|
||||
for _, s := range scenes {
|
||||
if s == scene {
|
||||
@@ -27,6 +35,12 @@ func AddScene(scene Scene) bool {
|
||||
}
|
||||
|
||||
scenes = append(scenes, scene)
|
||||
log.Print("Added scene: " + scene.GetName())
|
||||
|
||||
if activeScene == nil {
|
||||
activeScene = scene
|
||||
log.Print("Active scene: " + scene.GetName())
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
@@ -38,6 +52,7 @@ func RemoveScene(scene Scene) bool {
|
||||
if s == scene {
|
||||
s.Cleanup()
|
||||
scenes = append(scenes[:i], scenes[i+1:]...)
|
||||
log.Print("Removed scene: " + scene.GetName())
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -52,6 +67,7 @@ func RemoveAllScenes() {
|
||||
}
|
||||
|
||||
scenes = make([]Scene, 0)
|
||||
log.Print("Cleared scenes")
|
||||
}
|
||||
|
||||
// Finds and returns a scene by name, or nil if not found.
|
||||
@@ -73,6 +89,7 @@ func SwitchScene(scene Scene) {
|
||||
}
|
||||
|
||||
activeScene = scene
|
||||
log.Print("Active scene: " + scene.GetName())
|
||||
|
||||
for _, s := range scenes {
|
||||
if s == activeScene {
|
||||
|
||||
BIN
test/gopher.png
BIN
test/gopher.png
Binary file not shown.
|
Before Width: | Height: | Size: 40 KiB |
Reference in New Issue
Block a user