imgui cleanup

This commit is contained in:
Alex Yatskov 2018-12-31 11:46:12 -08:00
parent 90ce4409ce
commit 6c398bce8e
2 changed files with 23 additions and 15 deletions

View File

@ -21,6 +21,9 @@ var singleton struct {
lastTime uint64 lastTime uint64
fontTexture uint32 fontTexture uint32
context *imgui.Context context *imgui.Context
windowSize math.Vec2i
bufferSize math.Vec2i
} }
func Init() error { func Init() error {
@ -81,11 +84,14 @@ func Shutdown() error {
return nil return nil
} }
func NewFrame(windowSize math.Vec2i) error { func BeginFrame(windowSize, bufferSize math.Vec2i) error {
if !singleton.isInit { if !singleton.isInit {
return ErrWasNotInit return ErrWasNotInit
} }
singleton.windowSize = windowSize
singleton.bufferSize = bufferSize
if singleton.fontTexture == 0 { if singleton.fontTexture == 0 {
singleton.fontTexture = createFontTexture() singleton.fontTexture = createFontTexture()
} }
@ -182,14 +188,16 @@ func ProcessEvent(event sdl.Event) (bool, error) {
// OpenGL2 Render function. // OpenGL2 Render function.
// Note that this implementation is little overcomplicated because we are saving/setting up/restoring every OpenGL singleton explicitly, in order to be able to run within any OpenGL engine that doesn't do so. // Note that this implementation is little overcomplicated because we are saving/setting up/restoring every OpenGL singleton explicitly, in order to be able to run within any OpenGL engine that doesn't do so.
func Render(windowSize, fbSize math.Vec2i, drawData imgui.DrawData) error { func EndFrame() error {
if !singleton.isInit { if !singleton.isInit {
return ErrWasNotInit return ErrWasNotInit
} }
imgui.Render()
drawData := imgui.RenderedDrawData()
drawData.ScaleClipRects(imgui.Vec2{ drawData.ScaleClipRects(imgui.Vec2{
X: float32(fbSize.X) / float32(windowSize.X), X: float32(singleton.bufferSize.X) / float32(singleton.windowSize.X),
Y: float32(fbSize.Y) / float32(windowSize.Y), Y: float32(singleton.bufferSize.Y) / float32(singleton.windowSize.Y),
}) })
// We are using the OpenGL fixed pipeline to make the example code simpler to read! // We are using the OpenGL fixed pipeline to make the example code simpler to read!
@ -221,11 +229,11 @@ func Render(windowSize, fbSize math.Vec2i, drawData imgui.DrawData) error {
// Setup viewport, orthographic projection matrix // Setup viewport, orthographic projection matrix
// Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right). DisplayMin is typically (0,0) for single viewport apps. // Our visible imgui space lies from draw_data->DisplayPos (top left) to draw_data->DisplayPos+data_data->DisplaySize (bottom right). DisplayMin is typically (0,0) for single viewport apps.
gl.Viewport(0, 0, int32(fbSize.X), int32(fbSize.Y)) gl.Viewport(0, 0, int32(singleton.bufferSize.X), int32(singleton.bufferSize.Y))
gl.MatrixMode(gl.PROJECTION) gl.MatrixMode(gl.PROJECTION)
gl.PushMatrix() gl.PushMatrix()
gl.LoadIdentity() gl.LoadIdentity()
gl.Ortho(0, float64(windowSize.X), float64(windowSize.Y), 0, -1, 1) gl.Ortho(0, float64(singleton.windowSize.X), float64(singleton.windowSize.Y), 0, -1, 1)
gl.MatrixMode(gl.MODELVIEW) gl.MatrixMode(gl.MODELVIEW)
gl.PushMatrix() gl.PushMatrix()
gl.LoadIdentity() gl.LoadIdentity()
@ -253,7 +261,7 @@ func Render(windowSize, fbSize math.Vec2i, drawData imgui.DrawData) error {
command.CallUserCallback(commandList) command.CallUserCallback(commandList)
} else { } else {
clipRect := command.ClipRect() clipRect := command.ClipRect()
gl.Scissor(int32(clipRect.X), int32(fbSize.Y)-int32(clipRect.W), int32(clipRect.Z-clipRect.X), int32(clipRect.W-clipRect.Y)) gl.Scissor(int32(clipRect.X), int32(singleton.bufferSize.Y)-int32(clipRect.W), int32(clipRect.Z-clipRect.X), int32(clipRect.W-clipRect.Y))
gl.BindTexture(gl.TEXTURE_2D, uint32(command.TextureID())) gl.BindTexture(gl.TEXTURE_2D, uint32(command.TextureID()))
gl.DrawElements(gl.TRIANGLES, int32(command.ElementCount()), uint32(drawType), unsafe.Pointer(indexBufferOffset)) gl.DrawElements(gl.TRIANGLES, int32(command.ElementCount()), uint32(drawType), unsafe.Pointer(indexBufferOffset))
} }

View File

@ -1,7 +1,6 @@
package platform package platform
import ( import (
imgui "github.com/FooSoft/imgui-go"
"github.com/FooSoft/lazarus/math" "github.com/FooSoft/lazarus/math"
"github.com/FooSoft/lazarus/platform/imgui_backend" "github.com/FooSoft/lazarus/platform/imgui_backend"
"github.com/go-gl/gl/v2.1/gl" "github.com/go-gl/gl/v2.1/gl"
@ -94,22 +93,23 @@ func (w *Window) RenderTexture(texture *Texture, position math.Vec2i) {
} }
func (w *Window) advance() { func (w *Window) advance() {
size := w.displaySize() w.sdlWindow.GLMakeCurrent(w.sdlGlContext)
imgui_backend.NewFrame(size)
gl.Viewport(0, 0, int32(size.X), int32(size.Y)) displaySize := w.displaySize()
bufferSize := w.bufferSize()
imgui_backend.BeginFrame(displaySize, bufferSize)
gl.Viewport(0, 0, int32(displaySize.X), int32(displaySize.Y))
gl.Clear(gl.COLOR_BUFFER_BIT) gl.Clear(gl.COLOR_BUFFER_BIT)
gl.MatrixMode(gl.PROJECTION) gl.MatrixMode(gl.PROJECTION)
gl.LoadIdentity() gl.LoadIdentity()
gl.Ortho(0, float64(size.X), float64(size.Y), 0, -1, 1) gl.Ortho(0, float64(displaySize.X), float64(displaySize.Y), 0, -1, 1)
gl.MatrixMode(gl.MODELVIEW) gl.MatrixMode(gl.MODELVIEW)
gl.LoadIdentity() gl.LoadIdentity()
w.scene.Advance(w) w.scene.Advance(w)
imgui.Render() imgui_backend.EndFrame()
imgui_backend.Render(w.displaySize(), w.bufferSize(), imgui.RenderedDrawData())
w.sdlWindow.GLSwap() w.sdlWindow.GLSwap()
} }