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 KYRA_SPRITERESOURCE_INCLUDED
33 #define KYRA_SPRITERESOURCE_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 "kyraresource.h"
42 // #include "tags.h"
43 #include "../engine/krmath.h"
44 #include "action.h"
45 #include <map>
46 
47 /** Practically, a sprite resource is used to create a sprite.
48 	A sprite resource is a type of resource and a container for
49 	actions.
50 
51 	SpriteResource
52 		Action
53 			Frames ( Rle )
54 */
55 class KrSpriteResource : public KrResource
56 {
57   public:
58 	// Create by reading from a .dat file
59 	KrSpriteResource( U32 size, SDL_RWops* data );
60 
61 	/*  Create the resource from the program. (Used by the sprite
62 		editor for animating and aligning sprites.)
63 		After construction, SetAction is called.
64 	*/
65 	KrSpriteResource( const std::string& spriteName );
66 
67 	virtual ~KrSpriteResource();
68 
69 	// Pass in an action to the resource. Used by the sprite editor.
70 	// Will allocate the action, if necessary.
71 	void AddAction( KrAction* action );
72 
Type()73 	virtual	U32 Type()								{ return KYRATAG_SPRITE; }
TypeName()74 	virtual const std::string&	TypeName()			{ return spriteName; }
ToSpriteResource()75 	virtual KrSpriteResource* ToSpriteResource()	{ return this; }
76 
77 	/// Return the count of actions in this sprite.
NumActions()78 	int		  NumActions()					{ return actionArr.size(); }
79 	/// Fetch an action from this Sprite Resource by the name of the action.
80 	KrAction* GetAction( const std::string& actionName );
81 	/// Fetch an action from this Sprite Resource by the id of the action.
82 	KrAction* GetAction( U32 actionId );
83 
84 	/** It is sometimes useful to get a Canvas Resource that
85 		is created from a sprite. This method calculates the bounds,
86 		creates the Resource, and returns it. If there is an error,
87 		0 is returned.
88 	*/
89 	KrCanvasResource* CreateCanvasResource(	const std::string& actionName,
90 												int frame, int* hotx, int* hoty );
91 
92 	/// Same functionality, uses the id instead of the name.
93 	KrCanvasResource* CreateCanvasResource(	U32 actionId,
94 												int frame, int* hotx, int* hoty );
95 
96 	// -- internal -- //
GetActionByIndex(int i)97 	KrAction* GetActionByIndex( int i )		{ return actionArr[i]; }
ActionByIndex(int i)98 	const KrAction& ActionByIndex( int i )	{ return *actionArr[i]; }
99 
100 	/*  Draw a sprite resource.
101 		@param	surface		Target surface.
102 		@param  paintInfo	Information about the target surface for drawing (optimizing).
103 		@param	action		The name of the action to draw.
104 		@param	frame		A frame # to draw.
105 		@param	x			X location in pixels.
106 		@param	y			Y location in pixels.
107 		@param  cForm		Color transformation applied to the drawing.
108 		@param	clip		A clipping rectangle, which can be null.
109 	*/
110 	void Draw( KrPaintInfo* paintInfo,
111 			   const std::string& action,
112 			   int frame,
113 			   const KrMatrix2& matrix,
114 			   const KrColorTransform& cForm,
115 			   const grinliz::Rectangle2I& clip,
116 			   int quality );
117 
118 	/*  Do a HitTest (see KrImageTree::HitTest) in transformed
119 		coordinates. So the tree object that made this call
120 		has already transformed the x and y into local pixel coords.
121 	*/
HitTestTransformed(int x,int y,int hitFlags)122 	bool HitTestTransformed( int x, int y, int hitFlags )	{	GLASSERT( 0 ); return false; }	// never called directly. Use the action.
123 
124 	virtual void CacheScale( GlFixed xScale, GlFixed yScale );
125 	virtual bool IsScaleCached( GlFixed xScale, GlFixed yScale );
126 	virtual void FreeScaleCache();
127 	virtual KrCollisionMap* GetCollisionMap( KrImage* state, int window );
128 
129 	virtual void Save( KrEncoder* );
130 
131   protected:
132 	std::vector< KrAction* >	actionArr;			// the actions are stored here
133 
134   private:
135 
136 	const static std::string spriteName;
137 
138 	std::map< std::string, KrAction* >  actionMap;		// the map is fast access to the action
139 	std::map< U32, KrAction* >			actionIdMap;	// the map is fast access to the action
140 };
141 
142 
143 #endif
144