Jo Engine 2026.11.6
Jo Sega Saturn Engine
Loading...
Searching...
No Matches
input.h
Go to the documentation of this file.
1/*
2** Jo Sega Saturn Engine
3** Copyright (c) 2012-2026, 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*/
34
35#ifndef __JO_INPUT_H__
36# define __JO_INPUT_H__
37
38# define JO_INPUT_MAX_DEVICE (12)
39
41# define JO_INPUT_PORT1_FIRST_DEVICE (0)
42# define JO_INPUT_PORT2_FIRST_DEVICE (6)
43
45# define __JO_KEY_PRESSED(PORT, KEY) ((jo_inputs[PORT].data & KEY) == 0)
46
47
50typedef enum
51{
52 JO_KEY_RIGHT = 1 << 15,
53 JO_KEY_LEFT = 1 << 14,
54 JO_KEY_DOWN = 1 << 13,
55 JO_KEY_UP = 1 << 12,
56 JO_KEY_START = 1 << 11,
57 JO_KEY_A = 1 << 10,
58 JO_KEY_B = 1 << 8,
59 JO_KEY_C = 1 << 9,
60 JO_KEY_X = 1 << 6,
61 JO_KEY_Y = 1 << 5,
62 JO_KEY_Z = 1 << 4,
63 JO_KEY_L = 1 << 3,
64 JO_KEY_R = 1 << 7,
65
70
72 JO_GUN_TRIGGER = 1 << 10,
76
104
107typedef enum
108{
116
122
127static __jo_force_inline bool jo_is_input_analog(const int port)
128{
129#ifdef JO_DEBUG
130 if (__JO_INDEX_IS_WRONG(port, JO_INPUT_MAX_DEVICE, "port"))
131 return (false);
132#endif
133 // TODO: Add other analog devices (wheel, mission stick)
134 return (jo_inputs[port].id == JO_PERIPHERAL_ID_NIGHTS_PAD);
135}
136
141static __jo_force_inline bool jo_is_input_available(const int port)
142{
143#ifdef JO_DEBUG
144 if (__JO_INDEX_IS_WRONG(port, JO_INPUT_MAX_DEVICE, "port"))
145 return (false);
146#endif
147 return (jo_inputs[port].id != JO_PERIPHERAL_ID_NOT_CONNECTED);
148}
149
154
160int jo_get_player_input_port(const int player);
161
167unsigned char jo_get_input_axis(const int port, const jo_input_axis axis);
168
177int jo_get_input_axis_centered(const int port, const jo_input_axis axis);
178
184static __jo_force_inline bool jo_is_input_key_pressed(const int port, const jo_gamepad_keys key)
185{
186#ifdef JO_DEBUG
187 if (__JO_INDEX_IS_WRONG(port, JO_INPUT_MAX_DEVICE, "port"))
188 return (false);
189#endif
190 return __JO_KEY_PRESSED(port, key);
191}
192
198{
199#ifdef JO_DEBUG
200 if (__JO_INDEX_IS_WRONG(port, JO_INPUT_MAX_DEVICE, "port"))
201 return (NONE);
202#endif
203 if (__JO_KEY_PRESSED(port, JO_KEY_UP))
204 {
205 if (__JO_KEY_PRESSED(port, JO_KEY_LEFT))
206 return (UP_LEFT);
208 return (UP_RIGHT);
209 return (UP);
210 }
211 if (__JO_KEY_PRESSED(port, JO_KEY_DOWN))
212 {
213 if (__JO_KEY_PRESSED(port, JO_KEY_LEFT))
214 return (DOWN_LEFT);
216 return (DOWN_RIGHT);
217 return (DOWN);
218 }
219 if (__JO_KEY_PRESSED(port, JO_KEY_LEFT))
220 return (LEFT);
222 return (RIGHT);
223 return (NONE);
224}
225
230static __jo_force_inline unsigned short jo_get_raw_input_key_pressed(const int port)
231{
232#ifdef JO_DEBUG
233 if (__JO_INDEX_IS_WRONG(port, JO_INPUT_MAX_DEVICE, "port"))
234 return (0);
235#endif
236 return ((unsigned short)jo_inputs[port].data);
237}
238
244static __jo_force_inline bool jo_is_input_key_down(const int port, const jo_gamepad_keys key)
245{
246#ifdef JO_DEBUG
247 if (__JO_INDEX_IS_WRONG(port, JO_INPUT_MAX_DEVICE, "port"))
248 return (false);
249#endif
250 return ((jo_inputs[port].push & key) == 0);
251}
252
253
254
259static __jo_force_inline int jo_get_mouse_pos_x(const int port)
260{
261#ifdef JO_DEBUG
262 if (__JO_INDEX_IS_WRONG(port, JO_INPUT_MAX_DEVICE, "port"))
263 return (0);
264#endif
265 return (((jo_peripheral_pointer *)&jo_inputs[port])->x);
266}
267
272static __jo_force_inline int jo_get_mouse_pos_y(const int port)
273{
274#ifdef JO_DEBUG
275 if (__JO_INDEX_IS_WRONG(port, JO_INPUT_MAX_DEVICE, "port"))
276 return (0);
277#endif
278 return (((jo_peripheral_pointer *)&jo_inputs[port])->y);
279}
280
281
282/*
283** LIGHT GUN
284*/
285
310bool jo_get_input_gun_position(const int port, int * const x, int * const y);
311
324bool jo_calibrate_input_gun(const int port, const int target_x, const int target_y);
325
326/*
327** HELPER
328*/
329
337
342{
343 return (jo_get_player_input_port(1) >= 0);
344}
345
354
363
369{
370 int port;
371
372 port = jo_get_player_input_port(1);
373 return (port >= 0 && jo_is_input_key_pressed(port, key));
374}
375
381{
382 int port;
383
384 port = jo_get_player_input_port(1);
385 return (port >= 0 && jo_is_input_key_down(port, key));
386}
387
388#endif /* !__JO_INPUT_H__ */
389
390/*
391** END OF FILE
392*/
#define __JO_INDEX_IS_WRONG(INDEX, COUNT, WHAT)
Check an index given to a function of the engine (Internal usage, JO_DEBUG).
Definition core.h:55
void * data
Definition entity.h:19
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:350
bool jo_calibrate_input_gun(const int port, const int target_x, const int target_y)
Calibrate a light gun: the player shoots at a target, the position read is moved onto the target.
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:184
bool jo_get_input_gun_position(const int port, int *const x, int *const y)
Get the position on the screen where the light gun points.
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:230
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:141
static __jo_force_inline bool jo_is_pad1_available(void)
Check if the first gamepad is plugged.
Definition input.h:333
#define JO_INPUT_PORT1_FIRST_DEVICE
Index in jo_inputs of the first peripheral of each port (6 peripherals per port, see jo_get_inputs_vb...
Definition input.h:41
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:380
int jo_get_input_axis_centered(const int port, const jo_input_axis axis)
Get a centered analog axis (a stick or a wheel): 0 in the middle.
static __jo_force_inline bool jo_is_pad2_available(void)
Check if the second gamepad is plugged (the second device: on the port 2, or on a multitap of the por...
Definition input.h:341
int jo_get_player_input_port(const int player)
The port (the index of jo_inputs) of the gamepad of a player: the devices of the port 1 are 0 to 5 (a...
jo_gamepad_keys
Input keys.
Definition input.h:51
@ JO_KEY_RIGHT
Definition input.h:52
@ JO_MOUSE_LEFT_BUTTON
Definition input.h:67
@ JO_KEY_L
Definition input.h:63
@ JO_GUN_START_BUTTON
Start button of a light gun (the same bit as JO_KEY_START).
Definition input.h:74
@ JO_KEY_R
Definition input.h:64
@ JO_KEY_B
Definition input.h:58
@ JO_KEY_LEFT
Definition input.h:53
@ JO_GUN_TRIGGER
Trigger of a light gun (the same bit as JO_KEY_A: a gun works with the code written for a pad).
Definition input.h:72
@ JO_KEY_X
Definition input.h:60
@ JO_KEY_START
Definition input.h:56
@ JO_KEY_Y
Definition input.h:61
@ JO_MOUSE_MIDDLE_BUTTON
Definition input.h:68
@ JO_KEY_Z
Definition input.h:62
@ JO_KEY_UP
Definition input.h:55
@ JO_KEY_C
Definition input.h:59
@ JO_MOUSE_RIGHT_BUTTON
Definition input.h:69
@ JO_KEY_DOWN
Definition input.h:54
@ JO_KEY_A
Definition input.h:57
@ JO_MOUSE_START_BUTTON
Definition input.h:66
jo_gamepad_type jo_get_input_type(const int port)
Get input type for the given port.
static __jo_force_inline int jo_get_mouse_pos_y(const int port)
Get mouse Y position.
Definition input.h:272
static __jo_force_inline int jo_get_mouse_pos_x(const int port)
Get mouse X position.
Definition input.h:259
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:359
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:197
#define __JO_KEY_PRESSED(PORT, KEY)
Definition input.h:45
jo_peripheral jo_inputs[(12)]
unsigned char jo_get_input_axis(const int port, const jo_input_axis axis)
Get analog axis value.
jo_gamepad_type
Input type.
Definition input.h:80
@ JoMissionStick
Mission stick, Axis1 and Axis2 are the analog stick, Axis3 is throttle.
Definition input.h:94
@ JoShuttleMouse
Definition input.h:85
@ JoLightGun
Light gun (Virtua Gun, Stunner): JO_GUN_TRIGGER, JO_GUN_START_BUTTON and jo_get_input_gun_position().
Definition input.h:102
@ JoNightsPad
Nights 3D pad, Axis1 and Axis2 are analog stick, Axis3 is right trigger and Axis4 is left trigger.
Definition input.h:90
@ JoRegularKeyboard
Definition input.h:86
@ JoNotConnectedGamepad
Definition input.h:81
@ JoRegularGamepad
Definition input.h:83
@ JoUnsupportedGamepad
Definition input.h:82
@ JoRacingWheel
Racing wheel, Axis1 is the wheel.
Definition input.h:98
@ JoRegularMouse
Definition input.h:84
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:368
#define JO_INPUT_MAX_DEVICE
Definition input.h:38
jo_input_axis
Input axis type.
Definition input.h:108
@ JoAxis5
Definition input.h:113
@ JoAxis2
Definition input.h:110
@ JoAxis4
Definition input.h:112
@ JoAxis6
Definition input.h:114
@ JoAxis3
Definition input.h:111
@ JoAxis1
Definition input.h:109
static __jo_force_inline bool jo_is_input_analog(const int port)
Check if connected input is analog.
Definition input.h:127
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:244
int jo_get_input_count(void)
Get the number of input available.
struct __jo_may_alias jo_peripheral_pointer
Pointing peripheral data (mouse).
#define JO_PERIPHERAL_ID_NOT_CONNECTED
Peripheral ID: nothing connected.
Definition smpc.h:166
#define JO_PERIPHERAL_ID_NIGHTS_PAD
Peripheral ID: 3D pad (NiGHTS pad).
Definition smpc.h:174
Digital peripheral data (gamepad).
Definition smpc.h:211
#define __jo_force_inline
force inline attribute (and prevent Doxygen prototype parsing bug)
Definition types.h:39
jo_8_directions
8 directions definition
Definition types.h:326
@ DOWN_LEFT
Definition types.h:334
@ DOWN_RIGHT
Definition types.h:335
@ UP_RIGHT
Definition types.h:333
@ DOWN
Definition types.h:331
@ UP_LEFT
Definition types.h:332
@ UP
Definition types.h:330
@ NONE
Definition types.h:327
@ LEFT
Definition types.h:328
@ RIGHT
Definition types.h:329