comenzado a crear paneles con imgui para controlar/debugear la ejecucion

This commit is contained in:
2025-06-22 22:15:22 -04:00
parent eaec6ed8ab
commit 8df1300bd1
6 changed files with 83 additions and 15 deletions

6
.idea/misc.xml generated
View File

@@ -4,4 +4,10 @@
<option name="pythonIntegrationState" value="YES" /> <option name="pythonIntegrationState" value="YES" />
</component> </component>
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" /> <component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
<component name="CidrRootsConfiguration">
<libraryRoots>
<file path="$PROJECT_DIR$/vendor" />
<file path="$PROJECT_DIR$/vendor/imgui" />
</libraryRoots>
</component>
</project> </project>

View File

@@ -1,10 +1,13 @@
add_executable(${PROJECT_NAME} add_executable(${PROJECT_NAME})
target_sources(${PROJECT_NAME} PRIVATE main.cpp
bitops.h
Graphics.cpp
Graphics.h
Chip8.cpp Chip8.cpp
Chip8.h Chip8.h
Interpreter.cpp Interpreter.cpp
Interpreter.h) Interpreter.h
target_sources(${PROJECT_NAME} PRIVATE main.cpp Chip8ControlPanel.cpp
Graphics.cpp Chip8ControlPanel.h
Graphics.h
) )
target_link_libraries(${PROJECT_NAME} PRIVATE vendor) target_link_libraries(${PROJECT_NAME} PRIVATE vendor)

41
src/Chip8ControlPanel.cpp Normal file
View File

@@ -0,0 +1,41 @@
#include "Chip8ControlPanel.h"
#include <format>
#include "imgui.h"
void Chip8ControlPanel::render() {
constexpr auto full_width = ImVec2(-FLT_MIN, 0.0f);
if (ImGui::Begin("Chip-8 - Controls")) {
ImGui::Button("Load Rom", full_width);
if (ImGui::Button(run ? "Pause" : "Run", full_width)) {
run = !run;
}
ImGui::Text("Status: %s", "Stopped");
ImGui::SeparatorText("Debug");
ImGui::Button("Step One", full_width);
ImGui::Button(std::format("Step +{}", steps).data(), full_width);
ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x);
ImGui::InputInt("##steps", &steps);
ImGui::PopItemWidth();
ImGui::SeparatorText("Emulation speed (IPS)");
ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x);
ImGui::InputInt("##speed", &speed);
ImGui::PopItemWidth();
ImGui::SeparatorText("");
ImGui::Button("Reset", full_width);
ImGui::Button("Reload ROM", full_width);
}
ImGui::End();
}

14
src/Chip8ControlPanel.h Normal file
View File

@@ -0,0 +1,14 @@
#ifndef CHIP8CONTROLPANEL_H
#define CHIP8CONTROLPANEL_H
class Chip8ControlPanel {
bool run = false;
int steps = 10;
int speed = 700;
public:
void render();
};
#endif //CHIP8CONTROLPANEL_H

View File

@@ -3,6 +3,7 @@
// //
#include "Graphics.h" #include "Graphics.h"
#include "Chip8ControlPanel.h"
#include <array> #include <array>
#include <iostream> #include <iostream>
@@ -32,6 +33,7 @@ Graphics::Graphics(std::shared_ptr<MachineState> machine_state): machine_state{s
chip8_width(64), chip8_width(64),
chip8_height(32), chip8_height(32),
main_scale(1.0f) {} main_scale(1.0f) {}
bool Graphics::init_sdl() { bool Graphics::init_sdl() {
SDL_SetAppMetadata("CHIP-8 Emulator", "0.0.1", "fun.skrd.chip8"); SDL_SetAppMetadata("CHIP-8 Emulator", "0.0.1", "fun.skrd.chip8");
@@ -104,7 +106,6 @@ void Graphics::draw() {
build_chip8_texture(); build_chip8_texture();
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
ImGui_ImplSDLRenderer3_NewFrame(); ImGui_ImplSDLRenderer3_NewFrame();
@@ -113,6 +114,7 @@ void Graphics::draw() {
ImGui::DockSpaceOverViewport(0, ImGui::GetMainViewport()); ImGui::DockSpaceOverViewport(0, ImGui::GetMainViewport());
draw_chip8_widget(); draw_chip8_widget();
ctrPanel.render();
ImGui::Render(); ImGui::Render();
SDL_SetRenderScale(renderer.get(), io.DisplayFramebufferScale.x, io.DisplayFramebufferScale.y); SDL_SetRenderScale(renderer.get(), io.DisplayFramebufferScale.x, io.DisplayFramebufferScale.y);
@@ -153,20 +155,19 @@ void Graphics::build_chip8_texture() {
} }
void Graphics::draw_chip8_widget() { void Graphics::draw_chip8_widget() {
ImGui::Begin("CHIP-8"); ImGui::Begin("CHIP-8 - Display");
const ImVec2 available_size = ImGui::GetContentRegionAvail(); const ImVec2 available_size = ImGui::GetContentRegionAvail();
int scale_x = static_cast<int>(available_size.x / chip8_width); const int scale_x = static_cast<int>(static_cast<float>(available_size.x) / static_cast<float>(chip8_width));
int scale_y = static_cast<int>(available_size.y / chip8_height); const int scale_y = static_cast<int>(static_cast<float>(available_size.y) / static_cast<float>(chip8_height));
int scale = std::max(1, std::min(scale_x, scale_y)); const int scale = std::max(1, std::min(scale_x, scale_y));
ImVec2 scaled_size(chip8_width * scale, chip8_height * scale); const ImVec2 scaled_size(static_cast<float>(chip8_width * scale), static_cast<float>(chip8_height * scale));
ImVec2 cursor_pos = ImGui::GetCursorPos(); const ImVec2 cursor_pos = ImGui::GetCursorPos();
ImVec2 center_offset((available_size.x - scaled_size.x) / 2, (available_size.y - scaled_size.y) / 2); const ImVec2 center_offset((available_size.x - scaled_size.x) / 2, (available_size.y - scaled_size.y) / 2);
ImGui::SetCursorPos(ImVec2(cursor_pos.x + center_offset.x, cursor_pos.y + center_offset.y)); ImGui::SetCursorPos(ImVec2(cursor_pos.x + center_offset.x, cursor_pos.y + center_offset.y));
ImGui::Image((ImTextureID)(intptr_t)texture.get(), scaled_size); ImGui::Image(static_cast<ImTextureID>(reinterpret_cast<intptr_t>(texture.get())), scaled_size);
ImGui::End(); ImGui::End();
} }

View File

@@ -7,6 +7,7 @@
#include <bitset> #include <bitset>
#include <memory> #include <memory>
#include "Chip8ControlPanel.h"
#include "imgui.h" #include "imgui.h"
#include "MachineState.h" #include "MachineState.h"
#include "SDL3/SDL.h" #include "SDL3/SDL.h"
@@ -28,6 +29,8 @@ class Graphics {
std::unique_ptr<SDL_Renderer, SDLRendererDestroyer> renderer; std::unique_ptr<SDL_Renderer, SDLRendererDestroyer> renderer;
std::unique_ptr<SDL_Texture, SDLTextureDestroyer> texture; std::unique_ptr<SDL_Texture, SDLTextureDestroyer> texture;
Chip8ControlPanel ctrPanel;
int window_width; int window_width;
int window_height; int window_height;