From dc53a2d8deb41c583c985e274b766b4c61d5f0bb Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Wed, 1 Jan 2025 21:31:29 +0000 Subject: [PATCH] Snap view and entity positions to grid when not in motion This fixes some chunky movement badness. --- app/main.c | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/app/main.c b/app/main.c index 0e3d096..f9efee5 100644 --- a/app/main.c +++ b/app/main.c @@ -340,8 +340,8 @@ static void mapdraw(void) .h = TILESIZE, }; const SDL_Rect dest = { - .x = SCALE * (int)round(x - vpos.x), - .y = SCALE * (int)round(y - vpos.y), + .x = (int)rint(SCALE * (x - vpos.x)), + .y = (int)rint(SCALE * (y - vpos.y)), .w = SCALE * TILESIZE, .h = SCALE * TILESIZE, }; @@ -353,8 +353,12 @@ static void mapdraw(void) static void entityupdate(entity_t *e, double dt) { - if (0 == e->speed) + if (0 == e->speed) { + // Round position to nearest integer to align with pixel grid. + e->pos.x = rint(e->pos.x); + e->pos.y = rint(e->pos.y); return; + } // Update sprite variant if (e->dir.y >= 0) { @@ -395,8 +399,8 @@ static void entitydraw(entity_t *e, uint64_t t) e->src.x = e->animstep.x * frame + e->svarstep.x * e->svar; e->src.y = e->animstep.y * frame + e->svarstep.y * e->svar; SDL_Rect dest = { - .x = SCALE * (int)round(e->pos.x - e->src.w / 2 - vpos.x), - .y = SCALE * (int)round(e->pos.y - e->src.h / 2 - vpos.y), + .x = (int)rint(SCALE * (e->pos.x - e->src.w / 2 - vpos.x)), + .y = (int)rint(SCALE * (e->pos.y - e->src.h / 2 - vpos.y)), .w = SCALE * e->src.w, .h = SCALE * e->src.h, }; @@ -413,8 +417,8 @@ static void objsdraw(uint64_t t) SDL_Rect src = type->src; src.x += type->src.w * ((t / BASEANIMPERIOD) % type->animframes); SDL_Rect dest = { - .x = SCALE * (int)round(obj->pos.x - vpos.x), - .y = SCALE * (int)round(obj->pos.y - vpos.y - type->src.h), + .x = (int)rint(SCALE * (obj->pos.x - vpos.x)), + .y = (int)rint(SCALE * (obj->pos.y - vpos.y - type->src.h)), .w = SCALE * type->src.w, .h = SCALE * type->src.h, }; @@ -552,6 +556,11 @@ int main(int argc, char *argv[]) } else { p.tex = pidle; p.speed = 0; + + // Round view position to nearest integer to align with + // pixel grid. + vpos.x = rint(vpos.x); + vpos.y = rint(vpos.y); } entityupdate(&p, dt);