Optimizando memory viewer para que no cree un dump de una linea no visible

This commit is contained in:
2025-06-28 13:14:11 -04:00
parent ab4a43163a
commit d8e3aa16f8
2 changed files with 52 additions and 50 deletions

View File

@@ -24,76 +24,78 @@ void MemoryViewer::render()
ImGui::SliderInt("##memory_view_width", &width, 1, 32, "%d", ImGuiSliderFlags_AlwaysClamp); ImGui::SliderInt("##memory_view_width", &width, 1, 32, "%d", ImGuiSliderFlags_AlwaysClamp);
ImGui::PopItemWidth(); ImGui::PopItemWidth();
auto total_lines = (std::size(this->machine_state->memory) + width - 1) / width;
ImGuiListClipper clipper;
clipper.Begin(total_lines);
auto dump_lines = this->get_memory_dump(); ImGui::BeginChild("##memory_view");
for (auto dump_line : dump_lines) while (clipper.Step())
{ {
ImGui::TextUnformatted(dump_line.c_str()); for (int i = clipper.DisplayStart; i < clipper.DisplayEnd; i++)
{
ImGui::TextUnformatted(get_memory_line(i).c_str());
}
} }
ImGui::EndChild();
} }
ImGui::End(); ImGui::End();
} }
std::vector<std::string> MemoryViewer::get_memory_dump() const std::string MemoryViewer::get_memory_line(int line) const
{ {
auto& memory = this->machine_state->memory; auto& memory = this->machine_state->memory;
const auto size = std::size(memory); const auto size = std::size(memory);
const auto i = line * this->width;
std::vector<std::string> dump_lines; std::stringstream text_builder;
text_builder << "0x";
text_builder << std::hex
<< std::setw(4)
<< std::setfill('0')
<< std::uppercase
<< static_cast<unsigned int>(i)
<< " | ";
for (auto i = 0; i < size; i += this->width) for (auto j = 0; j < this->width; j++)
{ {
std::stringstream text_builder; if (i + j >= size)
text_builder << "0x";
text_builder << std::hex
<< std::setw(4)
<< std::setfill('0')
<< std::uppercase
<< static_cast<unsigned int>(i)
<< " | ";
for (auto j = 0; j < this->width; j++)
{ {
if (i + j >= size) text_builder << " ";
}
else
{
text_builder << std::hex
<< std::setw(2)
<< std::setfill('0')
<< std::uppercase
<< static_cast<unsigned int>(memory[i + j])
<< " ";
}
}
text_builder << "| ";
for (auto j = 0; j < this->width; j++)
{
if (i + j >= size)
{
text_builder << ".";
}
else
{
auto byte = memory[i + j];
if (byte >= 32 && byte <= 126)
{ {
text_builder << " "; text_builder << static_cast<char>(memory[i + j]);
} }
else else
{
text_builder << std::hex
<< std::setw(2)
<< std::setfill('0')
<< std::uppercase
<< static_cast<unsigned int>(memory[i + j])
<< " ";
}
}
text_builder << "| ";
for (auto j = 0; j < this->width; j++)
{
if (i + j >= size)
{ {
text_builder << "."; text_builder << ".";
} }
else
{
auto byte = memory[i + j];
if (byte >= 32 && byte <= 126)
{
text_builder << static_cast<char>(memory[i + j]);
}
else
{
text_builder << ".";
}
}
} }
dump_lines.push_back(text_builder.str());
} }
return dump_lines; return text_builder.str();
} }

View File

@@ -22,7 +22,7 @@ public:
); );
void render(); void render();
std::vector<std::string> get_memory_dump() const; std::string get_memory_line(int line) const;
}; };