wrap imgui

This commit is contained in:
Alex Yatskov 2018-12-31 17:30:59 -08:00
parent a90a36d235
commit 0523e41732
8 changed files with 59 additions and 18 deletions

3
graphics/types.go Normal file
View File

@ -0,0 +1,3 @@
package graphics
type Handle uintptr

View File

@ -1,4 +1,4 @@
package imgui_backend package imgui
import ( import (
"log" "log"

34
platform/imgui/imgui.go Normal file
View File

@ -0,0 +1,34 @@
package imgui
import (
imgui "github.com/FooSoft/imgui-go"
"github.com/FooSoft/lazarus/graphics"
"github.com/FooSoft/lazarus/math"
)
func (*Context) DialogBegin(label string) bool {
return imgui.Begin(label)
}
func (*Context) DialogEnd() {
imgui.End()
}
func (*Context) Button(label string) bool {
return imgui.Button(label)
}
func (*Context) Image(texture graphics.Handle, size math.Vec2i) {
imgui.Image(imgui.TextureID(texture), imgui.Vec2{X: float32(size.X), Y: float32(size.Y)})
}
func (*Context) SliderInt(label string, value *int, min, max int) bool {
temp := int32(*value)
result := imgui.SliderInt(label, &temp, int32(min), int32(max))
*value = int(temp)
return result
}
func (*Context) Text(label string) {
imgui.Text(label)
}

View File

@ -1,4 +1,4 @@
package imgui_backend package imgui
import ( import (
"unsafe" "unsafe"

View File

@ -14,8 +14,6 @@ var singleton struct {
windows []*Window windows []*Window
} }
type Handle uintptr
func Advance() (bool, error) { func Advance() (bool, error) {
if err := advanceWindows(); err != nil { if err := advanceWindows(); err != nil {
return false, err return false, err

View File

@ -3,6 +3,7 @@ package platform
import ( import (
"unsafe" "unsafe"
"github.com/FooSoft/lazarus/graphics"
"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"
) )
@ -45,8 +46,8 @@ func newTextureFromRgb(colors []math.Color3b, size math.Vec2i) (*Texture, error)
return &Texture{size, glTexture}, nil return &Texture{size, glTexture}, nil
} }
func (t *Texture) Handle() Handle { func (t *Texture) Handle() graphics.Handle {
return Handle(t.glTexture) return graphics.Handle(t.glTexture)
} }
func (t *Texture) Size() math.Vec2i { func (t *Texture) Size() math.Vec2i {

View File

@ -12,7 +12,7 @@ import (
type Window struct { type Window struct {
sdlWindow *sdl.Window sdlWindow *sdl.Window
sdlGlContext sdl.GLContext sdlGlContext sdl.GLContext
imguiContext *imgui_backend.Context imguiContext *imgui.Context
scene Scene scene Scene
} }
@ -43,7 +43,7 @@ func newWindow(title string, size math.Vec2i, scene Scene) (*Window, error) {
w.makeCurrent() w.makeCurrent()
w.imguiContext, err = imgui_backend.New(w.DisplaySize(), w.BufferSize()) w.imguiContext, err = imgui.New(w.DisplaySize(), w.BufferSize())
if err != nil { if err != nil {
w.Destroy() w.Destroy()
return nil, err return nil, err
@ -114,10 +114,12 @@ func (w *Window) Destroy() error {
} }
func (w *Window) CreateTextureRgba(colors []math.Color4b, size math.Vec2i) (*Texture, error) { func (w *Window) CreateTextureRgba(colors []math.Color4b, size math.Vec2i) (*Texture, error) {
w.makeCurrent()
return newTextureFromRgba(colors, size) return newTextureFromRgba(colors, size)
} }
func (w *Window) CreateTextureRgb(colors []math.Color3b, size math.Vec2i) (*Texture, error) { func (w *Window) CreateTextureRgb(colors []math.Color3b, size math.Vec2i) (*Texture, error) {
w.makeCurrent()
return newTextureFromRgb(colors, size) return newTextureFromRgb(colors, size)
} }
@ -149,6 +151,10 @@ func (w *Window) BufferSize() math.Vec2i {
return math.Vec2i{X: int(width), Y: int(height)} return math.Vec2i{X: int(width), Y: int(height)}
} }
func (w *Window) Imgui() *imgui.Context {
return w.imguiContext
}
func (w *Window) advance() (bool, error) { func (w *Window) advance() (bool, error) {
w.makeCurrent() w.makeCurrent()

View File

@ -7,7 +7,6 @@ import (
"path/filepath" "path/filepath"
"time" "time"
imgui "github.com/FooSoft/imgui-go"
"github.com/FooSoft/lazarus/formats/dat" "github.com/FooSoft/lazarus/formats/dat"
"github.com/FooSoft/lazarus/formats/dc6" "github.com/FooSoft/lazarus/formats/dc6"
"github.com/FooSoft/lazarus/math" "github.com/FooSoft/lazarus/math"
@ -37,8 +36,8 @@ type scene struct {
palette *dat.Palette palette *dat.Palette
texture *platform.Texture texture *platform.Texture
directionIndex int32 directionIndex int
frameIndex int32 frameIndex int
} }
func (s *scene) Name() string { func (s *scene) Name() string {
@ -61,21 +60,22 @@ func (s *scene) Advance(window *platform.Window) error {
} }
} }
imgui.Begin("DC6 Viewer") imgui := window.Imgui()
size := s.texture.Size()
imgui.Image(imgui.TextureID(s.texture.Handle()), imgui.Vec2{X: float32(size.X), Y: float32(size.Y)}) imgui.DialogBegin("DC6 Viewer")
imgui.Image(s.texture.Handle(), s.texture.Size())
direction := s.sprite.Directions[directionIndex] direction := s.sprite.Directions[directionIndex]
if imgui.SliderInt("Direction", &directionIndex, 0, int32(len(s.sprite.Directions))-1) { if imgui.SliderInt("Direction", &directionIndex, 0, len(s.sprite.Directions)-1) {
frameIndex = 0 frameIndex = 0
} }
frame := direction.Frames[frameIndex] frame := direction.Frames[frameIndex]
imgui.SliderInt("Frame", &frameIndex, 0, int32(len(direction.Frames))-1) imgui.SliderInt("Frame", &frameIndex, 0, len(direction.Frames)-1)
imgui.Text(fmt.Sprintf("Size: %+v", frame.Size)) imgui.Text(fmt.Sprintf("Size: %+v", frame.Size))
imgui.Text(fmt.Sprintf("Offset: %+v", frame.Offset)) imgui.Text(fmt.Sprintf("Offset: %+v", frame.Offset))
if imgui.Button("Exit") { if imgui.Button("Exit") {
window.SetScene(nil) window.SetScene(nil)
} }
imgui.DialogEnd()
if directionIndex != s.directionIndex || frameIndex != s.frameIndex { if directionIndex != s.directionIndex || frameIndex != s.frameIndex {
s.directionIndex = directionIndex s.directionIndex = directionIndex
@ -83,7 +83,6 @@ func (s *scene) Advance(window *platform.Window) error {
s.updateTexture(window) s.updateTexture(window)
} }
imgui.End()
return nil return nil
} }