From a90a36d235c0d58641a33faf53af8894f34765dc Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Mon, 31 Dec 2018 17:10:45 -0800 Subject: [PATCH] add debug logging --- platform/imgui/context.go | 19 ++++++++++++++++++- platform/platform.go | 7 +++++++ platform/scene.go | 12 +++++++++++- platform/window.go | 13 +++++++++---- tools/viewer/main.go | 6 +++++- 5 files changed, 50 insertions(+), 7 deletions(-) diff --git a/platform/imgui/context.go b/platform/imgui/context.go index 05012e1..2cef7b0 100644 --- a/platform/imgui/context.go +++ b/platform/imgui/context.go @@ -1,6 +1,8 @@ package imgui_backend import ( + "log" + imgui "github.com/FooSoft/imgui-go" "github.com/FooSoft/lazarus/math" "github.com/go-gl/gl/v2.1/gl" @@ -18,7 +20,9 @@ type Context struct { func New(displaySize, bufferSize math.Vec2i) (*Context, error) { singleton.refCount++ if singleton.refCount == 1 { + log.Println("imgui global create") singleton.context = imgui.CreateContext(nil) + keys := map[int]int{ imgui.KeyTab: sdl.SCANCODE_TAB, imgui.KeyLeftArrow: sdl.SCANCODE_LEFT, @@ -50,6 +54,7 @@ func New(displaySize, bufferSize math.Vec2i) (*Context, error) { } } + log.Println("imgui context create") c := &Context{displaySize: displaySize, bufferSize: bufferSize} // Build texture atlas @@ -66,7 +71,17 @@ func New(displaySize, bufferSize math.Vec2i) (*Context, error) { gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) gl.PixelStorei(gl.UNPACK_ROW_LENGTH, 0) - gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, int32(image.Width), int32(image.Height), 0, gl.RGBA, gl.UNSIGNED_BYTE, image.Pixels) + gl.TexImage2D( + gl.TEXTURE_2D, + 0, + gl.RGBA, + int32(image.Width), + int32(image.Height), + 0, + gl.RGBA, + gl.UNSIGNED_BYTE, + image.Pixels, + ) // Restore state gl.BindTexture(gl.TEXTURE_2D, uint32(lastTexture)) @@ -90,12 +105,14 @@ func (c *Context) Destroy() error { return nil } + log.Println("imgui context destroy") gl.DeleteTextures(1, &c.fontTexture) imgui.CurrentIO().Fonts().SetTextureID(0) c.fontTexture = 0 singleton.refCount-- if singleton.refCount == 0 { + log.Println("imgui global destroy") singleton.context.Destroy() singleton.context = nil } diff --git a/platform/platform.go b/platform/platform.go index 0c854a3..05728cd 100644 --- a/platform/platform.go +++ b/platform/platform.go @@ -1,6 +1,7 @@ package platform import ( + "log" "runtime" "github.com/FooSoft/lazarus/math" @@ -38,10 +39,16 @@ func NewWindow(title string, size math.Vec2i, scene Scene) (*Window, error) { if !singleton.sdlIsInit { runtime.LockOSThread() + log.Println("sdl global init") if err := sdl.Init(sdl.INIT_VIDEO); err != nil { return nil, err } + sdl.GLSetAttribute(sdl.GL_CONTEXT_MAJOR_VERSION, 2) + sdl.GLSetAttribute(sdl.GL_CONTEXT_MINOR_VERSION, 1) + sdl.GLSetAttribute(sdl.GL_DOUBLEBUFFER, 1) + + log.Println("opengl global init") if err := gl.Init(); err != nil { return nil, err } diff --git a/platform/scene.go b/platform/scene.go index ec71cdf..b580e00 100644 --- a/platform/scene.go +++ b/platform/scene.go @@ -1,6 +1,8 @@ package platform -type Scene interface{} +type Scene interface { + Name() string +} type SceneCreator interface { Create(window *Window) error @@ -13,3 +15,11 @@ type SceneAdvancer interface { type SceneDestroyer interface { Destroy(window *Window) error } + +func sceneName(scene Scene) string { + if scene == nil { + return "" + } else { + return scene.Name() + } +} diff --git a/platform/window.go b/platform/window.go index 59565e2..5aaf41f 100644 --- a/platform/window.go +++ b/platform/window.go @@ -1,6 +1,8 @@ package platform import ( + "log" + "github.com/FooSoft/lazarus/math" "github.com/FooSoft/lazarus/platform/imgui" "github.com/go-gl/gl/v2.1/gl" @@ -15,10 +17,7 @@ type Window struct { } func newWindow(title string, size math.Vec2i, scene Scene) (*Window, error) { - sdl.GLSetAttribute(sdl.GL_CONTEXT_MAJOR_VERSION, 2) - sdl.GLSetAttribute(sdl.GL_CONTEXT_MINOR_VERSION, 1) - sdl.GLSetAttribute(sdl.GL_DOUBLEBUFFER, 1) - + log.Println("window create") sdlWindow, err := sdl.CreateWindow( title, sdl.WINDOWPOS_CENTERED, @@ -63,7 +62,10 @@ func (w *Window) SetScene(scene Scene) error { return nil } + log.Printf("scene transition \"%v\" => \"%v\"\n", sceneName(w.scene), sceneName(scene)) + if sceneDestroyer, ok := w.scene.(SceneDestroyer); ok { + log.Printf("scene notify destroy \"%s\"\n", sceneName(w.scene)) if err := sceneDestroyer.Destroy(w); err != nil { return err } @@ -72,6 +74,7 @@ func (w *Window) SetScene(scene Scene) error { w.scene = scene if sceneCreator, ok := scene.(SceneCreator); ok { + log.Printf("scene notify create \"%s\"\n", sceneName(w.scene)) if err := sceneCreator.Create(w); err != nil { return err } @@ -104,7 +107,9 @@ func (w *Window) Destroy() error { } w.sdlWindow = nil + log.Println("window destroy") removeWindow(w) + return nil } diff --git a/tools/viewer/main.go b/tools/viewer/main.go index 8b9ba78..dab5e16 100644 --- a/tools/viewer/main.go +++ b/tools/viewer/main.go @@ -41,6 +41,10 @@ type scene struct { frameIndex int32 } +func (s *scene) Name() string { + return "Viewer" +} + func (s *scene) Destroy(window *platform.Window) error { return s.texture.Destroy() } @@ -143,7 +147,7 @@ func main() { } scene := &scene{sprite: sprite, palette: palette} - window, err := platform.NewWindow("Viewer", math.Vec2i{X: 1024, Y: 768}, scene) + window, err := platform.NewWindow("viewer", math.Vec2i{X: 1024, Y: 768}, scene) if err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1)