00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #include "Core/precomp.h"
00016
00017 #include <iostream>
00018 #include <windows.h>
00019 #include "input_joystick.h"
00020
00021
00022
00023
00024
00025 CL_Joystick_Win32::CL_Joystick_Win32(int _joystick_id)
00026 {
00027 joystick_id = _joystick_id;
00028
00029 MMRESULT err=joyGetDevCaps(joystick_id, &joycaps, sizeof(joycaps));
00030 if (err != JOYERR_NOERROR)
00031 {
00032 std::cout << "ClanLib: Failed to initialize joystick #" << joystick_id+1 << std::endl;
00033 return;
00034 }
00035 }
00036
00037 CL_Joystick_Win32::~CL_Joystick_Win32()
00038 {
00039 }
00040
00041 char *CL_Joystick_Win32::get_name() const
00042 {
00043
00044 return (char *) joycaps.szPname;
00045 }
00046
00047 int CL_Joystick_Win32::get_num_buttons() const
00048 {
00049 return joycaps.wMaxButtons;
00050 }
00051
00052 CL_InputButton *CL_Joystick_Win32::get_button(int button_num)
00053 {
00054 return &buttons[button_num];
00055 }
00056
00057 int CL_Joystick_Win32::get_num_axes() const
00058 {
00059 return joycaps.wMaxAxes;
00060 }
00061
00062 CL_InputAxis *CL_Joystick_Win32::get_axis(int axis_num)
00063 {
00064 return &axes[axis_num];
00065 }
00066
00067 int CL_Joystick_Win32::get_num_hats() const
00068 {
00069 return 1;
00070 }
00071
00072 CL_InputHat *CL_Joystick_Win32::get_hat(int )
00073 {
00074 return &hat;
00075 }
00076
00077 int CL_Joystick_Win32::get_num_cursors() const
00078 {
00079 return 0;
00080 }
00081
00082 CL_InputCursor *CL_Joystick_Win32::get_cursor(int )
00083 {
00084 return NULL;
00085 }
00086
00087 void CL_Joystick_Win32::keep_alive()
00088 {
00089 JOYINFOEX joyinfo;
00090 joyinfo.dwSize = sizeof(joyinfo);
00091 joyinfo.dwFlags = JOY_RETURNALL;
00092
00093 MMRESULT err = joyGetPosEx(joystick_id, &joyinfo);
00094 if (err != JOYERR_NOERROR)
00095 {
00096 std::cout << "ClanLib: Joystick polling failed" << std::endl;
00097 }
00098
00099 for (unsigned int button=0; button<joycaps.wMaxButtons; button++)
00100 {
00101 buttons[button].set_state(((joyinfo.dwButtons>>button)&1) ? true : false);
00102 }
00103
00104 axes[0].set_pos(calc_pos(joyinfo.dwXpos, joycaps.wXmin, joycaps.wXmax));
00105 axes[1].set_pos(calc_pos(joyinfo.dwYpos, joycaps.wYmin, joycaps.wYmax));
00106
00107 if (joycaps.wCaps&JOYCAPS_HASZ) axes[2].set_pos(calc_pos(joyinfo.dwZpos, joycaps.wZmin, joycaps.wZmax));
00108 if (joycaps.wCaps&JOYCAPS_HASR) axes[3].set_pos(calc_pos(joyinfo.dwRpos, joycaps.wRmin, joycaps.wRmax));
00109 if (joycaps.wCaps&JOYCAPS_HASU) axes[4].set_pos(calc_pos(joyinfo.dwUpos, joycaps.wUmin, joycaps.wUmax));
00110 if (joycaps.wCaps&JOYCAPS_HASV) axes[5].set_pos(calc_pos(joyinfo.dwVpos, joycaps.wVmin, joycaps.wVmax));
00111
00112 if (joyinfo.dwPOV==JOY_POVCENTERED) hat.set_dir(-1);
00113 else hat.set_dir(float(joyinfo.dwPOV)/100);
00114 }
00115
00116 float CL_Joystick_Win32::calc_pos(int pos, int min, int max)
00117 {
00118 pos-=min;
00119 max-=min;
00120
00121 int logical_pos=pos*13/max-6;
00122
00123 if (logical_pos>0) logical_pos--;
00124 if (logical_pos<0) logical_pos++;
00125
00126 if (logical_pos<-5) logical_pos=-5;
00127 if (logical_pos>5) logical_pos=5;
00128
00129 return logical_pos/(float) 5;
00130 }