2016-06-25 21:59:34 +00:00
|
|
|
#ifndef BBGE_JOYSTICK_H
|
|
|
|
#define BBGE_JOYSTICK_H
|
|
|
|
|
2016-10-03 21:01:19 +00:00
|
|
|
#include <string>
|
2016-06-25 21:59:34 +00:00
|
|
|
#include <SDL_joystick.h>
|
|
|
|
|
2016-06-30 00:58:55 +00:00
|
|
|
#ifdef BBGE_BUILD_SDL2
|
|
|
|
#include <SDL_gamecontroller.h>
|
|
|
|
#include <SDL_haptic.h>
|
|
|
|
#endif
|
|
|
|
|
2016-06-25 21:59:34 +00:00
|
|
|
#include "Vector.h"
|
|
|
|
|
|
|
|
#define MAX_JOYSTICK_BTN 32
|
2016-06-30 00:58:55 +00:00
|
|
|
#define MAX_JOYSTICK_AXIS 32
|
2020-07-07 14:24:03 +00:00
|
|
|
#define MAX_JOYSTICK_HATS 8
|
2016-06-25 21:59:34 +00:00
|
|
|
|
2020-07-07 20:30:25 +00:00
|
|
|
enum JoyHatDirection // bitmask
|
|
|
|
{
|
|
|
|
JOY_HAT_DIR_CENTERED = 0,
|
|
|
|
JOY_HAT_DIR_UP = 1,
|
|
|
|
JOY_HAT_DIR_DOWN = 2,
|
|
|
|
JOY_HAT_DIR_LEFT = 4,
|
|
|
|
JOY_HAT_DIR_RIGHT = 8
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-07-13 03:00:19 +00:00
|
|
|
const static float JOY_AXIS_THRESHOLD = 0.6f;
|
|
|
|
|
2016-06-25 21:59:34 +00:00
|
|
|
class Joystick
|
|
|
|
{
|
|
|
|
public:
|
2016-07-03 16:07:13 +00:00
|
|
|
static unsigned GetNumJoysticks();
|
|
|
|
|
2016-06-25 21:59:34 +00:00
|
|
|
Joystick();
|
2017-06-25 13:56:45 +00:00
|
|
|
~Joystick();
|
|
|
|
|
2016-07-03 16:07:13 +00:00
|
|
|
bool init(int stick=0);
|
2016-06-25 21:59:34 +00:00
|
|
|
void shutdown();
|
2016-06-30 00:58:55 +00:00
|
|
|
//Ranges from 0 to 1 (full speed).
|
2016-06-25 21:59:34 +00:00
|
|
|
void rumble(float leftMotor, float rightMotor, float time);
|
|
|
|
void update(float dt);
|
2016-06-30 00:58:55 +00:00
|
|
|
Vector position;
|
2016-06-25 21:59:34 +00:00
|
|
|
|
|
|
|
float deadZone1, deadZone2;
|
|
|
|
float clearRumbleTime;
|
|
|
|
|
2016-06-30 00:58:55 +00:00
|
|
|
void calibrate(Vector &vec, float dead);
|
|
|
|
bool anyButton() const;
|
2017-01-20 03:51:38 +00:00
|
|
|
bool getButton(size_t id) const { return !!(buttonBitmask & (1u << id)); }
|
2020-07-07 20:30:25 +00:00
|
|
|
float getAxisUncalibrated(unsigned id) const;
|
|
|
|
JoyHatDirection getHat(unsigned id) const;
|
2020-07-07 14:24:03 +00:00
|
|
|
unsigned getNumAxes() const;
|
|
|
|
unsigned getNumButtons() const;
|
|
|
|
unsigned getNumHats() const;
|
2016-06-30 00:58:55 +00:00
|
|
|
int getIndex() const { return stickIndex; }
|
|
|
|
int getInstanceID() const { return instanceID; }
|
2016-07-03 16:07:13 +00:00
|
|
|
inline bool isEnabled() const { return enabled; }
|
2016-07-16 20:08:39 +00:00
|
|
|
inline void setEnabled(bool on) { enabled = on; }
|
2016-06-25 21:59:34 +00:00
|
|
|
|
2020-07-07 14:24:03 +00:00
|
|
|
const char *getAxisName(unsigned axis) const;
|
|
|
|
const char *getButtonName(unsigned btn) const;
|
2016-07-15 01:22:27 +00:00
|
|
|
const char *getName() const;
|
|
|
|
const char *getGUID() const;
|
2016-07-13 03:00:19 +00:00
|
|
|
|
2016-06-25 21:59:34 +00:00
|
|
|
Vector rightStick;
|
|
|
|
|
|
|
|
int s1ax, s1ay, s2ax, s2ay;
|
|
|
|
|
|
|
|
private:
|
2016-07-13 03:00:19 +00:00
|
|
|
void clearAxes();
|
2016-07-03 16:07:13 +00:00
|
|
|
bool enabled;
|
2016-06-30 00:58:55 +00:00
|
|
|
int stickIndex;
|
|
|
|
int instanceID;
|
2016-07-13 03:00:19 +00:00
|
|
|
unsigned buttonBitmask;
|
2020-07-07 14:24:03 +00:00
|
|
|
unsigned numJoyAxes, numHats, numButtons;
|
2016-06-25 22:03:10 +00:00
|
|
|
SDL_Joystick *sdl_joy;
|
2016-07-13 03:00:19 +00:00
|
|
|
float axisRaw[MAX_JOYSTICK_AXIS];
|
2016-07-15 01:22:27 +00:00
|
|
|
std::string name;
|
|
|
|
std::string guid;
|
2016-06-25 21:59:34 +00:00
|
|
|
|
|
|
|
# ifdef BBGE_BUILD_SDL2
|
|
|
|
SDL_GameController *sdl_controller;
|
|
|
|
SDL_Haptic *sdl_haptic;
|
|
|
|
# endif
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|