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_ACTION_INCLUDED
33 #define KYRA_ACTION_INCLUDED
34 
35 #ifdef _MSC_VER
36 #pragma warning( disable : 4530 )
37 #pragma warning( disable : 4786 )
38 #endif
39 
40 #include <string>
41 #include <vector>
42 
43 #include "SDL.h"
44 #include "rle.h"
45 
46 class KrCanvasResource;
47 class KrEncoder;
48 class KrCollisionMap;
49 
50 
51 /** An action is contained by a sprite resource and is itself a
52 	container for frames.
53 	@sa KrSprite
54 */
55 class KrAction
56 {
57   public:
58 	/*  Create an empty KrAction. Used with the AddFrame method
59 		to create an action.
60 	*/
61 	KrAction( const std::string& name );
62 
63 	/// Create the action by reading from a file. Used by the sprite constructor.
64 	KrAction( SDL_RWops* data );
65 
66 	~KrAction();
67 
68 	/// Each action has a name (ex, "Walking")
Name()69 	const std::string& Name() const		{ return name; }
70 	/// Each action has a unique id. (ex, WALKING)
Id()71 	U32				   Id() const		{ return id; }
72 
73 	/*  Draw the action to the surface.
74 		Normally called by the Sprite.
75 		@param paintInfo	Target surface information
76 		@param frame		The frame number to draw.
77 		@param x			x value to draw -- measured at hotspot.
78 		@param y			y value to draw -- measured at hotspot.
79 		@param cForm		The color transformation to use.
80 		@param clip			A clipping rectangle. (Can be NULL)
81 	*/
82 	void Draw( KrPaintInfo* paintInfo,
83 			   int frame,
84 			   const KrMatrix2& matrix,
85 			   const KrColorTransform& cForm,
86 			   const grinliz::Rectangle2I& clip );
87 
88 	/// Total number of frames.
NumFrames()89 	int   NumFrames() const 			{ return numFrames; }
90 
91 	/// Get a frame.
Frame(int i)92 	const KrRle& Frame( int i ) const	{ GLASSERT( i >= 0 );
93 										  GLASSERT( i < numFrames );
94 										  return frame[ i ]; }
95 
96 	/*  Get a non-conts pointer to the frame. A special case call.
97 	*/
GetFrame(int i)98 	KrRle* GetFrame( int i ) const		{ GLASSERT( i >= 0 );
99 										  GLASSERT( i < numFrames );
100 										  return &frame[ i ]; }
101 
102 	bool HitTestTransformed( int frame, int x, int y, int hitFlags );
103 
104 	// internal
105 	void CacheScale( GlFixed xScale, GlFixed yScale );
106 	bool IsScaleCached( GlFixed xScale, GlFixed yScale );
107 	void FreeScaleCache();
108 	KrCollisionMap* GetCollisionMap( GlFixed xScale, GlFixed yScale, int frame );
109 
AddFrame()110 	void AddFrame()		{ GrowFrameArray( numFrames + 1 ); }
111 
112 	/** A strange -- but sometimes useful function. Creates
113 		a canvas from this action and returns it.
114 	*/
115 	KrCanvasResource* CreateCanvasResource( int frame, int* hotx, int* hoty );
116 
117 	void CalculateBounds( int frame, const KrMatrix2& xForm, grinliz::Rectangle2I* bounds );
118 
119 	void Save( KrEncoder* encoder );
120 
121   private:
122 	struct CachedBlock
123 	{
124 		GlFixed xScale,
125 				yScale;
126 		KrRle** frame;
127 
128 		bool operator==( const CachedBlock& )	{ GLASSERT( 0 ); return false; }		// be nice to buggy compilers.
129 	};
130 	void GrowFrameArray( int newSize );
131 
132 	std::vector< CachedBlock > cache;
133 
134 	std::string		name;
135 	U32				id;
136 	KrRle*			frame;			// Can't use dynamic array since it doesn't have proper copy
137 	int				numFrames;
138 };
139 
140 
141 #endif
142