00001
00002
00003 #include "InputSystem.h"
00004
00005 #include "SDL.h"
00006 #include "CoreEngine.h"
00007 #include "MathUtils.h"
00008
00009
00010 namespace pge {
00011
00012
00013
00014
00015
00016
00017
00018 InputSystem::InputSystem(void) {
00019
00020
00021 m_keyTurnLeft = SDLK_LEFT;
00022 m_keyTurnRight = SDLK_RIGHT;
00023 m_keySlideLeft = SDLK_a;
00024 m_keySlideRight = SDLK_d;
00025 m_keyForward = SDLK_w;
00026 m_keyBackward = SDLK_s;
00027 m_keyUp = SDLK_x;
00028 m_keyDown = SDLK_c;
00029 m_keyRun = SDLK_LSHIFT;
00030 m_keyJump = SDLK_SPACE;
00031 m_keyCrouch = SDLK_c;
00032 m_keyFire = SDLK_LCTRL;
00033
00034 m_turnLeft = false;
00035 m_turnRight = false;
00036 m_slideLeft = false;
00037 m_slideRight = false;
00038 m_forward = false;
00039 m_backward = false;
00040 m_up = false;
00041 m_down = false;
00042 m_run = false;
00043 m_jump = false;
00044 m_crouch = false;
00045 m_fire = false;
00046 }
00047
00048
00049
00050
00051
00052
00053
00054 InputSystem::~InputSystem(void) {
00055 }
00056
00057
00058
00059
00060
00061
00062
00063 InputSystem* InputSystem::getInstance() {
00064 static InputSystem instance;
00065 return &instance;
00066 }
00067
00068
00069
00070
00071
00072
00073
00074 void InputSystem::timer(unsigned int delay) {
00075
00076 if(m_forward) {
00077 CoreEngine::getInstance()->action(Forward, 0, 0);
00078 }
00079 if(m_backward) {
00080 CoreEngine::getInstance()->action(Backward, 0, 0);
00081 }
00082 if(m_turnLeft) {
00083 CoreEngine::getInstance()->action(TurnLeft, 0, 0);
00084 }
00085 if(m_turnRight) {
00086 CoreEngine::getInstance()->action(TurnRight, 0, 0);
00087 }
00088 if(m_slideLeft) {
00089 CoreEngine::getInstance()->action(SlideLeft, 0, 0);
00090 }
00091 if(m_slideRight) {
00092 CoreEngine::getInstance()->action(SlideRight, 0, 0);
00093 }
00094 if(m_up) {
00095 CoreEngine::getInstance()->action(Up, 0, 0);
00096 }
00097 if(m_down) {
00098 CoreEngine::getInstance()->action(Down, 0, 0);
00099 }
00100 if(m_run) {
00101 }
00102 if(m_jump) {
00103 }
00104 if(m_crouch) {
00105 }
00106 if(m_fire) {
00107 }
00108 }
00109
00110
00111
00112
00113
00114
00115
00116 void InputSystem::keyDown(int key) {
00117 if(key == m_keyTurnLeft) {
00118 m_turnLeft = true;
00119 }
00120 if(key == m_keyTurnRight) {
00121 m_turnRight = true;
00122 }
00123 if(key == m_keySlideLeft) {
00124 m_slideLeft = true;
00125 }
00126 if(key == m_keySlideRight) {
00127 m_slideRight = true;
00128 }
00129 if(key == m_keyForward) {
00130 m_forward = true;
00131 }
00132 if(key == m_keyBackward) {
00133 m_backward = true;
00134 }
00135 if(key == m_keyUp) {
00136 m_up = true;
00137 }
00138 if(key == m_keyDown) {
00139 m_down = true;
00140 }
00141 if(key == m_keyRun) {
00142 m_run = true;
00143 }
00144 if(key == m_keyJump) {
00145 m_jump = true;
00146 }
00147 if(key == m_keyCrouch) {
00148 m_crouch = true;
00149 }
00150
00151
00152 if(key == 27) {
00153 CoreEngine::getInstance()->shutdown();
00154 }
00155 }
00156
00157
00158
00159
00160
00161
00162
00163 void InputSystem::keyUp(int key) {
00164 if(key == m_keyTurnLeft) {
00165 m_turnLeft = false;
00166 }
00167 if(key == m_keyTurnRight) {
00168 m_turnRight = false;
00169 }
00170 if(key == m_keySlideLeft) {
00171 m_slideLeft = false;
00172 }
00173 if(key == m_keySlideRight) {
00174 m_slideRight = false;
00175 }
00176 if(key == m_keyForward) {
00177 m_forward = false;
00178 }
00179 if(key == m_keyBackward) {
00180 m_backward = false;
00181 }
00182 if(key == m_keyUp) {
00183 m_up = false;
00184 }
00185 if(key == m_keyDown) {
00186 m_down = false;
00187 }
00188 if(key == m_keyRun) {
00189 m_run = false;
00190 }
00191 if(key == m_keyJump) {
00192 m_jump = false;
00193 }
00194 if(key == m_keyCrouch) {
00195 m_crouch = false;
00196 }
00197 }
00198
00199
00200
00201
00202
00203
00204
00205 void InputSystem::mouseMotion(unsigned int delay, int x, int y) {
00206
00207
00208
00209 int halfX = CoreEngine::getInstance()->getResolutionWidth() / 2;
00210 int halfY = CoreEngine::getInstance()->getResolutionHeight() / 2;
00211 int dx = mathutils::absi(halfX - x);
00212 int dy = mathutils::absi(halfY - y);
00213
00214
00215 if(x < halfX) {
00216 CoreEngine::getInstance()->action(TurnLeft, dx, delay);
00217 } else if(x > halfX) {
00218 CoreEngine::getInstance()->action(TurnRight, dx, delay);
00219 }
00220
00221 if(y < halfY) {
00222 CoreEngine::getInstance()->action(TurnUp, dy, delay);
00223 } else if(y > halfY) {
00224 CoreEngine::getInstance()->action(TurnDown, dy, delay);
00225 }
00226 }
00227
00228
00229
00230
00231
00232
00233
00234 void InputSystem::mousePress(int button, int x, int y) {
00235
00236 m_fire = true;
00237 }
00238
00239
00240
00241
00242
00243
00244
00245 void InputSystem::mouseRelease(int button, int x, int y) {
00246
00247 m_fire = false;
00248 }
00249 };