Jo Engine  2020.06.22
Jo Sega Saturn Engine
input.h
Go to the documentation of this file.
1 /*
2 ** Jo Sega Saturn Engine
3 ** Copyright (c) 2012-2020, Johannes Fetz (johannesfetz@gmail.com)
4 ** All rights reserved.
5 **
6 ** Redistribution and use in source and binary forms, with or without
7 ** modification, are permitted provided that the following conditions are met:
8 ** * Redistributions of source code must retain the above copyright
9 ** notice, this list of conditions and the following disclaimer.
10 ** * Redistributions in binary form must reproduce the above copyright
11 ** notice, this list of conditions and the following disclaimer in the
12 ** documentation and/or other materials provided with the distribution.
13 ** * Neither the name of the Johannes Fetz nor the
14 ** names of its contributors may be used to endorse or promote products
15 ** derived from this software without specific prior written permission.
16 **
17 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 ** WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 ** DISCLAIMED. IN NO EVENT SHALL Johannes Fetz BE LIABLE FOR ANY
21 ** DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 ** (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 ** LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ** ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 ** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
36 #ifndef __JO_INPUT_H__
37 # define __JO_INPUT_H__
38 
39 # define JO_INPUT_MAX_DEVICE (12)
40 
41 #if JO_COMPILE_USING_SGL
42 extern PerDigital jo_inputs[JO_INPUT_MAX_DEVICE];
43 # define __JO_KEY_PRESSED(PORT, KEY) ((jo_inputs[PORT].data & KEY) == 0)
44 #else
45 # define __JO_KEY_PRESSED(PORT, KEY) ((jo_inputs[PORT].pressed & KEY) == KEY)
46 #endif
47 
48 #if !JO_COMPILE_USING_SGL
49 
53 typedef struct
54 {
55  unsigned char id;
56  unsigned short pressed;
57  unsigned short on_keydown;
58  unsigned short on_keyup;
59 } jo_input;
60 
64 extern jo_input jo_inputs[JO_INPUT_MAX_DEVICE];
65 
66 #endif
67 
70 #if JO_COMPILE_USING_SGL
71 typedef enum
72 {
73  JO_KEY_RIGHT = 1 << 15,
74  JO_KEY_LEFT = 1 << 14,
75  JO_KEY_DOWN = 1 << 13,
76  JO_KEY_UP = 1 << 12,
77  JO_KEY_START = 1 << 11,
78  JO_KEY_A = 1 << 10,
79  JO_KEY_B = 1 << 8,
80  JO_KEY_C = 1 << 9,
81  JO_KEY_X = 1 << 6,
82  JO_KEY_Y = 1 << 5,
83  JO_KEY_Z = 1 << 4,
84  JO_KEY_L = 1 << 3,
85  JO_KEY_R = 1 << 7,
86 
90  JO_MOUSE_RIGHT_BUTTON = 1 << 9
92 #else
93 typedef enum
94 {
95  JO_KEY_RIGHT = 1 << 7,
96  JO_KEY_LEFT = 1 << 6,
97  JO_KEY_DOWN = 1 << 5,
98  JO_KEY_UP = 1 << 4,
99  JO_KEY_START = 1 << 11,
100  JO_KEY_A = 1 << 10,
101  JO_KEY_B = 1 << 8,
102  JO_KEY_C = 1 << 9,
103  JO_KEY_X = 1 << 2,
104  JO_KEY_Y = 1 << 1,
105  JO_KEY_Z = 1 << 0,
106  JO_KEY_L = 1 << 15,
107  JO_KEY_R = 1 << 3,
108 
109  JO_MOUSE_START_BUTTON = 1 << 11,
110  JO_MOUSE_LEFT_BUTTON = 1 << 10,
111  JO_MOUSE_MIDDLE_BUTTON = 1 << 8,
112  JO_MOUSE_RIGHT_BUTTON = 1 << 9
114 #endif
115 
118 typedef enum
119 {
127 
133 
138 static __jo_force_inline bool jo_is_input_available(const int port)
139 {
140  return (jo_inputs[port].id != PER_ID_NotConnect);
141 }
142 
147 
153 static __jo_force_inline bool jo_is_input_key_pressed(const int port, const jo_gamepad_keys key)
154 {
155  return __JO_KEY_PRESSED(port, key);
156 }
157 
163 {
164  if (__JO_KEY_PRESSED(port, JO_KEY_UP))
165  {
166  if (__JO_KEY_PRESSED(port, JO_KEY_LEFT))
167  return (UP_LEFT);
168  if (__JO_KEY_PRESSED(port, JO_KEY_RIGHT))
169  return (UP_RIGHT);
170  return (UP);
171  }
172  if (__JO_KEY_PRESSED(port, JO_KEY_DOWN))
173  {
174  if (__JO_KEY_PRESSED(port, JO_KEY_LEFT))
175  return (DOWN_LEFT);
176  if (__JO_KEY_PRESSED(port, JO_KEY_RIGHT))
177  return (DOWN_RIGHT);
178  return (DOWN);
179  }
180  if (__JO_KEY_PRESSED(port, JO_KEY_LEFT))
181  return (LEFT);
182  if (__JO_KEY_PRESSED(port, JO_KEY_RIGHT))
183  return (RIGHT);
184  return (NONE);
185 }
186 
191 static __jo_force_inline unsigned short jo_get_raw_input_key_pressed(const int port)
192 {
193 #if JO_COMPILE_USING_SGL
194  return ((unsigned short)jo_inputs[port].data);
195 #else
196  return ((unsigned short)jo_inputs[port].pressed);
197 #endif
198 }
199 
205 static __jo_force_inline bool jo_is_input_key_down(const int port, const jo_gamepad_keys key)
206 {
207 #if JO_COMPILE_USING_SGL
208  return ((jo_inputs[port].push & key) == 0);
209 #else
210  return ((jo_inputs[port].on_keydown & key) == key);
211 #endif
212 }
213 
214 #if !JO_COMPILE_USING_SGL
215 
221 static __jo_force_inline bool jo_is_input_key_up(const int port, const jo_gamepad_keys key)
222 {
223  return ((jo_inputs[port].on_keyup & key) == key);
224 }
225 #endif
226 
231 static __jo_force_inline int jo_get_mouse_pos_x(const int port)
232 {
233  return (JO_DIV_BY_65536(jo_inputs[port].dummy2[0]));
234 }
235 
240 static __jo_force_inline int jo_get_mouse_pos_y(const int port)
241 {
242  return (JO_DIV_BY_65536((JO_MULT_BY_65536(jo_inputs[port].dummy2[0]))));
243 }
244 
245 /*
246 ** HELPER
247 */
248 
253 {
254  return jo_is_input_available(0);
255 }
256 
261 {
262  return jo_is_input_available(1);
263 }
264 
270 {
271  return (jo_is_input_key_pressed(0, key));
272 }
273 
279 {
280  return (jo_is_input_key_down(0, key));
281 }
282 
288 {
289  return (jo_is_input_key_pressed(1, key));
290 }
291 
297 {
298  return (jo_is_input_key_down(1, key));
299 }
300 
301 #endif /* !__JO_INPUT_H__ */
302 
303 /*
304 ** END OF FILE
305 */
JO_MOUSE_MIDDLE_BUTTON
@ JO_MOUSE_MIDDLE_BUTTON
Definition: input.h:89
JO_KEY_START
@ JO_KEY_START
Definition: input.h:77
jo_is_pad2_key_pressed
static __jo_force_inline bool jo_is_pad2_key_pressed(jo_gamepad_keys key)
Check if the key is pressed on the second gamepad.
Definition: input.h:287
RIGHT
@ RIGHT
Definition: types.h:286
JoRegularKeyboard
@ JoRegularKeyboard
Definition: input.h:125
__JO_KEY_PRESSED
#define __JO_KEY_PRESSED(PORT, KEY)
Definition: input.h:43
JO_MOUSE_LEFT_BUTTON
@ JO_MOUSE_LEFT_BUTTON
Definition: input.h:88
jo_is_input_available
static __jo_force_inline bool jo_is_input_available(const int port)
Check if the input is available for the given port.
Definition: input.h:138
JO_KEY_L
@ JO_KEY_L
Definition: input.h:84
UP_RIGHT
@ UP_RIGHT
Definition: types.h:290
JO_DIV_BY_65536
#define JO_DIV_BY_65536(X)
Devide a variable by 65536.
Definition: math.h:192
UP_LEFT
@ UP_LEFT
Definition: types.h:289
jo_get_input_type
jo_gamepad_type jo_get_input_type(const int port)
Get input type for the given port.
jo_get_input_count
int jo_get_input_count(void)
Get the number of input available.
DOWN_RIGHT
@ DOWN_RIGHT
Definition: types.h:292
jo_is_pad2_key_down
static __jo_force_inline bool jo_is_pad2_key_down(jo_gamepad_keys key)
Check if the key is down on the second gamepad.
Definition: input.h:296
__jo_force_inline
#define __jo_force_inline
force inline attribute (and prevent Doxygen prototype parsing bug)
Definition: conf.h:154
UP
@ UP
Definition: types.h:287
jo_get_raw_input_key_pressed
static __jo_force_inline unsigned short jo_get_raw_input_key_pressed(const int port)
Get raw input data for the given port.
Definition: input.h:191
JO_KEY_LEFT
@ JO_KEY_LEFT
Definition: input.h:74
JO_MULT_BY_65536
#define JO_MULT_BY_65536(X)
Multiply a variable by 65536.
Definition: math.h:138
jo_get_mouse_pos_x
static __jo_force_inline int jo_get_mouse_pos_x(const int port)
Get mouse X position.
Definition: input.h:231
jo_is_pad1_available
static __jo_force_inline bool jo_is_pad1_available(void)
Check if the first gamepad is plugged.
Definition: input.h:252
jo_is_input_key_down
static __jo_force_inline bool jo_is_input_key_down(const int port, const jo_gamepad_keys key)
Check if the key is down for the given port.
Definition: input.h:205
JO_INPUT_MAX_DEVICE
#define JO_INPUT_MAX_DEVICE
Definition: input.h:39
JO_KEY_X
@ JO_KEY_X
Definition: input.h:81
JoRegularGamepad
@ JoRegularGamepad
Definition: input.h:122
jo_gamepad_keys
jo_gamepad_keys
Input keys.
Definition: input.h:72
JO_KEY_B
@ JO_KEY_B
Definition: input.h:79
JO_MOUSE_START_BUTTON
@ JO_MOUSE_START_BUTTON
Definition: input.h:87
jo_gamepad_type
jo_gamepad_type
Input type.
Definition: input.h:119
JO_KEY_DOWN
@ JO_KEY_DOWN
Definition: input.h:75
NONE
@ NONE
Definition: types.h:284
jo_is_input_key_pressed
static __jo_force_inline bool jo_is_input_key_pressed(const int port, const jo_gamepad_keys key)
Check if the key is pressed for the given port.
Definition: input.h:153
jo_is_pad1_key_pressed
static __jo_force_inline bool jo_is_pad1_key_pressed(jo_gamepad_keys key)
Check if the key is pressed on the first gamepad.
Definition: input.h:269
JO_KEY_R
@ JO_KEY_R
Definition: input.h:85
jo_get_input_direction_pressed
static __jo_force_inline jo_8_directions jo_get_input_direction_pressed(const int port)
Get the direction pressed for the given port.
Definition: input.h:162
JO_MOUSE_RIGHT_BUTTON
@ JO_MOUSE_RIGHT_BUTTON
Definition: input.h:90
JoShuttleMouse
@ JoShuttleMouse
Definition: input.h:124
jo_inputs
PerDigital jo_inputs[(12)]
JO_KEY_Y
@ JO_KEY_Y
Definition: input.h:82
JO_KEY_C
@ JO_KEY_C
Definition: input.h:80
DOWN
@ DOWN
Definition: types.h:288
JoUnsupportedGamepad
@ JoUnsupportedGamepad
Definition: input.h:121
JoNotConnectedGamepad
@ JoNotConnectedGamepad
Definition: input.h:120
JO_KEY_A
@ JO_KEY_A
Definition: input.h:78
JO_KEY_UP
@ JO_KEY_UP
Definition: input.h:76
jo_8_directions
jo_8_directions
8 directions definition
Definition: types.h:283
jo_is_pad2_available
static __jo_force_inline bool jo_is_pad2_available(void)
Check if the second gamepad is plugged.
Definition: input.h:260
JO_KEY_RIGHT
@ JO_KEY_RIGHT
Definition: input.h:73
DOWN_LEFT
@ DOWN_LEFT
Definition: types.h:291
JoRegularMouse
@ JoRegularMouse
Definition: input.h:123
JO_KEY_Z
@ JO_KEY_Z
Definition: input.h:83
jo_is_pad1_key_down
static __jo_force_inline bool jo_is_pad1_key_down(jo_gamepad_keys key)
Check if the key is down on the first gamepad.
Definition: input.h:278
LEFT
@ LEFT
Definition: types.h:285
jo_get_mouse_pos_y
static __jo_force_inline int jo_get_mouse_pos_y(const int port)
Get mouse Y position.
Definition: input.h:240