Arch Game Engine  0.2
input.cpp
1 #include "input.h"
2 
3 Input::Input() {
4  reset();
5  left = 10;
6  right = 11;
7  up = 12;
8  down = 13;
9  q = 14;
10  e = 15;
11  r = 16;
12  t = 17;
13  y = 18;
14  u = 19;
15  i = 20;
16  o = 21;
17  p = 22;
18  a = 23;
19  s = 24;
20  d = 25;
21  f = 26;
22  g = 27;
23  h = 28;
24  j = 29;
25  k = 30;
26  l = 31;
27  z = 32;
28  x = 33;
29  c = 34;
30  v = 35;
31  b = 36;
32  n = 37;
33  m = 39;
34  lshift = 40;
35  rshift = 41;
36  shift = 42;
37  mouseleft = 43;
38  quit = 44;
39  esc = 45;
40  w = 46;
41  mouseright = 47;
42  mousemiddle = 48;
43  mouseup = 49;
44  mousedown = 50;
45 }
46 Input::~Input() {}
47 
49  //bool done = reset();
50  SDL_Event event;
51  while(SDL_PollEvent(&event)) {
52  if(event.type == SDL_QUIT) {keys[quit] = true; cout << "Quiting\n";}
53  if(event.type == SDL_MOUSEBUTTONDOWN) {
54  if((event.button.button == SDL_BUTTON_LEFT) && !keys[mouseleft]) {keys[mouseleft] = true; cout << "Left mouse button\n";}
55  if((event.button.button == SDL_BUTTON_RIGHT) && !keys[mouseright]) {keys[mouseright] = true; cout << "Right mouse button\n";}
56  if((event.button.button == SDL_BUTTON_MIDDLE) && !keys[mousemiddle]) {keys[mousemiddle] = true; cout << "Middle mouse button\n";}
57  }
58  if(event.type == SDL_MOUSEBUTTONUP) {
59  if((event.button.button == SDL_BUTTON_LEFT) && keys[mouseleft]) {keys[mouseleft] = false; cout << "Left mouse button up\n";}
60  if((event.button.button == SDL_BUTTON_RIGHT) && keys[mouseright]) {keys[mouseright] = false; cout << "Right mouse button up\n";}
61  if((event.button.button == SDL_BUTTON_MIDDLE) && keys[mousemiddle]) {keys[mousemiddle] = false; cout << "Middle mouse button up\n";}
62  }
63  if(event.type == SDL_KEYDOWN) {
64  if((event.key.keysym.sym == SDLK_LEFT) && !keys[left]) {keys[left] = true; cout << "left\n";}
65  if((event.key.keysym.sym == SDLK_RIGHT) && !keys[right]) {keys[right] = true; cout << "right\n";}
66  if((event.key.keysym.sym == SDLK_DOWN) && !keys[down]) {keys[down] = true; cout << "down\n";}
67  if((event.key.keysym.sym == SDLK_UP) && !keys[up]) {keys[up] = true; cout << "up\n";}
68  if((event.key.keysym.sym == SDLK_a) && !keys[a]) {keys[a] = true; cout << "a\n";}
69  if((event.key.keysym.sym == SDLK_b) && !keys[b]) {keys[b] = true; cout << "b\n";}
70  if((event.key.keysym.sym == SDLK_c) && !keys[c]) {keys[c] = true; cout << "c\n";}
71  if((event.key.keysym.sym == SDLK_d) && !keys[d]) {keys[d] = true; cout << "d\n";}
72  if((event.key.keysym.sym == SDLK_e) && !keys[e]) {keys[e] = true; cout << "e\n";}
73  if((event.key.keysym.sym == SDLK_f) && !keys[f]) {keys[f] = true; cout << "f\n";}
74  if((event.key.keysym.sym == SDLK_g) && !keys[g]) {keys[g] = true; cout << "g\n";}
75  if((event.key.keysym.sym == SDLK_h) && !keys[h]) {keys[h] = true; cout << "h\n";}
76  if((event.key.keysym.sym == SDLK_i) && !keys[i]) {keys[i] = true; cout << "i\n";}
77  if((event.key.keysym.sym == SDLK_j) && !keys[j]) {keys[j] = true; cout << "j\n";}
78  if((event.key.keysym.sym == SDLK_k) && !keys[k]) {keys[k] = true; cout << "k\n";}
79  if((event.key.keysym.sym == SDLK_l) && !keys[l]) {keys[l] = true; cout << "l\n";}
80  if((event.key.keysym.sym == SDLK_m) && !keys[m]) {keys[m] = true; cout << "m\n";}
81  if((event.key.keysym.sym == SDLK_n) && !keys[n]) {keys[n] = true; cout << "n\n";}
82  if((event.key.keysym.sym == SDLK_o) && !keys[o]) {keys[o] = true; cout << "o\n";}
83  if((event.key.keysym.sym == SDLK_p) && !keys[p]) {keys[p] = true; cout << "p\n";}
84  if((event.key.keysym.sym == SDLK_q) && !keys[q]) {keys[q] = true; cout << "q\n";}
85  if((event.key.keysym.sym == SDLK_r) && !keys[r]) {keys[r] = true; cout << "r\n";}
86  if((event.key.keysym.sym == SDLK_s) && !keys[s]) {keys[s] = true; cout << "s\n";}
87  if((event.key.keysym.sym == SDLK_t) && !keys[t]) {keys[t] = true; cout << "t\n";}
88  if((event.key.keysym.sym == SDLK_u) && !keys[u]) {keys[u] = true; cout << "u\n";}
89  if((event.key.keysym.sym == SDLK_v) && !keys[v]) {keys[v] = true; cout << "v\n";}
90  if((event.key.keysym.sym == SDLK_w) && !keys[w]) {keys[w] = true; cout << "w\n";}
91  if((event.key.keysym.sym == SDLK_x) && !keys[x]) {keys[x] = true; cout << "x\n";}
92  if((event.key.keysym.sym == SDLK_y) && !keys[y]) {keys[y] = true; cout << "y\n";}
93  if((event.key.keysym.sym == SDLK_z) && !keys[z]) {keys[z] = true; cout << "z\n";}
94  if((event.key.keysym.sym == SDLK_ESCAPE) && !keys[esc]) {keys[esc] = true; cout << "Esc\n";}
95  if((event.key.keysym.sym == SDLK_1) && !keys[1]) {keys[1] = true; cout << "1\n";}
96  if((event.key.keysym.sym == SDLK_2) && !keys[2]) {keys[2] = true; cout << "2\n";}
97  if((event.key.keysym.sym == SDLK_3) && !keys[3]) {keys[3] = true; cout << "3\n";}
98  if((event.key.keysym.sym == SDLK_4) && !keys[4]) {keys[4] = true; cout << "4\n";}
99  if((event.key.keysym.sym == SDLK_5) && !keys[5]) {keys[5] = true; cout << "5\n";}
100  if((event.key.keysym.sym == SDLK_6) && !keys[6]) {keys[6] = true; cout << "6\n";}
101  if((event.key.keysym.sym == SDLK_7) && !keys[7]) {keys[7] = true; cout << "7\n";}
102  if((event.key.keysym.sym == SDLK_8) && !keys[8]) {keys[8] = true; cout << "8\n";}
103  if((event.key.keysym.sym == SDLK_9) && !keys[9]) {keys[9] = true; cout << "9\n";}
104  if((event.key.keysym.sym == SDLK_0) && !keys[0]) {keys[0] = true; cout << "0\n";}
105  if((event.key.keysym.sym == SDLK_LSHIFT) && !keys[lshift]) {keys[lshift] = true; cout << "left shift\n";}
106  if((event.key.keysym.sym == SDLK_RSHIFT) && !keys[rshift]) {keys[rshift] = true; cout << "right shift\n";}
107  }
108  if(event.type == SDL_KEYUP) {
109  if((event.key.keysym.sym == SDLK_LEFT) && keys[left]) {keys[left] = false; cout << "left up\n";}
110  if((event.key.keysym.sym == SDLK_RIGHT) && keys[right]) {keys[right] = false; cout << "right up\n";}
111  if((event.key.keysym.sym == SDLK_DOWN) && keys[down]) {keys[down] = false; cout << "down up\n";}
112  if((event.key.keysym.sym == SDLK_UP) && keys[up]) {keys[up] = false; cout << "up up\n";}
113  if((event.key.keysym.sym == SDLK_a) && keys[a]) {keys[a] = false; cout << "a up\n";}
114  if((event.key.keysym.sym == SDLK_b) && keys[b]) {keys[b] = false; cout << "b up\n";}
115  if((event.key.keysym.sym == SDLK_c) && keys[c]) {keys[c] = false; cout << "c up\n";}
116  if((event.key.keysym.sym == SDLK_d) && keys[d]) {keys[d] = false; cout << "d up\n";}
117  if((event.key.keysym.sym == SDLK_e) && keys[e]) {keys[e] = false; cout << "e up\n";}
118  if((event.key.keysym.sym == SDLK_f) && keys[f]) {keys[f] = false; cout << "f up\n";}
119  if((event.key.keysym.sym == SDLK_g) && keys[g]) {keys[g] = false; cout << "g up\n";}
120  if((event.key.keysym.sym == SDLK_h) && keys[h]) {keys[h] = false; cout << "h up\n";}
121  if((event.key.keysym.sym == SDLK_i) && keys[i]) {keys[i] = false; cout << "i up\n";}
122  if((event.key.keysym.sym == SDLK_j) && keys[j]) {keys[j] = false; cout << "j up\n";}
123  if((event.key.keysym.sym == SDLK_k) && keys[k]) {keys[k] = false; cout << "k up\n";}
124  if((event.key.keysym.sym == SDLK_l) && keys[l]) {keys[l] = false; cout << "l up\n";}
125  if((event.key.keysym.sym == SDLK_m) && keys[m]) {keys[m] = false; cout << "m up\n";}
126  if((event.key.keysym.sym == SDLK_n) && keys[n]) {keys[n] = false; cout << "n up\n";}
127  if((event.key.keysym.sym == SDLK_o) && keys[o]) {keys[o] = false; cout << "o up\n";}
128  if((event.key.keysym.sym == SDLK_p) && keys[p]) {keys[p] = false; cout << "p up\n";}
129  if((event.key.keysym.sym == SDLK_q) && keys[q]) {keys[q] = false; cout << "q up\n";}
130  if((event.key.keysym.sym == SDLK_r) && keys[r]) {keys[r] = false; cout << "r up\n";}
131  if((event.key.keysym.sym == SDLK_s) && keys[s]) {keys[s] = false; cout << "s up\n";}
132  if((event.key.keysym.sym == SDLK_t) && keys[t]) {keys[t] = false; cout << "t up\n";}
133  if((event.key.keysym.sym == SDLK_u) && keys[u]) {keys[u] = false; cout << "u up\n";}
134  if((event.key.keysym.sym == SDLK_v) && keys[v]) {keys[v] = false; cout << "v up\n";}
135  if((event.key.keysym.sym == SDLK_w) && keys[w]) {keys[w] = false; cout << "w up\n";}
136  if((event.key.keysym.sym == SDLK_x) && keys[x]) {keys[x] = false; cout << "x up\n";}
137  if((event.key.keysym.sym == SDLK_y) && keys[y]) {keys[y] = false; cout << "y up\n";}
138  if((event.key.keysym.sym == SDLK_z) && keys[z]) {keys[z] = false; cout << "z up\n";}
139  if((event.key.keysym.sym == SDLK_ESCAPE) && keys[esc]) {keys[esc] = false; cout << "Esc up\n";}
140  if((event.key.keysym.sym == SDLK_1) && keys[1]) {keys[1] = false; cout << "1 up\n";}
141  if((event.key.keysym.sym == SDLK_2) && keys[2]) {keys[2] = false; cout << "2 up\n";}
142  if((event.key.keysym.sym == SDLK_3) && keys[3]) {keys[3] = false; cout << "3 up\n";}
143  if((event.key.keysym.sym == SDLK_4) && keys[4]) {keys[4] = false; cout << "4 up\n";}
144  if((event.key.keysym.sym == SDLK_5) && keys[5]) {keys[5] = false; cout << "5 up\n";}
145  if((event.key.keysym.sym == SDLK_6) && keys[6]) {keys[6] = false; cout << "6 up\n";}
146  if((event.key.keysym.sym == SDLK_7) && keys[7]) {keys[7] = false; cout << "7 up\n";}
147  if((event.key.keysym.sym == SDLK_8) && keys[8]) {keys[8] = false; cout << "8 up\n";}
148  if((event.key.keysym.sym == SDLK_9) && keys[9]) {keys[9] = false; cout << "9 up\n";}
149  if((event.key.keysym.sym == SDLK_0) && keys[0]) {keys[0] = false; cout << "0 up\n";}
150  if((event.key.keysym.sym == SDLK_LSHIFT) && keys[lshift]) {keys[lshift] = false; cout << "left shift up\n";}
151  if((event.key.keysym.sym == SDLK_RSHIFT) && keys[rshift]) {keys[rshift] = false; cout << "right shift up\n";}
152  }
153 /*
154  switch(event.type) {
155  case SDL_QUIT:
156  keys[quit] = true; cout << "\n"; break; break;
157  case SDL_KEYDOWN:
158  switch(event.key.keysym.sym) {
159  case SDLK_LEFT: keys[left] = true; cout << "left\n"; break;
160  case SDLK_RIGHT: keys[right] = true; cout << "right\n"; break;
161  case SDLK_DOWN: keys[down] = true; cout << "down\n"; break;
162  case SDLK_a: keys[a] = true; cout << "a\n"; break;
163  case SDLK_b: keys[b] = true; cout << "b\n"; break;
164  case SDLK_c: keys[c] = true; cout << "c\n"; break;
165  case SDLK_d: keys[d] = true; cout << "d\n"; break;
166  case SDLK_e: keys[e] = true; cout << "e\n"; break;
167  case SDLK_f: keys[f] = true; cout << "f\n"; break;
168  case SDLK_g: keys[g] = true; cout << "g\n"; break;
169  case SDLK_h: keys[h] = true; cout << "h\n"; break;
170  case SDLK_i: keys[i] = true; cout << "i\n"; break;
171  case SDLK_j: keys[j] = true; cout << "j\n"; break;
172  case SDLK_k: keys[k] = true; cout << "k\n"; break;
173  case SDLK_l: keys[l] = true; cout << "l\n"; break;
174  case SDLK_m: keys[m] = true; cout << "m\n"; break;
175  case SDLK_n: keys[n] = true; cout << "n\n"; break;
176  case SDLK_o: keys[o] = true; cout << "o\n"; break;
177  case SDLK_p: keys[p] = true; cout << "p\n"; break;
178  case SDLK_q: keys[q] = true; cout << "q\n"; break;
179  case SDLK_r: keys[r] = true; cout << "r\n"; break;
180  case SDLK_s: keys[s] = true; cout << "s\n"; break;
181  case SDLK_t: keys[t] = true; cout << "t\n"; break;
182  case SDLK_u: keys[u] = true; cout << "u\n"; break;
183  case SDLK_v: keys[v] = true; cout << "v\n"; break;
184  case SDLK_w: keys[w] = true; cout << "w\n"; break;
185  case SDLK_x: keys[x] = true; cout << "x\n"; break;
186  case SDLK_y: keys[y] = true; cout << "y\n"; break;
187  case SDLK_z: keys[z] = true; cout << "z\n"; break;
188  case SDLK_ESCAPE: keys[esc] = true; cout << "esc\n"; break;
189  case SDLK_1: keys[1] = true; cout << "1\n"; break;
190  case SDLK_2: keys[2] = true; cout << "2\n"; break;
191  case SDLK_3: keys[3] = true; cout << "3\n"; break;
192  case SDLK_4: keys[4] = true; cout << "4\n"; break;
193  case SDLK_5: keys[5] = true; cout << "5\n"; break;
194  case SDLK_6: keys[6] = true; cout << "6\n"; break;
195  case SDLK_7: keys[7] = true; cout << "7\n"; break;
196  case SDLK_8: keys[8] = true; cout << "8\n"; break;
197  case SDLK_9: keys[9] = true; cout << "9\n"; break;
198  case SDLK_0: keys[0] = true; cout << "0\n"; break;
199  case SDLK_LSHIFT: keys[lshift] = true; cout << "\n"; break; keys[shift] = true; cout << "lshift\n"; break;
200  case SDLK_RSHIFT: keys[rshift] = true; cout << "\n"; break; keys[shift] = true; cout << "rshift\n"; break;
201  }
202  break;
203  case SDL_MOUSEBUTTONDOWN:
204  switch (event.button.button) {
205  //case SDL_BUTTON_WHEELUP: keys[mouseup] = true; cout << "\n"; break;
206  //case SDL_BUTTON_WHEELDOWN: keys[mousedown] = true; cout << "\n"; break;
207  case SDL_BUTTON_LEFT: keys[mouseleft] = true; cout << "\n"; break;
208  case SDL_BUTTON_RIGHT: keys[mouseright] = true; cout << "\n"; break;
209  case SDL_BUTTON_MIDDLE: keys[mousemiddle] = true; cout << "\n"; break;
210  }
211  }
212 */
213  SDL_GetMouseState(&mousex, &mousey);
214  }
215 }
216 
217 bool Input::checkKey(int k) {
218  return keys[k];
219 }
220 
221 
222 bool Input::reset() {
223  //NEED TO LOOP THROUGH KEYS[] AND SET THEM ALL TO false
224  //THE LOOP BELOW CAUSES THE GAME TO CRASH
225  keys[44] = 0;
226  keys[45] = 0;
227  for(int i; i<sizeof(keys); i++) {
228  keys[i] = 0;
229  }
230  return true;
231 }
int esc
Log ID for esc.
Definition: input.h:55
int l
Log ID for l.
Definition: input.h:43
int g
Log ID for g.
Definition: input.h:39
int y
Log ID for y.
Definition: input.h:30
int m
Log ID for m.
Definition: input.h:50
int c
Log ID for c.
Definition: input.h:46
int f
Log ID for f.
Definition: input.h:38
bool checkKey(int k)
Check if a key has been pressed using a given key from this class. Ex: Input i; i.checkKey(i.up);.
Definition: input.cpp:217
int mousemiddle
Log ID for middle mouse click.
Definition: input.h:57
int mouseright
Log ID for right mouse click.
Definition: input.h:58
int x
Log ID for x.
Definition: input.h:45
int j
Log ID for j.
Definition: input.h:41
int up
Log ID for up.
Definition: input.h:23
int lshift
Log ID for left shift.
Definition: input.h:51
int shift
Shift ID for shift.
Definition: input.h:53
int h
Log ID for h.
Definition: input.h:40
int e
Log ID for e.
Definition: input.h:27
int i
Log ID for i.
Definition: input.h:32
int d
Log ID for d.
Definition: input.h:37
int mousedown
Log ID for scroll down on mouse wheel.
Definition: input.h:60
int p
Log ID for p.
Definition: input.h:34
int right
Log ID for right.
Definition: input.h:22
int r
Log ID for r.
Definition: input.h:28
int z
Log ID for z.
Definition: input.h:44
int left
Log ID for left.
Definition: input.h:21
int mouseleft
Log ID for left mouse click.
Definition: input.h:56
int v
Log ID for v.
Definition: input.h:47
bool reset()
Reset all pressed keystrokes and other inputs to false. Automatically down at the beginning of each l...
Definition: input.cpp:222
int a
Log ID for a.
Definition: input.h:35
int w
Log ID for w.
Definition: input.h:26
int b
Log ID for b.
Definition: input.h:48
int n
Log ID for n.
Definition: input.h:49
int u
Log ID for u.
Definition: input.h:31
int o
Log ID for o.
Definition: input.h:33
int down
Log ID for down.
Definition: input.h:24
int quit
Log ID for quit.
Definition: input.h:54
int q
Log ID for q.
Definition: input.h:25
int k
Log ID for k.
Definition: input.h:42
void logPress()
Log all current keys and buttons being pressed.
Definition: input.cpp:48
int rshift
Log ID for right shift.
Definition: input.h:52
int s
Log ID for s.
Definition: input.h:36
int mouseup
Log ID for scroll up on mouse wheel.
Definition: input.h:59
int t
Log ID for t.
Definition: input.h:29