diff --git a/platform/platform.go b/platform/platform.go index 9feadc4..2d67896 100644 --- a/platform/platform.go +++ b/platform/platform.go @@ -20,7 +20,7 @@ var state struct { windows []*Window } -type Handle uint32 +type Handle uintptr type Scene interface { Init(window *Window) error diff --git a/platform/texture.go b/platform/texture.go index 87be23a..3d7a7f8 100644 --- a/platform/texture.go +++ b/platform/texture.go @@ -26,7 +26,7 @@ func newTextureFromRgba(colors []color.RGBA, width, height int) (*Texture, error gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, int32(width), int32(height), 0, gl.RGBA, gl.UNSIGNED_BYTE, unsafe.Pointer(&colors[0])) gl.BindTexture(gl.TEXTURE_2D, uint32(glHandleLast)) - return &Texture{math.Vec2i{width, height}, glHandle}, nil + return &Texture{size: math.Vec2i{X: width, Y: height}, glHandle: glHandle}, nil } func (t *Texture) Handle() Handle { diff --git a/tools/viewer/main.go b/tools/viewer/main.go index 011475b..1c01883 100644 --- a/tools/viewer/main.go +++ b/tools/viewer/main.go @@ -11,7 +11,6 @@ import ( imgui "github.com/FooSoft/imgui-go" "github.com/FooSoft/lazarus/formats/dat" "github.com/FooSoft/lazarus/formats/dc6" - "github.com/FooSoft/lazarus/math" "github.com/FooSoft/lazarus/platform" ) @@ -60,36 +59,54 @@ func (s *scene) Advance(window *platform.Window) error { frameIndex = s.frameIndex ) + if s.texture == nil { + if err := s.updateTexture(window); err != nil { + return err + } + } + + imgui.Begin("DC6 Viewer") + size := s.texture.Size() + imgui.Image(imgui.TextureID(s.texture.Handle()), imgui.Vec2{X: float32(size.X), Y: float32(size.Y)}) direction := s.sprite.Directions[directionIndex] if imgui.SliderInt("Direction", &directionIndex, 0, int32(len(s.sprite.Directions))-1) { frameIndex = 0 } frame := direction.Frames[frameIndex] imgui.SliderInt("Frame", &frameIndex, 0, int32(len(direction.Frames))-1) - imgui.Text(fmt.Sprintf("Height: %d", frame.Height)) - imgui.Text(fmt.Sprintf("Width: %d", frame.Width)) - imgui.Text(fmt.Sprintf("OffsetX: %d", frame.OffsetX)) - imgui.Text(fmt.Sprintf("OffsetY: %d", frame.OffsetY)) + imgui.Text(fmt.Sprintf("Size: %dx%d", frame.Width, frame.Height)) + imgui.Text(fmt.Sprintf("Offset: %dx%d", frame.OffsetX, frame.OffsetY)) - if s.texture == nil || directionIndex != s.directionIndex || frameIndex != s.frameIndex { - colors := make([]color.RGBA, frame.Width*frame.Height) - for y := 0; y < frame.Height; y++ { - for x := 0; x < frame.Width; x++ { - colors[y*frame.Width+x] = s.palette.Colors[frame.Data[y*frame.Width+x]] - } + if directionIndex != s.directionIndex || frameIndex != s.frameIndex { + s.directionIndex = directionIndex + s.frameIndex = frameIndex + s.updateTexture(window) + } + + imgui.End() + return nil +} + +func (s *scene) updateTexture(window *platform.Window) error { + frame := s.sprite.Directions[s.directionIndex].Frames[s.frameIndex] + colors := make([]color.RGBA, frame.Width*frame.Height) + for y := 0; y < frame.Height; y++ { + for x := 0; x < frame.Width; x++ { + colors[y*frame.Width+x] = s.palette.Colors[frame.Data[y*frame.Width+x]] } + } - var err error - s.texture, err = window.CreateTextureRgba(colors, frame.Width, frame.Height) - if err != nil { + if s.texture != nil { + if err := s.texture.Destroy(); err != nil { return err } } - window.RenderTexture(s.texture, math.Vec2i{X: 0, Y: 0}) - - s.directionIndex = directionIndex - s.frameIndex = frameIndex + var err error + s.texture, err = window.CreateTextureRgba(colors, frame.Width, frame.Height) + if err != nil { + return err + } return nil }