home *** CD-ROM | disk | FTP | other *** search
/ Graphics Programming Black Book (Special Edition) / BlackBook.bin / disk1 / source / chapter53 / xsharp15.exe / OLIST.C < prev    next >
C/C++ Source or Header  |  1994-12-16  |  3KB  |  83 lines

  1. /* Object list-related functions. */
  2. #include <stdio.h>
  3. #include "polygon.h"
  4.  
  5. /* Set up the empty object list, with sentinels at both ends to 
  6.   terminate searches */
  7. void InitializeObjectList()
  8. {
  9.    ObjectListStart.NextObject = &ObjectListEnd;
  10.    ObjectListStart.PreviousObject = NULL;
  11.    ObjectListStart.CenterInView.Z = INT_TO_FIXED(-32768);
  12.    ObjectListEnd.NextObject = NULL;
  13.    ObjectListEnd.PreviousObject = &ObjectListStart;
  14.    ObjectListEnd.CenterInView.Z = 0x7FFFFFFFL;
  15.    NumObjects = 0;
  16. }
  17.  
  18. /* Adds an object to the object list, sorted by center Z coord. */
  19. void AddObject(Object *ObjectPtr)
  20. {
  21.    Object *ObjectListPtr = ObjectListStart.NextObject;
  22.  
  23.    /* Find the insertion point. Guaranteed to terminate because of
  24.       the end sentinel */
  25.    while (ObjectPtr->CenterInView.Z > ObjectListPtr->CenterInView.Z) {
  26.       ObjectListPtr = ObjectListPtr->NextObject;
  27.    }
  28.  
  29.    /* Link in the new object */
  30.    ObjectListPtr->PreviousObject->NextObject = ObjectPtr;
  31.    ObjectPtr->NextObject = ObjectListPtr;
  32.    ObjectPtr->PreviousObject = ObjectListPtr->PreviousObject;
  33.    ObjectListPtr->PreviousObject = ObjectPtr;
  34.    NumObjects++;
  35. }
  36.  
  37. /* Resorts the objects in order of ascending center Z coordinate in view space,
  38.    by moving each object in turn to the correct position in the object list. */
  39. void SortObjects()
  40. {
  41.    int i;
  42.    Object *ObjectPtr, *ObjectCmpPtr, *NextObjectPtr;
  43.  
  44.    /* Start checking with the second object */
  45.    ObjectCmpPtr = ObjectListStart.NextObject;
  46.    ObjectPtr = ObjectCmpPtr->NextObject;
  47.    for (i=1; i<NumObjects; i++) {
  48.       /* See if we need to move backward through the list */
  49.       if (ObjectPtr->CenterInView.Z < ObjectCmpPtr->CenterInView.Z) {
  50.          /* Remember where to resume sorting with the next object */
  51.          NextObjectPtr = ObjectPtr->NextObject;
  52.          /* Yes, move backward until we find the proper insertion
  53.             point. Termination guaranteed because of start sentinel */
  54.          do {
  55.             ObjectCmpPtr = ObjectCmpPtr->PreviousObject;
  56.          } while (ObjectPtr->CenterInView.Z <
  57.                ObjectCmpPtr->CenterInView.Z);
  58.  
  59.          /* Now move the object to its new location */
  60.          /* Unlink the object at the old location */
  61.          ObjectPtr->PreviousObject->NextObject =
  62.                ObjectPtr->NextObject;
  63.          ObjectPtr->NextObject->PreviousObject =
  64.                ObjectPtr->PreviousObject;
  65.  
  66.          /* Link in the object at the new location */
  67.          ObjectCmpPtr->NextObject->PreviousObject = ObjectPtr;
  68.          ObjectPtr->PreviousObject = ObjectCmpPtr;
  69.          ObjectPtr->NextObject = ObjectCmpPtr->NextObject;
  70.          ObjectCmpPtr->NextObject = ObjectPtr;
  71.  
  72.          /* Advance to the next object to sort */
  73.          ObjectCmpPtr = NextObjectPtr->PreviousObject;
  74.          ObjectPtr = NextObjectPtr;
  75.       } else {
  76.          /* Advance to the next object to sort */
  77.          ObjectCmpPtr = ObjectPtr;
  78.          ObjectPtr = ObjectPtr->NextObject;
  79.       }
  80.    }
  81. }
  82.  
  83.