add debug logging

This commit is contained in:
Alex Yatskov 2018-12-31 17:10:45 -08:00
parent 557608155d
commit a90a36d235
5 changed files with 50 additions and 7 deletions

View File

@ -1,6 +1,8 @@
package imgui_backend package imgui_backend
import ( import (
"log"
imgui "github.com/FooSoft/imgui-go" imgui "github.com/FooSoft/imgui-go"
"github.com/FooSoft/lazarus/math" "github.com/FooSoft/lazarus/math"
"github.com/go-gl/gl/v2.1/gl" "github.com/go-gl/gl/v2.1/gl"
@ -18,7 +20,9 @@ type Context struct {
func New(displaySize, bufferSize math.Vec2i) (*Context, error) { func New(displaySize, bufferSize math.Vec2i) (*Context, error) {
singleton.refCount++ singleton.refCount++
if singleton.refCount == 1 { if singleton.refCount == 1 {
log.Println("imgui global create")
singleton.context = imgui.CreateContext(nil) singleton.context = imgui.CreateContext(nil)
keys := map[int]int{ keys := map[int]int{
imgui.KeyTab: sdl.SCANCODE_TAB, imgui.KeyTab: sdl.SCANCODE_TAB,
imgui.KeyLeftArrow: sdl.SCANCODE_LEFT, 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} c := &Context{displaySize: displaySize, bufferSize: bufferSize}
// Build texture atlas // 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_MIN_FILTER, gl.LINEAR)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
gl.PixelStorei(gl.UNPACK_ROW_LENGTH, 0) 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 // Restore state
gl.BindTexture(gl.TEXTURE_2D, uint32(lastTexture)) gl.BindTexture(gl.TEXTURE_2D, uint32(lastTexture))
@ -90,12 +105,14 @@ func (c *Context) Destroy() error {
return nil return nil
} }
log.Println("imgui context destroy")
gl.DeleteTextures(1, &c.fontTexture) gl.DeleteTextures(1, &c.fontTexture)
imgui.CurrentIO().Fonts().SetTextureID(0) imgui.CurrentIO().Fonts().SetTextureID(0)
c.fontTexture = 0 c.fontTexture = 0
singleton.refCount-- singleton.refCount--
if singleton.refCount == 0 { if singleton.refCount == 0 {
log.Println("imgui global destroy")
singleton.context.Destroy() singleton.context.Destroy()
singleton.context = nil singleton.context = nil
} }

View File

@ -1,6 +1,7 @@
package platform package platform
import ( import (
"log"
"runtime" "runtime"
"github.com/FooSoft/lazarus/math" "github.com/FooSoft/lazarus/math"
@ -38,10 +39,16 @@ func NewWindow(title string, size math.Vec2i, scene Scene) (*Window, error) {
if !singleton.sdlIsInit { if !singleton.sdlIsInit {
runtime.LockOSThread() runtime.LockOSThread()
log.Println("sdl global init")
if err := sdl.Init(sdl.INIT_VIDEO); err != nil { if err := sdl.Init(sdl.INIT_VIDEO); err != nil {
return nil, err 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 { if err := gl.Init(); err != nil {
return nil, err return nil, err
} }

View File

@ -1,6 +1,8 @@
package platform package platform
type Scene interface{} type Scene interface {
Name() string
}
type SceneCreator interface { type SceneCreator interface {
Create(window *Window) error Create(window *Window) error
@ -13,3 +15,11 @@ type SceneAdvancer interface {
type SceneDestroyer interface { type SceneDestroyer interface {
Destroy(window *Window) error Destroy(window *Window) error
} }
func sceneName(scene Scene) string {
if scene == nil {
return "<nil>"
} else {
return scene.Name()
}
}

View File

@ -1,6 +1,8 @@
package platform package platform
import ( import (
"log"
"github.com/FooSoft/lazarus/math" "github.com/FooSoft/lazarus/math"
"github.com/FooSoft/lazarus/platform/imgui" "github.com/FooSoft/lazarus/platform/imgui"
"github.com/go-gl/gl/v2.1/gl" "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) { func newWindow(title string, size math.Vec2i, scene Scene) (*Window, error) {
sdl.GLSetAttribute(sdl.GL_CONTEXT_MAJOR_VERSION, 2) log.Println("window create")
sdl.GLSetAttribute(sdl.GL_CONTEXT_MINOR_VERSION, 1)
sdl.GLSetAttribute(sdl.GL_DOUBLEBUFFER, 1)
sdlWindow, err := sdl.CreateWindow( sdlWindow, err := sdl.CreateWindow(
title, title,
sdl.WINDOWPOS_CENTERED, sdl.WINDOWPOS_CENTERED,
@ -63,7 +62,10 @@ func (w *Window) SetScene(scene Scene) error {
return nil return nil
} }
log.Printf("scene transition \"%v\" => \"%v\"\n", sceneName(w.scene), sceneName(scene))
if sceneDestroyer, ok := w.scene.(SceneDestroyer); ok { if sceneDestroyer, ok := w.scene.(SceneDestroyer); ok {
log.Printf("scene notify destroy \"%s\"\n", sceneName(w.scene))
if err := sceneDestroyer.Destroy(w); err != nil { if err := sceneDestroyer.Destroy(w); err != nil {
return err return err
} }
@ -72,6 +74,7 @@ func (w *Window) SetScene(scene Scene) error {
w.scene = scene w.scene = scene
if sceneCreator, ok := scene.(SceneCreator); ok { if sceneCreator, ok := scene.(SceneCreator); ok {
log.Printf("scene notify create \"%s\"\n", sceneName(w.scene))
if err := sceneCreator.Create(w); err != nil { if err := sceneCreator.Create(w); err != nil {
return err return err
} }
@ -104,7 +107,9 @@ func (w *Window) Destroy() error {
} }
w.sdlWindow = nil w.sdlWindow = nil
log.Println("window destroy")
removeWindow(w) removeWindow(w)
return nil return nil
} }

View File

@ -41,6 +41,10 @@ type scene struct {
frameIndex int32 frameIndex int32
} }
func (s *scene) Name() string {
return "Viewer"
}
func (s *scene) Destroy(window *platform.Window) error { func (s *scene) Destroy(window *platform.Window) error {
return s.texture.Destroy() return s.texture.Destroy()
} }
@ -143,7 +147,7 @@ func main() {
} }
scene := &scene{sprite: sprite, palette: palette} 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 { if err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
os.Exit(1) os.Exit(1)