Update to v094r21 release.

byuu says:

This updates ruby to return shared_pointer<HID::Device> objects instead
of HID::Device* objects. It also fixes an ID bug where joypads were
starting at ID# 2+, but mice were also set to ID# 2. I also revised
nall/hid a lot, with getters and setters instead of stabbing at internal
state. I didn't yet patch nall::string to safely consume nullptr const
char* values, though.
This commit is contained in:
Tim Allen
2015-05-24 19:44:28 +10:00
parent 4e0223d590
commit 99b2b4b57c
17 changed files with 289 additions and 302 deletions

View File

@@ -5,48 +5,47 @@ namespace ruby {
struct InputJoypadSDL {
struct Joypad {
HID::Joypad hid;
shared_pointer<HID::Joypad> hid{new HID::Joypad};
unsigned id = 0;
SDL_Joystick* handle = nullptr;
};
vector<Joypad> joypads;
void assign(HID::Joypad& hid, unsigned groupID, unsigned inputID, int16_t value) {
auto& group = hid.group[groupID];
if(group.input[inputID].value == value) return;
if(input.onChange) input.onChange(hid, groupID, inputID, group.input[inputID].value, value);
group.input[inputID].value = value;
auto assign(shared_pointer<HID::Joypad> hid, unsigned groupID, unsigned inputID, int16_t value) -> void {
auto& group = hid->group(groupID);
if(group.input(inputID).value() == value) return;
if(input.onChange) input.onChange(hid, groupID, inputID, group.input(inputID).value(), value);
group.input(inputID).setValue(value);
}
void poll(vector<HID::Device*>& devices) {
auto poll(vector<shared_pointer<HID::Device>>& devices) -> void {
SDL_JoystickUpdate();
for(auto& jp : joypads) {
for(unsigned n = 0; n < jp.hid.axis().input.size(); n++) {
for(auto n : range(jp.hid->axes())) {
assign(jp.hid, HID::Joypad::GroupID::Axis, n, (int16_t)SDL_JoystickGetAxis(jp.handle, n));
}
for(signed n = 0; n < (signed)jp.hid.hat().input.size() - 1; n += 2) {
for(signed n = 0; n < (signed)jp.hid->hats().size() - 1; n += 2) {
uint8_t state = SDL_JoystickGetHat(jp.handle, n >> 1);
assign(jp.hid, HID::Joypad::GroupID::Hat, n + 0, state & SDL_HAT_LEFT ? -32768 : state & SDL_HAT_RIGHT ? +32767 : 0);
assign(jp.hid, HID::Joypad::GroupID::Hat, n + 1, state & SDL_HAT_UP ? -32768 : state & SDL_HAT_DOWN ? +32767 : 0);
}
for(unsigned n = 0; n < jp.hid.button().input.size(); n++) {
for(auto n : range(jp.hid->buttons())) {
assign(jp.hid, HID::Joypad::GroupID::Button, n, (bool)SDL_JoystickGetButton(jp.handle, n));
}
devices.append(&jp.hid);
devices.append(jp.hid);
}
}
bool init() {
auto init() -> bool {
SDL_InitSubSystem(SDL_INIT_JOYSTICK);
SDL_JoystickEventState(SDL_IGNORE);
unsigned joypadCount = SDL_NumJoysticks();
for(unsigned id = 0; id < joypadCount; id++) {
for(auto id : range(SDL_NumJoysticks())) {
Joypad jp;
jp.id = id;
jp.handle = SDL_JoystickOpen(id);
@@ -55,11 +54,11 @@ struct InputJoypadSDL {
unsigned hats = SDL_JoystickNumHats(jp.handle) * 2;
unsigned buttons = 32; //there is no SDL_JoystickNumButtons()
jp.hid.id = 2 + jp.id;
for(unsigned n = 0; n < axes; n++) jp.hid.axis().append({n});
for(unsigned n = 0; n < hats; n++) jp.hid.hat().append({n});
for(unsigned n = 0; n < buttons; n++) jp.hid.button().append({n});
jp.hid.rumble = false;
jp.hid->setID(3 + jp.id);
for(auto n : range(axes)) jp.hid->axes().append(n);
for(auto n : range(hats)) jp.hid->hats().append(n);
for(auto n : range(buttons)) jp.hid->buttons().append(n);
jp.hid->setRumble(false);
joypads.append(jp);
}
@@ -67,7 +66,7 @@ struct InputJoypadSDL {
return true;
}
void term() {
auto term() -> void {
for(auto& jp : joypads) {
SDL_JoystickClose(jp.handle);
}