Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
JoystickManager.h
Go to the documentation of this file.
1#pragma once
2
3#include "message.h"
4#include <SDL3/SDL.h>
5#include <unordered_map>
6#include <vector>
7#include <string>
8#include <mutex>
9
10// Forward declare to access constant
11struct Controller_data;
12
14{
15public:
16 static constexpr int MAX_BUTTONS = 16; // Must match Controller_data::MAX_BUTTONS
17 static constexpr int MAX_AXES = 6;
18public:
20 {
21 name = "JoystickManager";
22 Initialize();
23 }
25 {
26 Shutdown();
27 }
28
30 static JoystickManager& Get() { return GetInstance(); }
31
32 // Initialize joystick subsystem and open currently connected devices
33 void Initialize();
34 void Scan_Joysticks();
35 void Shutdown();
36
37 // Process per-frame (optional)
38 void Process(float dt);
39
40 // Handle an incoming SDL_Event (forwarded from the application event pump)
41 void HandleEvent(const SDL_Event* ev);
42
43 // Query
44 std::vector<SDL_JoystickID> GetConnectedJoysticks();
46
47 // Pull API for reading joystick state
48 void BeginFrame();
49 bool GetButton(SDL_JoystickID id, int button) ;
52 float GetAxis(SDL_JoystickID id, int axis) ;
53
54private:
55 std::string name;
56
58 {
61 std::string name;
62 int numAxes = 0;
63 int numButtons = 0;
64 std::vector<Sint16> axes;
65 std::vector<bool> buttons;
66 };
67
68 // State tracking for pull API
70 {
71 bool connected = false;
72 float axes[MAX_AXES] = {0};
73 bool buttons[MAX_BUTTONS] = {false};
74 bool buttonsPressed[MAX_BUTTONS] = {false};
76 };
77
78 std::unordered_map<SDL_JoystickID, JoystickInfo> m_joysticks;
79 std::unordered_map<SDL_JoystickID, JoystickState> m_joyStates;
80 std::mutex m_mutex;
81
87};
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
float GetAxis(SDL_JoystickID id, int axis)
void PostJoystickButtonEvent(SDL_JoystickID which, int button, bool down)
bool GetButton(SDL_JoystickID id, int button)
bool IsButtonReleased(SDL_JoystickID id, int button)
bool IsButtonPressed(SDL_JoystickID id, int button)
std::unordered_map< SDL_JoystickID, JoystickState > m_joyStates
static constexpr int MAX_BUTTONS
virtual ~JoystickManager()
bool IsJoystickConnected(SDL_JoystickID id)
static JoystickManager & GetInstance()
static JoystickManager & Get()
void Process(float dt)
void OpenJoystick(SDL_JoystickID instance_id)
void PostJoystickConnectedEvent(SDL_JoystickID which, bool bconnected)
void HandleEvent(const SDL_Event *ev)
void CloseJoystick(SDL_JoystickID instance_id)
std::unordered_map< SDL_JoystickID, JoystickInfo > m_joysticks
std::vector< SDL_JoystickID > GetConnectedJoysticks()
void PostJoystickAxisEvent(SDL_JoystickID which, int axis, Sint16 value)
static constexpr int MAX_AXES
std::vector< Sint16 > axes