Main Page   Class Hierarchy   Compound List   File List   Compound Members  

rle.h

00001 /*--License:
00002         Kyra Sprite Engine
00003         Copyright Lee Thomason (Grinning Lizard Software) 2001-2002
00004         www.grinninglizard.com/kyra
00005         www.sourceforge.net/projects/kyra
00006 
00007         Kyra is provided under 2 licenses:
00008 
00009         - The GPL, with no additional restrictions.
00010         - The LGPL, provided you display the Kyra splash screen, described below.
00011 
00012 
00013 --- GPL License --
00014         This program is free software; you can redistribute it and/or
00015         modify it under the terms of the GNU General Public License
00016         as published by the Free Software Foundation; either version 2
00017         of the License, or (at your option) any later version.
00018 
00019         This program is distributed in the hope that it will be useful,
00020         but WITHOUT ANY WARRANTY; without even the implied warranty of
00021         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022         GNU General Public License for more details.
00023 
00024         You should have received a copy of the GNU General Public License
00025         along with this program; if not, write to the Free Software
00026         Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00027 
00028         The full text of the license can be found in license.txt
00029 
00030 
00031 --- LGPL License --
00032   **Provided you kindly display the Kyra splash screen (details below), 
00033         you     may use the LGPL license:**
00034 
00035     This library is free software; you can redistribute it and/or
00036     modify it under the terms of the GNU Lesser General Public
00037     License as published by the Free Software Foundation; either
00038     version 2.1 of the License, or (at your option) any later version.
00039 
00040     This library is distributed in the hope that it will be useful,
00041     but WITHOUT ANY WARRANTY; without even the implied warranty of
00042     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00043     Lesser General Public License for more details.
00044 
00045     You should have received a copy of the GNU Lesser General Public
00046     License along with this library; if not, write to the Free Software
00047     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00048 
00049         The full text of the license can be found in lgpl.txt
00050 
00051 
00052 --- Kyra Splash Screen.
00053 
00054         It would be appreciate if you display the Kyra splash screen when using
00055         either license, however it is only required for the LGPL. All the
00056         resources for the splash are compiled into the library, and it can be
00057         accessed through the following API:
00058 
00059                 KrEngine::StartSplash
00060                 KrEngine::UpdateSplash
00061                 KrEngine::EndSplash
00062 
00063         Full documentation is provided with the KrEngine class. The splash screen
00064         should be displayed for 2 seconds.
00065 
00066         Thank you.
00067 */
00068 
00069 #ifndef KYRA_RLE_INCLUDED
00070 #define KYRA_RLE_INCLUDED
00071 
00072 #include <stdlib.h>
00073 
00074 #include "../util/gltypes.h"
00075 #include "../util/gldebug.h"
00076 #include "../engine/krmath.h"
00077 #include "../util/glmemorypool.h"
00078 #include "../util/glbitstream.h"
00079 #include "SDL.h"
00080 #include "painter.h"
00081 
00082 class KrCanvasResource;
00083 class KrTexture;
00084 class KrCollisionMap;
00085 
00086 
00087 class KrRleSegment
00088 {
00089         friend class KrRle;
00090   public:
00091         KrRleSegment()  : start( 0 ), end( 0 ), rgba( 0 )       {}
00092         ~KrRleSegment();
00093 
00094         void Clear()    { start = 0; end = 0; rgba = 0; }
00095         
00096         U32                             Alpha()         { return flags.IsSet( ALPHA ); }
00097         U16                             Start()         { return start; }
00098         U16                             End()           { return end; }
00099         U16                             Len()           { return end - start + 1; }
00100 
00101         KrRGBA*                 RGBA()      { return rgba; }
00102         GlFlag<U32>             Flags()         { return flags; }
00103 
00104         // For creating the RLE in the reader.
00105         bool Read( GlReadBitStream* reader, KrRGBA minColor, KrRGBA bits );
00106 
00107         // For creating the RLE
00108         bool Create( KrPaintInfo* surface, 
00109                                  int x, int xMax, int y, int objectOriginX );
00110 
00111         bool Write( GlWriteBitStream* writer, KrRGBA minColor, KrRGBA bits );
00112         void CalcRange( KrRGBA* minColor, KrRGBA* maxColor );
00113 
00114         #ifdef DEBUG
00115                 static U32 numRGBA;
00116         #endif
00117 
00118   protected:
00119 //      static GlLinearMemoryPool*      memoryPool;
00120 
00121         enum {
00122                 ALPHA =                 0x01,
00123                 //COMPRESS8BIT =        0x02,   // Set if we can write the length and skip in 8 bits.
00124 
00125                 BITS_USED =     3,                      // For compression.
00126                 MEMORYPOOL =    0x02,           // Is the rgba out of a memory pool?
00127         };
00128 
00129         GlFlag<U32>     flags;                  
00130         U16                     start;          // measured from the start of the line, which can be a surprise.        
00131         U16                     end;            // measured from the start of the line
00132         KrRGBA*         rgba;
00133 };
00134 
00135 
00136 class KrRleLine
00137 {
00138         friend class KrRle;
00139   public:
00140         KrRleLine()     :               nSegments( 0 ), segment( 0 ) {}
00141         ~KrRleLine()            { if (!flags.IsSet( MEMORYPOOL ) ) delete [] segment; }
00142 
00143         void                    Clear()                 {       nSegments = 0; segment = 0; }
00144         U32                             Alpha()                 {       return flags.IsSet( ALPHA ); }
00145         int                             NumSegments()   {       return nSegments; }
00146         KrRleSegment*   Segment( int i ){       GLASSERT( i < nSegments );
00147                                                                                 GLASSERT( segment );
00148                                                                                 return &segment[i]; }
00149         int                             CalcSize()              {       if ( nSegments )
00150                                                                                         return ( segment[ nSegments-1 ].End() - segment[ 0 ].Start() + 1 );
00151                                                                                 else
00152                                                                                         return 0; }
00153 
00154         /*
00155         Pushed to the RLE
00156         void Draw( U8* target,                          // pointer to beginning of scanline
00157                            KrPaintInfo* paintInfo,
00158                            const KrRect& bounds,        // bounds of the RLE
00159                            const KrRect& isect,         // intersection of the RLE and DirtyRectangle
00160                            const KrColorTransform& cForm );     // color transform to use
00161         */
00162 
00163         void DrawScaled(U8* target,                             // pointer to beginning of line (will be offset from here.)
00164                                         KrPaintInfo* paintInfo,
00165                                         const KrRect& bounds,   // bounds of the RLE
00166                                         const KrRect& isect,            // intersection of the RLE and DirtyRectangle
00167                                         const KrColorTransform& cForm,  // color transform to use
00168                                         U32 xInc );
00169 
00170         // For creating the RLE in the reader.
00171         bool Read( GlReadBitStream* writer, KrRGBA minColor, KrRGBA bits );
00172 
00173         // For creating the RLE
00174         bool Create( KrPaintInfo* surface, int x, int y, int w );
00175         bool Write( GlWriteBitStream* writer, KrRGBA minColor, KrRGBA bits );
00176         void CalcRange( KrRGBA* minColor, KrRGBA* maxColor );
00177 
00178   protected:
00179         enum {
00180                 ALPHA =                 0x01,
00181                 MEMORYPOOL =    0x02,           // Is the semgment allocated out of a pool?
00182 
00183                 BITS_USED = 2
00184         };
00185 
00186         GlFlag<U32>             flags;                  
00187         int                             nSegments;              
00188         
00189         KrRleSegment*   segment;
00190 };
00191 
00192 
00193 /*      A basic drawing thing. A Run length encoded image.
00194 
00195         WARNING: The KrAction::GrowFrameArray copies these things
00196         like structures. It is careful to 0 out old memory, but if
00197         anything ever has a pointer *back* to the Rle, the pointers
00198         will be trashed. Could be fixed with a proper copy constructor,
00199         although that would be difficult.
00200 */
00201 class KrRle
00202 {
00203   public:
00204         // The flags are never used directly by the engine code using
00205         // this class. However, the tile code is borrowing id's
00206         // for consistency.
00207         enum {
00208                 ALPHA =         0x01,
00209                 MEMORYPOOL = 0x02,      // is the line out of a memory pool?
00210         };
00211 
00212         KrRle() : line( 0 ), texture( 0 ), collisionMap( 0 )
00213                                                                 { deltaHotToOrigin.Set(); size.Set(); delta.Set(); }
00214         ~KrRle();
00215         
00216         void Draw( KrPaintInfo* paintInfo,
00217                            const KrMatrix2& matrix,
00218                            const KrColorTransform& cForm,       // color transform to use
00219                            const KrRect& clipping,
00220                            int openGLZ );
00221                 
00222         // Reads the RLE from a stream. Returns true if not empty.
00223         bool Read( SDL_RWops* stream );
00224 
00225         // The encoder uses this to create the sprite. Returns true if not empty.
00226         bool Create( KrPaintInfo* surface, 
00227                                  int x, int y, int width, int height,
00228                                  int hotx, int hoty,
00229                                  int deltax, int deltay );
00230 
00231         // Writes the RLE sprite to the stream.
00232         bool Write( SDL_RWops* stream );
00233 
00234         // The x and y "step" for this sprite.
00235         const KrVector2& StepSize()     const   { return delta; }
00236 
00237         int Width()     const   { return size.x; }      
00238         int Height() const      { return size.y; }      
00239 
00240         bool Alpha() const      { return flags.IsSet( ALPHA ); }
00241         const KrVector2& Delta() const                  { return delta; }
00242         KrVector2                Hotspot() const                {       KrVector2 hot = deltaHotToOrigin;
00243                                                                                                 hot.x = -hot.x; hot.y = -hot.y;
00244                                                                                                 return hot;
00245                                                                                         }
00246                 
00247         /*      Given a hotspot x and y, return the bounding box
00248                 for the rle.
00249         */
00250         void CalculateBounds( const KrMatrix2& xForm,
00251                                                   KrRect* bounds ) const;
00252 
00253         bool HitTestTransformed( int x, int y, int hitFlags );
00254 
00255         // Count all the parts of this object. Used by the encoder.
00256         void CountComponents( U32* numLines, U32* numSegments, U32* numRGBA );
00257 
00258         KrCanvasResource* CreateCanvasResource( int* hotx, int* hoty );
00259         KrRle*            CreateScaledRle( GlFixed xScale, GlFixed yScale, int* hotx, int* hoty );
00260         KrCollisionMap*   GetCollisionMap( GlFixed xScale, GlFixed yScale );
00261 
00262         // Scary function: basically sets the global memory allocator.
00263         // The vault will set this, allocate a bunch of sprites,
00264         // then clear it. Will cause problems in a multi-threaded version...
00265         static void SetMemoryPools(             GlLinearMemoryPool*     _memoryPoolRgba,
00266                                                                         GlLinearMemoryPool*     _memoryPoolLine,
00267                                                                         GlLinearMemoryPool*     _memoryPoolSegment );
00268         static bool PoolOut();
00269 
00270         // Treat as private:
00271         // These are NOT owned by the Rle, but the vault. Set here temporarily.
00272         static GlLinearMemoryPool*      memoryPoolRGBA;
00273         static GlLinearMemoryPool*      memoryPoolLine;
00274         static GlLinearMemoryPool*      memoryPoolSegment;
00275 
00276   protected:
00277         void DrawScaled(        KrPaintInfo* paintInfo,
00278                                                 const KrMatrix2& xForm,
00279                                                 const KrColorTransform& cForm,
00280                                                 const KrRect& clipping );
00281 
00282         void DrawOpenGL(        KrPaintInfo* paintInfo,
00283                                                 const KrMatrix2& matrix,
00284                                                 const KrColorTransform& cForm,
00285                                                 const KrRect& clipping,
00286                                                 int openGLZ );
00287 
00288         GlFlag<U32>     flags;                          
00289         KrRleLine*      line;                           // An array of lines, each which can (somewhat) draw themselves.
00290         KrVector2       deltaHotToOrigin;       // Offset between the hotspot and the start of the image
00291         KrVector2       size;                           // x and y extends of this RLE
00292         KrVector2   delta;                              // motion delta for this RLE
00293         KrTexture*      texture;                        // used for openGL texturing
00294         KrCollisionMap* collisionMap;   
00295 };
00296 
00297 
00298 
00299 #endif

Generated on Mon Sep 15 12:01:11 2003 for Kyra by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001