📄 gstexturegroup.cpp
字号:
// GsTextureGroup.cpp: implementation of the CGsTextureGroup class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "GSLib_Internal.h"
#include "gif89a.h"
#pragma pack(push, 1)
struct t_texture_group_file_head
{
char str_version[12];
FLAG flag;
DWORD num_texture;
DWORD num_team;
float base_angle;
DWORD cache_size;
DWORD default_delay;
GPOINT base_point;
// DWORD re5;
};
#pragma pack(pop)
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CGsTextureGroup::CGsTextureGroup(CGsEngine* pEngine)
{
m_nUser = 1;
m_nRef_in_use = 0;
m_keySource = "";
m_pEngine = pEngine;
m_bLoadAll = false;
m_dwNumTeam = 1;
m_fBaseAngle = 0;
m_default_delay = 1000;
m_cache_size = 0;
m_base_point.x = 0;
m_base_point.y = 0;
m_tick_last_use = 0;
m_flag = 0;
m_source_fft = GSF_UNKNOWN;
m_extern_cache_data = NULL;
}
CGsTextureGroup::CGsTextureGroup( CGsEngine* pEngine, const char* strSource, BOOL bLoadAll )
{
m_nUser = 1;
m_nRef_in_use = 0;
m_pEngine = pEngine;
assert(TRUE==SetStreamSource( strSource, bLoadAll ));
m_extern_cache_data = NULL;
}
CGsTextureGroup::~CGsTextureGroup()
{
m_pEngine->FreeTextureGroup(m_keySource.c_str());
Cleanup();
}
VOID CGsTextureGroup::Cleanup()
{
for(int i=0; i<m_apTexture.size(); i++)
{
SAFE_DELETE(m_apTexture[i]);
}
m_apTexture.clear();
// m_nUser = 0;
m_bLoadAll = false;
m_dwNumTeam = 1;
m_fBaseAngle = 0;
// m_keySource = "";
m_default_delay = 1000;
// m_source_fft = GSF_UNKNOWN;
m_base_point.x = 0;
m_base_point.y = 0;
m_flag = 0;
m_tick_last_use = 0;
m_cache_size = 0;
m_cache_indices.clear();
SAFE_DELETE(m_extern_cache_data);
}
CGsTexture* CGsTextureGroup::GetTexture( int index )
{
if(index>=m_apTexture.size())
return NULL;
//find in cache
// if(m_cache_size>0)
// {
// std::list<int>::iterator it = std::find(m_cache_indices.begin(), m_cache_indices.end(), index);
// if(it!=m_cache_indices.end())
// {
// if(NULL==m_apTexture[index]->m_pdds)
// {
// m_apTexture[index]->RestoreObjects();
// m_apTexture[index]->m_BasePoint += m_base_point;
// }
// return m_apTexture[index];
// }
// }
if(m_apTexture[index])
{
if(NULL==m_apTexture[index]->m_pdds)
{
m_apTexture[index]->RestoreObjects();
m_apTexture[index]->m_BasePoint += m_base_point;
m_tick_last_use = m_pEngine->m_dwEngineTime;
return m_apTexture[index];
}
else
{
m_apTexture[index]->UpdateFromCache();
m_tick_last_use = m_pEngine->m_dwEngineTime;
return m_apTexture[index];
}
}
else if(!m_keySource.empty())
{
if((SetStreamSource(m_keySource.c_str(), 0)))//尝试从文件导入
{
// GSFBUF gsbuf;
// if(NULL==m_pEngine->GetSourcePtr()->FindSource(m_keySource, gsbuf))
// return NULL;
//// gsbuf.pfstream->seekg(gsbuf.posBegin + sizeof(t_texture_group_file_head) + sizeof(DWORD)*index, ios::beg);
// fseek(gsbuf.p_fstream, gsbuf.pos_begin + sizeof(t_texture_group_file_head) + sizeof(DWORD)*index, SEEK_SET);
// DWORD pos_data;
//// gsbuf.pfstream->read((char*)&pos_data, sizeof(DWORD));
// fread(&pos_data, sizeof(DWORD), 1, gsbuf.p_fstream);
// m_apTexture[index] = new CGsTexture(m_pEngine);
// gsbuf.pos_begin = pos_data;
// gsbuf.file_size = 0;
// if(FAILED(m_apTexture[index]->Import(gsbuf)))
// {
// SAFE_DELETE(m_apTexture[index]);
// return NULL;
// }
}
else
{
return NULL;
}
}
else
{
return NULL;
}
// if(m_cache_size>0)
// {
// while(m_cache_size<=m_cache_indices.size())
// {
// m_apTexture[*m_cache_indices.begin()]->ReleaseObjects();
// m_cache_indices.erase(m_cache_indices.begin());
// }
// m_cache_indices.push_back(index);
// }
m_tick_last_use = m_pEngine->m_dwEngineTime;
return m_apTexture[index];
}
CGsTexture* CGsTextureGroup::GetPreTexture(int index)
{
switch(m_source_fft) {
case GSF_GIF:
{
LPCFRAME pFrame = ((CGif89a*)m_extern_cache_data)->getFrame(index);
if (pFrame && pFrame)
{
}
}
break;
default:
if (TestFlag(TXG_FLAG_SHADOW))
{
}
break;
}
return NULL;
}
VOID CGsTextureGroup::ClearCache()
{
m_tick_last_use = 0;
m_nRef_in_use = 0;
for(int i=0; i<m_apTexture.size(); i++)
{
if(m_apTexture[i])
{
if(m_cache_size>0)
{
m_apTexture[i]->ClearCache();
}
m_apTexture[i]->ReleaseObjects();
}
}
// m_cache_indices.clear();
}
BOOL CGsTextureGroup::SetStreamSource( const char* strSource, BOOL bLoadAll )
{
if (strSource==m_keySource)
{
return TRUE;
}
Cleanup();
m_keySource = strSource;
char* sz;
char strRes[MAX_PATH];
strcpy(strRes, strSource);
// if(CGsFunc::FileCompareExt(strRes,"jpg"))
{//from string
if(sz=strstr(strRes, "%"))
{
sz = strstr(strRes, "#");
char* sz_scanf;
int start = _tcstol(sz+1,&sz_scanf,10);
int end = _tcstol(sz_scanf+1,&sz_scanf,10);
if(start>end)
return FALSE;
for(int i=start; i<=end; i++)
{
strcpy(sz, "");
char strTex[MAX_PATH];
sprintf(strTex, strRes, i);
CGsTexture* pTex = InsertTexture();
if(pTex==NULL)
{
Cleanup();
return FALSE;
}
if(!pTex->SetStreamSource(strTex, m_cache_size))
{
Cleanup();
return FALSE;
}
}
return TRUE;
}
else if(sz=strstr(strRes, ","))
{
char seps[] = ", ";
sz = strtok(strRes, seps);
while(sz)
{
CGsTexture* pTex = InsertTexture();
if(pTex==NULL)
{
Cleanup();
return FALSE;
}
if(!pTex->SetStreamSource(sz, m_cache_size))
{
Cleanup();
return FALSE;
}
sz = strtok(NULL, seps);
}
return TRUE;
}
}
GSFILE_INFO* file_info = m_pEngine->FindSource(strSource);
if(file_info)
{
m_source_fft = file_info->fft;
switch(file_info->fft)
{
case GSF_TEX:
case GSF_JPG:
case GSF_BMP:
case GSF_TGA:
case GSF_DDS:
{
CGsTexture* pTex = InsertTexture();
if(pTex==NULL)
{
Cleanup();
return FALSE;
}
if(!pTex->SetStreamSource(strSource, m_cache_size))
{
Cleanup();
return FALSE;
}
}
break;
case GSF_GIF:
{
if(FALSE==LoadFromGif(file_info->strFile))
return FALSE;
}
break;
case GSF_PGG:
if(FALSE==LoadFromPGG(file_info->strFile))
return FALSE;
break;
case GSF_TXG:
m_bLoadAll = true;
if(FAILED(Import(file_info->strFile, bLoadAll)))
return FALSE;
break;
case GSF_INI:
return FALSE;
break;
case GSF_TXT:
return FALSE;
break;
default:
return FALSE;
break;
}
}
else
{//from
FILE* file = fopen(strSource, "rb");
if(file == NULL)
return FALSE;
fclose(file);
m_source_fft = CGsFunc::File_GetFFT(strSource);
switch(m_source_fft)
{
case GSF_PGG:
if(FALSE==LoadFromPGG(strSource))
return FALSE;
case GSF_TEX:
case GSF_JPG:
case GSF_BMP:
case GSF_TGA:
case GSF_DDS:
{
CGsTexture* pTex = InsertTexture();
if(pTex==NULL)
{
Cleanup();
return FALSE;
}
pTex->SetStreamSource(strSource, m_cache_size);
}
break;
case GSF_GIF:
{
if(FALSE==LoadFromGif(strSource))
return FALSE;
}
break;
case GSF_TXG:
m_bLoadAll = true;
if(FAILED(Import(strSource, bLoadAll)))
return FALSE;
default:
{
}
}
}
OnInitialize();
return true;
}
BOOL CGsTextureGroup::LoadFromPGG(const char *strFile)
{
GSFBUF gsbuf = CGsFunc::GSFBUF_Create(strFile, "rb");
if(NULL==gsbuf.p_fstream)
{
return FALSE;
}
else
{
char version[16];
fread(version, 1, 16, gsbuf.p_fstream);
if(strstr(version, "PGG"))
{
CGsFunc::GSFBUF_Set(&gsbuf, 20);
t_PGM_group_head head;
fread(&head, sizeof(head), 1, gsbuf.p_fstream);
CGsFunc::GSFBUF_Set(&gsbuf, head.pos_guide);
char strPGM[PATH_SIZE];
for(int i=0; i<head.num_PGM; i++)
{
long pos_PGM;
fread(&pos_PGM, sizeof(pos_PGM), 1, gsbuf.p_fstream);
sprintf(strPGM, "%s,%d,%d", strFile, GSF_PGG, pos_PGM);
CGsTexture* pTex = InsertTexture();
pTex->SetStreamSource(strPGM, m_cache_size);
}
}
else
{
fclose(gsbuf.p_fstream);
return FALSE;
}
}
return TRUE;
}
VOID CGsTextureGroup::SetCacheSize(DWORD size)
{
if(size!=m_cache_size)
{
for(int i=0; i<m_apTexture.size(); i++)
{
if(m_apTexture[i])
m_apTexture[i]->SetUseCache(size);
}
}
m_cache_size = size;
}
HRESULT CGsTextureGroup::Import( const TCHAR* strFile, BOOL bLoadAll )
{
// fstream fbuf;
// fbuf.open(strFile, ios::in | ios::out | ios::binary);
// if(!fbuf)
// return false;
//
// GSBUF gsbuf;
// gsbuf.pfstream = &fbuf;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -