UI Para visor de memoria
This commit is contained in:
@@ -22,6 +22,8 @@ target_sources(
|
||||
UI/ControlPanel.h
|
||||
UI/Display.cpp
|
||||
UI/Display.h
|
||||
UI/MemoryViewer.cpp
|
||||
UI/MemoryViewer.h
|
||||
UI/RomInfo.cpp
|
||||
UI/RomInfo.h
|
||||
UI/UIManager.cpp
|
||||
|
||||
99
src/UI/MemoryViewer.cpp
Normal file
99
src/UI/MemoryViewer.cpp
Normal file
@@ -0,0 +1,99 @@
|
||||
#include "MemoryViewer.h"
|
||||
|
||||
#include <iomanip>
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
|
||||
#include "imgui.h"
|
||||
|
||||
MemoryViewer::MemoryViewer(std::shared_ptr<Graphics> graphics,
|
||||
std::shared_ptr<MachineState> machine_state,
|
||||
std::shared_ptr<CallbackManager> callback_manager
|
||||
) : graphics{std::move(graphics)},
|
||||
machine_state{std::move(machine_state)},
|
||||
callback_manager{std::move(callback_manager)},
|
||||
width{16}
|
||||
{
|
||||
}
|
||||
|
||||
void MemoryViewer::render()
|
||||
{
|
||||
if (ImGui::Begin("CHIP-8 - CPU Memory"))
|
||||
{
|
||||
ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x);
|
||||
ImGui::SliderInt("##memory_view_width", &width, 1, 32, "%d", ImGuiSliderFlags_AlwaysClamp);
|
||||
ImGui::PopItemWidth();
|
||||
|
||||
|
||||
auto dump_lines = this->get_memory_dump();
|
||||
for (auto dump_line : dump_lines)
|
||||
{
|
||||
ImGui::TextUnformatted(dump_line.c_str());
|
||||
}
|
||||
}
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
std::vector<std::string> MemoryViewer::get_memory_dump() const
|
||||
{
|
||||
auto& memory = this->machine_state->memory;
|
||||
const auto size = std::size(memory);
|
||||
|
||||
std::vector<std::string> dump_lines;
|
||||
|
||||
for (auto i = 0; i < size; i += this->width)
|
||||
{
|
||||
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 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 << static_cast<char>(memory[i + j]);
|
||||
}
|
||||
else
|
||||
{
|
||||
text_builder << ".";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
dump_lines.push_back(text_builder.str());
|
||||
}
|
||||
|
||||
return dump_lines;
|
||||
}
|
||||
29
src/UI/MemoryViewer.h
Normal file
29
src/UI/MemoryViewer.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#ifndef MEMORYVIEWER_H
|
||||
#define MEMORYVIEWER_H
|
||||
#include <memory>
|
||||
|
||||
#include "CallbackManager.h"
|
||||
#include "../Graphics/Graphics.h"
|
||||
#include "../Interpreter/MachineState.h"
|
||||
|
||||
class MemoryViewer
|
||||
{
|
||||
std::shared_ptr<Graphics> graphics;
|
||||
std::shared_ptr<MachineState> machine_state;
|
||||
std::shared_ptr<CallbackManager> callback_manager;
|
||||
|
||||
int width;
|
||||
|
||||
public:
|
||||
MemoryViewer(
|
||||
std::shared_ptr<Graphics> graphics,
|
||||
std::shared_ptr<MachineState> machine_state,
|
||||
std::shared_ptr<CallbackManager> callback_manager
|
||||
);
|
||||
|
||||
void render();
|
||||
std::vector<std::string> get_memory_dump() const;
|
||||
};
|
||||
|
||||
|
||||
#endif //MEMORYVIEWER_H
|
||||
@@ -40,5 +40,5 @@ void RomInfo::on_rom_load(const std::string& rom_path)
|
||||
std::filesystem::path home = SDL_GetUserFolder(SDL_FOLDER_HOME);
|
||||
std::filesystem::path path = rom_path;
|
||||
|
||||
this->rom_path = std::filesystem::relative(path, home).string();
|
||||
this->rom_path = "~/" + std::filesystem::relative(path, home).string();
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "UIManager.h"
|
||||
|
||||
#include "MemoryViewer.h"
|
||||
#include "RomInfo.h"
|
||||
|
||||
UIManager::UIManager(
|
||||
@@ -13,7 +14,8 @@ UIManager::UIManager(
|
||||
|
||||
display{std::make_unique<Display>(this->graphics, this->machine_state)},
|
||||
control_panel{std::make_unique<ControlPanel>(this->graphics, this->machine_state, this->callback_manager)},
|
||||
rom_info{std::make_unique<RomInfo>(this->graphics, this->machine_state, this->callback_manager)}
|
||||
rom_info{std::make_unique<RomInfo>(this->graphics, this->machine_state, this->callback_manager)},
|
||||
memory_viewer{std::make_unique<MemoryViewer>(this->graphics, this->machine_state, this->callback_manager)}
|
||||
{
|
||||
}
|
||||
|
||||
@@ -22,4 +24,5 @@ void UIManager::render()
|
||||
this->display->render();
|
||||
this->control_panel->render();
|
||||
this->rom_info->render();
|
||||
this->memory_viewer->render();
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "CallbackManager.h"
|
||||
#include "ControlPanel.h"
|
||||
#include "Display.h"
|
||||
#include "MemoryViewer.h"
|
||||
#include "RomInfo.h"
|
||||
#include "../Graphics/Graphics.h"
|
||||
|
||||
@@ -16,6 +17,7 @@ class UIManager
|
||||
std::unique_ptr<Display> display;
|
||||
std::unique_ptr<ControlPanel> control_panel;
|
||||
std::unique_ptr<RomInfo> rom_info;
|
||||
std::unique_ptr<MemoryViewer> memory_viewer;
|
||||
|
||||
public:
|
||||
UIManager(
|
||||
|
||||
Reference in New Issue
Block a user