- added imgui
[laserbrain_demo] / src / dbg_gui.cc
1 #include "dbg_gui.h"
2 #include "imgui/imgui.h"
3 #include "app.h"
4 #include "image.h"
5 #include "texture.h"
6
7 static void render_func(ImDrawData *ddat);
8
9 static ImGuiIO *io;
10 static Texture *tex;
11
12 bool init_debug_gui()
13 {
14         io = ImGui::GetIOPtr();
15         io->DisplaySize.x = win_width;
16         io->DisplaySize.y = win_height;
17         io->RenderDrawListsFn = render_func;
18
19         unsigned char *pixels;
20         int width, height;
21         io->Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
22
23         Image img;
24         img.set_pixels(width, height, pixels, Image::FMT_RGBA);
25
26         tex = new Texture;
27         tex->set_image(img);
28         io->Fonts->TexID = (void*)tex;
29
30         return true;
31 }
32
33 void cleanup_debug_gui()
34 {
35         delete tex;
36         tex = 0;
37 }
38
39 void debug_gui_reshape(int x, int y)
40 {
41         io->DisplaySize.x = x;
42         io->DisplaySize.y = y;
43 }
44
45 bool debug_gui_key(int key, bool press, unsigned int modstate)
46 {
47         io->KeysDown[key] = press;
48         io->KeyShift = (modstate & MOD_SHIFT) != 0;
49         io->KeyCtrl = (modstate & MOD_CTRL) != 0;
50         io->KeyAlt = (modstate & MOD_ALT) != 0;
51         io->KeySuper = false;
52
53         return true;    // TODO
54 }
55
56 bool debug_gui_mbutton(int bn, bool press, int x, int y)
57 {
58         io->MouseDown[bn] = press;
59         io->MousePos.x = x;
60         io->MousePos.y = y;
61
62         return true;    // TODO
63 }
64
65 bool debug_gui_mmotion(int x, int y)
66 {
67         io->MousePos.x = x;
68         io->MousePos.y = y;
69
70         return true;    // TODO
71 }
72
73 static void render_func(ImDrawData *ddat)
74 {
75         glViewport(0, 0, win_width, win_height);
76
77         glPushAttrib(GL_ENABLE_BIT);
78         glEnable(GL_BLEND);
79         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
80         glDisable(GL_CULL_FACE);
81         glDisable(GL_DEPTH_TEST);
82         glDisable(GL_LIGHTING);
83         glEnable(GL_SCISSOR_TEST);
84         glEnable(GL_TEXTURE_2D);
85         glUseProgram(0);
86
87         glMatrixMode(GL_PROJECTION);
88         glPushMatrix();
89         glLoadIdentity();
90         glOrtho(0, win_width, win_height, 0.0, -1, 1);
91
92         glMatrixMode(GL_MODELVIEW);
93         glPushMatrix();
94         glLoadIdentity();
95
96         for(int i=0; i<ddat->CmdListsCount; i++) {
97                 ImDrawList *cmdlist = ddat->CmdLists[i];
98                 ImDrawVert *vbuf = cmdlist->VtxBuffer.Data;
99                 ImDrawIdx *ibuf = cmdlist->IdxBuffer.Data;
100
101                 for(int j=0; j<cmdlist->CmdBuffer.Size; j++) {
102                         ImDrawCmd *cmd = &cmdlist->CmdBuffer[j];
103                         if(cmd->UserCallback) {
104                                 cmd->UserCallback(cmdlist, cmd);
105                         } else {
106
107                                 bind_texture((Texture*)cmd->TextureId);
108
109                                 glScissor(cmd->ClipRect.x, win_height - cmd->ClipRect.w,
110                                                 cmd->ClipRect.z - cmd->ClipRect.x, cmd->ClipRect.w - cmd->ClipRect.y);
111
112                                 glBindBuffer(GL_ARRAY_BUFFER, 0);
113                                 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
114
115                                 glEnableClientState(GL_VERTEX_ARRAY);
116                                 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
117                                 glEnableClientState(GL_COLOR_ARRAY);
118
119                                 glVertexPointer(2, GL_FLOAT, sizeof *vbuf, &vbuf[0].pos);
120                                 glTexCoordPointer(2, GL_FLOAT, sizeof *vbuf, &vbuf[0].uv);
121                                 glColorPointer(4, GL_UNSIGNED_BYTE, sizeof *vbuf, &vbuf[0].col);
122
123                                 glDrawElements(GL_TRIANGLES, cmd->ElemCount, GL_UNSIGNED_SHORT, ibuf);
124
125                                 glDisableClientState(GL_VERTEX_ARRAY);
126                                 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
127                                 glDisableClientState(GL_COLOR_ARRAY);
128                         }
129                         ibuf += cmd->ElemCount;
130                 }
131         }
132
133         glMatrixMode(GL_PROJECTION);
134         glPopMatrix();
135
136         glMatrixMode(GL_MODELVIEW);
137         glPopMatrix();
138
139         glPopAttrib();
140 }
141