glDrawRangeElements in dbg_gui to shut the intel performance warnings up
[laserbrain_demo] / src / dbg_gui.cc
1 #include <ctype.h>
2 #include "dbg_gui.h"
3 #include "imgui/imgui.h"
4 #include "app.h"
5 #include "image.h"
6 #include "texture.h"
7
8 static void render_func(ImDrawData *ddat);
9
10 bool debug_gui, parent_expanded;
11 SceneNode *dbg_sel_node;
12
13 static ImGuiIO *io;
14 static Texture *tex;
15
16 bool init_debug_gui()
17 {
18         io = ImGui::GetIOPtr();
19         io->DisplaySize.x = win_width;
20         io->DisplaySize.y = win_height;
21         io->RenderDrawListsFn = render_func;
22
23         io->KeyMap[ImGuiKey_Tab] = '\t';
24         io->KeyMap[ImGuiKey_LeftArrow] = KEY_LEFT;
25         io->KeyMap[ImGuiKey_RightArrow] = KEY_RIGHT;
26         io->KeyMap[ImGuiKey_UpArrow] = KEY_UP;
27         io->KeyMap[ImGuiKey_DownArrow] = KEY_DOWN;
28         io->KeyMap[ImGuiKey_PageUp] = KEY_PGUP;
29         io->KeyMap[ImGuiKey_PageDown] = KEY_PGDOWN;
30         io->KeyMap[ImGuiKey_Home] = KEY_HOME;
31         io->KeyMap[ImGuiKey_End] = KEY_END;
32         io->KeyMap[ImGuiKey_Delete] = KEY_DEL;
33         io->KeyMap[ImGuiKey_Backspace] = '\b';
34         io->KeyMap[ImGuiKey_Enter] = '\n';
35         io->KeyMap[ImGuiKey_Escape] = 27;
36         io->KeyMap[ImGuiKey_A] = 'a';
37         io->KeyMap[ImGuiKey_C] = 'c';
38         io->KeyMap[ImGuiKey_V] = 'v';
39         io->KeyMap[ImGuiKey_X] = 'x';
40         io->KeyMap[ImGuiKey_Y] = 'y';
41         io->KeyMap[ImGuiKey_Z] = 'z';
42
43         unsigned char *pixels;
44         int width, height;
45         io->Fonts->GetTexDataAsRGBA32(&pixels, &width, &height);
46
47         Image img;
48         img.set_pixels(width, height, pixels, Image::FMT_RGBA);
49
50         tex = new Texture;
51         tex->set_image(img);
52         io->Fonts->TexID = (void*)tex;
53
54         return true;
55 }
56
57 void cleanup_debug_gui()
58 {
59         delete tex;
60         tex = 0;
61 }
62
63 void debug_gui_reshape(int x, int y)
64 {
65         io->DisplaySize.x = x;
66         io->DisplaySize.y = y;
67 }
68
69 void debug_gui_key(int key, bool press, unsigned int modstate)
70 {
71         if(key < 512) {
72                 io->KeysDown[key] = press;
73         }
74         io->KeyShift = (modstate & MOD_SHIFT) != 0;
75         io->KeyCtrl = (modstate & MOD_CTRL) != 0;
76         io->KeyAlt = (modstate & MOD_ALT) != 0;
77         io->KeySuper = false;
78
79         if(press && key < 256 && isprint(key)) {
80                 io->AddInputCharacter(key);
81         }
82 }
83
84 void debug_gui_mbutton(int bn, bool press, int x, int y)
85 {
86         io->MouseDown[bn] = press;
87         io->MousePos.x = x;
88         io->MousePos.y = y;
89 }
90
91 void debug_gui_mmotion(int x, int y)
92 {
93         io->MousePos.x = x;
94         io->MousePos.y = y;
95 }
96
97 void debug_gui_wheel(int dir)
98 {
99         io->MouseWheel = dir;
100 }
101
102 static void render_func(ImDrawData *ddat)
103 {
104         glViewport(0, 0, win_width, win_height);
105
106         glPushAttrib(GL_ENABLE_BIT);
107         glEnable(GL_BLEND);
108         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
109         glDisable(GL_CULL_FACE);
110         glDisable(GL_DEPTH_TEST);
111         glDisable(GL_LIGHTING);
112         glEnable(GL_SCISSOR_TEST);
113         glEnable(GL_TEXTURE_2D);
114         glUseProgram(0);
115
116         glMatrixMode(GL_PROJECTION);
117         glPushMatrix();
118         glLoadIdentity();
119         glOrtho(0, win_width, win_height, 0.0, -1, 1);
120
121         glMatrixMode(GL_MODELVIEW);
122         glPushMatrix();
123         glLoadIdentity();
124
125         for(int i=0; i<ddat->CmdListsCount; i++) {
126                 ImDrawList *cmdlist = ddat->CmdLists[i];
127                 ImDrawVert *vbuf = cmdlist->VtxBuffer.Data;
128                 ImDrawIdx *ibuf = cmdlist->IdxBuffer.Data;
129
130                 for(int j=0; j<cmdlist->CmdBuffer.Size; j++) {
131                         ImDrawCmd *cmd = &cmdlist->CmdBuffer[j];
132                         if(cmd->UserCallback) {
133                                 cmd->UserCallback(cmdlist, cmd);
134                         } else {
135
136                                 bind_texture((Texture*)cmd->TextureId);
137
138                                 glScissor(cmd->ClipRect.x, win_height - cmd->ClipRect.w,
139                                                 cmd->ClipRect.z - cmd->ClipRect.x, cmd->ClipRect.w - cmd->ClipRect.y);
140
141                                 glBindBuffer(GL_ARRAY_BUFFER, 0);
142                                 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
143
144                                 glEnableClientState(GL_VERTEX_ARRAY);
145                                 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
146                                 glEnableClientState(GL_COLOR_ARRAY);
147
148                                 glVertexPointer(2, GL_FLOAT, sizeof *vbuf, &vbuf[0].pos);
149                                 glTexCoordPointer(2, GL_FLOAT, sizeof *vbuf, &vbuf[0].uv);
150                                 glColorPointer(4, GL_UNSIGNED_BYTE, sizeof *vbuf, &vbuf[0].col);
151
152                                 if(glcaps.draw_range) {
153                                         int max_vidx = cmdlist->VtxBuffer.Size - 1;
154                                         glDrawRangeElements(GL_TRIANGLES, 0, max_vidx, cmd->ElemCount, GL_UNSIGNED_SHORT, ibuf);
155                                 } else {
156                                         glDrawElements(GL_TRIANGLES, cmd->ElemCount, GL_UNSIGNED_SHORT, ibuf);
157                                 }
158
159                                 glDisableClientState(GL_VERTEX_ARRAY);
160                                 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
161                                 glDisableClientState(GL_COLOR_ARRAY);
162                         }
163                         ibuf += cmd->ElemCount;
164                 }
165         }
166
167         glMatrixMode(GL_PROJECTION);
168         glPopMatrix();
169
170         glMatrixMode(GL_MODELVIEW);
171         glPopMatrix();
172
173         glPopAttrib();
174 }
175