00001 #include "mouse.h"
00002
00003 void MOUSE::Update(CAMERA & cam, int screenw, int screenh, float timefactor, float fps)
00004 {
00005 int mousePos_x,mousePos_y;
00006 int middleX = screenw >> 1;
00007 int middleY = screenh >> 1;
00008
00009
00010 SDL_GetMouseState(&mousePos_x,&mousePos_y);
00011
00012
00013
00014
00015
00016 SDL_WarpMouse(middleX, middleY);
00017
00018 int angleY = 0;
00019 int angleZ = 0;
00020
00021
00022 angleY = ( (middleX - mousePos_x) );
00023 angleZ = ( (middleY - mousePos_y) );
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 UpdateSteering(angleY, angleZ);
00039
00040
00041 }
00042
00043 MOUSE::MOUSE()
00044 {
00045 mousezoom = 0.5;
00046 steer_x = 0;
00047 steer_y = 0;
00048
00049
00050
00051 }
00052
00053 void MOUSE::UpdateCamera(CAMERA & cam, int x, int y, float timefactor, float fps)
00054 {
00055 static float currentRotX = 0.0f, currentRotY = 0.0f;
00056
00057 float angleY = (float)x / 500.0f;
00058 float angleZ = (float)y / 500.0f;
00059
00060
00061
00062 currentRotX -= angleZ;
00063 currentRotY -= angleY;
00064
00065 double temp_radius = sqrt(cam.position.x*cam.position.x+cam.position.z*cam.position.z+(cam.position.y+EARTH_RADIUS)*(cam.position.y+EARTH_RADIUS));
00066 double temp_long = atan2((double)(cam.position.y+EARTH_RADIUS),(double)cam.position.x);
00067 double temp_lat = acos(cam.position.z/temp_radius);
00068 temp_lat -= 1.570796f;
00069 temp_long -= 1.570796f;
00070
00071 VERTEX tpos = cam.position;
00072 tpos.y += EARTH_RADIUS;
00073 tpos = tpos.normalize();
00074
00075 float wrapat = 3.141592f*2.0f;
00076 if (currentRotY > wrapat)
00077 currentRotY -= wrapat;
00078 if (currentRotY < -wrapat)
00079 currentRotY += wrapat;
00080
00081
00082 if(currentRotX > 1.4f)
00083 currentRotX = 1.4f;
00084
00085 else if(currentRotX < -1.4f)
00086 currentRotX = -1.4f;
00087
00088
00089
00090
00091
00092
00093
00094
00095 VERTEX upvector, rightvector;
00096 upvector.x = 0; upvector.y = 1; upvector.z = 0;
00097 rightvector.x = 1; rightvector.y = 0; rightvector.z = 0;
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112 QUATERNION curveoffset;
00113 curveoffset.SetEuler(temp_lat, temp_long, temp_lat);
00114 VERTEX up, posx, posz;
00115 up.y = 1.0f;
00116 posx.x = 1.0f;
00117 posz.z = 1.0f;
00118 VERTEX newup = curveoffset.RotateVec(up);
00119 VERTEX newx = curveoffset.RotateVec(posx);
00120 VERTEX newz = curveoffset.RotateVec(posz);
00121
00122
00123
00124
00125 QUATERNION rightangle;
00126 rightangle.SetAxisAngle(1.570796f, 0,0,1);
00127 newx = rightangle.RotateVec(tpos);
00128
00129
00130
00131 QUATERNION cor, qx, qy, qz;
00132
00133 cor.SetAxisAngle(temp_long, 0, 0, 1);
00134
00135 qx.SetAxisAngle(currentRotX, 1, 0, 0);
00136
00137 qy.SetAxisAngle(currentRotY, -tpos.x, tpos.y, -tpos.z);
00138
00139
00140
00141
00142
00143
00144
00145
00146 viewdir = cor*qx*qy*qz;
00147
00148
00149
00150
00151 if(SDL_GetMouseState(NULL, NULL)&SDL_BUTTON(1))
00152 {
00153 mousezoom += ZOOMSPEED * timefactor/fps;
00154 if (mousezoom > 1.0f)
00155 mousezoom = 1.0f;
00156 }
00157
00158 if(SDL_GetMouseState(NULL, NULL)&SDL_BUTTON(3))
00159 {
00160 mousezoom -= ZOOMSPEED * timefactor/fps;
00161 if (mousezoom < 0.0f)
00162 mousezoom = 0.0f;
00163 }
00164 }
00165
00166 void MOUSE::UpdateSteering(int x, int y)
00167 {
00168 float fx = ((float) x*mouse_sensitivity_x) / 1500.0;
00169 float fy = ((float) y*mouse_sensitivity_y) / 250.0;
00170
00171 steer_x += fx;
00172 steer_y += fy;
00173
00174 if (steer_x > 1.0)
00175 steer_x = 1.0;
00176 if (steer_y > 1.0)
00177 steer_y = 1.0;
00178
00179 if (steer_x < -1.0)
00180 steer_x = -1.0;
00181 if (steer_y < -1.0)
00182 steer_y = -1.0;
00183
00184
00185 }
00186
00187 bool MOUSE::GetMouseControls(float * x, float * y, bool * click_l, bool * click_r)
00188 {
00189 *x = steer_x;
00190 *y = steer_y;
00191
00192 if (steer_y >= 0 && steer_y < deadzone_y)
00193 *y = 0;
00194 else if (steer_y < 0 && steer_y > -deadzone_y)
00195 *y = 0;
00196 else
00197 {
00198 if (steer_y < 0)
00199 *y = (steer_y + deadzone_y)*(1.0/(1.0-deadzone_y));
00200 else
00201 *y = (steer_y - deadzone_y)*(1.0/(1.0-deadzone_y));
00202 }
00203
00204 upbounce = (SDL_GetMouseState(NULL, NULL)&SDL_BUTTON(5));
00205 downbounce = (SDL_GetMouseState(NULL, NULL)&SDL_BUTTON(4));
00206 lbounce = (SDL_GetMouseState(NULL, NULL)&SDL_BUTTON(3));
00207 mbounce = (SDL_GetMouseState(NULL, NULL)&SDL_BUTTON(2));
00208 rbounce = (SDL_GetMouseState(NULL, NULL)&SDL_BUTTON(1));
00209
00210 *click_l = (lbounce && !lastl);
00211 *click_r = (rbounce && !lastr);
00212
00213
00214
00215 lastup = upbounce;
00216 lastdown = downbounce;
00217 lastl = lbounce;
00218 lastm = mbounce;
00219 lastr = rbounce;
00220
00221 return true;
00222 }
00223
00224 bool MOUSE::IsPressed( int which_btn )
00225 {
00226 return ( SDL_GetMouseState( NULL, NULL ) & SDL_BUTTON( which_btn ) );
00227 }
00228
00229 void MOUSE::GetMousePos( int &xpos, int &ypos )
00230 {
00231 int mouse_x, mouse_y;
00232
00233
00234 SDL_GetMouseState(&mouse_x, &mouse_y);
00235
00236 xpos = mouse_x;
00237 ypos = mouse_y;
00238 }
00239
00240 void MOUSE::GetMouseButtons( bool &btn_l, bool &btn_m, bool &btn_r, bool &scroll_up, bool &scroll_down )
00241 {
00242 upbounce = ( SDL_GetMouseState( NULL, NULL ) & SDL_BUTTON( 5 ) );
00243 downbounce = ( SDL_GetMouseState( NULL, NULL ) & SDL_BUTTON( 4 ) );
00244 rbounce = ( SDL_GetMouseState( NULL, NULL ) & SDL_BUTTON( 3 ) );
00245 mbounce = ( SDL_GetMouseState( NULL, NULL ) & SDL_BUTTON( 2 ) );
00246 lbounce = ( SDL_GetMouseState( NULL, NULL ) & SDL_BUTTON( 1 ) );
00247
00248 scroll_up = upbounce;
00249 scroll_down = downbounce;
00250 btn_r = rbounce;
00251 btn_m = mbounce;
00252 btn_l = lbounce;
00253 }
00254
00255 void MOUSE::UpdateSettings()
00256 {
00257 settings.Get( "mouse.xsens", mouse_sensitivity_x );
00258 settings.Get( "mouse.ysens", mouse_sensitivity_y );
00259 settings.Get( "mouse.ydead", deadzone_y );
00260 }