netpanzer-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[netPanzer-CVS] netpanzer/src/NetPanzer/Classes SpriteSorter.hp...


From: Matthias Braun
Subject: [netPanzer-CVS] netpanzer/src/NetPanzer/Classes SpriteSorter.hp...
Date: Wed, 10 Sep 2003 09:20:57 -0400

CVSROOT:        /cvsroot/netpanzer
Module name:    netpanzer
Branch:         
Changes by:     Matthias Braun <address@hidden> 03/09/10 09:20:57

Modified files:
        src/NetPanzer/Classes: SpriteSorter.hpp SpriteSorter.cpp 

Log message:
        removed a usage of PArrayGrowable by using STL

Patches:
Index: netpanzer/src/NetPanzer/Classes/SpriteSorter.cpp
diff -u netpanzer/src/NetPanzer/Classes/SpriteSorter.cpp:1.5 
netpanzer/src/NetPanzer/Classes/SpriteSorter.cpp:1.6
--- netpanzer/src/NetPanzer/Classes/SpriteSorter.cpp:1.5        Wed Sep 10 
08:32:53 2003
+++ netpanzer/src/NetPanzer/Classes/SpriteSorter.cpp    Wed Sep 10 09:20:57 2003
@@ -17,59 +17,36 @@
 */
 #include <config.h>
 
+#include <algorithm>
+#include <functional>
 #include "Exception.hpp"
 #include "SpriteSorter.hpp"
 
 SpriteSorter SPRITE_SORTER;
 
+class SpriteCompare 
+       : public std::binary_function<const Sprite*, const Sprite*, bool>
+{
+public:
+       bool operator()(const Sprite* sprite1, const Sprite* sprite2) const
+       {
+               long y1 = sprite1->world_pos.y;
+               long y2 = sprite2->world_pos.y;
 
-int sprite_key( const void *elem1, const void *elem2 )
- {
-
-  // !KLUDGE! for stupid Micro$oft.......
-  //
-  // Their stupid qsort implementation compares the same element
-  // against itself.  We must safeguard against this and return
-  // "same" in this case.  (And only in this case.....)
-
-  if (elem1 == elem2) return 0;
-
-  Sprite *sprite1 = *(Sprite **) (elem1);
-  Sprite *sprite2 = *(Sprite **) (elem2);
-
-  long y1 = sprite1->world_pos.y;
-  long y2 = sprite2->world_pos.y;
-
-  // @FD 06/25/98 - Rewrote these tests.  I think there was a bug on
-  // with (y1 > y1) test.  Also, for sprites that tie, I will impose
-  // a second comparison to break the tie, so that our comparison
-  // function is always consistent.  Whoever the bonehead at Microsoft
-  // was who wrote their qsort not only compares the same element
-  // against itself, but they also coded it in such a way that it
-  // can get stuck in a loop and never terminate (or even crash) if
-  // the comparison function is not consistent.  Morons.
-  
-  //if( y1 > y1 )
-  // return( 1 );
-  //else
-  // if( y1 < y2 )
-  //  return( -1 );
+               //if (y1 > y2) return +1;
+               if (y1 < y2) return true; else return false;
 
-  if (y1 > y2) return +1;
-  if (y1 < y2) return -1;
-
-  // Tie based on the real comparison basis - impose an arbitrary 
-  // but consistent sort order to break the tie.
+               // Tie based on the real comparison basis - impose an arbitrary 
+               // but consistent sort order to break the tie.
   
-  if (sprite1 > sprite2) return +1;
-  if (sprite1 < sprite2) return -1;
-
-  // We should never get here, unless the same sprite is
-  // inserted in the list twice
-  throw Exception("sprite_key called to compare the same sprite against 
itself!");
-  return 0;
-}
+               //if (sprite1 > sprite2) return +1;
+               if (sprite1 < sprite2) return true; else return false;
 
+               // We should never get here, unless the same sprite is
+               // inserted in the list twice
+               throw Exception("sprite_key called to compare the same sprite 
against itself!");
+       }
+};
 
 SpriteSorter::SpriteSorter( )
  {
@@ -82,26 +59,25 @@
 
   for ( i = 0; i < 3; i++ )
    {
-    sprite_lists[ i ].initialize( 50, 50, 0xFFFFFFFF );    
-       list_counts[ i ] = 0;
+    sprite_lists[i].resize(50);    
+       list_counts[i] = 0;
    }
 
-  sprite_lists[ 3 ].initialize( 500, 100, 0xFFFFFFFF );
-  sprite_lists[ 4 ].initialize( 500, 100, 0xFFFFFFFF );
-  sprite_lists[ 5 ].initialize( 500, 100, 0xFFFFFFFF );
+  sprite_lists[3].resize(500);
+  sprite_lists[4].resize(500);
+  sprite_lists[5].resize(500);
 
   for ( i = 6; i < _MAX_HEIGHT_LEVELS; i++ )
    {
-    sprite_lists[ i ].initialize( 50, 50, 0xFFFFFFFF );
-    list_counts[ i ] = 0;
+    sprite_lists[i].resize(50);
+    list_counts[i] = 0;
    }
 
   for ( i = 0; i < _MAX_HEIGHT_LEVELS; i++ )
    {
-    max_sprite_stats[ i ] = 0;
+    max_sprite_stats[i] = 0;
    }
-
- }
+}
 
 void SpriteSorter::reset( void )
  {
@@ -120,72 +96,75 @@
   reset();
  }
 
-void SpriteSorter::addSprite( Sprite *sprite )
- {
-  unsigned char height;
+void SpriteSorter::addSprite(Sprite *sprite)
+{
+       unsigned char height;
   
-  if ( sprite->isVisible( world_window ) )
-   {
-    height = sprite->getHeight();
+       if ( sprite->isVisible( world_window ) )
+       {
+               height = sprite->getHeight();
        
-       assert( height < _MAX_HEIGHT_LEVELS );
+               assert( height < _MAX_HEIGHT_LEVELS );
         
-       sprite_lists[ height ].add( sprite, list_counts[ height ] );
-    list_counts[ height ]++;
-   }
- 
- }
-
+               if(sprite_lists[height].size() < list_counts[height])
+                       sprite_lists[height].resize(list_counts[height]);
+               sprite_lists[height] [list_counts[height]] = sprite;
+               list_counts[ height ]++;
+       } 
+}
  
-void SpriteSorter::forceAddSprite( Sprite *sprite )
- {
-  unsigned char height;
+void SpriteSorter::forceAddSprite(Sprite *sprite)
+{
+       unsigned char height;
 
-  height = sprite->getHeight();
+       height = sprite->getHeight();
        
-  assert( height < _MAX_HEIGHT_LEVELS );
-        
-  sprite_lists[ height ].add( sprite, list_counts[ height ] );
-  list_counts[ height ]++;
- }
-
+       assert( height < _MAX_HEIGHT_LEVELS );
 
-void SpriteSorter::sortLists( void ) 
- {
-  unsigned long i;
+       if(sprite_lists[height].size() < list_counts[height])
+               sprite_lists[height].resize(list_counts[height]);
+       sprite_lists[height] [list_counts[height]] = sprite;
+  
+       list_counts[ height ]++;
+}
 
-  for ( i = 0; i < _MAX_HEIGHT_LEVELS; i++ )
-   {
-    sprite_lists[ i ].sort( list_counts[ i ], sprite_key );   
-   }
- 
- }
+void SpriteSorter::sortLists()
+{
+       unsigned long i;
+
+       for ( i = 0; i < _MAX_HEIGHT_LEVELS; i++ )
+       {
+               sort(sprite_lists[i].begin(), sprite_lists[i].end(),
+                        SpriteCompare());
 
+               //sprite_lists[ i ].sort( list_counts[ i ], sprite_key );   
+       }
+}
 
 void SpriteSorter::blitLists( Surface *render_surf )
- {
-  Sprite *sprite;
-  unsigned long list_index, sprite_index;
-
-  //sortLists();
-
-  for ( list_index = 0; list_index < _MAX_HEIGHT_LEVELS; list_index++ )
-   {
-    for ( sprite_index = 0; sprite_index < list_counts[ list_index]; 
sprite_index++ )
-        {
-         sprite = (Sprite *) sprite_lists[ list_index ][ sprite_index ];
-         sprite->blitAll( render_surf, world_window );   
-     }
+{
+       Sprite *sprite;
+       unsigned long list_index, sprite_index;
+
+       //sortLists();
+
+       for ( list_index = 0; list_index < _MAX_HEIGHT_LEVELS; list_index++ )
+       {
+               for ( sprite_index = 0; sprite_index < list_counts[ 
list_index]; sprite_index++ )
+               {
+                       sprite = (Sprite *) sprite_lists[ list_index ][ 
sprite_index ];
+                       sprite->blitAll( render_surf, world_window );   
+               }
    
-    if ( list_counts[ list_index ] > max_sprite_stats[ list_index ] )
-        {
-         max_sprite_stats[ list_index ] = list_counts[ list_index ];
-        } 
-   }
+               if ( list_counts[ list_index ] > max_sprite_stats[ list_index ] 
)
+               {
+                       max_sprite_stats[ list_index ] = list_counts[ 
list_index ];
+               } 
+       }       
+}
 
- }
+unsigned long SpriteSorter::getMaxSprites(unsigned long height_level) const
+{
+       return( max_sprite_stats[ height_level ] );
+}
 
-unsigned long SpriteSorter::getMaxSprites( unsigned long height_level )
- {
-  return( max_sprite_stats[ height_level ] );
- }
Index: netpanzer/src/NetPanzer/Classes/SpriteSorter.hpp
diff -u netpanzer/src/NetPanzer/Classes/SpriteSorter.hpp:1.4 
netpanzer/src/NetPanzer/Classes/SpriteSorter.hpp:1.5
--- netpanzer/src/NetPanzer/Classes/SpriteSorter.hpp:1.4        Wed Sep 10 
08:32:53 2003
+++ netpanzer/src/NetPanzer/Classes/SpriteSorter.hpp    Wed Sep 10 09:20:57 2003
@@ -18,14 +18,14 @@
 #ifndef _SPRITESORTER_HPP
 #define _SPRITESORTER_HPP
 
+#include <vector>
 #include "Sprite.hpp"
-#include "PArrayGrowable.hpp"
 
 class SpriteSorter
  {  
   protected:
    iRect world_window;
-   PArrayGrowable sprite_lists[ _MAX_HEIGHT_LEVELS ];
+   std::vector<Sprite*> sprite_lists[ _MAX_HEIGHT_LEVELS ];
    unsigned long  list_counts[ _MAX_HEIGHT_LEVELS ];
  
    void sortLists( void );
@@ -62,11 +62,9 @@
 
    void blitLists( Surface *render_surf );
  
-   unsigned long getMaxSprites( unsigned long height_level );
- };
-
+   unsigned long getMaxSprites(unsigned long height_level) const;
+};
 
 extern SpriteSorter SPRITE_SORTER;
-
 
 #endif // _SPRITESORTER_HPP




reply via email to

[Prev in Thread] Current Thread [Next in Thread]