Snap view and entity positions to grid when not in motion

This fixes some chunky movement badness.
This commit is contained in:
Camden Dixie O'Brien 2025-01-01 21:31:29 +00:00
parent e8eba7e90c
commit dc53a2d8de

View File

@ -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);