Starshatter_Open
Open source Starshatter engine
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Keyboard.cpp
Go to the documentation of this file.
1 /* Project nGenEx
2  Destroyer Studios LLC
3  Copyright © 1997-2004. All Rights Reserved.
4 
5  SUBSYSTEM: nGenEx.lib
6  FILE: Keyboard.cpp
7  AUTHOR: John DiCamillo
8 
9 
10  OVERVIEW
11  ========
12  Keyboard Input class
13 */
14 
15 #include "MemDebug.h"
16 #include "Keyboard.h"
17 #include "Game.h"
18 
19 // +--------------------------------------------------------------------+
20 
21 static Keyboard* instance = 0;
24 
26 : x(0), y(0), z(0), p(0), r(0), w(0), c(0), p1(0), r1(0), w1(0), t(0)
27 {
28  instance = this;
29  sensitivity = 25;
30  dead_zone = 100;
31 
32  for (int i = 0; i < MotionController::MaxActions; i++)
33  action[i] = 0;
34 
35  memset(map, 0, sizeof(map));
36  memset(alt, 0, sizeof(alt));
37 
38  map[KEY_PLUS_X] = 'R';
39  map[KEY_MINUS_X] = 'E';
40  map[KEY_PLUS_Y] = VK_HOME;
41  map[KEY_MINUS_Y] = VK_END;
42  map[KEY_PLUS_Z] = VK_PRIOR; // page up
43  map[KEY_MINUS_Z] = VK_NEXT; // page down
44 
45  map[KEY_PITCH_UP] = VK_DOWN;
46  map[KEY_PITCH_DOWN] = VK_UP;
47  map[KEY_YAW_LEFT] = VK_LEFT;
48  map[KEY_YAW_RIGHT] = VK_RIGHT;
49  map[KEY_ROLL_ENABLE] = 0; // used to be VK_CONTROL;
50 }
51 
53 {
54  instance = 0;
55 }
56 
57 Keyboard*
59 {
60  return instance;
61 }
62 
63 // +--------------------------------------------------------------------+
64 
65 void
66 Keyboard::MapKeys(KeyMapEntry* mapping, int nkeys)
67 {
68  for (int i = 0; i < nkeys; i++) {
69  KeyMapEntry k = mapping[i];
70 
71  if (k.act >= KEY_MAP_FIRST && k.act <= KEY_MAP_LAST) {
72  if (k.key == 0 || k.key > VK_MBUTTON && k.key < KEY_JOY_1) {
73  map[k.act] = k.key;
74  alt[k.act] = k.alt;
75  }
76  }
77  }
78 }
79 
80 // +--------------------------------------------------------------------+
81 
82 bool Keyboard::KeyDown(int key)
83 {
84  if (key) {
85  short k = GetAsyncKeyState(key);
86  return (k<0)||(k&1);
87  }
88 
89  return 0;
90 }
91 
92 // +--------------------------------------------------------------------+
93 
94 bool Keyboard::KeyDownMap(int key)
95 {
96  if (key >= KEY_MAP_FIRST && key <= KEY_MAP_LAST && map[key]) {
97  short k = GetAsyncKeyState(map[key]);
98  short a = -1;
99 
100  if (alt[key] > 0 && alt[key] < KEY_JOY_1) {
101  a = GetAsyncKeyState(alt[key]);
102  }
103  else {
104  a = !GetAsyncKeyState(VK_SHIFT) &&
105  !GetAsyncKeyState(VK_MENU);
106  }
107 
108  return ((k<0)||(k&1)) && ((a<0)||(a&1));
109  }
110 
111  return 0;
112 }
113 
114 // +--------------------------------------------------------------------+
115 
116 void
118 {
119  for (int i = 0; i < 255; i++)
120  GetAsyncKeyState(i);
121 }
122 
123 // +--------------------------------------------------------------------+
124 
125 static inline double sqr(double a) { return a; } //*a; }
126 
127 void
129 {
130  t = x = y = z = p = r = w = c = 0;
131 
132  for (int i = 0; i < MotionController::MaxActions; i++)
133  action[i] = 0;
134 
135  int speed = 10;
136 
137  // lateral translations:
138  if (KeyDownMap(KEY_PLUS_Y)) y = 1;
139  else if (KeyDownMap(KEY_MINUS_Y)) y = -1;
140 
141  if (KeyDownMap(KEY_PLUS_Z)) z = 1;
142  else if (KeyDownMap(KEY_MINUS_Z)) z = -1;
143 
144  if (KeyDownMap(KEY_MINUS_X)) x = -1;
145  else if (KeyDownMap(KEY_PLUS_X)) x = 1;
146 
147  const double steps=10;
148 
149  // if roll and yaw are swapped --------------------------
150  if (swapped) {
151  // yaw:
152  if (KeyDownMap(KEY_ROLL_LEFT)) { if (w1<steps) w1+=1; w = -sqr(w1/steps); }
153  else if (KeyDownMap(KEY_ROLL_RIGHT)) { if (w1<steps) w1+=1; w = sqr(w1/steps); }
154 
155  // another way to yaw:
157  if (KeyDownMap(KEY_YAW_LEFT)) { if (w1<steps) w1+=1; w = -sqr(w1/steps); }
158  else if (KeyDownMap(KEY_YAW_RIGHT)) { if (w1<steps) w1+=1; w = sqr(w1/steps); }
159  else w1 = 0;
160  }
161 
162  // roll:
163  else {
164  if (KeyDownMap(KEY_YAW_LEFT)) { if (r1<steps) r1+=1; r = sqr(r1/steps); }
165  else if (KeyDownMap(KEY_YAW_RIGHT)) { if (r1<steps) r1+=1; r = -sqr(r1/steps); }
166  else r1 = 0;
167  }
168  }
169 
170  // else roll and yaw are NOT swapped ---------------------
171  else {
172  // roll:
173  if (KeyDownMap(KEY_ROLL_LEFT)) { if (r1<steps) r1+=1; r = sqr(r1/steps); }
174  else if (KeyDownMap(KEY_ROLL_RIGHT)) { if (r1<steps) r1+=1; r = -sqr(r1/steps); }
175 
176  // another way to roll:
178  if (KeyDownMap(KEY_YAW_LEFT)) { if (r1<steps) r1+=1; r = sqr(r1/steps); }
179  else if (KeyDownMap(KEY_YAW_RIGHT)) { if (r1<steps) r1+=1; r = -sqr(r1/steps); }
180  else r1 = 0;
181  }
182 
183  // yaw left-right
184  else {
185  if (KeyDownMap(KEY_YAW_LEFT)) { if (w1<steps) w1+=1; w = -sqr(w1/steps); }
186  else if (KeyDownMap(KEY_YAW_RIGHT)) { if (w1<steps) w1+=1; w = sqr(w1/steps); }
187  else w1 = 0;
188  }
189  }
190 
191  // if pitch is inverted ----------------------------------
192  if (inverted) {
193  if (KeyDownMap(KEY_PITCH_DOWN)) { if (p1<steps) p1+=1; p = -sqr(p1/steps); }
194  else if (KeyDownMap(KEY_PITCH_UP)) { if (p1<steps) p1+=1; p = sqr(p1/steps); }
195  else p1 = 0;
196  }
197 
198  // else pitch is NOT inverted ----------------------------
199  else {
200  if (KeyDownMap(KEY_PITCH_UP)) { if (p1<steps) p1+=1; p = -sqr(p1/steps); }
201  else if (KeyDownMap(KEY_PITCH_DOWN)) { if (p1<steps) p1+=1; p = sqr(p1/steps); }
202  else p1 = 0;
203  }
204 
205  if (KeyDownMap(KEY_CENTER)) c = 1;
206 
207  // actions
208  if (KeyDownMap(KEY_ACTION_0)) action[0] = 1;
209  if (KeyDownMap(KEY_ACTION_1)) action[1] = 1;
210  if (KeyDownMap(KEY_ACTION_2)) action[2] = 1;
211  if (KeyDownMap(KEY_ACTION_3)) action[3] = 1;
212 }
213 
214 // +--------------------------------------------------------------------+
215 
216 
217