#pragma region CPL License
/*
Nuclex Native Framework
Copyright (C) 2002-2013 Nuclex Development Labs
This library is free software; you can redistribute it and/or
modify it under the terms of the IBM Common Public License as
published by the IBM Corporation; either version 1.0 of the
License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
IBM Common Public License for more details.
You should have received a copy of the IBM Common Public
License along with this library
*/
#pragma endregion // CPL License
// If the library is compiled as a DLL, this ensures symbols are exported
#define NUCLEX_INPUT_SOURCE 1
#include "Nuclex/Input/Devices/GamePadState.h"
namespace {
// ------------------------------------------------------------------------------------------- //
/// Returns the number of bits set in an unsigned integer
/// Value whose bits will be counted
/// The number of bits set in the unsigned integer
template std::size_t countBits(std::size_t value);
// ------------------------------------------------------------------------------------------- //
/// Returns the number of bits set in an unsigned integer
/// Value whose bits will be counted
/// The number of bits set in the unsigned integer
///
/// Based on a trick revealed here: http://stackoverflow.com/questions/109023
///
template <>
std::size_t countBits<4>(std::size_t value) { // 32 bit version
value = value - ((value >> 1) & 0x55555555);
value = (value & 0x33333333) + ((value >> 2) & 0x33333333);
return ((value + (value >> 4) & 0xF0F0F0F) * 0x1010101) >> 24;
}
// ------------------------------------------------------------------------------------------- //
/// Returns the number of bits set in an unsigned integer
/// Value whose bits will be counted
/// The number of bits set in the unsigned integer
///
/// Based on a trick revealed here: http://stackoverflow.com/questions/2709430
///
template <>
std::size_t countBits<8>(std::size_t value) { // 64 bit version
value = value - ((value >> 1) & 0x5555555555555555UL);
value = (value & 0x3333333333333333UL) + ((value >> 2) & 0x3333333333333333UL);
return (((value + (value >> 4)) & 0xF0F0F0F0F0F0F0FUL) * 0x101010101010101UL) >> 56;
}
// ------------------------------------------------------------------------------------------- //
} // anonymous namespace
namespace Nuclex { namespace Input { namespace Devices {
// ------------------------------------------------------------------------------------------- //
std::size_t GamePadState::CountSliders() const {
return countBits(this->SliderMask);
}
// ------------------------------------------------------------------------------------------- //
std::size_t GamePadState::CountAxes() const {
return countBits(this->AxisMask);
}
// ------------------------------------------------------------------------------------------- //
void GamePadState::Reset() {
// Clear all buttons
this->Buttons.reset();
this->ButtonCount = 0;
// Clear all sliders
for(std::size_t index = 0; index < 8; ++index) {
this->Sliders[index] = 0.0f;
}
this->SliderMask = static_cast(0);
// Clear all axes
for(std::size_t index = 0; index < 24; ++index) {
this->Axes[index] = 0.0f;
}
this->AxisMask = static_cast(0);
// Clear all directional pads
for(std::size_t index = 0; index < 4; ++index) {
DirectionalPad &pad = this->DirectionalPads[index];
pad.Up = pad.Down = pad.Left = pad.Right = false;
}
this->DirectionalPadCount = 0;
}
// ------------------------------------------------------------------------------------------- //
}}} // namespace Nuclex::Input::Devices