-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdebug.cpp
260 lines (227 loc) · 7.09 KB
/
debug.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
* Audio Overload SDK
*
* Debug GUI
*
* Author: Nmlgc
*/
#include <imgui.h>
#include "imgui/examples/opengl_example/imgui_impl_glfw.h"
#include <stdio.h>
#include <GLFW/glfw3.h>
#include "ao.h"
#include "corlett.h"
#include "debug.h"
GLFWwindow *window;
bool show_test_window = true;
bool show_another_window = false;
ImVec4 clear_color = ImColor(114, 144, 154);
/// Widgets
/// -------
void debug_memory(const char *view_id, DebugMemoryState *state, uint8 *mem_buf, int32 mem_size)
{
const int ROWS_MAX = 64;
IM_ASSERT(view_id);
IM_ASSERT(state);
auto& io = ImGui::GetIO();
auto& style = ImGui::GetStyle();
int byte_width = (int)ImGui::CalcTextSize("00").x;
auto& x_spacing = style.ItemSpacing.x;
auto item_spacing_half = ImVec2(style.ItemSpacing.x / 2, style.ItemSpacing.y);
enum ByteColors : uint8 {
Normal,
Control,
MAX
};
const ImVec4 BYTE_COLORS[MAX] {
style.Colors[ImGuiCol_Text],
style.Colors[ImGuiCol_TextDisabled],
};
ImGui::PushID(state);
ImGui::PushItemWidth(ROWS_MAX * 2);
ImGui::DragIntClamp("##rows", &state->rows, 0.2f, 1, ROWS_MAX, "%.0f bytes per line");
if(state->offset > state->rows - 1) {
state->offset = state->rows - 1;
}
if(state->rows > 1) {
ImGui::SameLine();
ImGui::DragIntClamp("##offset", &state->offset, 0.2f, 0, state->rows - 1, "Offset: %.0f");
}
ImGui::PopItemWidth();
ImGui::BeginChild(view_id);
auto mem_lines = (mem_size + state->offset + (state->rows - 1)) / state->rows;
ImGuiListClipper clipper(mem_lines, ImGui::GetTextLineHeightWithSpacing());
int32 byte = -state->offset + (clipper.DisplayStart * state->rows);
auto line = clipper.DisplayStart;
float vline_x[2];
while(line < clipper.DisplayEnd && byte < mem_size) {
ByteColors ascii_colors[ROWS_MAX + 1];
char ascii[ROWS_MAX + 1];
ImGui::Text("%08x", byte < 0 ? 0 : byte);
ImGui::SameLine();
vline_x[0] = ImGui::GetCursorScreenPos().x;
ImGui::SetCursorPosX(ImGui::GetCursorPosX() + x_spacing);
for(int column = 0; column < state->rows; byte++, column++) {
ascii_colors[column] = Normal;
if(byte < 0 || byte >= mem_size) {
ImGui::SetCursorPosX(ImGui::GetCursorPosX() + byte_width + x_spacing);
ascii[column] = ' ';
} else {
auto b = mem_buf[byte];
if(b == 0) {
ascii_colors[column] = Control;
ascii[column] = '.';
} else if(b < ' ') {
ascii_colors[column] = Control;
ascii[column] = '_';
} else if(b > 127) {
ascii_colors[column] = Control;
ascii[column] = '?';
} else {
ascii[column] = b;
}
ImGui::Text("%02x", b);
ImGui::SameLine();
}
}
vline_x[1] = ImGui::GetCursorScreenPos().x;
ImGui::SetCursorPosX(ImGui::GetCursorPosX() + x_spacing);
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, item_spacing_half);
for(int column = 0; column < state->rows; column++) {
auto color = BYTE_COLORS[ascii_colors[column]];
ImGui::PushStyleColor(ImGuiCol_Text, color);
ImGui::TextUnformatted(&ascii[column], &ascii[column + 1]);
if(column != state->rows - 1) {
ImGui::SameLine();
}
ImGui::PopStyleColor();
}
ImGui::PopStyleVar();
line++;
}
clipper.End();
auto vline_y_end = ImGui::GetCursorScreenPos().y;
for(int i = 0; i < countof(vline_x); i++) {
ImGui::GetWindowDrawList()->AddLine(
ImVec2(vline_x[i], 0.0f), ImVec2(vline_x[i], vline_y_end),
ImColor(style.Colors[ImGuiCol_Column])
);
}
ImGui::EndChild();
ImGui::PopID();
}
/// -------
static void error_callback(int error, const char* description)
{
fprintf(stderr, "Error %d: %s\n", error, description);
}
ao_bool debug_start(void)
{
// Setup window
glfwSetErrorCallback(error_callback);
if(!glfwInit()) {
return false;
}
window = glfwCreateWindow(1280, 720, "ImGui OpenGL2 example", NULL, NULL);
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
// Setup ImGui binding
ImGui_ImplGlfw_Init(window, true);
ImGuiStyle& style = ImGui::GetStyle();
style.WindowRounding = 0.0f;
return true;
}
ao_bool debug_frame(debug_hw_t *hw)
{
auto& io = ImGui::GetIO();
auto& style = ImGui::GetStyle();
glfwPollEvents();
ImGui_ImplGlfw_NewFrame();
// 1. Show a simple window
// Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets appears in a window automatically called "Debug"
{
static float f = 0.0f;
ImGui::Text("Hello, world!");
ImGui::SliderFloat("float", &f, 0.0f, 1.0f);
ImGui::ColorEdit3("clear color", (float*)&clear_color);
if(ImGui::Button("Test Window")) {
show_test_window ^= 1;
}
if(ImGui::Button("Another Window")) {
show_another_window ^= 1;
}
}
// 2. Show another simple window, this time using an explicit Begin/End pair
if(show_another_window) {
ImGui::SetNextWindowSize(ImVec2(200,100), ImGuiSetCond_FirstUseEver);
ImGui::Begin("Another Window", &show_another_window);
ImGui::Text("Hello");
ImGui::End();
}
// 3. Show the ImGui test window. Most of the sample code is in ImGui::ShowTestWindow()
if(show_test_window) {
ImGui::SetNextWindowPos(ImVec2(650, 20), ImGuiSetCond_FirstUseEver);
ImGui::ShowTestWindow(&show_test_window);
}
if(hw) {
hw();
}
// Status bar
auto status_bar_flags =
ImGuiWindowFlags_NoTitleBar
| ImGuiWindowFlags_NoResize
| ImGuiWindowFlags_NoMove
| ImGuiWindowFlags_NoScrollbar
| ImGuiWindowFlags_NoScrollWithMouse
| ImGuiWindowFlags_NoCollapse
| ImGuiWindowFlags_NoSavedSettings
| ImGuiWindowFlags_NoInputs
| ImGuiWindowFlags_NoFocusOnAppearing;
auto status_bar_pos = ImVec2(0.0f, io.DisplaySize.y - ImGui::GetWindowContentRegionMin().y);
auto progress = (double)corlett_sample_count() / (double)corlett_sample_total();
auto progress_x = progress * io.DisplaySize.x;
// ... Well, creating a second window just for the progress bar seems
// to be the cleanest way to work around the window padding. -.-
ImGui::PushStyleVar(ImGuiStyleVar_WindowPadding, ImVec2(0.0f, 0.0f));
ImGui::SetNextWindowPos(ImVec2(0, status_bar_pos.y));
ImGui::SetNextWindowSize(ImVec2(io.DisplaySize.x, 0.0f));
ImGui::Begin("Progress Bar", NULL, status_bar_flags);
ImGui::GetWindowDrawList()->AddRectFilled(
status_bar_pos,
ImVec2(progress_x, status_bar_pos.y + ImGui::GetWindowSize().y),
ImColor(style.Colors[ImGuiCol_TitleBg])
);
ImGui::End();
ImGui::PopStyleVar();
char fps[32];
sprintf(fps, "%.1f FPS", io.Framerate);
ImGui::PushStyleColor(ImGuiCol_WindowBg, ImVec4());
ImGui::SetNextWindowPos(status_bar_pos);
ImGui::SetNextWindowSize(ImVec2(io.DisplaySize.x, 0.0f));
ImGui::Begin("Status Bar", NULL, status_bar_flags);
ImGui::Columns(2);
ImGui::SetColumnOffset(1, ImGui::GetWindowContentRegionWidth() - ImGui::CalcTextSize(fps).x);
ImGui::NextColumn();
ImGui::PushItemWidth(-1);
ImGui::TextUnformatted(fps);
ImGui::PopItemWidth();
ImGui::Columns(1);
ImGui::End();
ImGui::PopStyleColor();
// Rendering
int display_w, display_h;
glfwGetFramebufferSize(window, &display_w, &display_h);
glViewport(0, 0, display_w, display_h);
glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
glClear(GL_COLOR_BUFFER_BIT);
ImGui::Render();
glfwSwapBuffers(window);
return glfwWindowShouldClose(window);
}
ao_bool debug_stop(void)
{
// Cleanup
ImGui_ImplGlfw_Shutdown();
glfwTerminate();
return true;
}