1 
2 /**
3  *
4  * @file controls.h
5  *
6  * Part of the OpenJazz project
7  *
8  * @par History:
9  * - 23rd August 2005: Created OpenJazz.h
10  * - 13th July 2009: Created controls.h from parts of OpenJazz.h
11  *
12  * @par Licence:
13  * Copyright (c) 2005-2012 Alister Thomson
14  *
15  * OpenJazz is distributed under the terms of
16  * the GNU General Public License, version 2.0
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  *
22  */
23 
24 #ifndef _INPUT_H
25 #define _INPUT_H
26 
27 
28 #include "loop.h"
29 #include "OpenJazz.h"
30 
31 #include <SDL.h>
32 
33 
34 // Constants
35 
36 // Indexes for the keys / buttons / axes player controls arrays
37 #define C_UP      0
38 #define C_DOWN    1
39 #define C_LEFT    2
40 #define C_RIGHT   3
41 #define C_JUMP    4
42 #define C_SWIM    5
43 #define C_FIRE    6
44 #define C_CHANGE  7 /* Change weapon */
45 #define C_ENTER   8
46 #define C_ESCAPE  9
47 #define C_STATS  10
48 #define C_PAUSE  11
49 #define C_YES    12
50 #define C_NO     13
51 // Size of those arrays
52 #define CONTROLS 14
53 
54 // Time interval
55 #define T_KEY   200
56 
57 
58 // Class
59 
60 /// Keeps track of all control input
61 class Controls {
62 
63 	private:
64 		struct {
65 
66 			int  key; ///< Keyboard key
67 			bool pressed; ///< Whether or not the key is pressed
68 
69 		} keys[CONTROLS];
70 
71 		struct {
72 
73 			int  button; ///< Joystick button
74 			bool pressed; ///< Whether or not the button is pressed
75 
76 		} buttons[CONTROLS];
77 
78 		struct {
79 
80 			int  axis; ///< Joystick axis
81 			bool direction; ///< Axis direction
82 			bool pressed; ///< Whether or not the axis is pressed in the given direction
83 
84 		} axes[CONTROLS];
85 
86 		struct {
87 
88 			unsigned int time; ///< The time from which the control will respond to being pressed
89 			bool         state; ///< Whether or not the control is being used
90 
91 		} controls[CONTROLS];
92 
93 		int          cursorX; ///< X-coordinate of the cursor
94 		int          cursorY; ///< Y-coordinate of the cursor
95 		bool         cursorPressed; ///< Whether or not the cursor is being pressed
96 		bool         cursorReleased; ///< Whether or not the cursor has been released
97 		int          wheelUp; ///< How many times the wheel has been scrolled upwards
98 		int          wheelDown; ///< How many times the wheel has been scrolled downwards
99 
100 		void setCursor (int x, int y, bool pressed);
101 
102 	public:
103 		Controls ();
104 
105 		void setKey           (int control, int key);
106 		void setButton        (int control, int button);
107 		void setAxis          (int control, int axis, bool direction);
108 		int  getKey           (int control);
109 		int  getButton        (int control);
110 		int  getAxis          (int control);
111 		int  getAxisDirection (int control);
112 
113 		int  update           (SDL_Event *event, LoopType type);
114 		void loop             ();
115 
116 		bool getState          (int control);
117 		bool release           (int control);
118 		bool getCursor         (int& x, int& y);
119 		bool wasCursorReleased ();
120 
121 };
122 
123 
124 // Variable
125 
126 EXTERN Controls controls;
127 
128 #endif
129