dirtyrectangle.h

00001 /*--License:
00002         Kyra Sprite Engine
00003         Copyright Lee Thomason (Grinning Lizard Software) 2001-2005
00004         www.grinninglizard.com/kyra
00005         www.sourceforge.net/projects/kyra
00006 
00007         Kyra is provided under the LGPL. 
00008         
00009         I kindly request you display a splash screen (provided in the HTML documentation)
00010         to promote Kyra and acknowledge the software and everyone who has contributed to it, 
00011         but it is not required by the license.
00012 
00013 --- LGPL License --
00014 
00015     This library is free software; you can redistribute it and/or
00016     modify it under the terms of the GNU Lesser General Public
00017     License as published by the Free Software Foundation; either
00018     version 2.1 of the License, or (at your option) any later version.
00019 
00020     This library is distributed in the hope that it will be useful,
00021     but WITHOUT ANY WARRANTY; without even the implied warranty of
00022     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00023     Lesser General Public License for more details.
00024 
00025     You should have received a copy of the GNU Lesser General Public
00026     License along with this library; if not, write to the Free Software
00027     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00028 
00029         The full text of the license can be found in lgpl.txt
00030 */
00031 
00032 
00033 #ifndef DIRTY_RECTANGLE_INCLUDED
00034 #define DIRTY_RECTANGLE_INCLUDED
00035 
00036 
00037 #include "../engine/krmath.h"
00038 #include "../util/gllist.h"
00039 
00040 
00041 #ifdef DEBUG
00042         #include "SDL.h"
00043 #endif
00044 
00045 
00046 class KrDirtyRectangle;
00047 struct SDL_Surface;
00048 
00049 
00050 struct KrMappedRectInfo
00051 {
00052         int xmin;
00053         int ymin;
00054         int hPixelsPerBit;
00055         int vPixelsPerRow;
00056 
00057         void Set( const grinliz::Rectangle2I& bounds );
00058 };
00059 
00060 
00061 /*      Specialized rectangles.
00062         This adds a 4x8 pixel map (32 bits) that can be used to "quick compare" 
00063         the rectangles.
00064 
00065         Per byte, the min bit is at x=0, the max bit the right x.
00066         Each byte is a row, starting with y=0.
00067 */
00068 class KrMappedRect : public grinliz::Rectangle2I
00069 {
00070   public:
00071         KrMappedRect() { min.x = -1; min.y = -1; max.x = -2; max.y = -2; }
00072         KrMappedRect( const grinliz::Rectangle2I& from, const KrMappedRectInfo& info ) 
00073                 { min.x = from.min.x; min.y = from.min.y; max.x = from.max.x; max.y = from.max.y; CalcMap( info ); }
00074         KrMappedRect( const KrMappedRect& from ) 
00075                 { min.x = from.min.x; min.y = from.min.y; max.x = from.max.x; max.y = from.max.y; map = from.map; }
00076 
00077         void Set( const grinliz::Rectangle2I& from, const KrMappedRectInfo& info )
00078         {
00079                 min.x = from.min.x;
00080                 min.y = from.min.y;
00081                 max.x = from.max.x;
00082                 max.y = from.max.y;
00083                 CalcMap( info ); 
00084         }
00085 
00086         void operator=( const KrMappedRect& from )
00087         {
00088                 min.x = from.min.x;
00089                 min.y = from.min.y;
00090                 max.x = from.max.x;
00091                 max.y = from.max.y;
00092                 map  = from.map; 
00093         }
00094 
00095         void CalcMap( const KrMappedRectInfo& );
00096 
00097         bool Intersect( const KrMappedRect& rect ) const
00098         {
00099                 GLASSERT( rect.map );
00100 
00101                 if (    ( ( rect.map & map ) == 0 )
00102                              || rect.max.x < min.x
00103                                  || rect.min.x > max.x
00104                                  || rect.max.y < min.y
00105                                  || rect.min.y > max.y )
00106                 {
00107                         return false;
00108                 }
00109                 return true;
00110         }       
00111 
00112         U32 Map() const         { return map; }
00113   private:
00114         U32 map;
00115 };
00116 
00131 class KrDirtyRectangle
00132 {
00133   public:
00134         KrDirtyRectangle();
00135         ~KrDirtyRectangle();
00136 
00138         void SetClipping( const grinliz::Rectangle2I& rect )            
00139                                         { clippingRect = rect; clipping = true; mappedInfo.Set( clippingRect ); }
00141         bool IsClipping()                                                       { return clipping; }
00142 
00144         void AddRectangle( const grinliz::Rectangle2I& rect );
00146         void Clear()                                                            {       //GLOUTPUT( "Rects cleared.\n" );
00147                                                                                                         nRect = 0; }
00148         int                       NumRect()                                             { return nRect;         }
00149         const KrMappedRect& Rect( int i )                       {       GLASSERT( i>=0 && i<nRect );
00150                                                                                                         return rectArray[i];    }
00151 
00152         // The DR maintains both a blit list and a dirty rectangle list.
00153         // The blit rects are >= the dirty rectangle list.
00154         // This blits the blit rects to the screen.
00155         //void UpdateScreen( SDL_Surface* screen );
00156 
00157         #ifdef DEBUG
00158                 void DrawAllRects( SDL_Surface* surface );
00159 //              void DrawBlitRects( SDL_Surface* surface );
00160                 void DrawRects( SDL_Surface* surface );
00161                 void DrawWindow( int y0, int y1 );
00162 
00163                 void PrintRects( const char* message );
00164         #endif
00165 
00166         enum
00167         {
00168                 // Should be greater than 32. 35+ for some
00169                 // working room. The test I have seem to settle
00170                 // at the best performance at this point, but
00171                 // other apps might be happier with a different
00172                 // number.
00173                 MAX_DIRTY_RECTANGLES = 128,
00174         };
00175 
00176   private:
00177         void Remove( int index );
00178         void HandleOutOfRect( const KrMappedRect& rect );
00179         
00180         KrMappedRect rectArray[ MAX_DIRTY_RECTANGLES ];
00181         
00182         grinliz::Rectangle2I    clippingRect;
00183         bool    clipping;
00184 
00185         int nRect;
00186         KrMappedRectInfo mappedInfo;
00187 };
00188 
00189 
00190 
00191 #endif

Generated on Thu Jul 20 20:45:31 2006 for Kyra by  doxygen 1.4.7