yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
input_backend.h
Go to the documentation of this file.
1#ifndef YAZE_APP_EMU_INPUT_INPUT_BACKEND_H_
2#define YAZE_APP_EMU_INPUT_INPUT_BACKEND_H_
3
4#include <cstdint>
5#include <memory>
6#include <string>
7
8namespace yaze {
9namespace emu {
10namespace input {
11
15enum class SnesButton : uint8_t {
16 B = 0, // Bit 0
17 Y = 1, // Bit 1
18 SELECT = 2, // Bit 2
19 START = 3, // Bit 3
20 UP = 4, // Bit 4
21 DOWN = 5, // Bit 5
22 LEFT = 6, // Bit 6
23 RIGHT = 7, // Bit 7
24 A = 8, // Bit 8
25 X = 9, // Bit 9
26 L = 10, // Bit 10
27 R = 11 // Bit 11
28};
29
34 uint16_t buttons = 0; // Bit field matching SNES hardware layout
35
36 bool IsPressed(SnesButton button) const {
37 return (buttons & (1 << static_cast<uint8_t>(button))) != 0;
38 }
39
40 void SetButton(SnesButton button, bool pressed) {
41 if (pressed) {
42 buttons |= (1 << static_cast<uint8_t>(button));
43 } else {
44 buttons &= ~(1 << static_cast<uint8_t>(button));
45 }
46 }
47
48 void Clear() { buttons = 0; }
49};
50
55 // Platform-agnostic key codes (mapped to platform-specific in backend)
56 // Using generic names that can be mapped to SDL2/SDL3/other
57 int key_a = 0; // Default: X key
58 int key_b = 0; // Default: Z key
59 int key_x = 0; // Default: S key
60 int key_y = 0; // Default: A key
61 int key_l = 0; // Default: D key
62 int key_r = 0; // Default: C key
63 int key_start = 0; // Default: Enter
64 int key_select = 0; // Default: RShift
65 int key_up = 0; // Default: Up arrow
66 int key_down = 0; // Default: Down arrow
67 int key_left = 0; // Default: Left arrow
68 int key_right = 0; // Default: Right arrow
69
70 // Enable/disable continuous polling (vs event-based)
71 bool continuous_polling = true;
72
73 // Enable gamepad support
74 bool enable_gamepad = false;
75 int gamepad_index = 0; // Which gamepad to use (0-3)
76
77 // Allow game input even when ImGui text widgets request keyboard focus
78 // Default true since SNES buttons use non-typing keys (arrows, X, Z, etc.)
80};
81
88
96 public:
97 virtual ~IInputBackend() = default;
98
102 virtual bool Initialize(const InputConfig& config) = 0;
103
107 virtual void Shutdown() = 0;
108
114 virtual ControllerState Poll(int player = 1) = 0;
115
120 virtual void ProcessEvent(void* event) = 0;
121
125 virtual InputConfig GetConfig() const = 0;
126
130 virtual void SetConfig(const InputConfig& config) = 0;
131
135 virtual std::string GetBackendName() const = 0;
136
140 virtual bool IsInitialized() const = 0;
141};
142
147 public:
148 enum class BackendType {
149 SDL2,
150 SDL3, // Future
151 NULL_BACKEND // For testing/replay
152 };
153
154 static std::unique_ptr<IInputBackend> Create(BackendType type);
155};
156
157} // namespace input
158} // namespace emu
159} // namespace yaze
160
161#endif // YAZE_APP_EMU_INPUT_INPUT_BACKEND_H_
Abstract input backend interface.
virtual ControllerState Poll(int player=1)=0
Poll current input state (call every frame)
virtual void Shutdown()=0
Shutdown the input backend.
virtual bool IsInitialized() const =0
Check if backend is initialized.
virtual void ProcessEvent(void *event)=0
Process platform-specific events (optional)
virtual InputConfig GetConfig() const =0
Get current configuration.
virtual std::string GetBackendName() const =0
Get backend name for debugging.
virtual ~IInputBackend()=default
virtual void SetConfig(const InputConfig &config)=0
Update configuration (hot-reload)
virtual bool Initialize(const InputConfig &config)=0
Initialize the input backend.
Factory for creating input backends.
static std::unique_ptr< IInputBackend > Create(BackendType type)
void ApplyDefaultKeyBindings(InputConfig &config)
Apply default keyboard bindings if unset.
SnesButton
SNES controller button mapping (platform-agnostic)
Controller state (16-bit SNES controller format)
void SetButton(SnesButton button, bool pressed)
bool IsPressed(SnesButton button) const
Input configuration (platform-agnostic key codes)