summaryrefslogtreecommitdiff
path: root/platformer/src/controls.c
diff options
context:
space:
mode:
authorSchark <jordan@schark.online>2026-09-24 00:06:00 -0400
committerSchark <jordan@schark.online>2026-09-24 00:06:00 -0400
commit5d078ec17b27afd159f8b9a8c1298fcac15d695d (patch)
tree59c2d29d64933e849f17bb50d34ea71b88c13602 /platformer/src/controls.c
parent23ef8bc20310bdfad52b07a3f1e4e61c98896841 (diff)
downloadgamedev-5d078ec17b27afd159f8b9a8c1298fcac15d695d.tar.gz
gamedev-5d078ec17b27afd159f8b9a8c1298fcac15d695d.zip
Fix recentering, default button
Diffstat (limited to 'platformer/src/controls.c')
-rw-r--r--platformer/src/controls.c42
1 files changed, 24 insertions, 18 deletions
diff --git a/platformer/src/controls.c b/platformer/src/controls.c
index f9f55c0..3bcf7b7 100644
--- a/platformer/src/controls.c
+++ b/platformer/src/controls.c
@@ -14,7 +14,8 @@
*
* 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;`
+ * edges), e.g. `static bool s_prev_jump, s_prev_reset, s_prev_recenter,
+ * 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
@@ -48,6 +49,7 @@
* look.y = -GLFW_GAMEPAD_AXIS_RIGHT_Y
* jump = GLFW_GAMEPAD_BUTTON_A
* reset = GLFW_GAMEPAD_BUTTON_BACK
+ * recenter = GLFW_GAMEPAD_BUTTON_RIGHT_THUMB (click the right stick)
* quit = GLFW_GAMEPAD_BUTTON_START
*
* - RADIAL deadzone (write a small static helper, used for both sticks):
@@ -65,7 +67,7 @@
* - 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
+ * Space -> jump, R -> reset, C -> recenter camera, Esc -> quit
* Read keys with glfwGetKey(window_handle(), key).
* Esc also quits when a gamepad IS connected (so you can always exit).
*
@@ -115,7 +117,7 @@ static const char *EXTRA_GAMEPAD_MAPPINGS =
"dpleft:h0.8,platform:Linux,\n";
/* Last tick's button states, for turning "down" into "pressed this tick". */
-static bool s_prev_jump, s_prev_reset, s_prev_quit;
+static bool s_prev_jump, s_prev_reset, s_prev_recenter, s_prev_quit;
/* The joystick id we're reading, or NO_GAMEPAD. Only used to notice (and
* print) when a pad connects or disconnects. */
@@ -123,7 +125,7 @@ static int s_active_jid = NO_GAMEPAD;
/* Raw buttons for this tick, before edge detection. */
typedef struct {
- bool jump, reset, quit;
+ bool jump, reset, recenter, quit;
} Buttons;
/* Radial deadzone. WHY radial and not per-axis: a per-axis deadzone snaps
@@ -168,7 +170,7 @@ static void report_gamepad_change(int jid)
static Buttons read_gamepad(int jid, Input *out)
{
- Buttons buttons = { false, false, false };
+ Buttons buttons = { false, false, false, false };
GLFWgamepadstate state;
if (!glfwGetGamepadState(jid, &state))
return buttons;
@@ -181,9 +183,10 @@ static Buttons read_gamepad(int jid, Input *out)
-state.axes[GLFW_GAMEPAD_AXIS_RIGHT_Y],
LOOK_DEADZONE);
- buttons.jump = state.buttons[GLFW_GAMEPAD_BUTTON_A] == GLFW_PRESS;
- buttons.reset = state.buttons[GLFW_GAMEPAD_BUTTON_BACK] == GLFW_PRESS;
- buttons.quit = state.buttons[GLFW_GAMEPAD_BUTTON_START] == GLFW_PRESS;
+ buttons.jump = state.buttons[GLFW_GAMEPAD_BUTTON_A] == GLFW_PRESS;
+ buttons.reset = state.buttons[GLFW_GAMEPAD_BUTTON_BACK] == GLFW_PRESS;
+ buttons.recenter = state.buttons[GLFW_GAMEPAD_BUTTON_RIGHT_THUMB] == GLFW_PRESS;
+ buttons.quit = state.buttons[GLFW_GAMEPAD_BUTTON_START] == GLFW_PRESS;
out->using_gamepad = true;
return buttons;
}
@@ -217,9 +220,10 @@ static Buttons read_keyboard(Input *out)
look = vec2_make(look.x / len, look.y / len);
out->look = look;
- buttons.jump = key_down(GLFW_KEY_SPACE);
- buttons.reset = key_down(GLFW_KEY_R);
- buttons.quit = false; /* Esc is handled for every device in controls_poll */
+ buttons.jump = key_down(GLFW_KEY_SPACE);
+ buttons.reset = key_down(GLFW_KEY_R);
+ buttons.recenter = key_down(GLFW_KEY_C);
+ buttons.quit = false; /* Esc is handled for every device in controls_poll */
out->using_gamepad = false;
return buttons;
}
@@ -264,12 +268,14 @@ void controls_poll(Input *out)
now.quit = true;
/* Edges: "pressed" is true only on the first tick the button is down. */
- out->jump_held = now.jump;
- out->jump_pressed = now.jump && !s_prev_jump;
- out->reset_pressed = now.reset && !s_prev_reset;
- out->quit_pressed = now.quit && !s_prev_quit;
+ out->jump_held = now.jump;
+ out->jump_pressed = now.jump && !s_prev_jump;
+ out->reset_pressed = now.reset && !s_prev_reset;
+ out->recenter_pressed = now.recenter && !s_prev_recenter;
+ out->quit_pressed = now.quit && !s_prev_quit;
- s_prev_jump = now.jump;
- s_prev_reset = now.reset;
- s_prev_quit = now.quit;
+ s_prev_jump = now.jump;
+ s_prev_reset = now.reset;
+ s_prev_recenter = now.recenter;
+ s_prev_quit = now.quit;
}