📄 gsanimation.cpp
字号:
// GsAnimation.cpp: implementation of the CGsAnimation class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "GsAnimation.h"
#include "gif89a.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CGsAnimation::CGsAnimation(CGsEngine* pEngine)
{
m_pEngine = pEngine;
m_texture_group = NULL;
m_current_frame = 0;
m_start_frame = 0;
m_end_frame = 0;
m_num_frame = 0;
_m_frame_delay = 30;
_m_base_tick = 0;
_m_pass_tick = 0;
_m_play_tick = 0;
_m_is_frame_up = TRUE;
_m_play_flag = GS_ANI_PLAY_NONE;
m_key = "";
m_key_config = "";
m_apFrameData = NULL;
m_num_direction = 1;
m_frame_per_direction = 0;
m_angle_base = 0.0f;
m_base_frame = 0;
m_angle_face_to = 0;
m_percent_by = 0;
m_flag = 0;
}
CGsAnimation::~CGsAnimation()
{
Cleanup();
}
VOID CGsAnimation::Cleanup()
{
SAFE_FREE(m_texture_group);
m_current_frame = 0;
m_start_frame = 0;
m_end_frame = 0;
m_num_frame = 0;
_m_frame_delay = 300;
_m_base_tick = 0;
_m_pass_tick = 0;
_m_play_tick = 0;
_m_is_frame_up = TRUE;
_m_play_flag = GS_ANI_PLAY_NONE;
m_key = "";
m_key_config = "";
m_num_direction = 1;
m_frame_per_direction = 0;
m_angle_base = 0.0f;
m_base_frame = 0;
m_angle_face_to = 0;
SAFE_DELETE_ARRAY(m_apFrameData);
}
BOOL CGsAnimation::Create(const char *strDesc)
{
Cleanup();
m_key = strDesc;
if(m_pEngine==NULL)
return false;
char* sz;
char* sz_scanf;
char strRes[1000];
char strSeting[100]="";
strcpy(strRes, strDesc);
if(sz=strstr(strRes, ","))
{
strcpy(strSeting, sz+1);
strcpy(sz,"");
}
m_texture_group = m_pEngine->FindTextureGroup(strRes);
if(NULL==m_texture_group)
{
CConfig cfg;
if(!m_pEngine->LoadConfig(cfg, strRes))
return FALSE;
if(cfg.GetString("GLOBALS", "config")!=KEY("animation"))
return FALSE;
m_frame_per_direction = cfg.GetLong("MAIN", "frame_count");
if(m_frame_per_direction<=0)
return FALSE;
m_texture_group = m_pEngine->FindTextureGroup(cfg.GetString("MAIN", "image"));
if(m_texture_group==NULL)
return FALSE;
m_flag = m_texture_group->GetFlag();
m_num_frame = m_texture_group->GetNumTexture();
if(m_num_frame==0)
{
Cleanup();
return FALSE;
}
m_num_direction = m_texture_group->GetTeam();
if(m_num_direction<=0)
m_num_direction = 1;
m_apFrameData = new GSANI_FRAME[m_frame_per_direction];
if(m_apFrameData==NULL)
{
Cleanup();
return FALSE;
}
char strTemp[10];
for(int i=0; i<m_frame_per_direction; i++)
{
sprintf(strTemp, "frame_%d_index", i+1);
m_apFrameData[i].index = cfg.GetLong("MAIN", strTemp);
if(m_apFrameData[i].index<0)
m_apFrameData[i].index=i;
if(m_apFrameData[i].index>=m_num_frame/m_num_direction)
m_apFrameData[i].index=m_num_frame/m_num_direction-1;
sprintf(strTemp, "frame_%d_flag", i+1);
m_apFrameData[i].flag = cfg.GetLong("MAIN", strTemp);
}
m_key_config = strRes;
m_angle_base = m_texture_group->GetBaseAngle();
m_start_frame = 0;
m_end_frame = m_frame_per_direction-1;
if(m_num_frame==0)
m_num_frame=1;
_m_frame_delay = cfg.GetLong("MAIN", "frame_delay");
if(_m_frame_delay<=0)
_m_frame_delay = m_texture_group->GetDefaultDelay();
m_current_frame = cfg.GetLong("MAIN", "current_frame");
// DESC play = cfg.GetString("MAIN", "autoplay");
}
else
{
if (m_texture_group->GetSourceFFT()==GSF_GIF)
{
CGif89a* pGif = (CGif89a*)m_texture_group->GetExternCacheData();
if (!pGif)
return FALSE;
m_num_frame = m_texture_group->GetNumTexture();
m_num_direction = 1;
if (m_num_frame>1)
{
m_frame_per_direction = 0;
for (int i=0; i<m_num_frame; i++)
{
m_frame_per_direction += pGif->getFrame(i)->ctrlExt.delayTime;
}
m_apFrameData = new GSANI_FRAME[m_frame_per_direction];
int k = 0;
for (int j=0; j<m_num_frame; j++)
{
for (int i=0; i<pGif->getFrame(j)->ctrlExt.delayTime; i++)
{
m_apFrameData[k].index = j;
m_apFrameData[k].flag = 0;
k++;
}
}
}
else
{
m_frame_per_direction = 1;
}
m_start_frame = 0;
m_end_frame = m_frame_per_direction-1;
_m_frame_delay = 10;
}
else
{
m_flag = m_texture_group->GetFlag();
m_num_frame = m_texture_group->GetNumTexture();
m_num_direction = m_texture_group->GetTeam();
if(m_num_direction<=0)
m_num_direction = 1;
m_frame_per_direction = m_num_frame/m_num_direction;
if(m_flag&TXG_FLAG_SHADOW)
{
m_frame_per_direction /=2;
}
m_angle_base = m_texture_group->GetBaseAngle();
m_start_frame = 0;
m_end_frame = m_frame_per_direction-1;
if(m_num_frame==0)
m_num_frame=1;
_m_frame_delay = m_texture_group->GetDefaultDelay();
}
}
if(strSeting[0]!=0)
{
m_current_frame = _tcstol(strSeting, &sz_scanf, 10);
if(sz_scanf)
sz_scanf = strchr(sz_scanf, ',');
if(sz_scanf)
{
m_start_frame = _tcstol(sz_scanf+1, &sz_scanf, 10);
if(m_start_frame>m_current_frame)
m_start_frame = m_current_frame;
}
else
{
m_start_frame = m_current_frame;
}
if(sz_scanf)
sz_scanf = strchr(sz_scanf, ',');
if(sz_scanf)
{
m_end_frame = _tcstol(sz_scanf+1, &sz_scanf, 10);
if(m_end_frame<m_current_frame)
m_end_frame = m_current_frame;
}
else
{
m_end_frame = m_current_frame;
}
// if(sz_scanf)
// {
// m_end_frame = _tcstol(sz_scanf+1, &sz_scanf, 10);
// if(m_end_frame<m_current_frame)
// m_end_frame = m_current_frame;
// }
if(sz_scanf)
sz_scanf = strchr(sz_scanf, ',');
if(sz_scanf)
{
LONG temp = _tcstol(sz_scanf+1, &sz_scanf, 10);
if(temp>0)
_m_frame_delay = temp;
}
if(sz_scanf)
sz_scanf = strchr(sz_scanf, ',');
if(sz_scanf)
{
DESC state = sz_scanf+1;
if(state=="loop")
Play(GS_ANI_PLAY_LOOP);
else if(state=="loop_back")
Play(GS_ANI_PLAY_LOOP);
else if(state=="rotate")
Play(GS_ANI_PLAY_ROTATE);
else if(state=="rotate_back")
Play(GS_ANI_PLAY_ROTATE_BACK);
}
m_frame_per_direction = m_end_frame - m_start_frame +1;
}
return TRUE;
}
FLAG CGsAnimation::Update()
{
if(NULL==m_texture_group)
return 0;
if(m_num_frame==1)
return 0;
if(_m_play_flag==GS_ANI_PLAY_NONE||NULL==m_pEngine)
return 0;
DWORD dwPassTick = m_pEngine->m_dwPassTime;
DWORD tick;
_m_pass_tick += dwPassTick;
tick = dwPassTick+_m_base_tick;
if(_m_play_tick>0 && _m_pass_tick>_m_play_tick)
{
tick -= _m_pass_tick-_m_play_tick;
}
else
{
}
LONG frame_move=0;
FLAG flag_ret = GS_ANI_UPDATE_NORMAL;
if(tick<_m_frame_delay)
{
_m_base_tick = tick;
}
else
{
_m_base_tick = tick%_m_frame_delay;
frame_move = tick/_m_frame_delay;
if(frame_move<=0)
return GS_ANI_UPDATE_NORMAL;
if(_m_is_frame_up)
{
if(m_current_frame!=m_end_frame && m_current_frame+frame_move>=m_end_frame)
{
flag_ret = GS_ANI_UPDATE_END;
m_percent_by = 0;
}
m_current_frame = m_start_frame + (m_current_frame+frame_move);
}
else
{
if(m_current_frame!=m_start_frame && m_current_frame-frame_move<=m_start_frame)
{
flag_ret = GS_ANI_UPDATE_BEGIN;
m_percent_by = 0;
}
m_current_frame = m_start_frame + (m_frame_per_direction)*2 + (m_current_frame-frame_move);
}
switch(_m_play_flag)
{
case GS_ANI_PLAY_LOOP:
break;
case GS_ANI_PLAY_LOOP_BACK:
break;
case GS_ANI_PLAY_ROTATE:
case GS_ANI_PLAY_ROTATE_BACK:
if(((m_current_frame)/(m_frame_per_direction))%2)
{
_m_is_frame_up ^= 1;
m_current_frame = m_end_frame - m_current_frame%(m_frame_per_direction);
m_percent_by = 0;
}
break;
}
}
m_current_frame = m_current_frame%(m_frame_per_direction);
if(_m_play_tick>0 && _m_pass_tick>_m_play_tick)
{
_m_play_flag = GS_ANI_PLAY_NONE;
return GS_ANI_UPDATE_FINISH;
}
// if(m_apFrameData)
// return m_apFrameData[m_current_frame].flag;
// else
return flag_ret;
}
/*
HRESULT CGsAnimation::Draw(LONG x, LONG y, float z)
{
if(!m_pEngine || !m_texture_group)
return S_OK;
return m_pEngine->Render_PaintFast(x, y, m_texture_group->GetTexture(m_current_frame), z);
}
*/
CGsTexture* CGsAnimation::GetFrame(int frame_index)
{
if (m_texture_group)
{
int base_frame = m_base_frame;
if(m_apFrameData)
{
return m_texture_group->GetTexture(m_apFrameData[m_start_frame+(frame_index)%m_frame_per_direction].index + m_base_frame);
}
else
{
return m_texture_group->GetTexture(m_start_frame+(frame_index)%m_frame_per_direction + m_base_frame);
}
}
return NULL;
}
CGsTexture* CGsAnimation::GetShadow(int frame_index)
{
if(m_texture_group && m_flag&TXG_FLAG_SHADOW)
{
if(m_apFrameData)
{
return m_texture_group->GetTexture(m_apFrameData[m_start_frame+(frame_index)%m_frame_per_direction].index + m_base_frame + m_num_frame/m_num_direction/2);
}
else
{
return m_texture_group->GetTexture(m_start_frame+(frame_index)%m_frame_per_direction + m_base_frame+m_frame_per_direction);
}
}
return NULL;
}
CGsTexture* CGsAnimation::GetCurrentFrame(int frame_move)
{
if(m_texture_group)
{
int base_frame = m_base_frame;
if(m_apFrameData)
{
return m_texture_group->GetTexture(m_apFrameData[m_start_frame+(m_current_frame+frame_move)%m_frame_per_direction].index + m_base_frame);
}
else
{
return m_texture_group->GetTexture(m_start_frame+(m_current_frame+frame_move)%m_frame_per_direction + m_base_frame);
}
}
return NULL;
}
CGsTexture* CGsAnimation::GetCurrentShadow(int frame_move)
{
if(m_texture_group && m_flag&TXG_FLAG_SHADOW)
{
if(m_apFrameData)
{
return m_texture_group->GetTexture(m_apFrameData[m_start_frame+(m_current_frame+frame_move)%m_frame_per_direction].index + m_base_frame + m_num_frame/m_num_direction/2);
}
else
{
return m_texture_group->GetTexture(m_start_frame+(m_current_frame+frame_move)%m_frame_per_direction + m_base_frame+m_frame_per_direction);
}
}
return NULL;
}
int CGsAnimation::GetFrameCount()
{
return m_frame_per_direction;
}
void CGsAnimation::Play(FLAG style, BOOL bReset, float loop, float time, float delay)
{
if(NULL==m_texture_group)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -