Index: src/viewport.cpp =================================================================== --- src/viewport.cpp (revision 10160) +++ src/viewport.cpp (working copy) @@ -89,6 +89,9 @@ int32 ymin; int32 xmax; int32 ymax; + + int32 prec; /* index while sorting */ + ChildScreenSpriteToDraw *child; byte unk16; byte zmin; @@ -1104,61 +1107,42 @@ static void ViewportSortParentSprites(ParentSpriteToDraw *psd[]) { - while (*psd != NULL) { - ParentSpriteToDraw* ps = *psd; +//printf("sort\n"); + for (int i=0;psd[i]!=NULL;i++) { +// printf("%d %d %d %d %d %d %d %d\n",i++,ps->xmin,ps->xmax,ps->ymin,ps->ymax,ps->zmin,ps->zmax,ps->zmin+ps->zmax); + psd[i]->prec =(psd[i]->xmin+psd[i]->xmax)+(psd[i]->ymin+psd[i]->ymax)+(psd[i]->zmin+psd[i]->zmax); + } - if (!(ps->unk16 & 1)) { - ParentSpriteToDraw** psd2 = psd; - - ps->unk16 |= 1; - - while (*++psd2 != NULL) { - ParentSpriteToDraw* ps2 = *psd2; - ParentSpriteToDraw** psd3; - - if (ps2->unk16 & 1) continue; - - /* Decide which comparator to use, based on whether the bounding - * boxes overlap - */ - if (ps->xmax > ps2->xmin && ps->xmin < ps2->xmax && // overlap in X? - ps->ymax > ps2->ymin && ps->ymin < ps2->ymax && // overlap in Y? - ps->zmax > ps2->zmin && ps->zmin < ps2->zmax) { // overlap in Z? - /* Use X+Y+Z as the sorting order, so sprites closer to the bottom of - * the screen and with higher Z elevation, are drawn in front. - * Here X,Y,Z are the coordinates of the "center of mass" of the sprite, - * i.e. X=(left+right)/2, etc. - * However, since we only care about order, don't actually divide / 2 - */ - if (ps->xmin + ps->xmax + ps->ymin + ps->ymax + ps->zmin + ps->zmax <= - ps2->xmin + ps2->xmax + ps2->ymin + ps2->ymax + ps2->zmin + ps2->zmax) { - continue; - } - } else { - if (ps->xmax < ps2->xmin || - ps->ymax < ps2->ymin || - ps->zmax < ps2->zmin || ( - ps->xmin < ps2->xmax && - ps->ymin < ps2->ymax && - ps->zmin < ps2->zmax - )) { - continue; - } - } - - /* Swap the two sprites ps and ps2 using bubble-sort algorithm. */ - psd3 = psd; - do { - ParentSpriteToDraw* temp = *psd3; - *psd3 = ps2; - ps2 = temp; - - psd3++; - } while (psd3 <= psd2); + for ( int i = 0 ; psd[i] != NULL ; i++ ) { + int pmin; + int jmin; + for ( int j = i ; psd[j] != NULL ; j++ ) { + ParentSpriteToDraw* ps = psd[j]; + int prec=0; + for ( int k = j+1 ; psd[k] != NULL ; k++ ) { + ParentSpriteToDraw* ps2 = psd[k]; + const Sprite* spr = GetSprite(ps->image & SPRITE_MASK); + const Sprite* spr2 = GetSprite(ps2->image & SPRITE_MASK); + if ( + RemapCoords(ps->xmin,ps->ymin,ps->zmin).x+spr->x_offs <= RemapCoords(ps2->xmin,ps2->ymin,ps2->zmin).x+spr2->x_offs+spr2->width && + RemapCoords(ps->xmin,ps->ymin,ps->zmin).x+spr->x_offs+spr->width >= RemapCoords(ps2->xmin,ps2->ymin,ps2->zmin).x+spr2->x_offs && + RemapCoords(ps->xmin,ps->ymin,ps->zmin).y+spr->y_offs <= RemapCoords(ps2->xmin,ps2->ymin,ps2->zmin).y+spr2->y_offs+spr2->height && + RemapCoords(ps->xmin,ps->ymin,ps->zmin).y+spr->y_offs+spr->height >= RemapCoords(ps2->xmin,ps2->ymin,ps2->zmin).y+spr2->y_offs + ) + if (ps->zmin > ps2->zmax || ( ps->zmax >= ps2->zmin && ps->prec > ps2->prec ) ) prec++; } - } else { - psd++; + if ( ( j == i ) || ( pmin > prec ) ) { + pmin = prec; + jmin = j; + } } + if ( jmin != i ) { + ParentSpriteToDraw* swap = psd[i]; + psd[i] = psd[jmin]; + psd[jmin] = swap; + } +// printf("ss %d %d\n",pmin,jmin); +// printf("%d %d %d %d %d %d\n",psd[i]->xmin,psd[i]->xmax,psd[i]->ymin,psd[i]->ymax,psd[i]->zmin,psd[i]->zmax); } }