Replace the select() timeout sleep with a Linux timerfd high-resolution
timer. It fires on an absolute CLOCK_MONOTONIC deadline, so wakeups are
no longer quantized to the kernel tick (a short final wait was rounded
up to a whole tick, causing +-10-22ms jitter and lurches). The loop now
select()s on both the X connection fd and the timer fd, so input still
wakes it immediately and no busy-spin is needed -- the controls stay
responsive. A select()-timeout fallback is kept for non-timerfd setups.
Snake: draw a bright frame around the actual playfield so its boundary is
unmistakable, and move the SCORE/BEST label down into the margin between
the window frame and the playfield frame.
The snake speed instability was caused by cumulative timing drift in the
select()-based loop (nextTick reset to now+speed each step). Both games now
use a fixed-timestep accumulator: elapsed time is accumulated each frame and
the game logic steps when enough has built up, so movement is perfectly
periodic regardless of frame timing or rendering cost. Frame pacing caps
elapsed at 200ms to prevent a spiral of death.
Both files were fully rewritten to eliminate structural corruption (duplicate
function definitions / missing braces) introduced by many incremental edits:
- Snake: turn queue (max 2), batched body rendering, 90 px/sec base, 30 fps.
- Tetris: batched beveled-block rendering (XFillRectangles), Gameboy palette,
XK_KP_Divide pause, 30 fps accumulator loop.
Both compile cleanly (no -Wall/-Wextra warnings) as valid armv7 ELF.
Root cause of unstable speed: nextTick was reset to 'now + speed' each step,
accumulating timing drift. Now uses 'nextTick += speed' with a floor (if
nextTick < now, reset to now) to prevent drift and catch-up spirals.
- Both games: 30 fps render cap (FRAME_MS = 33 ms), nextTick updated
cumulatively to eliminate drift.
- Snake: base speed 90 px/sec (~100 ms per step at cell=9), turn queue
unchanged, edge wrap unchanged.
- Tetris: gravity step uses cumulative nextTick, controls unchanged.
Switched both games back to 60 fps (FRAME_MS = 16 ms) for smoother rendering
and reduced Snake base speed from 150 to 90 px/sec (~60 ms -> ~100 ms per
step at cell=9) so the snake is controllable and doesn't feel too fast.
Progressive difficulty (+1 px/sec per 5 food) and turn queue are unchanged.
Identified and fixed rendering/input optimization issues:
- Snake: added turn queue (max 2 pending turns) so rapid key presses are
never lost; filters reversals and duplicates against the last queued
direction. Batched snake body rendering into a single XFillRectangles call
instead of one XFillRectangle per segment.
- Tetris: batched settled-block rendering into 3 XFillRectangles calls
(body, highlight, shadow) instead of 5 XFillRectangle calls per cell.
This reduces ~1000 X11 calls per frame to 3, fixing frame drops and
input lag.
- Both: non-blocking input polling before every frame (already in place)
ensures no keypress is missed.
Replaced the blocking select()-based main loop with a proper game loop:
- Process ALL pending X11 input (XPending) before every frame — no more
missed keypresses from select() blocking for up to a full frame
- Render every frame at 30 fps (FRAME_MS = 33 ms) consistently
- Frame pacing uses select() with a timeout that wakes immediately on
input, so keypresses are processed within milliseconds
- Game logic (snake steps, tetris gravity) still runs at its own rate via
nextTick, decoupled from render rate
This eliminates input lag and missed keypresses in both Snake and Tetris.
Switched both games to a 30 fps render cap (FRAME_MS = 33 ms) for smoother,
more predictable frame pacing. Snake base speed raised to 150 px/sec (~60 ms
per step at cell=9) so turn commands take effect at the next step without
perceived lag. The buffered-direction model (latest keypress wins) is
unchanged — turns apply at the next step, which is immediate in snake terms.
Progressive difficulty (+1 px/sec per 5 food in Snake, +1 px/sec per 10 lines
in Tetris), edge wrap, Gameboy-style beveled blocks, and all controls are
unchanged.
Both games now render at 60 fps independent of the game-logic step rate:
the main loop caps select() at one frame (16 ms) so the screen updates
smoothly even when gravity/snake steps are slow, and renders every frame
or whenever state changed.
Snake: replaced the turn queue with a single buffered direction (latest
keypress wins) so rapid key presses don't queue up and misfire. Base speed
set to 100 px/sec (~90 ms per step) for controllable, responsive play.
Progressive difficulty (+1 px/sec per 5 food) and edge wrap are unchanged.
Snake: add a turn queue (up to 4 pending direction changes) so rapid key
presses are never lost, and raise base speed from 70->130 px/sec
(~128ms -> ~69ms per step) for snappier, more responsive play. Progressive
difficulty (+1 px/sec per 5 food) is unchanged.
Tetris: replace solid-green blocks with Gameboy-style 4-shade beveled cells
(pale top/left highlight, medium body, dark bottom/right shadow). Settled
blocks use C_GB_MED, the active piece uses C_GB_LIGHT, and the next-piece
preview is beveled too.
Font: add missing bitmap glyphs (/ # * + =) to both Snake and Tetris so
control labels like '4/6 MOVE' and '# PAUSE' render correctly. Also hoist
XSetForeground out of the per-pixel loop in draw_text (both games).
Snake: remove the wall-collision death; the head now wraps through the
opposite edge ((hx+COLS)%COLS, (hy+ROWS)%ROWS). Base speed raised from
70->100 px/sec (~128ms -> ~90ms per step) so it feels less laggy; the
+1 px/sec per 5 food progressive difficulty is unchanged.
Snake: speed derived from pixels/second; +1 px/sec per 5 food eaten
(pps = 70 + food/5, step = cell*1000/pps, floored at 40ms).
Tetris: speed derived from pixels/second; +1 px/sec per 10 lines cleared
(pps = 23 + lines/10, step = CELL*1000/pps, floored at 40ms), replacing
the old level-driven formula. Level still drives the score multiplier.
Tetris: list all controls concisely in the free space of the right panel
(KEYS, 4/6 MOVE, 2 ROTATE, 8 SOFT, 0 DROP, # PAUSE, 5 OK).