00001 00005 /* Copyright © 2009 James Legg. 00006 This program is free software: you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation, either version 3 of the License, or 00009 (at your option) any later version. 00010 */ 00011 #include "InputDeviceKeyboard.h" 00012 #include "InputReport.h" 00013 #include "SDL.h" 00014 #include <Debug.h> 00015 #include "GameObjects/Car.h" 00016 00017 namespace Engine 00018 { 00019 00020 InputDeviceKeyboard::InputDeviceKeyboard() 00021 : accelerate_down(false), // assume keys are not held during startup. 00022 break_down(false), 00023 steer_left_down(false), 00024 steer_right_down(false), 00025 slide_left_down(false), 00026 slide_right_down(false) 00027 { 00028 00029 } 00030 00031 InputDeviceKeyboard::~InputDeviceKeyboard() 00032 { 00033 00034 } 00035 00036 int InputDeviceKeyboard::get_acceleration() 00037 { 00038 if (accelerate_down && !break_down) return 32767; 00039 else if (break_down && !accelerate_down) return -32767; 00040 else return 0; 00041 } 00042 00043 int InputDeviceKeyboard::get_slide() 00044 { 00045 if (slide_left_down && !slide_right_down) return -32767; 00046 else if (slide_right_down && !slide_left_down) return 32767; 00047 else return 0; 00048 } 00049 00050 int InputDeviceKeyboard::get_steering() 00051 { 00052 if (steer_left_down && !steer_right_down) return -32767; 00053 else if (steer_right_down && !steer_left_down) return 32767; 00054 else return 0; 00055 } 00056 00057 void InputDeviceKeyboard::poll() 00058 { 00059 SDL_Event event; 00060 SDL_PumpEvents(); 00061 while (SDL_PeepEvents 00062 (&event, 1, SDL_GETEVENT, 00063 SDL_EVENTMASK(SDL_KEYUP) | SDL_EVENTMASK(SDL_KEYDOWN) 00064 ) > 0 00065 ) 00066 { 00067 bool new_state = event.type == SDL_KEYDOWN; 00068 InputReport::ReportType report_type = InputReport::RT_MAX; 00069 int value(0); 00070 switch (event.key.keysym.sym) 00071 { 00072 case SDLK_q: 00073 report_type = InputReport::RT_CHANGE_SLIDE; 00074 slide_left_down = new_state; 00075 value = get_slide(); 00076 break; 00077 case SDLK_e: 00078 report_type = InputReport::RT_CHANGE_SLIDE; 00079 slide_right_down = new_state; 00080 value = get_slide(); 00081 break; 00082 case SDLK_w: 00083 report_type = InputReport::RT_CHANGE_ACCEL; 00084 accelerate_down = new_state; 00085 value = get_acceleration(); 00086 break; 00087 case SDLK_s: 00088 report_type = InputReport::RT_CHANGE_ACCEL; 00089 break_down = new_state; 00090 value = get_acceleration(); 00091 break; 00092 case SDLK_a: 00093 report_type = InputReport::RT_CHANGE_STEERING; 00094 steer_left_down = new_state; 00095 value = get_steering(); 00096 break; 00097 case SDLK_d: 00098 report_type = InputReport::RT_CHANGE_STEERING; 00099 steer_right_down = new_state; 00100 value = get_steering(); 00101 break; 00102 case SDLK_UP: 00103 report_type = InputReport::RT_MENU_UP; 00104 if (!new_state) return; // no menu events for letting a key go. 00105 break; 00106 case SDLK_DOWN: 00107 report_type = InputReport::RT_MENU_DOWN; 00108 if (!new_state) return; // no menu events for letting a key go. 00109 break; 00110 case SDLK_LEFT: 00111 report_type = InputReport::RT_MENU_LEFT; 00112 if (!new_state) return; // no menu events for letting a key go. 00113 break; 00114 case SDLK_RIGHT: 00115 report_type = InputReport::RT_MENU_RIGHT; 00116 if (!new_state) return; // no menu events for letting a key go. 00117 break; 00118 case SDLK_RETURN: 00119 case SDLK_SPACE: 00120 report_type = InputReport::RT_MENU_SELECT; 00121 if (!new_state) return; // no menu events for letting a key go. 00122 break; 00123 case SDLK_ESCAPE: 00124 case SDLK_BACKSPACE: 00125 report_type = InputReport::RT_MENU_BACK; 00126 if (!new_state) return; // no menu events for letting a key go. 00127 break; 00128 default: 00129 // unhandled key. Don't report any events. 00130 return; 00131 break; 00132 } 00133 // Update the scene with a report for the keypress. 00134 report(report_type, value); 00135 } 00136 } 00137 00138 }
Generated at Mon Sep 6 00:41:12 2010 by Doxygen version 1.4.7 for Racer version svn335.