From 3209e4e467bdee42f1e51f009a7aa3dbb70d10f4 Mon Sep 17 00:00:00 2001 From: Fabian Greffrath Date: Tue, 8 Aug 2023 12:30:57 +0200 Subject: [PATCH] alternative approach to stable vissprite sorting in Vanilla order (#1169) --- src/r_things.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/r_things.c b/src/r_things.c index e31e3006..21eae9be 100644 --- a/src/r_things.c +++ b/src/r_things.c @@ -901,7 +901,7 @@ static void msort(vissprite_t **s, vissprite_t **t, int n) msort(s1, t, n1); msort(s2, t, n2); - while ((*s1)->scale > (*s2)->scale ? + while ((*s1)->scale >= (*s2)->scale ? (*d++ = *s1++, --n1) : (*d++ = *s2++, --n2)); if (n2) @@ -917,12 +917,10 @@ static void msort(vissprite_t **s, vissprite_t **t, int n) for (i = 1; i < n; i++) { vissprite_t *temp = s[i]; - // [FG] change '<' to '<=' here and below, so that vissprites with the same scale - // are reordered, so that the object with the higher map index appears in front - if (s[i-1]->scale <= temp->scale) + if (s[i-1]->scale < temp->scale) { int j = i; - while ((s[j] = s[j-1])->scale <= temp->scale && --j); + while ((s[j] = s[j-1])->scale < temp->scale && --j); s[j] = temp; } } @@ -946,8 +944,12 @@ void R_SortVisSprites (void) * sizeof *vissprite_ptrs, PU_STATIC, 0); } + // Sprites of equal distance need to be sorted in inverse order. + // This is most easily achieved by filling the sort array + // backwards before the sort. + while (--i>=0) - vissprite_ptrs[i] = vissprites+i; + vissprite_ptrs[num_vissprite-i-1] = vissprites+i; // killough 9/22/98: replace qsort with merge sort, since the keys // are roughly in order to begin with, due to BSP rendering.