Subversion Repositories spk

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
11 cycrow 1
// GfxEntitySprite.cpp: implementation of the CRenderSprite class.
2
//
3
//////////////////////////////////////////////////////////////////////
4
 
5
#include "../Hook/MyDirect3DDevice9.h"
6
#include "RenderSprite.h"
7
#include <stdio.h>
8
 
9
//////////////////////////////////////////////////////////////////////
10
// Construction/Destruction
11
//////////////////////////////////////////////////////////////////////
12
 
13
CRenderSprite::CRenderSprite( MyDirect3DDevice9 *device )
14
{
15
	m_init = Init ( device );
16
 
17
	if ( !m_init )
18
		return;
19
 
20
	m_animStop = true;
21
	ResetAnimation ( &m_anim );
22
	m_iAnimation = 0;
23
	m_iCurFrame = 1;
24
}
25
 
26
bool CRenderSprite::LoadFile ( const char *filename )
27
{
28
	m_tfile = new CTextureFile ();
29
	if ( !m_tfile->Load ( filename ) )
30
	{
31
		m_init = false;
32
		return false;
33
	}
34
 
35
	strcpy_s ( m_file, 30, filename );
36
 
37
	m_init = InitialiseData ();
38
 
39
	return m_init;
40
}
41
 
42
void CRenderSprite::StartAnimation ( SAnimate *anim )
43
{
44
	if ( anim->iType == ANIM_FRAME )
45
	{
46
		SAnimateFrame *a = (SAnimateFrame *)anim;
47
		m_iAnimation = 0;
48
		ResetAnimation ( &m_anim );
49
		m_iCurFrame = a->iFrame;
50
		EndAnimation ( anim );
51
	}
52
	else if ( anim->iType == ANIM_ANIMATION )
53
	{
54
		ResetAnimation ( &m_anim );
55
		SAnimateAnim *a = (SAnimateAnim *)anim;
56
		m_iAnimation = a->iAnimation;
57
		EndAnimation ( anim );
58
	}
59
	else if ( anim->iType == ANIM_ANIMATIONSTOP )
60
	{
61
		m_animStop = true;
62
		EndAnimation ( anim );
63
	}
64
	else if ( anim->iType == ANIM_ANIMATIONCONT )
65
	{
66
		m_animStop = false;
67
		EndAnimation ( anim );
68
	}
69
}
70
 
71
void CRenderSprite::ParseSetting ( COverlay *overlay, CyString cmd, CyString rest, CyString section )
72
{
73
	cmd.ToUpper();
74
 
75
	if ( section == "ANIMATE" )
76
	{
77
		if ( cmd == "FRAME" )
78
		{
79
			SAnimateFrame *a = new SAnimateFrame;
80
			a->iStartTime = ConvertAnimationTime ( rest.GetToken ( ",", 1, 1 ) );
81
			a->iFrame = rest.GetToken ( ",", 2, 2 ).ConvertEquation();
82
			m_lAnimations.push_back ( a );
83
		}
84
		else if ( cmd == "ANIMATION" )
85
		{
86
			SAnimateAnim *a = new SAnimateAnim;
87
			a->iStartTime = ConvertAnimationTime ( rest.GetToken ( ",", 1, 1 ) );
88
			a->iAnimation = rest.GetToken ( ",", 2, 2 ).ConvertEquation();
89
			m_lAnimations.push_back ( a );
90
		}
91
		else if ( cmd == "ANIMATIONSTOP" )
92
		{
93
			SAnimateAnimStop *a = new SAnimateAnimStop;
94
			a->iStartTime = ConvertAnimationTime ( rest.GetToken ( ",", 1, 1 ) );
95
			m_lAnimations.push_back ( a );
96
		}
97
		else if ( cmd == "ANIMATIONREPEAT" )
98
		{
99
			SAnimateAnimCont *a = new SAnimateAnimCont;
100
			a->iStartTime = ConvertAnimationTime ( rest.GetToken ( ",", 1, 1 ) );
101
			m_lAnimations.push_back ( a );
102
		}
103
		else
104
			CRenderObject::ParseSetting ( overlay, cmd, rest, section );
105
	}
106
	else if ( (section == "MOUSEOVER") || (section == "ONCLICK") )
107
	{
108
		if ( cmd == "TEXTURE" )
109
		{
110
			SMOTexture *c = new SMOTexture;
111
			D3DXCreateTextureFromFile ( m_pDevice, rest.c_str(), &c->pTexture );
112
			if ( c->pTexture )
113
				m_lMouseOver.push_back ( c );
114
			else
115
				delete c;
116
		}
117
		else
118
			CRenderObject::ParseSetting ( overlay, cmd, rest, section );
119
	}
120
	else if ( section.Empty() )
121
	{
122
		if ( cmd == "FILE" )
123
			LoadFile ( rest.c_str() );
124
		else if ( cmd == "ANIMATION" )
125
			m_iAnimation = rest.ConvertEquation();
126
		else if ( cmd == "FRAME" )
127
			m_iCurFrame = rest.ConvertEquation();
128
		else 
129
			CRenderObject::ParseSetting ( overlay, cmd, rest, section );
130
	}
131
	else
132
		CRenderObject::ParseSetting ( overlay, cmd, rest, section );
133
}
134
 
135
bool CRenderSprite::Init( MyDirect3DDevice9 *device )
136
{
137
	m_fRot = 0;
138
	m_iCurFrame = 0;
139
 
140
	m_texture = NULL;
141
	m_collisionTexture = NULL;
142
	m_init = false;
143
 
144
	m_height = 0;
145
	m_width = 0;
146
	m_alphamap = NULL;
147
 
148
	m_center = false;
149
 
150
	m_frames.rows = 1;
151
	m_frames.cols = 1;
152
	m_frames.x = 0;
153
	m_frames.y = 0;
154
	m_tfile = NULL;
155
 
156
	m_file[0] = '\0';
157
 
158
	return true;
159
}
160
 
161
 
162
CRenderSprite::~CRenderSprite()
163
{ // release the texture and sprite when
164
	if ( m_texture != NULL )
165
		m_texture->Release ();
166
 
167
	if ( m_tfile )
168
		delete m_tfile;
169
}
170
 
171
void CRenderSprite::render ( MyDirect3DDevice9 *device, int x, int y, LPD3DXSPRITE sprite )
172
{
173
	int frame = m_iCurFrame;
174
 
175
	if ( (!m_anim.anim) && (m_iAnimation) )
176
	{
177
		m_anim.anim = m_tfile->GetAnimation ( m_iAnimation - 1 );
178
		m_anim.curframe = 1;
179
	}
180
 
181
	if ( m_anim.anim )
182
	{
183
		IncAnimationFrame ( &m_anim, m_animStop );
184
		frame = m_anim.curframe;
185
	}
186
 
187
	if ( !m_texture )
188
		return;
189
 
190
	D3DXVECTOR2 vCenter, vRotCenter;
191
 
192
	vCenter = D3DXVECTOR2( 0.0f, 0.0f );
193
	if ( m_fRot )
194
		vCenter = D3DXVECTOR2( (float)m_tfile->GetFrameWidth() / 2.0f, (float)m_tfile->GetFrameHeight() / 2.0f );
195
 
196
	D3DXVECTOR2 trans ( (float)(x + m_iX), (float)(y + m_iY) );
197
	D3DXVECTOR2 scaling ( 1.0f, 1.0f );
198
	D3DXMATRIX mat;
199
 
200
	vRotCenter = D3DXVECTOR2( (float)m_tfile->GetFrameWidth() / 2.0f, (float)m_tfile->GetFrameHeight() / 2.0f );
201
 
202
	D3DXMatrixTransformation2D ( &mat, NULL, 0.0, &scaling, &vRotCenter, DEGTORAD(m_fRot), &trans);
203
 
204
	sprite->SetTransform(&mat);
205
 
206
	D3DCOLOR color = D3DCOLOR_RGBA ( 255, 255, 255, m_iAlpha );
207
 
208
	if ( (frame) && (m_frames.x) && (m_frames.y) )
209
		sprite->Draw( m_texture, GetFrame ( frame ), NULL, &D3DXVECTOR3( 0.0, 0.0, 0.0), color);
210
	else
211
		sprite->Draw( m_texture, NULL, NULL, &D3DXVECTOR3( 0.0, 0.0, 0.0), color);
212
}
213
 
214
bool CRenderSprite::InitialiseData ()
215
{
216
	if ( (!m_pDevice) || (!m_tfile) )
217
		return false;
218
 
219
	m_texture = m_tfile->CreateTexture ( m_pDevice );
220
	if ( m_texture == NULL )
221
		return false;
222
 
223
	m_height = m_tfile->GetHeight ();
224
	m_width  = m_tfile->GetWidth ();
225
 
226
	SetFrames ( m_tfile->GetFrames() );
227
 
228
	return true;
229
}
230
 
231
bool CRenderSprite::CheckFile(const char *filename)
232
{
233
	if ( m_file[0] == '\0' )
234
		return false;
235
 
236
	if ( strcmp ( filename, m_file ) )
237
		return false;
238
 
239
	return true;
240
}
241
 
242
 
243
int CRenderSprite::GetHeight()
244
{
245
	return m_height;
246
}
247
 
248
int CRenderSprite::GetWidth()
249
{
250
	return m_width;
251
}
252
 
253
void CRenderSprite::SetCenter()
254
{
255
	m_center = true;
256
}
257
 
258
 
259
void CRenderSprite::SetFrames(const STextureFrames *frames)
260
{
261
	m_frames.cols = frames->cols;
262
	m_frames.rows = frames->rows;
263
	m_frames.x = frames->x;
264
	m_frames.y = frames->y;
265
 
266
	if ( (m_frames.x) && (m_frames.y) )
267
	{
268
		m_height = m_frames.y;
269
		m_width  = m_frames.x;
270
	}
271
}
272
 
273
RECT *CRenderSprite::GetFrame(int frame)
274
{
275
	RECT *rect = new RECT;
276
 
277
	int row = ((frame - 1) / m_frames.cols) + 1;
278
	int col = frame % m_frames.cols;
279
	if ( col == 0)
280
		col = m_frames.cols;
281
 
282
	int x = m_frames.x * col;
283
	int y = m_frames.y * row;
284
 
285
	SetRect ( rect, x - m_frames.x, y - m_frames.y, x, y );
286
 
287
	return rect;
288
}
289
 
290
CTextureFile * CRenderSprite::GetTextureFile()
291
{
292
	return m_tfile;
293
}