yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
input_manager.cc
Go to the documentation of this file.
2
3#include "app/emu/snes.h"
4#include "util/log.h"
5
6namespace yaze {
7namespace emu {
8namespace input {
9
12 if (!backend_) {
13 LOG_ERROR("InputManager", "Failed to create input backend");
14 return false;
15 }
16
17 InputConfig config;
18 config.continuous_polling = true;
19 config.enable_gamepad = false;
20
21 if (!backend_->Initialize(config)) {
22 LOG_ERROR("InputManager", "Failed to initialize input backend");
23 return false;
24 }
25
26 LOG_INFO("InputManager", "Initialized with backend: %s",
27 backend_->GetBackendName().c_str());
28 return true;
29}
30
31void InputManager::Initialize(std::unique_ptr<IInputBackend> backend) {
32 backend_ = std::move(backend);
33
34 if (backend_) {
35 LOG_INFO("InputManager", "Initialized with custom backend: %s",
36 backend_->GetBackendName().c_str());
37 }
38}
39
41 if (backend_) {
42 backend_->Shutdown();
43 backend_.reset();
44 }
45}
46
47void InputManager::Poll(Snes* snes, int player) {
48 if (!snes || !backend_) return;
49
50 ControllerState physical_state = backend_->Poll(player);
51
52 // Combine physical input with agent-controlled input (OR operation)
53 ControllerState final_state;
54 final_state.buttons = physical_state.buttons | agent_controller_state_.buttons;
55
56 // Apply button state directly to SNES
57 // Just send the raw button state on every Poll() call
58 // The button state will be latched by HandleInput() at VBlank
59 for (int i = 0; i < 12; i++) {
60 bool button_held = (final_state.buttons & (1 << i)) != 0;
61 snes->SetButtonState(player, i, button_held);
62 }
63
64 // Debug: Log complete button state when any button is pressed
65 static int poll_log_count = 0;
66 if (final_state.buttons != 0 && poll_log_count++ < 30) {
67 LOG_DEBUG("InputManager", "Poll: buttons=0x%04X", final_state.buttons);
68 }
69}
70
71void InputManager::ProcessEvent(void* event) {
72 if (backend_) {
73 backend_->ProcessEvent(event);
74 }
75}
76
78 if (backend_) {
79 return backend_->GetConfig();
80 }
81 return InputConfig{};
82}
83
85 if (backend_) {
86 backend_->SetConfig(config);
87 }
88}
89
93
97
98} // namespace input
99} // namespace emu
100} // namespace yaze
void SetButtonState(int player, int button, bool pressed)
Definition snes.cc:698
static std::unique_ptr< IInputBackend > Create(BackendType type)
void Poll(Snes *snes, int player=1)
void PressButton(SnesButton button)
std::unique_ptr< IInputBackend > backend_
ControllerState agent_controller_state_
void SetConfig(const InputConfig &config)
void ReleaseButton(SnesButton button)
InputConfig GetConfig() const
bool Initialize(InputBackendFactory::BackendType type=InputBackendFactory::BackendType::SDL2)
#define LOG_DEBUG(category, format,...)
Definition log.h:104
#define LOG_ERROR(category, format,...)
Definition log.h:110
#define LOG_INFO(category, format,...)
Definition log.h:106
SnesButton
SNES controller button mapping (platform-agnostic)
Main namespace for the application.
Controller state (16-bit SNES controller format)
void SetButton(SnesButton button, bool pressed)
Input configuration (platform-agnostic key codes)