/* * ============================================================ * 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. 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. * ============================================================ */