]> git.localhorst.tv Git - l2e.git/blob - src/app/Input.h
4df4685e0c986b3dd7d7b3b229b4174820dc49c5
[l2e.git] / src / app / Input.h
1 /*
2  * Input.h
3  *
4  *  Created on: Aug 6, 2012
5  *      Author: holy
6  */
7
8 #ifndef APP_INPUT_H_
9 #define APP_INPUT_H_
10
11 #include <SDL.h>
12 #include <map>
13
14 namespace app {
15
16 /// Maps SDL key events to virtual buttons.
17 /// Records the state and whether it was pressed/released in the current
18 /// iteration.
19 /// Multiple buttons can be passed by ORing them bitwise.
20 /// The MapKey(SDLKey, Button) function introduces a mapping for the given key
21 /// to the given virtual button. Each key can be assigned to one button only,
22 /// but there is no limit (well, except for memory, I guess) on how many keys
23 /// may map to the same button.
24 /// Each iteration should first call ResetInteractiveState() to drop the just
25 /// pressed/released information and then pass each SDL_KeyboardEvent to
26 /// HandleKeyboardEvent(const SDL_KeyboardEvent &).
27 /// The four DEBUG_? buttons do not map to any real SNES/Lufia 2 button and may
28 /// be used for debugging or non-gameplay-related input.
29 class Input {
30
31 public:
32         enum Button {
33                 PAD_UP = 0x0001,
34                 PAD_RIGHT = 0x0002,
35                 PAD_DOWN = 0x0004,
36                 PAD_LEFT = 0x0008,
37                 ACTION_A = 0x0010,
38                 ACTION_B = 0x0020,
39                 ACTION_X = 0x0040,
40                 ACTION_Y = 0x0080,
41                 START = 0x0100,
42                 SELECT = 0x0200,
43                 SHOULDER_RIGHT = 0x0400,
44                 SHOULDER_LEFT = 0x0800,
45
46                 DEBUG_1 = 0x1000,
47                 DEBUG_2 = 0x2000,
48                 DEBUG_3 = 0x4000,
49                 DEBUG_4 = 0x8000,
50         };
51
52 public:
53         Input();
54
55 public:
56         bool IsDown(int b) const {
57                 return down & b;
58         }
59         bool JustPressed(int b) const {
60                 return pressed & b;
61         }
62         bool JustReleased(int b) const {
63                 return released & b;
64         }
65
66 public:
67         void ResetInteractiveState() {
68                 pressed = 0;
69                 released = 0;
70         }
71         void HandleKeyboardEvent(const SDL_KeyboardEvent &);
72
73 public:
74         void MapKey(SDLKey k, Button b) {
75                 mapping[k] = b;
76         }
77
78 private:
79         std::map<SDLKey, Button> mapping;
80         Uint16 down;
81         Uint16 pressed;
82         Uint16 released;
83
84 };
85
86 }
87
88 #endif /* APP_INPUT_H_ */