Simplificando callback manager

This commit is contained in:
2025-06-26 23:35:33 -04:00
parent 8b20f4499d
commit cb0d31fb9e
10 changed files with 123 additions and 152 deletions

View File

@@ -1,4 +1,5 @@
add_executable(${PROJECT_NAME})
add_executable(${PROJECT_NAME}
)
target_sources(
${PROJECT_NAME}
@@ -9,11 +10,11 @@ target_sources(
Graphics/Graphics.cpp
Graphics/Graphics.h
Graphics/Color.h
Interpreter/MachineState.cpp
Interpreter/MachineState.h
Interpreter/Interpreter.cpp
Interpreter/Interpreter.h
Interpreter/Instruction.h
Interpreter/MachineState.cpp
Interpreter/MachineState.h
Interpreter/OpCode.h
UI/CallbackManager.cpp
UI/CallbackManager.h
@@ -21,6 +22,8 @@ target_sources(
UI/ControlPanel.h
UI/Display.cpp
UI/Display.h
UI/RomInfo.cpp
UI/RomInfo.h
UI/UIManager.cpp
UI/UIManager.h
)

View File

@@ -21,12 +21,13 @@ Machine::Machine() :
interpreter{std::make_unique<Interpreter>(this->machine_state)},
ui_manager{std::make_unique<UIManager>(this->graphics, this->machine_state, this->callback_manager)},
running{false},
ips{700},
ips{60},
last_update_time{0},
accumulator{0},
target_cycle_time{1.0 / this->ips}
{
this->register_callbacks();
this->callback_manager->trigger(this->callback_manager->ips_callback, this->ips);
}
void Machine::iterate()
@@ -69,34 +70,34 @@ void Machine::execute_interpreter()
void Machine::register_callbacks()
{
callback_manager->set_rom_load_callback(std::bind(
callback_manager->rom_load_callback.push_back(std::bind(
&Machine::on_rom_load,
this,
std::placeholders::_1
));
callback_manager->set_reset_callback(std::bind(
callback_manager->reset_callback.push_back(std::bind(
&Machine::on_reset,
this
));
callback_manager->set_stop_callback(std::bind(
callback_manager->stop_callback.push_back(std::bind(
&Machine::on_stop,
this
));
callback_manager->set_resume_callback(std::bind(
callback_manager->resume_callback.push_back(std::bind(
&Machine::on_resume,
this
));
callback_manager->set_step_callback(std::bind(
callback_manager->step_callback.push_back(std::bind(
&Machine::on_step,
this,
std::placeholders::_1
));
callback_manager->set_ips_callback(std::bind(
callback_manager->ips_callback.push_back(std::bind(
&Machine::on_speed_change,
this,
std::placeholders::_1
));
callback_manager->set_reload_callback(std::bind(
callback_manager->reload_callback.push_back(std::bind(
&Machine::on_reload,
this
));
@@ -127,7 +128,7 @@ void Machine::on_rom_load(const std::string& path)
);
this->rom = path;
std::cout << "Cargado rom: " << path << std::endl;
std::cout << "ROM Loaded: " << path << std::endl;
}
void Machine::on_reset() const

View File

@@ -1,95 +1,3 @@
#include "CallbackManager.h"
//----------------------------------------------------------------
// SET CALLBACKS
//----------------------------------------------------------------
void CallbackManager::set_rom_load_callback(const RomLoadCallback& callback)
{
this->rom_load_callback = callback;
}
void CallbackManager::set_reset_callback(const ResetCallback& callback)
{
this->reset_callback = callback;
}
void CallbackManager::set_resume_callback(const ResumeCallback& callback)
{
this->resume_callback = callback;
}
void CallbackManager::set_stop_callback(const StopCallback& callback)
{
this->stop_callback = callback;
}
void CallbackManager::set_step_callback(const StepCallback& callback)
{
this->step_callback = callback;
}
void CallbackManager::set_ips_callback(const IPSCallback& callback)
{
this->ips_callback = callback;
}
void CallbackManager::set_reload_callback(const ReloadCallback& callback)
{
this->reload_callback = callback;
}
//----------------------------------------------------------------
// TRIGGER CALLBACKS
//----------------------------------------------------------------
void CallbackManager::trigger_rom_load(const std::string& path)
{
if (this->rom_load_callback)
{
this->rom_load_callback(path);
}
}
void CallbackManager::trigger_reset()
{
if (this->reset_callback)
{
this->reset_callback();
}
}
void CallbackManager::trigger_resume()
{
if (this->resume_callback)
{
this->resume_callback();
}
}
void CallbackManager::trigger_stop()
{
if (this->stop_callback)
{
this->stop_callback();
}
}
void CallbackManager::trigger_step(int steps)
{
if (this->step_callback)
{
this->step_callback(steps);
}
}
void CallbackManager::trigger_ips(int speed)
{
if (this->ips_callback)
{
this->ips_callback(speed);
}
}
void CallbackManager::trigger_reload()
{
if (this->reload_callback) this->reload_callback();
}

View File

@@ -3,43 +3,26 @@
#include <functional>
#include <string>
#include <vector>
struct CallbackManager
{
using RomLoadCallback = std::function<void(const std::string&)>;
using ResetCallback = std::function<void()>;
using ResumeCallback = std::function<void()>;
using StopCallback = std::function<void()>;
using StepCallback = std::function<void(int)>;
using IPSCallback = std::function<void(int)>;
using ReloadCallback = std::function<void()>;
std::vector<std::function<void(const std::string&)>> rom_load_callback;
std::vector<std::function<void()>> reset_callback;
std::vector<std::function<void()>> resume_callback;
std::vector<std::function<void()>> stop_callback;
std::vector<std::function<void(int)>> step_callback;
std::vector<std::function<void(int)>> ips_callback;
std::vector<std::function<void()>> reload_callback;
void set_rom_load_callback(const RomLoadCallback& callback);
void set_reset_callback(const ResetCallback& callback);
void set_resume_callback(const ResumeCallback& callback);
void set_stop_callback(const StopCallback& callback);
void set_step_callback(const StepCallback& callback);
void set_ips_callback(const IPSCallback& callback);
void set_reload_callback(const ReloadCallback& callback);
void trigger_rom_load(const std::string& path);
void trigger_reset();
void trigger_resume();
void trigger_stop();
void trigger_step(int);
void trigger_ips(int);
void trigger_reload();
private:
RomLoadCallback rom_load_callback;
ResetCallback reset_callback;
ResumeCallback resume_callback;
StopCallback stop_callback;
StepCallback step_callback;
IPSCallback ips_callback;
ReloadCallback reload_callback;
template <typename Func, typename... Args>
void trigger(const std::vector<Func>& callbacks, Args&&... args)
{
for (const auto& callback : callbacks)
{
callback(std::forward<Args>(args)...);
}
}
};
#endif //CALLBACKMANAGER_H

View File

@@ -1,8 +1,10 @@
#include "ControlPanel.h"
#include <format>
#include <iostream>
#include <ranges>
#include "CallbackManager.h"
#include "imgui.h"
@@ -14,6 +16,11 @@ ControlPanel::ControlPanel(
machine_state{std::move(machine_state)},
callback_manager{std::move(callback_manager)}
{
this->callback_manager->ips_callback.push_back(std::bind(
&ControlPanel::initial_ips_load,
this,
std::placeholders::_1
));
}
void ControlPanel::render()
@@ -34,11 +41,11 @@ void ControlPanel::render()
this->run = !run;
if (this->run)
{
this->callback_manager->trigger_resume();
this->callback_manager->trigger(this->callback_manager->resume_callback);
}
else
{
this->callback_manager->trigger_stop();
this->callback_manager->trigger(this->callback_manager->stop_callback);
}
}
@@ -49,12 +56,12 @@ void ControlPanel::render()
if (ImGui::Button("Step One", full_width))
{
this->callback_manager->trigger_step(1);
this->callback_manager->trigger(this->callback_manager->step_callback, 1);
}
if (ImGui::Button(std::format("Step +{}", steps).data(), full_width))
{
this->callback_manager->trigger_step(steps);
this->callback_manager->trigger(this->callback_manager->step_callback, steps);
}
ImGui::PushItemWidth(ImGui::GetContentRegionAvail().x);
@@ -72,17 +79,26 @@ void ControlPanel::render()
if (ImGui::Button("Reset", full_width))
{
this->callback_manager->trigger_reset();
this->callback_manager->trigger(this->callback_manager->reset_callback);
}
if (ImGui::Button("Reload ROM", full_width))
{
this->callback_manager->trigger_reload();
this->callback_manager->trigger(this->callback_manager->reload_callback);
}
}
if (starting_speed != this->ips)
if (ips < 0)
{
this->callback_manager->trigger_ips(this->ips);
ips = 0;
}
if (steps < 1)
{
steps = 1;
}
if (starting_speed != ips)
{
this->callback_manager->trigger(this->callback_manager->ips_callback, ips);
}
ImGui::End();
@@ -115,5 +131,13 @@ void ControlPanel::on_callback_load_rom(void* self, const char* const* filelist,
return;
}
control_panel->callback_manager->trigger_rom_load(filelist[0]);
control_panel->callback_manager->trigger(
control_panel->callback_manager->rom_load_callback,
std::string(filelist[0])
);
}
void ControlPanel::initial_ips_load(int ips)
{
this->ips = ips;
}

View File

@@ -15,9 +15,10 @@ class ControlPanel
bool run = false;
int steps = 1;
int ips = 700;
int ips = 0;
static SDLCALL void on_callback_load_rom(void* self, const char* const* filelist, int filter);
void initial_ips_load(int ips);
public:
ControlPanel(

20
src/UI/RomInfo.cpp Normal file
View File

@@ -0,0 +1,20 @@
#include "RomInfo.h"
#include "imgui.h"
RomInfo::RomInfo(
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)}
{
}
void RomInfo::render()
{
if (ImGui::Begin("Chip-8 - ROM Info"))
{
}
}

26
src/UI/RomInfo.h Normal file
View File

@@ -0,0 +1,26 @@
#ifndef ROMINFO_H
#define ROMINFO_H
#include <memory>
#include "CallbackManager.h"
#include "../Graphics/Graphics.h"
#include "../Interpreter/MachineState.h"
class RomInfo
{
std::shared_ptr<Graphics> graphics;
std::shared_ptr<MachineState> machine_state;
std::shared_ptr<CallbackManager> callback_manager;
public:
RomInfo(
std::shared_ptr<Graphics> graphics,
std::shared_ptr<MachineState> machine_state,
std::shared_ptr<CallbackManager> callback_manager
);
void render();
};
#endif //ROMINFO_H

View File

@@ -1,5 +1,7 @@
#include "UIManager.h"
#include "RomInfo.h"
UIManager::UIManager(
std::shared_ptr<Graphics> graphics,
std::shared_ptr<MachineState> machine_state,
@@ -8,8 +10,10 @@ UIManager::UIManager(
graphics{std::move(graphics)},
machine_state{std::move(machine_state)},
callback_manager{std::move(callback_manager)},
display{std::make_unique<Display>(this->graphics, this->machine_state)},
control_panel{std::make_unique<ControlPanel>(this->graphics, this->machine_state, this->callback_manager)}
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)}
{
}

View File

@@ -5,6 +5,7 @@
#include "CallbackManager.h"
#include "ControlPanel.h"
#include "Display.h"
#include "RomInfo.h"
#include "../Graphics/Graphics.h"
class UIManager
@@ -12,11 +13,11 @@ class UIManager
std::shared_ptr<Graphics> graphics;
std::shared_ptr<MachineState> machine_state;
std::shared_ptr<CallbackManager> callback_manager;
public:
std::unique_ptr<Display> display;
std::unique_ptr<ControlPanel> control_panel;
std::unique_ptr<RomInfo> rom_info;
public:
UIManager(
std::shared_ptr<Graphics> graphics,
std::shared_ptr<MachineState> machine_state,