📄 c3_shape.cpp
字号:
#include "..\include\c3_shape.h"
#include "..\include\c3_texture.h"
#include "..\include\c3_datafile.h"
const int TEARAIR_TEX_SIZE = 1024;
const int TEARAIR_SEGMENT_NUM = 4;
C3_CORE_DLL_API
void SMotion_Clear ( C3SMotion *lpSMotion )
{
lpSMotion->dwFrames = 0;
lpSMotion->lpFrames = 0;
lpSMotion->nFrame = 0;
}
C3_CORE_DLL_API
BOOL SMotion_LoadPack ( C3SMotion **lpSMotion, HANDLE f )
{
*lpSMotion = new C3SMotion;
SMotion_Clear ( *lpSMotion );
DWORD bytes ;
// matrix
ReadFile ( f, &( *lpSMotion )->dwFrames, sizeof ( DWORD ), &bytes, 0 ) ;
( *lpSMotion )->lpFrames = new D3DXMATRIX[( *lpSMotion )->dwFrames];
ReadFile ( f,
( *lpSMotion )->lpFrames,
sizeof ( D3DXMATRIX ) * ( *lpSMotion )->dwFrames,
&bytes,
0 ) ;
D3DXMatrixIdentity ( &( *lpSMotion )->matrix );
return true;
}
C3_CORE_DLL_API
BOOL SMotion_Load ( C3SMotion **lpSMotion, FILE *file )
{
*lpSMotion = new C3SMotion;
SMotion_Clear ( *lpSMotion );
// matrix
fread ( &( *lpSMotion )->dwFrames, sizeof ( DWORD ), 1, file );
( *lpSMotion )->lpFrames = new D3DXMATRIX[( *lpSMotion )->dwFrames];
fread ( ( *lpSMotion )->lpFrames,
sizeof ( D3DXMATRIX ),
( *lpSMotion )->dwFrames,
file );
D3DXMatrixIdentity ( &( *lpSMotion )->matrix );
return true;
}
C3_CORE_DLL_API
BOOL SMotion_Save ( char *lpName, C3SMotion *lpSMotion, BOOL bNew )
{
FILE *file = fopen ( lpName, bNew ? "w+b" : "r+b" );
if ( !file )
return false;
fseek ( file, 0, SEEK_END );
// phy
ChunkHeader chunk;
chunk.byChunkID[0] = 'S';
chunk.byChunkID[1] = 'M';
chunk.byChunkID[2] = 'O';
chunk.byChunkID[3] = 'T';
chunk.dwChunkSize = 0;
fwrite ( &chunk, sizeof ( chunk ), 1, file );
// matrix
fwrite ( &lpSMotion->dwFrames, sizeof ( DWORD ), 1, file );
chunk.dwChunkSize += sizeof ( DWORD );
fwrite ( lpSMotion->lpFrames,
sizeof ( D3DXMATRIX ),
lpSMotion->dwFrames,
file );
chunk.dwChunkSize += sizeof ( D3DXMATRIX ) * lpSMotion->dwFrames;
fseek ( file, -( int )( chunk.dwChunkSize + sizeof ( chunk ) ), SEEK_CUR );
fwrite ( &chunk, sizeof ( chunk ), 1, file );
fseek ( file, 0, SEEK_END );
fclose ( file );
return true;
}
C3_CORE_DLL_API
void SMotion_Unload ( C3SMotion **lpSMotion )
{
SafeDeleteEx ( ( *lpSMotion )->lpFrames );
SafeDelete ( *lpSMotion );
}
C3_CORE_DLL_API
void Shape_Clear ( C3Shape *lpShape )
{
lpShape->lpName = 0;
lpShape->dwLineCount = 0;
lpShape->lpLine = 0;
lpShape->vb = 0;
lpShape->dwSegment = 0;
lpShape->dwSegmentCur = 0;
lpShape->bFrist = true;
lpShape->dwSmooth = 0;
lpShape->lpTexName = 0;
lpShape->nTex = -1;
lpShape->lpMotion = 0;
lpShape->pTearAirTex = NULL;
lpShape->pScreenPnt = NULL;
}
C3_CORE_DLL_API
BOOL Shape_LoadPack ( C3Shape **lpShape, HANDLE f, BOOL bTex )
{
*lpShape = new C3Shape;
Shape_Clear ( *lpShape );
DWORD bytes ;
// 读入名称
DWORD temp;
ReadFile ( f, &temp, sizeof ( unsigned long ), &bytes, 0 ) ;
( *lpShape )->lpName = new char[temp + 1];
ReadFile ( f, ( *lpShape )->lpName, temp, &bytes, 0 ) ;
( *lpShape )->lpName[temp] = '\0';
ReadFile ( f, &( *lpShape )->dwLineCount, sizeof ( DWORD ), &bytes, 0 ) ;
( *lpShape )->lpLine = new C3Line[( *lpShape )->dwLineCount];
for ( DWORD n = 0; n < ( *lpShape )->dwLineCount; n++ )
{
ReadFile ( f,
&( *lpShape )->lpLine[n].dwVecCount,
sizeof ( DWORD ),
&bytes,
0 ) ;
( *lpShape )->lpLine[n].lpVB = new D3DXVECTOR3[( *lpShape )->lpLine[n].dwVecCount];
ReadFile ( f,
( *lpShape )->lpLine[n].lpVB,
sizeof ( D3DXVECTOR3 ) * ( *lpShape )->lpLine[n].dwVecCount,
&bytes,
0 ) ;
}
// 读入贴图
ReadFile ( f, &temp, sizeof ( DWORD ), &bytes, 0 ) ;
( *lpShape )->lpTexName = new char[temp + 1];
ReadFile ( f, ( *lpShape )->lpTexName, temp, &bytes, 0 ) ;
( *lpShape )->lpTexName[temp] = '\0';
// 创建贴图
if ( bTex )
{
C3Texture *tex;
( *lpShape )->nTex = Texture_Load ( &tex, ( *lpShape )->lpTexName );
if ( ( *lpShape )->nTex == -1 )
return false;
}
DWORD segment;
ReadFile ( f, &segment, sizeof ( DWORD ), &bytes, 0 ) ;
Shape_SetSegment ( *lpShape, segment );
return true ;
}
C3_CORE_DLL_API
BOOL Shape_Load ( C3Shape **lpShape, FILE *file, BOOL bTex )
{
*lpShape = new C3Shape;
Shape_Clear ( *lpShape );
// 读入名称
DWORD temp;
fread ( &temp, sizeof ( unsigned long ), 1, file );
( *lpShape )->lpName = new char[temp + 1];
fread ( ( *lpShape )->lpName, 1, temp, file );
( *lpShape )->lpName[temp] = '\0';
fread ( &( *lpShape )->dwLineCount, sizeof ( DWORD ), 1, file );
( *lpShape )->lpLine = new C3Line[( *lpShape )->dwLineCount];
for ( DWORD n = 0; n < ( *lpShape )->dwLineCount; n++ )
{
fread ( &( *lpShape )->lpLine[n].dwVecCount,
sizeof ( DWORD ),
1,
file );
( *lpShape )->lpLine[n].lpVB = new D3DXVECTOR3[( *lpShape )->lpLine[n].dwVecCount];
fread ( ( *lpShape )->lpLine[n].lpVB,
sizeof ( D3DXVECTOR3 ),
( *lpShape )->lpLine[n].dwVecCount,
file );
}
// 读入贴图
fread ( &temp, sizeof ( DWORD ), 1, file );
( *lpShape )->lpTexName = new char[temp + 1];
fread ( ( *lpShape )->lpTexName, 1, temp, file );
( *lpShape )->lpTexName[temp] = '\0';
// 创建贴图
if ( bTex )
{
C3Texture *tex;
( *lpShape )->nTex = Texture_Load ( &tex, ( *lpShape )->lpTexName );
if ( ( *lpShape )->nTex == -1 )
return false;
}
DWORD segment;
fread ( &segment, sizeof ( DWORD ), 1, file );
Shape_SetSegment ( *lpShape, segment );
return true;
}
C3_CORE_DLL_API
BOOL Shape_Save ( char *lpName, C3Shape *lpShape, BOOL bNew )
{
FILE *file = fopen ( lpName, bNew ? "w+b" : "r+b" );
if ( !file )
return false;
fseek ( file, 0, SEEK_END );
// shape
ChunkHeader chunk;
chunk.byChunkID[0] = 'S';
chunk.byChunkID[1] = 'H';
chunk.byChunkID[2] = 'A';
chunk.byChunkID[3] = 'P';
chunk.dwChunkSize = 0;
fwrite ( &chunk, sizeof ( chunk ), 1, file );
// name
DWORD length = strlen ( lpShape->lpName );
fwrite ( &length, sizeof ( DWORD ), 1, file );
chunk.dwChunkSize += sizeof ( DWORD );
fwrite ( lpShape->lpName, sizeof ( char ), length, file );
chunk.dwChunkSize += sizeof ( char ) * length;
//
fwrite ( &lpShape->dwLineCount, sizeof ( DWORD ), 1, file );
chunk.dwChunkSize += sizeof ( DWORD );
for ( DWORD n = 0; n < lpShape->dwLineCount; n++ )
{
fwrite ( &lpShape->lpLine[n].dwVecCount,
sizeof ( DWORD ),
1,
file );
chunk.dwChunkSize += sizeof ( DWORD );
fwrite ( lpShape->lpLine[n].lpVB,
sizeof ( D3DXVECTOR3 ),
lpShape->lpLine[n].dwVecCount,
file );
chunk.dwChunkSize += sizeof ( D3DXVECTOR3 ) * lpShape->lpLine[n].dwVecCount;
}
// texture
length = strlen ( lpShape->lpTexName );
fwrite ( &length, sizeof ( DWORD ), 1, file );
chunk.dwChunkSize += sizeof ( DWORD );
fwrite ( lpShape->lpTexName, sizeof ( char ), length, file );
chunk.dwChunkSize += sizeof ( char ) * length;
fwrite ( &lpShape->dwSegment, sizeof ( DWORD ), 1, file );
chunk.dwChunkSize += sizeof ( DWORD );
fseek ( file, -( int )( chunk.dwChunkSize + sizeof ( chunk ) ), SEEK_CUR );
fwrite ( &chunk, sizeof ( chunk ), 1, file );
fseek ( file, 0, SEEK_END );
fclose ( file );
return true;
}
C3_CORE_DLL_API
void Shape_Unload ( C3Shape **lpShape )
{
SafeDeleteEx ( ( *lpShape )->lpName );
for ( DWORD n = 0; n < ( *lpShape )->dwLineCount; n++ )
{
SafeDeleteEx ( ( *lpShape )->lpLine[n].lpVB );
}
if(( *lpShape )->lpLine)
{
SafeDeleteEx (( *lpShape )->lpLine);
SafeDeleteEx (( *lpShape )->lpTexName);
SafeDeleteEx (( *lpShape )->lpName);
}
SafeDeleteEx ( (*lpShape)->vb );
if ( (*lpShape)->pTearAirTex != NULL )
{
SafeRelease( (*lpShape)->pTearAirTex );
}
if ( (*lpShape)->pScreenPnt != NULL )
{
SafeDeleteEx( (*lpShape)->pScreenPnt );
}
SafeDelete ( *lpShape );
}
/*
C3_CORE_DLL_API
void Shape_Link ( C3Shape *lpShape, D3DXMATRIX *lpMatrix )
{
D3DXMATRIX matrix;
D3DXMatrixInverse ( &matrix, 0, lpMatrix );
for ( DWORD n = 0; n < lpShape->dwLineCount; n++ )
{
for ( DWORD m = 0; m < lpShape->lpLine[n].dwVecCount; m++ )
{
D3DXVec3TransformCoord ( ( D3DXVECTOR3* )&lpShape->lpLine[n].lpVB[m],
( D3DXVECTOR3* )&lpShape->lpLine[n].lpVB[m],
&matrix );
}
}
}*/
C3_CORE_DLL_API
void Shape_SetSegment ( C3Shape *lpShape, DWORD dwSegment, DWORD dwSmooth )
{
dwSmooth = 10;
dwSegment = dwSegment * ( dwSmooth + 1 );
lpShape->dwSegment = dwSegment;
lpShape->dwSmooth = dwSmooth;
lpShape->dwSegmentCur = 0;
SafeDeleteEx ( lpShape->vb );
lpShape->vb = new ShapeOutVertex[dwSegment * 6];
ZeroMemory ( lpShape->vb, sizeof ( ShapeOutVertex ) * dwSegment * 6 );
}
C3_CORE_DLL_API
BOOL Shape_Draw ( C3Shape *lpShape, BOOL bLocal, int nAsb, int nAdb)
{
SetRenderState ( D3DRS_ALPHABLENDENABLE, true );
SetRenderState ( D3DRS_SRCBLEND, nAsb );
SetRenderState ( D3DRS_DESTBLEND, nAdb );
// 首先转换线顶点
D3DXMATRIX mm;
if ( bLocal )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -