kyraresource.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 #ifndef KR_RESOURCE_INCLUDED
00033 #define KR_RESOURCE_INCLUDED
00034 
00035 #ifdef _MSC_VER
00036 // Disable the no-exception handling warning.
00037 #pragma warning( disable : 4530 )
00038 #pragma warning( disable : 4786 )
00039 #endif
00040 
00041 #include <string>
00042 #include "../../grinliz/gltypes.h"
00043 #include "../../grinliz/glutil.h"
00044 #include "../util/glfixed.h"
00045 #include "../../grinliz/glgeometry.h"
00046 
00047 
00048 struct SDL_RWops;
00049 class KrSpriteResource;
00050 class KrTileResource;
00051 class KrCanvasResource;
00052 class KrFontResource;
00053 class KrBoxResource;
00054 class KrTextDataResource;
00055 class KrBinaryDataResource;
00056 class KrEncoder;
00057 class KrImage;
00058 
00059 
00062 enum
00063 {
00064         KYRATAG_END    = 1,
00065         KYRATAG_SPRITE,
00066         KYRATAG_TILE,
00067         KYRATAG_CANVAS,         // never in a .dat file - created at run-time
00068         KYRATAG_FONT,
00069         KYRATAG_BOX,
00070         KYRATAG_TEXTDATA,
00071         KYRATAG_BINARYDATA
00072 };
00073 
00074 
00075 // A structure -- cached in the resource -- that describes a collision bit map.
00076 class KrCollisionMap
00077 {
00078   public:
00079         KrCollisionMap( GlFixed xScale, GlFixed yScale,         // the scale is information only
00080                                         int width, int height );                        // pixel size of the map to create
00081 
00082         ~KrCollisionMap()                                                       { delete [] map; }
00083 
00084         // Input: creating the map.
00085         U32* GetRow( int i )                    {       GLASSERT( i>=0 && i<cy );
00086                                                                                 return map + cx * i;
00087                                                                         }
00088 
00089         // Output: using the map
00090         // Check a collision of something to the right.
00091         bool Collide( int dx, int dy, const grinliz::Rectangle2I& intersection, KrCollisionMap* );
00092 
00093         // Informational
00094         GlFixed XScale()                { return xScale; }
00095         GlFixed YScale()                { return yScale; }
00096 
00097   private:
00098         int cx, cy;
00099         U32* map;
00100         GlFixed xScale, yScale;
00101 };
00102 
00103 
00108 class KrResource
00109 {
00110   public:
00111         KrResource()                            : resId( 0 ) {}
00112         virtual ~KrResource()           {}
00113 
00115         virtual U32                                     Type() = 0;
00117         virtual const std::string&      TypeName() = 0;
00118 
00120         const std::string&      ResourceName()          { return resName; }
00122         const U32                       ResourceId()            { return resId; }       
00123 
00124         virtual KrSpriteResource* ToSpriteResource()    { return 0; }   
00125         virtual KrTileResource*   ToTileResource()              { return 0; }   
00126         virtual KrCanvasResource* ToCanvasResource()    { return 0; }   
00127         virtual KrFontResource*   ToFontResource()              { return 0; }   
00128         virtual KrBoxResource*    ToBoxResource()               { return 0; }   
00129         virtual KrTextDataResource*       ToTextDataResource()          { return 0; }   
00130         virtual KrBinaryDataResource* ToBinaryDataResource()    { return 0; }   
00131 
00138         virtual void CacheScale( GlFixed xScale, GlFixed yScale )       {}
00139 
00141         virtual bool IsScaleCached( GlFixed xScale, GlFixed yScale )    { return false; }
00142 
00144         virtual void FreeScaleCache()   {}
00145         
00146         // Used by the encoder:
00147         virtual void Save( KrEncoder* ) { GLASSERT( 0 ); }
00148 
00149         // If this is a type that supports a collision map, this will create it (if
00150         // necessary) and return a pointer to the map. The KrImage and window are
00151         // passed in to determine which map to fetch.
00152         virtual KrCollisionMap* GetCollisionMap( KrImage* state, int window )           { return 0; }
00153 
00154   protected:
00155         void SetNameAndId( const std::string& _name, U32 _id )  { resName = _name; resId = _id; }
00156         KrCollisionMap* collisionMap;
00157 
00158   private:
00159         std::string resName;
00160         U32                     resId;
00161 
00162 };
00163 
00164 
00165 class KrResourceFactory
00166 {
00167   public:
00168         virtual ~KrResourceFactory();
00169 
00170         static KrResourceFactory*       Instance();
00171 
00172         virtual KrResource*     Create( U32 id, 
00173                                                                 U32 size, 
00174                                                                 SDL_RWops* file );
00175 
00176   private:
00177         KrResourceFactory();
00178         static KrResourceFactory* instance;
00179 };
00180 
00181 
00182 
00183 #endif

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