1 /*--License:
2 	Kyra Sprite Engine
3 	Copyright Lee Thomason (Grinning Lizard Software) 2001-2005
4 	www.grinninglizard.com/kyra
5 	www.sourceforge.net/projects/kyra
6 
7 	Kyra is provided under the LGPL.
8 
9 	I kindly request you display a splash screen (provided in the HTML documentation)
10 	to promote Kyra and acknowledge the software and everyone who has contributed to it,
11 	but it is not required by the license.
12 
13 --- LGPL License --
14 
15     This library is free software; you can redistribute it and/or
16     modify it under the terms of the GNU Lesser General Public
17     License as published by the Free Software Foundation; either
18     version 2.1 of the License, or (at your option) any later version.
19 
20     This library is distributed in the hope that it will be useful,
21     but WITHOUT ANY WARRANTY; without even the implied warranty of
22     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23     Lesser General Public License for more details.
24 
25     You should have received a copy of the GNU Lesser General Public
26     License along with this library; if not, write to the Free Software
27     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28 
29 	The full text of the license can be found in lgpl.txt
30 */
31 
32 #ifndef KR_RESOURCE_INCLUDED
33 #define KR_RESOURCE_INCLUDED
34 
35 #ifdef _MSC_VER
36 // Disable the no-exception handling warning.
37 #pragma warning( disable : 4530 )
38 #pragma warning( disable : 4786 )
39 #endif
40 
41 #include <string>
42 #include "../../grinliz/gltypes.h"
43 #include "../../grinliz/glutil.h"
44 #include "../util/glfixed.h"
45 #include "../../grinliz/glgeometry.h"
46 
47 
48 struct SDL_RWops;
49 class KrSpriteResource;
50 class KrTileResource;
51 class KrCanvasResource;
52 class KrFontResource;
53 class KrBoxResource;
54 class KrTextDataResource;
55 class KrBinaryDataResource;
56 class KrEncoder;
57 class KrImage;
58 
59 
60 /** An enumeration of the possible data types in an Io DAT file.
61 */
62 enum
63 {
64 	KYRATAG_END    = 1,
65 	KYRATAG_SPRITE,
66 	KYRATAG_TILE,
67 	KYRATAG_CANVAS,		// never in a .dat file - created at run-time
68 	KYRATAG_FONT,
69 	KYRATAG_BOX,
70 	KYRATAG_TEXTDATA,
71 	KYRATAG_BINARYDATA
72 };
73 
74 
75 // A structure -- cached in the resource -- that describes a collision bit map.
76 class KrCollisionMap
77 {
78   public:
79 	KrCollisionMap( GlFixed xScale, GlFixed yScale,		// the scale is information only
80 					int width, int height );			// pixel size of the map to create
81 
~KrCollisionMap()82 	~KrCollisionMap()							{ delete [] map; }
83 
84 	// Input: creating the map.
GetRow(int i)85 	U32* GetRow( int i )			{	GLASSERT( i>=0 && i<cy );
86 										return map + cx * i;
87 									}
88 
89 	// Output: using the map
90 	// Check a collision of something to the right.
91 	bool Collide( int dx, int dy, const grinliz::Rectangle2I& intersection, KrCollisionMap* );
92 
93 	// Informational
XScale()94 	GlFixed XScale()		{ return xScale; }
YScale()95 	GlFixed YScale()		{ return yScale; }
96 
97   private:
98 	int cx, cy;
99 	U32* map;
100 	GlFixed xScale, yScale;
101 };
102 
103 
104 /**	The KrImNode's children - Sprite, Canvas, etc - are instances
105 	of corresponding resources. KrResource is the parent class for
106 	all resources.
107 */
108 class KrResource
109 {
110   public:
KrResource()111 	KrResource()				: resId( 0 ) {}
~KrResource()112 	virtual ~KrResource()		{}
113 
114 	/// The type of the resource. ( ex, KYRATAG_SPRITE )
115 	virtual U32					Type() = 0;
116 	/// The name of the type. ( ex, "Sprite" )
117 	virtual const std::string&	TypeName() = 0;
118 
119 	/// A unique name for this particular resource. ( "Warrior" )
ResourceName()120 	const std::string&	ResourceName()		{ return resName; }
121 	/// A unique id for this resource. (BEM_WARRIOR).
ResourceId()122 	const U32			ResourceId()		{ return resId; }
123 
ToSpriteResource()124 	virtual KrSpriteResource* ToSpriteResource()	{ return 0; }	///< Return a pointer if this is a sprite resource.
ToTileResource()125 	virtual KrTileResource*   ToTileResource()		{ return 0; }	///< Return a pointer if this is a tile resource.
ToCanvasResource()126 	virtual KrCanvasResource* ToCanvasResource()	{ return 0; }	///< Return a pointer if this is a canvas resource.
ToFontResource()127 	virtual KrFontResource*	  ToFontResource()		{ return 0; }	///< Return a pointer if this is a font resource.
ToBoxResource()128 	virtual KrBoxResource*	  ToBoxResource()		{ return 0; }	///< Return a pointer if this is a box resource.
ToTextDataResource()129 	virtual KrTextDataResource*	  ToTextDataResource()		{ return 0; }	///< Return a pointer if this is a text data resource.
ToBinaryDataResource()130 	virtual KrBinaryDataResource* ToBinaryDataResource()	{ return 0; }	///< Return a pointer if this is a binary data resource.
131 
132 	/**	Create a cached resourced. A resource can be drawn much
133 		more quickly if its scaled image is pre-calculated. This
134 		generates and scaled version of the resource and stores
135 		it for fast drawing. The cached version will always be
136 		used before a version generated on the fly.
137 	*/
CacheScale(GlFixed xScale,GlFixed yScale)138 	virtual void CacheScale( GlFixed xScale, GlFixed yScale )	{}
139 
140 	/// Check the cache for a given scale.
IsScaleCached(GlFixed xScale,GlFixed yScale)141 	virtual bool IsScaleCached( GlFixed xScale, GlFixed yScale )	{ return false; }
142 
143 	/// Free up the cache
FreeScaleCache()144 	virtual void FreeScaleCache()	{}
145 
146 	// Used by the encoder:
Save(KrEncoder *)147 	virtual void Save( KrEncoder* )	{ GLASSERT( 0 ); }
148 
149 	// If this is a type that supports a collision map, this will create it (if
150 	// necessary) and return a pointer to the map. The KrImage and window are
151 	// passed in to determine which map to fetch.
GetCollisionMap(KrImage * state,int window)152 	virtual KrCollisionMap* GetCollisionMap( KrImage* state, int window )		{ return 0; }
153 
154   protected:
SetNameAndId(const std::string & _name,U32 _id)155 	void SetNameAndId( const std::string& _name, U32 _id )	{ resName = _name; resId = _id; }
156 	KrCollisionMap* collisionMap;
157 
158   private:
159 	std::string resName;
160 	U32			resId;
161 
162 };
163 
164 
165 class KrResourceFactory
166 {
167   public:
168 	virtual ~KrResourceFactory();
169 
170 	static KrResourceFactory*	Instance();
171 
172 	virtual KrResource*	Create( U32 id,
173 								U32 size,
174 								SDL_RWops* file );
175 
176   private:
177 	KrResourceFactory();
178 	static KrResourceFactory* instance;
179 };
180 
181 
182 
183 #endif
184