- added imgui
[laserbrain_demo] / src / dbg_gui.cc
diff --git a/src/dbg_gui.cc b/src/dbg_gui.cc
new file mode 100644 (file)
index 0000000..c0c7984
--- /dev/null
@@ -0,0 +1,141 @@
+#include "dbg_gui.h"
+#include "imgui/imgui.h"
+#include "app.h"
+#include "image.h"
+#include "texture.h"
+
+static void render_func(ImDrawData *ddat);
+
+static ImGuiIO *io;
+static Texture *tex;
+
+bool init_debug_gui()
+{
+       io = ImGui::GetIOPtr();
+       io->DisplaySize.x = win_width;
+       io->DisplaySize.y = win_height;
+       io->RenderDrawListsFn = render_func;
+
+       unsigned char *pixels;
+       int width, height;
+       io->Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
+
+       Image img;
+       img.set_pixels(width, height, pixels, Image::FMT_RGBA);
+
+       tex = new Texture;
+       tex->set_image(img);
+       io->Fonts->TexID = (void*)tex;
+
+       return true;
+}
+
+void cleanup_debug_gui()
+{
+       delete tex;
+       tex = 0;
+}
+
+void debug_gui_reshape(int x, int y)
+{
+       io->DisplaySize.x = x;
+       io->DisplaySize.y = y;
+}
+
+bool debug_gui_key(int key, bool press, unsigned int modstate)
+{
+       io->KeysDown[key] = press;
+       io->KeyShift = (modstate & MOD_SHIFT) != 0;
+       io->KeyCtrl = (modstate & MOD_CTRL) != 0;
+       io->KeyAlt = (modstate & MOD_ALT) != 0;
+       io->KeySuper = false;
+
+       return true;    // TODO
+}
+
+bool debug_gui_mbutton(int bn, bool press, int x, int y)
+{
+       io->MouseDown[bn] = press;
+       io->MousePos.x = x;
+       io->MousePos.y = y;
+
+       return true;    // TODO
+}
+
+bool debug_gui_mmotion(int x, int y)
+{
+       io->MousePos.x = x;
+       io->MousePos.y = y;
+
+       return true;    // TODO
+}
+
+static void render_func(ImDrawData *ddat)
+{
+       glViewport(0, 0, win_width, win_height);
+
+       glPushAttrib(GL_ENABLE_BIT);
+       glEnable(GL_BLEND);
+       glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+       glDisable(GL_CULL_FACE);
+       glDisable(GL_DEPTH_TEST);
+       glDisable(GL_LIGHTING);
+       glEnable(GL_SCISSOR_TEST);
+       glEnable(GL_TEXTURE_2D);
+       glUseProgram(0);
+
+       glMatrixMode(GL_PROJECTION);
+       glPushMatrix();
+       glLoadIdentity();
+       glOrtho(0, win_width, win_height, 0.0, -1, 1);
+
+       glMatrixMode(GL_MODELVIEW);
+       glPushMatrix();
+       glLoadIdentity();
+
+       for(int i=0; i<ddat->CmdListsCount; i++) {
+               ImDrawList *cmdlist = ddat->CmdLists[i];
+               ImDrawVert *vbuf = cmdlist->VtxBuffer.Data;
+               ImDrawIdx *ibuf = cmdlist->IdxBuffer.Data;
+
+               for(int j=0; j<cmdlist->CmdBuffer.Size; j++) {
+                       ImDrawCmd *cmd = &cmdlist->CmdBuffer[j];
+                       if(cmd->UserCallback) {
+                               cmd->UserCallback(cmdlist, cmd);
+                       } else {
+
+                               bind_texture((Texture*)cmd->TextureId);
+
+                               glScissor(cmd->ClipRect.x, win_height - cmd->ClipRect.w,
+                                               cmd->ClipRect.z - cmd->ClipRect.x, cmd->ClipRect.w - cmd->ClipRect.y);
+
+                               glBindBuffer(GL_ARRAY_BUFFER, 0);
+                               glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
+
+                               glEnableClientState(GL_VERTEX_ARRAY);
+                               glEnableClientState(GL_TEXTURE_COORD_ARRAY);
+                               glEnableClientState(GL_COLOR_ARRAY);
+
+                               glVertexPointer(2, GL_FLOAT, sizeof *vbuf, &vbuf[0].pos);
+                               glTexCoordPointer(2, GL_FLOAT, sizeof *vbuf, &vbuf[0].uv);
+                               glColorPointer(4, GL_UNSIGNED_BYTE, sizeof *vbuf, &vbuf[0].col);
+
+                               glDrawElements(GL_TRIANGLES, cmd->ElemCount, GL_UNSIGNED_SHORT, ibuf);
+
+                               glDisableClientState(GL_VERTEX_ARRAY);
+                               glDisableClientState(GL_TEXTURE_COORD_ARRAY);
+                               glDisableClientState(GL_COLOR_ARRAY);
+                       }
+                       ibuf += cmd->ElemCount;
+               }
+       }
+
+       glMatrixMode(GL_PROJECTION);
+       glPopMatrix();
+
+       glMatrixMode(GL_MODELVIEW);
+       glPopMatrix();
+
+       glPopAttrib();
+}
+