From 4a3da1a2b74bf9b421ab78005887b33b6191d6ba Mon Sep 17 00:00:00 2001 From: Max Horn Date: Wed, 2 Jul 2003 11:32:32 +0000 Subject: cleaned up the actor ordering code a bit - it should be now somewhat clearer what it does exactly svn-id: r8709 --- scumm/actor.cpp | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) (limited to 'scumm') diff --git a/scumm/actor.cpp b/scumm/actor.cpp index 3002df55f5..08e2be16f3 100644 --- a/scumm/actor.cpp +++ b/scumm/actor.cpp @@ -810,24 +810,32 @@ void Scumm::playActorSounds() { } } -inline int32 drawOrder (const Actor* x) { return (x)->y - ((x)->layer << 11); } - -int sortByDrawOrder (const void* a, const void* b) +static int compareDrawOrder(const void* a, const void* b) { const Actor* actor1 = *(const Actor *const*)a; const Actor* actor2 = *(const Actor *const*)b; - int32 order1 = drawOrder(actor1); - int32 order2 = drawOrder(actor2); + int diff; + + // The actor in the higher layer is ordered lower + diff = actor1->layer - actor2->layer; + if (diff < 0) + return +1; + if (diff > 0) + return -1; - // The qsort() function is apparently not guaranteed to be stable (i.e. - // it may re-order actors with the same draw order value). Use the + // The actor with higher y value is ordered higher + diff = actor1->y - actor2->y; + if (diff < 0) + return -1; + if (diff > 0) + return +1; + + // The qsort() function is not guaranteed to be stable (i.e. it may + // re-order "equal" elements in an array it sorts). Hence we use the // actor number as tie-breaker. This is needed for the Sam & Max intro, // and possibly other cases as well. See bug #758167. - if (order1 == order2) - return actor1->number - actor2->number; - - return order1 - order2; + return actor1->number - actor2->number; } void Scumm::processActors() { @@ -851,10 +859,7 @@ void Scumm::processActors() { // Sort actors by position before we draw them (to ensure that actors in // front are drawn after those "behind" them). - // Bertrand TODO : Put a std::sort with a inlined comparison operator? - // I suppose only STL containers are not allowed, not algorithms, but I prefered leaving a good old qsort - // (Which might be slower that the previous code but just fits on one line) - qsort(actors, numactors, sizeof (Actor*), sortByDrawOrder); + qsort(actors, numactors, sizeof (Actor*), compareDrawOrder); Actor** end = actors + numactors; -- cgit v1.2.3