1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
/*
* ============================================================
* FILE: controls.c
*
* PURPOSE: Turns the physical gamepad (primary) or keyboard (fallback)
* into the abstract Input struct from controls.h. Getting
* the analog feel right here (the deadzone and stick scaling)
* matters as much as the player code for movement feel.
*
* FITS IN: main.c calls controls_poll() once per fixed tick. It uses
* GLFW's gamepad API directly and window_handle() (window.h)
* only for keyboard reads. It's the only file that reads raw
* input devices.
*
* IMPLEMENT:
* - Private state: the previous tick's button states (for the "pressed"
* edges), e.g. `static bool s_prev_jump, s_prev_reset, s_prev_quit;`
*
* - Finding a gamepad (keep it simple; this also handles hot-plugging):
* each poll, loop GLFW_JOYSTICK_1..GLFW_JOYSTICK_LAST and use the first
* jid where glfwJoystickIsGamepad(jid) is true, then read it with
* glfwGetGamepadState(jid, &state). GLFW 3.4 has SDL's controller
* mapping database built in, so Xbox/PlayStation/Switch pads all show
* up in the standard layout. Print a line to stdout when the active
* pad connects or disconnects (glfwGetGamepadName), so it's clear
* which device is in use.
*
* - Gamepad mapping:
* move.x = GLFW_GAMEPAD_AXIS_LEFT_X
* move.y = -GLFW_GAMEPAD_AXIS_LEFT_Y (GLFW reports up as NEGATIVE,
* so flip it: up = forward = +y)
* look.x = GLFW_GAMEPAD_AXIS_RIGHT_X
* look.y = -GLFW_GAMEPAD_AXIS_RIGHT_Y
* jump = GLFW_GAMEPAD_BUTTON_A
* reset = GLFW_GAMEPAD_BUTTON_BACK
* quit = GLFW_GAMEPAD_BUTTON_START
*
* - RADIAL deadzone (write a small static helper, used for both sticks):
* len = length of the raw (x, y)
* if len < deadzone -> (0, 0)
* else rescale so the edge of the deadzone maps to 0 and full tilt maps
* to 1: new_len = (len - deadzone) / (1 - deadzone), clamped to 1,
* keeping the original direction.
* WHY radial and not per-axis: a per-axis deadzone snaps diagonals
* to the cardinal directions and makes slow walking feel "sticky". SM64
* also computes one magnitude from the whole stick and uses it for speed.
* Also clamp the final length to 1, because some pads report corners past 1.
* Use MOVE_DEADZONE / LOOK_DEADZONE from config.h.
*
* - Keyboard fallback, used ONLY when no gamepad is connected:
* W/S/A/D -> move (normalize diagonals so the length is <= 1)
* Arrow keys -> look
* Space -> jump, R -> reset, Esc -> quit
* Read keys with glfwGetKey(window_handle(), key).
* Esc also quits when a gamepad IS connected (so you can always exit).
*
* - Edge detection: pressed = down_now && !down_last_tick. Then store
* down_now for next tick.
*
* - Zero the whole Input at the start of controls_poll so no stale data leaks.
*
* CONVENTIONS: `s_` prefix for statics. <GLFW/glfw3.h> is included here (not
* in controls.h).
*
* NOT IN SCOPE: Rebinding, loading an external gamecontrollerdb.txt,
* rumble, triggers/shoulder buttons (add them when a move needs
* them), input recording/replay.
*
* DONE WHEN: With a pad: a small stick tilt gives a small |move|, full
* tilt gives ~1.0, the resting stick gives exactly (0, 0), and each
* A press sets jump_pressed for exactly one tick. Unplugging the
* pad switches to the keyboard without a crash, and plugging it back
* in switches back.
* ============================================================
*/
|