Files
Leviathan/Client/Game/engine/Renderer/KSpriteLoader.cpp
T
2026-06-01 12:46:52 +02:00

316 lines
8.0 KiB
C++

// KSpriteLoader.cpp: implementation of the KSpriteLoader class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
//#include "Sprite_TML.h"
#include "KSpriteLoader.h"
#include "KResourceManager.h"
#include "KResource.h"
#include <kfile/KBinaryFiler.h>
#include <kfile/KStream.h>
//#include "Util.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
KSpriteLoader::KSpriteLoader()
{
}
KSpriteLoader::~KSpriteLoader()
{
//_LEAK_SUN
m_vecRes.clear();
}
int KSpriteLoader::GetResCount()
{
return static_cast<int>(m_vecRes.size());
}
KResSpriteAnimation* KSpriteLoader::GetRes( int nIndex)
{
return m_vecRes.at(nIndex);
}
bool KSpriteLoader::Load( KStream &stream )
{
KMemoryStream* pMem = (KMemoryStream*)(&stream);
if( !pMem->IsValid() ) return false;
if( pMem->Eos() ) return false;
void* pSrc = pMem->GetMappedPtr( KStream::MAPPED_READ );
if( pSrc == NULL ) return false;
char* pBuf = (char*)(pSrc);
std::vector<std::string> vecLineList;
XStringUtil::Split( pBuf, vecLineList, "\r\n" );
//CStringUtil::GetTextLinesFromString( pBuf, vecLineList );
for( int i = 0; i < static_cast<int>(vecLineList.size()); )
{
std::string strName = vecLineList[i];
if( i+1 >= static_cast<int>(vecLineList.size()) ) break;
int nFrameCount = ::atoi( vecLineList[i+1].c_str() );
int j = i+2;
std::vector<std::string> vecFrameList;
while( nFrameCount > 0 )
{
vecFrameList.push_back( vecLineList[j] );
j += 2; //숫자 건너 띄기
j += 1; //가로, 세로 크기 건너 띄기
nFrameCount--;
}
// 실행시간이 너무 오래걸려서 디버그 모드일때만 출력 하도록 수정함, prodongi
/*
#ifdef _DEBUG
OutputDebugString("Add Sprite Animation - " );
OutputDebugString( strName.c_str() );
OutputDebugString("\r\n");
#endif
*/
loadAnimation( strName.c_str(), vecFrameList );
i = j;
}
return true;
}
bool KSpriteLoader::Load_Icon( const char * pFileName )
{
std::string strFileName = pFileName;
std::string strAniName = pFileName;
KResSpriteAnimation *pRes = new KResSpriteAnimation;
if( !CStringUtil::GetIconAniName( pFileName, strAniName ) )
return false;
pRes->SetName( strAniName.c_str() );
pRes->SetKeyCount( 1 );
pRes->SetLoad( false );
for( int i = 0; i < 1; i++ )
{
pRes->AddFrame( strFileName.c_str() );
NX3LoadPack loadpack;
loadpack.Init();
// K3DTexture *tex = KTextureManager::GetManager()->GetTextureOriginal( strLibPath.c_str(), &loadpack );
float attr[8];
attr[ATTR_VISIBILITY] = 1.0f;
attr[ATTR_XSCALE] = 1.0f;
attr[ATTR_YSCALE] = 1.0f;
attr[ATTR_XMOVE] = 0.0f;
attr[ATTR_YMOVE] = 0.0f;
attr[ATTR_ROTATE] = 0.0f;
attr[ATTR_MIRRORX] = 0.0f;
attr[ATTR_MIRRORY] = 0.0f;
float vis = attr[0];
K3DMatrix mat, scalemat;
K3DMatrixIdentity( mat );
K3DMatrixRotationZ( mat, attr[5] / 180.f * K3D_PI );
K3DMatrixScaling( scalemat, attr[1], attr[2], 1.0f );
mat *= scalemat;
mat.SetPosVector( K3DVector( attr[3], attr[4], .0f ) );
KResSpriteAnimation::Key key;
key.time = 0;
key.spSprite = new KResSprite;
//// 추가. bintitle.
//if( bLoad )
//{
// KTextureManager::GetManager()->SetTextureSearchPolicy( KTextureManager::SearchPolicy::ORG_ONLY );
// K3DTexture *tex = KTextureManager::GetManager()->GetTextureOriginal( pFileName, &loadpack );
// key.spSprite->SetTexture( tex, NULL );
// KTextureManager::GetManager()->SetTextureSearchPolicy( KTextureManager::SearchPolicy::DDS_ONLY );
//}
//else
//{
// //텍스쳐를 로딩 해야만, 알 수 있는 정보를 포함하고 있다.
// key.spSprite->SetTexture( NULL, NULL );
//}
//텍스쳐를 로딩 해야만, 알 수 있는 정보를 포함하고 있다.
key.spSprite->SetTexture( NULL, NULL );
key.spSprite->SetTransform( mat );
key.spSprite->SetVisibility( vis );
pRes->SetKey( i, &key );
}
m_vecRes.push_back( KResSpriteAnimationSPtr(pRes) );
return true;
}
#include "KResourceDX.h"
#include "KRenderDeviceDX.h"
#include "KDeviceManager.h"
// bintitle.
bool KSpriteLoader::Load_IconEx( const char * pFileName )
{
std::string strFileName = pFileName;
std::string strAniName = pFileName;
KResSpriteAnimation *pRes = new KResSpriteAnimation;
if( !CStringUtil::GetIconAniName( pFileName, strAniName ) )
return false;
pRes->SetName( strAniName.c_str() );
pRes->SetKeyCount( 1 );
pRes->SetLoad( false );
for( int i = 0; i < 1; i++ )
{
pRes->AddFrame( strFileName.c_str() );
// K3DTexture *tex = KTextureManager::GetManager()->GetTextureOriginal( strLibPath.c_str(), &loadpack );
float attr[8];
attr[ATTR_VISIBILITY] = 1.0f;
attr[ATTR_XSCALE] = 1.0f;
attr[ATTR_YSCALE] = 1.0f;
attr[ATTR_XMOVE] = 0.0f;
attr[ATTR_YMOVE] = 0.0f;
attr[ATTR_ROTATE] = 0.0f;
attr[ATTR_MIRRORX] = 0.0f;
attr[ATTR_MIRRORY] = 0.0f;
float vis = attr[0];
K3DMatrix mat, scalemat;
K3DMatrixIdentity( mat );
K3DMatrixRotationZ( mat, attr[5] / 180.f * K3D_PI );
K3DMatrixScaling( scalemat, attr[1], attr[2], 1.0f );
mat *= scalemat;
mat.SetPosVector( K3DVector( attr[3], attr[4], .0f ) );
KResSpriteAnimation::Key key;
key.time = 0;
key.spSprite = new KResSprite;
K3DTextureDX * pTex = new K3DTextureDX(
static_cast< K3DRenderDeviceDX * >( KDeviceManagerDX::GetDeviceManager()->GetRenderDevice() )
);
pTex->LoadFromFile( std::string( pFileName ) );
//텍스쳐를 로딩 해야만, 알 수 있는 정보를 포함하고 있다.
key.spSprite->SetTexture( pTex, NULL );
key.spSprite->SetTransform( mat );
key.spSprite->SetVisibility( vis );
pRes->SetKey( i, &key );
}
m_vecRes.push_back(KResSpriteAnimationSPtr(pRes));
return true;
}
void KSpriteLoader::loadAnimation( const char* szAniName, std::vector<std::string> & vecFrameList )
{
if( vecFrameList.empty() ) return;
if( vecFrameList.size() < 1 ) return;
KResSpriteAnimation *pRes = new KResSpriteAnimation; // [MEMORY_LEAK] 2012. 3. 12 - marine 스마트 포인터로 릭 아님
pRes->SetName( szAniName );
pRes->SetKeyCount( static_cast<int>(vecFrameList.size()) );
pRes->SetLoad( false );
for( int i = 0; i < static_cast<int>(vecFrameList.size()); i++ )
{
std::string strLibPath = vecFrameList[i];
pRes->AddFrame( vecFrameList[i].c_str() );
NX3LoadPack loadpack;
loadpack.Init();
// K3DTexture *tex = KTextureManager::GetManager()->GetTextureOriginal( strLibPath.c_str(), &loadpack );
float attr[8];
attr[ATTR_VISIBILITY] = 1.0f;
attr[ATTR_XSCALE] = 1.0f;
attr[ATTR_YSCALE] = 1.0f;
attr[ATTR_XMOVE] = 0.0f;
attr[ATTR_YMOVE] = 0.0f;
attr[ATTR_ROTATE] = 0.0f;
attr[ATTR_MIRRORX] = 0.0f;
attr[ATTR_MIRRORY] = 0.0f;
float vis = attr[0];
K3DMatrix mat, scalemat;
K3DMatrixIdentity( mat );
K3DMatrixRotationZ( mat, attr[5] / 180.f * K3D_PI );
K3DMatrixScaling( scalemat, attr[1], attr[2], 1.0f );
mat *= scalemat;
mat.SetPosVector( K3DVector( attr[3], attr[4], .0f ) );
KResSpriteAnimation::Key key;
key.time = 0;
key.spSprite = new KResSprite; //[MEMORY_LEAK] 2012. 3. 2 - marine 스마트 포인터 사용
//텍스쳐를 로딩 해야만, 알 수 있는 정보를 포함하고 있다.
key.spSprite->SetTexture( NULL, NULL );
key.spSprite->SetTransform( mat );
key.spSprite->SetVisibility( vis );
pRes->SetKey( i, &key );
}
m_vecRes.push_back(KResSpriteAnimationSPtr(pRes));
}
static int getTexCorrectSize( int size )
{
int res = 2;
while( size > res )
{
res *= 2;
}
return res;
}
static void copyBufToTex( K3DTexture *tex, char *imgbuf, int bufrow, int bufstride )
{
char *pBuf;
int texstride;
int texrow = tex->GetHeight();
tex->LockRect( NULL, (void**)&pBuf, texstride );
for ( int i = 0 ; i < texrow ; ++i )
{
if ( i < bufrow )
{
memset( pBuf, 0, texstride );
memcpy( pBuf, imgbuf, bufstride );
}
else
{
memset( pBuf, 0, texstride );
}
pBuf += texstride;
imgbuf += bufstride;
}
tex->Unlock();
}