add imgui sample, cleanup imgui
This commit is contained in:
parent
24ba631864
commit
1914d490fe
@ -51,8 +51,8 @@ var keyMapping = map[int]C.int{
|
|||||||
}
|
}
|
||||||
|
|
||||||
var imguiState struct {
|
var imguiState struct {
|
||||||
imguiContext *C.ImGuiContext
|
context *C.ImGuiContext
|
||||||
imguiIo *C.ImGuiIO
|
io *C.ImGuiIO
|
||||||
|
|
||||||
fontTexture uint32
|
fontTexture uint32
|
||||||
displaySize math.Vec2i
|
displaySize math.Vec2i
|
||||||
@ -67,16 +67,17 @@ func Create() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
log.Println("imgui create")
|
log.Println("imgui create")
|
||||||
imguiState.imguiContext = C.igCreateContext(nil)
|
|
||||||
imguiState.imguiIo = C.igGetIO()
|
|
||||||
|
|
||||||
|
imguiState.context = C.igCreateContext(nil)
|
||||||
|
imguiState.io = C.igGetIO()
|
||||||
|
imguiState.io.IniFilename = nil
|
||||||
for imguiKey, nativeKey := range keyMapping {
|
for imguiKey, nativeKey := range keyMapping {
|
||||||
imguiState.imguiIo.KeyMap[imguiKey] = nativeKey
|
imguiState.io.KeyMap[imguiKey] = nativeKey
|
||||||
}
|
}
|
||||||
|
|
||||||
var imageData *C.uchar
|
var imageData *C.uchar
|
||||||
var imageWidth, imageHeight C.int
|
var imageWidth, imageHeight C.int
|
||||||
C.ImFontAtlas_GetTexDataAsRGBA32(imguiState.imguiIo.Fonts, &imageData, &imageWidth, &imageHeight, nil)
|
C.ImFontAtlas_GetTexDataAsRGBA32(imguiState.io.Fonts, &imageData, &imageWidth, &imageHeight, nil)
|
||||||
|
|
||||||
var lastTexture int32
|
var lastTexture int32
|
||||||
gl.GetIntegerv(gl.TEXTURE_BINDING_2D, &lastTexture)
|
gl.GetIntegerv(gl.TEXTURE_BINDING_2D, &lastTexture)
|
||||||
@ -92,7 +93,7 @@ func Create() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func IsCreated() bool {
|
func IsCreated() bool {
|
||||||
return imguiState.imguiContext != nil
|
return imguiState.context != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func Destroy() error {
|
func Destroy() error {
|
||||||
@ -100,14 +101,15 @@ func Destroy() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
log.Println("imgui destroy")
|
||||||
|
|
||||||
gl.DeleteTextures(1, &imguiState.fontTexture)
|
gl.DeleteTextures(1, &imguiState.fontTexture)
|
||||||
imguiState.imguiIo.Fonts.TexID = C.nativeHandleCast(C.uintptr_t(imguiState.fontTexture))
|
imguiState.io.Fonts.TexID = C.nativeHandleCast(C.uintptr_t(imguiState.fontTexture))
|
||||||
imguiState.fontTexture = 0
|
imguiState.fontTexture = 0
|
||||||
|
|
||||||
log.Println("imgui destroy")
|
C.igDestroyContext(imguiState.context)
|
||||||
C.igDestroyContext(imguiState.imguiContext)
|
imguiState.context = nil
|
||||||
imguiState.imguiContext = nil
|
imguiState.io = nil
|
||||||
imguiState.imguiIo = nil
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -120,23 +122,23 @@ func BeginFrame(displaySize, bufferSize math.Vec2i) error {
|
|||||||
imguiState.displaySize = displaySize
|
imguiState.displaySize = displaySize
|
||||||
imguiState.bufferSize = bufferSize
|
imguiState.bufferSize = bufferSize
|
||||||
|
|
||||||
imguiState.imguiIo.Fonts.TexID = C.nativeHandleCast(C.uintptr_t(imguiState.fontTexture))
|
imguiState.io.Fonts.TexID = C.nativeHandleCast(C.uintptr_t(imguiState.fontTexture))
|
||||||
imguiState.imguiIo.DisplaySize.x = C.float(displaySize.X)
|
imguiState.io.DisplaySize.x = C.float(displaySize.X)
|
||||||
imguiState.imguiIo.DisplaySize.y = C.float(displaySize.Y)
|
imguiState.io.DisplaySize.y = C.float(displaySize.Y)
|
||||||
|
|
||||||
currentTime := sdl.GetPerformanceCounter()
|
currentTime := sdl.GetPerformanceCounter()
|
||||||
if imguiState.lastTime > 0 {
|
if imguiState.lastTime > 0 {
|
||||||
imguiState.imguiIo.DeltaTime = C.float(float32(currentTime-imguiState.lastTime) / float32(sdl.GetPerformanceFrequency()))
|
imguiState.io.DeltaTime = C.float(float32(currentTime-imguiState.lastTime) / float32(sdl.GetPerformanceFrequency()))
|
||||||
} else {
|
} else {
|
||||||
imguiState.imguiIo.DeltaTime = C.float(1.0 / 60.0)
|
imguiState.io.DeltaTime = C.float(1.0 / 60.0)
|
||||||
}
|
}
|
||||||
imguiState.lastTime = currentTime
|
imguiState.lastTime = currentTime
|
||||||
|
|
||||||
x, y, state := sdl.GetMouseState()
|
x, y, state := sdl.GetMouseState()
|
||||||
imguiState.imguiIo.MousePos.x = C.float(x)
|
imguiState.io.MousePos.x = C.float(x)
|
||||||
imguiState.imguiIo.MousePos.y = C.float(y)
|
imguiState.io.MousePos.y = C.float(y)
|
||||||
for i, button := range []uint32{sdl.BUTTON_LEFT, sdl.BUTTON_RIGHT, sdl.BUTTON_MIDDLE} {
|
for i, button := range []uint32{sdl.BUTTON_LEFT, sdl.BUTTON_RIGHT, sdl.BUTTON_MIDDLE} {
|
||||||
imguiState.imguiIo.MouseDown[i] = C.bool(imguiState.buttonsDown[i] || (state&sdl.Button(button)) != 0)
|
imguiState.io.MouseDown[i] = C.bool(imguiState.buttonsDown[i] || (state&sdl.Button(button)) != 0)
|
||||||
imguiState.buttonsDown[i] = false
|
imguiState.buttonsDown[i] = false
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -152,8 +154,8 @@ func ProcessEvent(event sdl.Event) (bool, error) {
|
|||||||
switch event.GetType() {
|
switch event.GetType() {
|
||||||
case sdl.MOUSEWHEEL:
|
case sdl.MOUSEWHEEL:
|
||||||
wheelEvent := event.(*sdl.MouseWheelEvent)
|
wheelEvent := event.(*sdl.MouseWheelEvent)
|
||||||
imguiState.imguiIo.MouseWheelH += C.float(wheelEvent.X)
|
imguiState.io.MouseWheelH += C.float(wheelEvent.X)
|
||||||
imguiState.imguiIo.MouseWheel += C.float(wheelEvent.Y)
|
imguiState.io.MouseWheel += C.float(wheelEvent.Y)
|
||||||
return true, nil
|
return true, nil
|
||||||
case sdl.MOUSEBUTTONDOWN:
|
case sdl.MOUSEBUTTONDOWN:
|
||||||
buttonEvent := event.(*sdl.MouseButtonEvent)
|
buttonEvent := event.(*sdl.MouseButtonEvent)
|
||||||
@ -171,22 +173,22 @@ func ProcessEvent(event sdl.Event) (bool, error) {
|
|||||||
return true, nil
|
return true, nil
|
||||||
case sdl.TEXTINPUT:
|
case sdl.TEXTINPUT:
|
||||||
inputEvent := event.(*sdl.TextInputEvent)
|
inputEvent := event.(*sdl.TextInputEvent)
|
||||||
C.ImGuiIO_AddInputCharactersUTF8(imguiState.imguiIo, (*C.char)(unsafe.Pointer(&inputEvent.Text[0])))
|
C.ImGuiIO_AddInputCharactersUTF8(imguiState.io, (*C.char)(unsafe.Pointer(&inputEvent.Text[0])))
|
||||||
return true, nil
|
return true, nil
|
||||||
case sdl.KEYDOWN:
|
case sdl.KEYDOWN:
|
||||||
keyEvent := event.(*sdl.KeyboardEvent)
|
keyEvent := event.(*sdl.KeyboardEvent)
|
||||||
imguiState.imguiIo.KeysDown[keyEvent.Keysym.Scancode] = true
|
imguiState.io.KeysDown[keyEvent.Keysym.Scancode] = true
|
||||||
modState := sdl.GetModState()
|
modState := sdl.GetModState()
|
||||||
imguiState.imguiIo.KeyCtrl = C.bool(modState&sdl.KMOD_CTRL != 0)
|
imguiState.io.KeyCtrl = C.bool(modState&sdl.KMOD_CTRL != 0)
|
||||||
imguiState.imguiIo.KeyAlt = C.bool(modState&sdl.KMOD_ALT != 0)
|
imguiState.io.KeyAlt = C.bool(modState&sdl.KMOD_ALT != 0)
|
||||||
imguiState.imguiIo.KeyShift = C.bool(modState&sdl.KMOD_SHIFT != 0)
|
imguiState.io.KeyShift = C.bool(modState&sdl.KMOD_SHIFT != 0)
|
||||||
case sdl.KEYUP:
|
case sdl.KEYUP:
|
||||||
keyEvent := event.(*sdl.KeyboardEvent)
|
keyEvent := event.(*sdl.KeyboardEvent)
|
||||||
imguiState.imguiIo.KeysDown[keyEvent.Keysym.Scancode] = false
|
imguiState.io.KeysDown[keyEvent.Keysym.Scancode] = false
|
||||||
modState := sdl.GetModState()
|
modState := sdl.GetModState()
|
||||||
imguiState.imguiIo.KeyCtrl = C.bool(modState&sdl.KMOD_CTRL != 0)
|
imguiState.io.KeyCtrl = C.bool(modState&sdl.KMOD_CTRL != 0)
|
||||||
imguiState.imguiIo.KeyAlt = C.bool(modState&sdl.KMOD_ALT != 0)
|
imguiState.io.KeyAlt = C.bool(modState&sdl.KMOD_ALT != 0)
|
||||||
imguiState.imguiIo.KeyShift = C.bool(modState&sdl.KMOD_SHIFT != 0)
|
imguiState.io.KeyShift = C.bool(modState&sdl.KMOD_SHIFT != 0)
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,3 +70,7 @@ func Columns(count int) {
|
|||||||
func NextColumn() {
|
func NextColumn() {
|
||||||
C.igNextColumn()
|
C.igNextColumn()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ShowDemoWindow() {
|
||||||
|
C.igShowDemoWindow(nil)
|
||||||
|
}
|
||||||
|
49
samples/imgui/imgui.go
Normal file
49
samples/imgui/imgui.go
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/FooSoft/lazarus/math"
|
||||||
|
"github.com/FooSoft/lazarus/platform"
|
||||||
|
"github.com/FooSoft/lazarus/platform/imgui"
|
||||||
|
)
|
||||||
|
|
||||||
|
type scene struct{}
|
||||||
|
|
||||||
|
func (s *scene) Name() string {
|
||||||
|
return "imgui"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *scene) Advance() error {
|
||||||
|
imgui.ShowDemoWindow()
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
if err := platform.Initialize(); err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
defer platform.Shutdown()
|
||||||
|
|
||||||
|
if err := platform.WindowCreate("ImGui", math.Vec2i{X: 1024, Y: 768}, new(scene)); err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
defer platform.WindowDestroy()
|
||||||
|
|
||||||
|
for {
|
||||||
|
run, err := platform.Advance()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
if !run {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
<-time.After(time.Millisecond * 25)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user