18 #include <boost/math/constants/constants.hpp>
22 #define ERR_JOY LOG_STREAM(err, log_joystick)
23 #define LOG_JOY LOG_STREAM(info, log_joystick)
24 #define DBG_JOY LOG_STREAM(debug, log_joystick)
40 const std::vector<SDL_Joystick*>& joysticks
43 return SDL_JoystickGetAttached(joysticks[index]) == SDL_TRUE;
47 const std::vector<SDL_Joystick*>& joysticks
50 return SDL_JoystickName(joysticks[index]);
55 if(SDL_WasInit(SDL_INIT_JOYSTICK) == 0)
59 bool all_closed =
true;
61 for (
int i = 0;
i<joysticks;
i++) {
67 ERR_JOY <<
"Joystick" <<
i <<
" closing failed.";
81 LOG_JOY <<
"Joystick support is disabled.";
85 LOG_JOY <<
"Initializing joysticks...\n";
86 if(SDL_WasInit(SDL_INIT_JOYSTICK) == 0)
87 if(SDL_InitSubSystem(SDL_INIT_JOYSTICK) == -1)
92 int joysticks = SDL_NumJoysticks();
93 if (joysticks == 0)
return false;
95 SDL_JoystickEventState(SDL_ENABLE);
97 bool joystick_found =
false;
98 for (
int i = 0;
i<joysticks;
i++) {
104 joystick_found =
true;
113 ERR_JOY <<
"Couldn't open Joystick" <<
i;
116 return joystick_found;
127 std::pair<int, int>
values;
130 values =
get_axis_pair(mouse_joystick_x, mouse_xaxis, mouse_joystick_y, mouse_yaxis);
134 const int radius =
round_double(sqrt(pow(values.first, 2.0f) + pow(values.second, 2.0f)));
136 const double multiplier = 1.0 + thrust;
138 if (deadzone > radius)
139 return std::make_pair(0.0, 0.0);
147 return std::make_pair(
148 ((static_cast<double>(values.first)) / 32768.0) * multiplier
149 , ((static_cast<double>(values.second)) / 32768.0) * multiplier );
163 std::pair<int, int>
values;
166 values =
get_axis_pair(scroll_joystick_x, scroll_axis, scroll_joystick_y, scroll_yaxis);
170 const int radius =
round_double(sqrt(pow(values.first, 2.0f) + pow(values.second, 2.0f)));
172 const double multiplier = 1.0 + thrust;
174 if (deadzone > radius)
175 return std::make_pair(0.0, 0.0);
177 return std::make_pair(
178 ((static_cast<double>(values.first)) / 32768.0) * multiplier
179 , ((static_cast<double>(values.second)) / 32768.0) * multiplier );
189 const int value =
get_axis(thrust_joystick_x, thrust_axis_x) + 32768;
190 if (value < thrust_deadzone)
return 0.0;
191 return static_cast<double>(
value) / 65536.0;
201 const int value =
get_axis(thrustb_joystick, thrustb_axis) + 32768;
202 if (value < thrustb_deadzone)
return 0.0;
203 return static_cast<double>(
value) / 65536.0;
213 return get_polar_coordinates(cursor_joystick_xaxis, cursor_xaxis, cursor_joystick_yaxis, cursor_yaxis);
218 const std::pair<int, int>
values =
get_axis_pair(joystick_xaxis, xaxis, joystick_yaxis, yaxis);
219 const double radius = (sqrt(pow(values.first, 2.0f) + pow(values.second, 2.0f))) / 32768.0;
220 const double angle = (atan2(
221 static_cast<double>(values.second)
222 , static_cast<double>(values.first))) * 180.0 / pi<double>();
224 return std::make_pair(radius, angle);
229 if(!SDL_WasInit(SDL_INIT_JOYSTICK))
230 return std::make_pair(0, 0);
232 int x_axis = 0, y_axis = 0;
233 bool get_xaxis =
false, get_yaxis =
false;
236 if(SDL_JoystickNumAxes(
joysticks_[joystick_xaxis]) > xaxis)
240 if(SDL_JoystickNumAxes(
joysticks_[joystick_yaxis]) > yaxis)
246 if (get_xaxis) x_axis = SDL_JoystickGetAxis(
joysticks_[joystick_xaxis], xaxis);
247 if (get_yaxis) y_axis = SDL_JoystickGetAxis(
joysticks_[joystick_yaxis], yaxis);
249 return std::make_pair(x_axis, y_axis);
253 if(!SDL_WasInit(SDL_INIT_JOYSTICK))
257 if(SDL_JoystickNumAxes(
joysticks_[joystick_axis]) > axis)
258 return SDL_JoystickGetAxis(
joysticks_[joystick_axis], axis);
271 const std::pair<int, int>
values =
get_axis_pair(cursor_joystick_xaxis, cursor_xaxis, cursor_joystick_yaxis, cursor_yaxis);
273 const int x_axis = values.first;
274 const int y_axis = values.second;
307 const std::pair<int, int>
values =
get_axis_pair(cursor_joystick_xaxis, cursor_xaxis, cursor_joystick_yaxis, cursor_yaxis);
309 const int x_axis = values.first;
310 const int y_axis = values.second;
312 const int radius =
round_double(sqrt(pow(x_axis, 2.0
f) + pow(y_axis, 2.0
f)));
317 const int max = 100000;
319 const bool greater_deadzone = radius > deadzone;
320 const bool greater_threshold2 = radius > threshold;
322 if (!greater_deadzone) {
328 highlighted_hex =
get_next_hex(x_axis, y_axis, highlighted_hex);
330 if (!greater_threshold2) {
337 highlighted_hex =
get_next_hex(x_axis, y_axis, highlighted_hex);
374 const std::pair<int, int>
values =
get_axis_pair(cursor_joystick_xaxis, cursor_xaxis, cursor_joystick_yaxis, cursor_yaxis);
376 const int x_axis = values.first;
377 const int y_axis = values.second;
379 const double angle = (atan2(
380 static_cast<double>(y_axis)
381 , static_cast<double>(x_axis))) * 180.0 / pi<double>();
393 const double angle = (atan2(
394 static_cast<double>(y_axis)
395 , static_cast<double>(x_axis))) * 180.0 / pi<double>();
409 if (angle > 22.5 && angle < 67.5 )
412 if (angle > 67.5 && angle < 113.5)
415 if (angle > 113.5 && angle < 158.5)
418 if (angle > 158.5 || angle < -157.5)
int joystick_mouse_xaxis_num()
int joystick_thrustb_axis_num()
std::pair< int, int > get_axis_pair(int joystick_xaxis, int xaxis, int joystick_yaxis, int yaxis)
int joystick_mouse_yaxis_num()
static bool attached(const std::vector< SDL_Joystick * > &joysticks, const size_t index)
int joystick_num_mouse_xaxis()
int get_axis(int joystick_axis, int axis)
int joystick_thrusta_axis_num()
int joystick_num_cursor_yaxis()
bool update_highlighted_hex(map_location &highlighted_hex)
Used for absolute movement of the cursor.
int joystick_thrustb_deadzone()
int joystick_num_mouse_yaxis()
int joystick_num_thrustb_axis()
GLint GLint GLint GLint GLint GLint y
int joystick_num_scroll_xaxis()
int joystick_num_cursor_xaxis()
int round_double(double d)
std::pair< double, double > get_scroll_axis_pair()
static lg::log_domain log_joystick("joystick")
GLboolean GLenum GLenum GLvoid * values
int joystick_cursor_yaxis_num()
int joystick_thrusta_deadzone()
bool joystick_support_enabled()
int joystick_scroll_yaxis_num()
std::pair< double, double > get_polar_coordinates(int joystick_xaxis, int xaxis, int joystick_yaxis, int yaxis)
TODO fendrin.
GLsizei const GLfloat * value
double get_thrusta_axis()
TODO fendrin.
static const map_location & null_location()
int joystick_scroll_xaxis_num()
double get_angle()
TODO fendrin.
Encapsulates the map of the game.
int joystick_num_thrusta_axis()
const map_location get_direction(const map_location &loc, joystick_manager::DIRECTION direction)
std::vector< SDL_Joystick * > joysticks_
double get_thrustb_axis()
TODO fendrin.
GLint GLint GLint GLint GLint x
int joystick_cursor_xaxis_num()
int joystick_cursor_threshold()
Standard logging facilities (interface).
int joystick_scroll_deadzone()
static const char * name(const std::vector< SDL_Joystick * > &joysticks, const size_t index)
int joystick_mouse_deadzone()
int joystick_cursor_deadzone()
const map_location get_next_hex(int x_axis, int y_axis, map_location old_hex)
int joystick_num_scroll_yaxis()
std::pair< double, double > get_mouse_axis_pair()
TODO fendrin.
std::pair< double, double > get_cursor_polar_coordinates()
TODO fendrin.