games: fix input/rendering lag and missed keypresses

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.
This commit is contained in:
Luxferre committed 2026-10-24 09:36:38 +03:00
1 parent eee12181c7
commit ff29160461
6 files changed
+69 -16

No files matched your search

+24 -3
View File
@@ -286,11 +286,32 @@ static void render() {
// window edges, so it is visible on all four sides including right/bottom
XSetForeground(dpy, gc, C_DIM);
XDrawRectangle(dpy, back, gc, 2, 2, winW - 4, winH - 4);
// settled blocks
// settled blocks (batched for speed)
XRectangle bodies[PCOLS * PROWS], highlights[PCOLS * PROWS * 2], shadows[PCOLS * PROWS * 2];
int nb = 0, nh = 0, ns = 0;
for (int x = 0; x < PCOLS; x++)
for (int y = 0; y < PROWS; y++)
if (board[x][y])
draw_gb_cell(px0 + x * CELL, py0 + y * CELL, CELL, C_GB_MED);
if (board[x][y]) {
int bx = px0 + x * CELL, by = py0 + y * CELL;
bodies[nb].x = bx + 1; bodies[nb].y = by + 1;
bodies[nb].width = CELL - 2; bodies[nb].height = CELL - 2;
nb++;
highlights[nh].x = bx + 1; highlights[nh].y = by;
highlights[nh].width = CELL - 2; highlights[nh].height = 1;
nh++;
highlights[nh].x = bx; highlights[nh].y = by + 1;
highlights[nh].width = 1; highlights[nh].height = CELL - 2;
nh++;
shadows[ns].x = bx + 1; shadows[ns].y = by + CELL - 1;
shadows[ns].width = CELL - 2; shadows[ns].height = 1;
ns++;
shadows[ns].x = bx + CELL - 1; shadows[ns].y = by + 1;
shadows[ns].width = 1; shadows[ns].height = CELL - 2;
ns++;
}
XSetForeground(dpy, gc, C_GB_MED); XFillRectangles(dpy, back, gc, bodies, nb);
XSetForeground(dpy, gc, C_GB_PALE); XFillRectangles(dpy, back, gc, highlights, nh);
XSetForeground(dpy, gc, C_GB_DARK); XFillRectangles(dpy, back, gc, shadows, ns);
// active piece
if (state == PLAYING) {