00001 #include <assert.h>
00002 #include "input.h"
00003
00004
00005 Input::Input()
00006 {
00007 init();
00008 }
00009
00010 Input::~Input()
00011 {
00012 }
00013
00014 void Input::init()
00015 {
00016 int i;
00017
00018 for(i = 0; i < SDLK_LAST; i++)
00019 Map(i, UNKNOWN);
00020
00021 for(i = 0; i < KLAST; i++)
00022 presses[i] = false;
00023
00024 Map(SDLK_F12, CANCEL);
00025 Map(SDLK_ESCAPE, FAST_QUIT);
00026 Map(SDLK_f, FULLSCREEN_TOGGLE);
00027 Map(SDLK_w, WALK_NORTH);
00028 Map(SDLK_a, WALK_WEST);
00029 Map(SDLK_s, WALK_SOUTH);
00030 Map(SDLK_d, WALK_EAST);
00031 Map(SDLK_m, LOCKMOUSE_TOGGLE);
00032 Map(SDLK_u, ADD_UFO);
00033 Map(SDLK_b, MUSIC_TOGGLE);
00034 Map(SDLK_r, RAPID_FIRE);
00035 Map(SDLK_p, SWITCH_MUSIC);
00036 Map(SDLK_COMMA, ROT_UP);
00037 Map(SDLK_PERIOD, ROT_DOWN);
00038
00039 mouse_x = mouse_y = 0;
00040 mouse_press = 0;
00041 }
00042
00043
00044 int Input::GetMouse (int &x, int &y)
00045 {
00046 x = mouse_x;
00047 y = mouse_y;
00048 return mouse_press;
00049 }
00050
00051 bool Input::ResetMouse (unsigned int mask)
00052 {
00053 mouse_x = mouse_y = 0;
00054 mouse_press &= ~mask;
00055 return true;
00056 }
00057
00058 bool Input::ResetMouseButton (unsigned int mask)
00059 {
00060 mouse_press &= ~mask;
00061 return true;
00062 }
00063
00064 bool Input::IsPressed(Key k)
00065 {
00066 return presses[k];
00067 }
00068
00069 bool Input::ResetKey(Key k)
00070 {
00071 bool old = presses[k];
00072 presses[k] = false;
00073 return old;
00074 }
00075
00076 void Input::HandleKeyDown(int sdlkeysym)
00077 {
00078 presses[mapping[sdlkeysym]] = true;
00079 }
00080
00081 void Input::HandleKeyUp(int sdlkeysym)
00082 {
00083 presses[mapping[sdlkeysym]] = false;
00084 }
00085
00086 void Input::HandleMouseMotion(int x, int y)
00087 {
00088 mouse_x = x;
00089 mouse_y = y;
00090 }
00091
00092 void Input::HandleMouseButtonDown(int button)
00093 {
00094 mouse_press |= SDL_BUTTON(button);
00095 }
00096
00097 void Input::HandleMouseButtonUp(int button)
00098 {
00099 mouse_press &= ~SDL_BUTTON(button);
00100 }
00101
00102 bool Input::Update(Event &e)
00103 {
00104 switch(e.type) {
00105 case SDL_KEYDOWN:
00106 HandleKeyDown(e.key.keysym.sym);
00107 break;
00108 case SDL_KEYUP:
00109 HandleKeyUp(e.key.keysym.sym);
00110 break;
00111 case SDL_MOUSEMOTION:
00112 HandleMouseMotion(e.motion.x, e.motion.y);
00113 break;
00114 case SDL_MOUSEBUTTONDOWN:
00115 HandleMouseButtonDown(e.button.button);
00116 break;
00117 case SDL_MOUSEBUTTONUP:
00118 HandleMouseButtonUp(e.button.button);
00119 break;
00120 default:
00121 return false;
00122 }
00123 return true;
00124 }
00125
00126 void Input::Map(int sdlkey, Key inputkey)
00127 {
00128 assert(sdlkey < SDLK_LAST);
00129 mapping[sdlkey] = inputkey;
00130 }
00131