Blame | Last modification | View Log | RSS feed
// GfxEntitySprite.cpp: implementation of the CRenderSprite class.
//
//////////////////////////////////////////////////////////////////////
#include "../Hook/MyDirect3DDevice9.h"
#include "RenderSprite.h"
#include <stdio.h>
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CRenderSprite::CRenderSprite( MyDirect3DDevice9 *device )
{
m_init = Init ( device );
if ( !m_init )
return;
m_animStop = true;
ResetAnimation ( &m_anim );
m_iAnimation = 0;
m_iCurFrame = 1;
}
bool CRenderSprite::LoadFile ( const char *filename )
{
m_tfile = new CTextureFile ();
if ( !m_tfile->Load ( filename ) )
{
m_init = false;
return false;
}
strcpy_s ( m_file, 30, filename );
m_init = InitialiseData ();
return m_init;
}
void CRenderSprite::StartAnimation ( SAnimate *anim )
{
if ( anim->iType == ANIM_FRAME )
{
SAnimateFrame *a = (SAnimateFrame *)anim;
m_iAnimation = 0;
ResetAnimation ( &m_anim );
m_iCurFrame = a->iFrame;
EndAnimation ( anim );
}
else if ( anim->iType == ANIM_ANIMATION )
{
ResetAnimation ( &m_anim );
SAnimateAnim *a = (SAnimateAnim *)anim;
m_iAnimation = a->iAnimation;
EndAnimation ( anim );
}
else if ( anim->iType == ANIM_ANIMATIONSTOP )
{
m_animStop = true;
EndAnimation ( anim );
}
else if ( anim->iType == ANIM_ANIMATIONCONT )
{
m_animStop = false;
EndAnimation ( anim );
}
}
void CRenderSprite::ParseSetting ( COverlay *overlay, CyString cmd, CyString rest, CyString section )
{
cmd.ToUpper();
if ( section == "ANIMATE" )
{
if ( cmd == "FRAME" )
{
SAnimateFrame *a = new SAnimateFrame;
a->iStartTime = ConvertAnimationTime ( rest.GetToken ( ",", 1, 1 ) );
a->iFrame = rest.GetToken ( ",", 2, 2 ).ConvertEquation();
m_lAnimations.push_back ( a );
}
else if ( cmd == "ANIMATION" )
{
SAnimateAnim *a = new SAnimateAnim;
a->iStartTime = ConvertAnimationTime ( rest.GetToken ( ",", 1, 1 ) );
a->iAnimation = rest.GetToken ( ",", 2, 2 ).ConvertEquation();
m_lAnimations.push_back ( a );
}
else if ( cmd == "ANIMATIONSTOP" )
{
SAnimateAnimStop *a = new SAnimateAnimStop;
a->iStartTime = ConvertAnimationTime ( rest.GetToken ( ",", 1, 1 ) );
m_lAnimations.push_back ( a );
}
else if ( cmd == "ANIMATIONREPEAT" )
{
SAnimateAnimCont *a = new SAnimateAnimCont;
a->iStartTime = ConvertAnimationTime ( rest.GetToken ( ",", 1, 1 ) );
m_lAnimations.push_back ( a );
}
else
CRenderObject::ParseSetting ( overlay, cmd, rest, section );
}
else if ( (section == "MOUSEOVER") || (section == "ONCLICK") )
{
if ( cmd == "TEXTURE" )
{
SMOTexture *c = new SMOTexture;
D3DXCreateTextureFromFile ( m_pDevice, rest.c_str(), &c->pTexture );
if ( c->pTexture )
m_lMouseOver.push_back ( c );
else
delete c;
}
else
CRenderObject::ParseSetting ( overlay, cmd, rest, section );
}
else if ( section.Empty() )
{
if ( cmd == "FILE" )
LoadFile ( rest.c_str() );
else if ( cmd == "ANIMATION" )
m_iAnimation = rest.ConvertEquation();
else if ( cmd == "FRAME" )
m_iCurFrame = rest.ConvertEquation();
else
CRenderObject::ParseSetting ( overlay, cmd, rest, section );
}
else
CRenderObject::ParseSetting ( overlay, cmd, rest, section );
}
bool CRenderSprite::Init( MyDirect3DDevice9 *device )
{
m_fRot = 0;
m_iCurFrame = 0;
m_texture = NULL;
m_collisionTexture = NULL;
m_init = false;
m_height = 0;
m_width = 0;
m_alphamap = NULL;
m_center = false;
m_frames.rows = 1;
m_frames.cols = 1;
m_frames.x = 0;
m_frames.y = 0;
m_tfile = NULL;
m_file[0] = '\0';
return true;
}
CRenderSprite::~CRenderSprite()
{ // release the texture and sprite when
if ( m_texture != NULL )
m_texture->Release ();
if ( m_tfile )
delete m_tfile;
}
void CRenderSprite::render ( MyDirect3DDevice9 *device, int x, int y, LPD3DXSPRITE sprite )
{
int frame = m_iCurFrame;
if ( (!m_anim.anim) && (m_iAnimation) )
{
m_anim.anim = m_tfile->GetAnimation ( m_iAnimation - 1 );
m_anim.curframe = 1;
}
if ( m_anim.anim )
{
IncAnimationFrame ( &m_anim, m_animStop );
frame = m_anim.curframe;
}
if ( !m_texture )
return;
D3DXVECTOR2 vCenter, vRotCenter;
vCenter = D3DXVECTOR2( 0.0f, 0.0f );
if ( m_fRot )
vCenter = D3DXVECTOR2( (float)m_tfile->GetFrameWidth() / 2.0f, (float)m_tfile->GetFrameHeight() / 2.0f );
D3DXVECTOR2 trans ( (float)(x + m_iX), (float)(y + m_iY) );
D3DXVECTOR2 scaling ( 1.0f, 1.0f );
D3DXMATRIX mat;
vRotCenter = D3DXVECTOR2( (float)m_tfile->GetFrameWidth() / 2.0f, (float)m_tfile->GetFrameHeight() / 2.0f );
D3DXMatrixTransformation2D ( &mat, NULL, 0.0, &scaling, &vRotCenter, DEGTORAD(m_fRot), &trans);
sprite->SetTransform(&mat);
D3DCOLOR color = D3DCOLOR_RGBA ( 255, 255, 255, m_iAlpha );
if ( (frame) && (m_frames.x) && (m_frames.y) )
sprite->Draw( m_texture, GetFrame ( frame ), NULL, &D3DXVECTOR3( 0.0, 0.0, 0.0), color);
else
sprite->Draw( m_texture, NULL, NULL, &D3DXVECTOR3( 0.0, 0.0, 0.0), color);
}
bool CRenderSprite::InitialiseData ()
{
if ( (!m_pDevice) || (!m_tfile) )
return false;
m_texture = m_tfile->CreateTexture ( m_pDevice );
if ( m_texture == NULL )
return false;
m_height = m_tfile->GetHeight ();
m_width = m_tfile->GetWidth ();
SetFrames ( m_tfile->GetFrames() );
return true;
}
bool CRenderSprite::CheckFile(const char *filename)
{
if ( m_file[0] == '\0' )
return false;
if ( strcmp ( filename, m_file ) )
return false;
return true;
}
int CRenderSprite::GetHeight()
{
return m_height;
}
int CRenderSprite::GetWidth()
{
return m_width;
}
void CRenderSprite::SetCenter()
{
m_center = true;
}
void CRenderSprite::SetFrames(const STextureFrames *frames)
{
m_frames.cols = frames->cols;
m_frames.rows = frames->rows;
m_frames.x = frames->x;
m_frames.y = frames->y;
if ( (m_frames.x) && (m_frames.y) )
{
m_height = m_frames.y;
m_width = m_frames.x;
}
}
RECT *CRenderSprite::GetFrame(int frame)
{
RECT *rect = new RECT;
int row = ((frame - 1) / m_frames.cols) + 1;
int col = frame % m_frames.cols;
if ( col == 0)
col = m_frames.cols;
int x = m_frames.x * col;
int y = m_frames.y * row;
SetRect ( rect, x - m_frames.x, y - m_frames.y, x, y );
return rect;
}
CTextureFile * CRenderSprite::GetTextureFile()
{
return m_tfile;
}