⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 c3_phy.cpp

📁 网络游戏魔域的服务端与客户端完整源代码 包括详细的说明文档与开发日志
💻 CPP
📖 第 1 页 / 共 3 页
字号:

	for ( DWORD n = 0; n < ( *lpPhy )->dwNVecCount + ( *lpPhy )->dwAVecCount; n++ )
	{
		vertex[n].color = D3DCOLOR_XRGB ( 255, 255, 255 );
		vertex[n].u = ( *lpPhy )->lpVB[n].u;
		vertex[n].v = ( *lpPhy )->lpVB[n].v;
	}

	if ( FAILED ( ( *lpPhy )->vb->Unlock () ) )
		return false;


	// tri count
	fread ( &( *lpPhy )->dwNTriCount, sizeof ( DWORD ), 1, file );
	fread ( &( *lpPhy )->dwATriCount, sizeof ( DWORD ), 1, file );
	// tri
	if ( FAILED ( g_D3DDevice->CreateIndexBuffer ( ( ( *lpPhy )->dwNTriCount + ( *lpPhy )->dwATriCount ) * sizeof ( WORD ) * 3,
												   D3DUSAGE_WRITEONLY,
												   D3DFMT_INDEX16,
												   D3DPOOL_MANAGED,
												   &( *lpPhy )->ib ) ) )
		return false;

	WORD *tri;
	if ( FAILED ( ( *lpPhy )->ib->Lock ( 0,
									0,
									( BYTE** )&tri,
									D3DLOCK_NOSYSLOCK ) ) )
		return false;
	fread ( tri, sizeof ( WORD ), ( ( *lpPhy )->dwNTriCount + ( *lpPhy )->dwATriCount ) * 3, file );

	/*
	// 优化
	PrimitiveGroup *nl, *al;
	unsigned short ns, as;

	SetListsOnly ( true );

	if ( ( *lpPhy )->dwNTriCount > 0 )
	{
		if ( !( tri[0] == tri[1] ||
			 tri[1] == tri[2] ||
			 tri[2] == tri[0] ) )
		{
			GenerateStrips ( ( const unsigned short* )tri,
							 ( *lpPhy )->dwNTriCount * 3,
							 &nl,
							 &ns );
			CopyMemory ( tri,
						 nl->indices,
						 nl->numIndices * sizeof ( WORD ) );
			SafeDeleteEx ( nl );
		}
		else
		{
			//MessageBox ( g_hWnd, lpName, "file erro", MB_OK );
		}
	}
	if ( ( *lpPhy )->dwATriCount > 0 )
	{
		if ( !( tri[0] == tri[1] ||
			 tri[1] == tri[2] ||
			 tri[2] == tri[0] ) )
		{
			GenerateStrips ( ( const unsigned short* )( tri + ( *lpPhy )->dwNTriCount * 3 ),
							 ( *lpPhy )->dwATriCount * 3,
							 &al,
							 &as );

			CopyMemory ( tri + ( *lpPhy )->dwNTriCount * 3,
						 al->indices,
						 al->numIndices * sizeof ( WORD ) );
			SafeDeleteEx ( al );
		}
		else
		{
			//MessageBox ( g_hWnd, lpName, "file erro", MB_OK );
		}
	}
	*/

	if ( FAILED ( ( *lpPhy )->ib->Unlock () ) )
		return false;

	// 读入贴图
	fread ( &temp, sizeof ( DWORD ), 1, file );
	( *lpPhy )->lpTexName = new char[temp + 1];
	fread ( ( *lpPhy )->lpTexName, 1, temp, file );
	( *lpPhy )->lpTexName[temp] = '\0';
	// 创建贴图
	if ( bTex )
	{
		C3Texture *tex;
		( *lpPhy )->nTex = Texture_Load ( &tex, ( *lpPhy )->lpTexName );
		if ( ( *lpPhy )->nTex == -1 )
			return false;
	}

	// bounding box
	fread ( &( *lpPhy )->bboxMin, sizeof ( D3DXVECTOR3 ), 1, file );
	fread ( &( *lpPhy )->bboxMax, sizeof ( D3DXVECTOR3 ), 1, file );

	fread ( &( *lpPhy )->InitMatrix, sizeof ( D3DXMATRIXA16 ), 1, file );

	// 用户自定义消息
	fread ( &( *lpPhy )->dwTexRow, sizeof ( DWORD ), 1, file );

	fread ( &( *lpPhy )->Key.dwAlphas, sizeof ( DWORD ), 1, file );
	( *lpPhy )->Key.lpAlphas = new C3Frame[( *lpPhy )->Key.dwAlphas];
	fread ( ( *lpPhy )->Key.lpAlphas, sizeof ( C3Frame ), ( *lpPhy )->Key.dwAlphas, file );

	fread ( &( *lpPhy )->Key.dwDraws, sizeof ( DWORD ), 1, file );
	( *lpPhy )->Key.lpDraws = new C3Frame[( *lpPhy )->Key.dwDraws];
	fread ( ( *lpPhy )->Key.lpDraws, sizeof ( C3Frame ), ( *lpPhy )->Key.dwDraws, file );

	fread ( &( *lpPhy )->Key.dwChangeTexs, sizeof ( DWORD ), 1, file );
	( *lpPhy )->Key.lpChangeTexs = new C3Frame[( *lpPhy )->Key.dwChangeTexs];
	fread ( ( *lpPhy )->Key.lpChangeTexs, sizeof ( C3Frame ), ( *lpPhy )->Key.dwChangeTexs, file );

	char flag[4];
	// 12-20-2002 : STEP
	fread ( flag, sizeof ( char ), 4, file );

	if ( flag[0] == 'S' &&
		 flag[1] == 'T' &&
		 flag[2] == 'E' &&
		 flag[3] == 'P' )
	{
		fread ( &( *lpPhy )->uvstep.x, sizeof ( float ), 1, file );
		fread ( &( *lpPhy )->uvstep.y, sizeof ( float ), 1, file );
	}
	else
		fseek ( file, -4, SEEK_CUR ) ;

	Phy_SetColor ( *lpPhy, 1, 1, 1, 1 );
	return true;
}

C3_CORE_DLL_API
BOOL Phy_LoadPack ( C3Phy **lpPhy, HANDLE f, BOOL bTex )
{
	*lpPhy = new C3Phy;
	Phy_Clear ( *lpPhy );
	DWORD bytes ;

	// Load name
	DWORD temp;
	ReadFile ( f, &temp, sizeof ( unsigned long ), &bytes, 0 ) ;
	( *lpPhy )->lpName = new char[temp + 1];
	ReadFile ( f, ( *lpPhy )->lpName, temp, &bytes, 0 ) ;
	( *lpPhy )->lpName[temp] = '\0';

	// Blend
	ReadFile ( f, &( *lpPhy )->dwBlendCount, sizeof ( DWORD ), &bytes, 0 ) ;

	// Vec count
	ReadFile ( f, &( *lpPhy )->dwNVecCount , sizeof ( DWORD ), &bytes, 0 ) ;
	ReadFile ( f, &( *lpPhy )->dwAVecCount , sizeof ( DWORD ), &bytes, 0 ) ;

	// 创建输出 vb
	if ( FAILED ( g_D3DDevice->CreateVertexBuffer ( ( ( *lpPhy )->dwNVecCount + ( *lpPhy )->dwAVecCount ) * sizeof ( PhyOutVertex ),
													D3DUSAGE_WRITEONLY,
													0,
													D3DPOOL_MANAGED,
													&( *lpPhy )->vb ) ) )
		return false;

	// 读取顶点
	( *lpPhy )->lpVB = new PhyVertex[( *lpPhy )->dwNVecCount + ( *lpPhy )->dwAVecCount];
	ReadFile (	f,
				( *lpPhy )->lpVB,
				sizeof ( PhyVertex ) * ( ( *lpPhy )->dwNVecCount + ( *lpPhy )->dwAVecCount ),
				&bytes,
				0 ) ;

	PhyOutVertex *vertex;
	if ( FAILED ( ( *lpPhy )->vb->Lock ( 0,
										 0,
										 ( BYTE** )&vertex,
										 D3DLOCK_NOSYSLOCK ) ) )
		return false;

	for ( DWORD n = 0; n < ( *lpPhy )->dwNVecCount + ( *lpPhy )->dwAVecCount; n++ )
	{
		vertex[n].color = D3DCOLOR_XRGB ( 255, 255, 255 );
		vertex[n].u = ( *lpPhy )->lpVB[n].u;
		vertex[n].v = ( *lpPhy )->lpVB[n].v;
	}

	if ( FAILED ( ( *lpPhy )->vb->Unlock () ) )
		return false;

	// Tri count
	ReadFile ( f, &( *lpPhy )->dwNTriCount, sizeof ( DWORD ), &bytes, 0 ) ;
	ReadFile ( f, &( *lpPhy )->dwATriCount, sizeof ( DWORD ), &bytes, 0 ) ;

	// tri
	if ( FAILED ( g_D3DDevice->CreateIndexBuffer ( ( ( *lpPhy )->dwNTriCount + ( *lpPhy )->dwATriCount ) * sizeof ( WORD ) * 3,
												   D3DUSAGE_WRITEONLY,
												   D3DFMT_INDEX16,
												   D3DPOOL_MANAGED,
												   &( *lpPhy )->ib ) ) )
		return false;

	WORD *tri;
	if ( FAILED ( ( *lpPhy )->ib->Lock ( 0,
										 0,
										 ( BYTE** )&tri,
										 D3DLOCK_NOSYSLOCK ) ) )
		return false;

	ReadFile (	f,
				tri,
				sizeof ( WORD ) * ( ( ( *lpPhy )->dwNTriCount + ( *lpPhy )->dwATriCount ) * 3 ),
				&bytes,
				0 ) ;
	
	/*
	// 优化
	PrimitiveGroup *nl, *al;
	unsigned short ns, as;

	SetListsOnly ( true );






	if ( ( *lpPhy )->dwNTriCount > 0 )
	{
		if ( !( tri[0] == tri[1] ||
			 tri[1] == tri[2] ||
			 tri[2] == tri[0] ) )
		{
			GenerateStrips ( ( const unsigned short* )tri,
							 ( *lpPhy )->dwNTriCount * 3,
							 &nl,
							 &ns );
			CopyMemory ( tri,
						 nl->indices,
						 nl->numIndices * sizeof ( WORD ) );
			SafeDeleteEx ( nl );
		}
		else
		{
			//MessageBox ( g_hWnd, lpName, "file erro", MB_OK );
		}
	}
	if ( ( *lpPhy )->dwATriCount > 0 )
	{
		if ( !( tri[0] == tri[1] ||
			 tri[1] == tri[2] ||
			 tri[2] == tri[0] ) )
		{
			GenerateStrips ( ( const unsigned short* )( tri + ( *lpPhy )->dwNTriCount * 3 ),
							 ( *lpPhy )->dwATriCount * 3,
							 &al,
							 &as );

			CopyMemory ( tri + ( *lpPhy )->dwNTriCount * 3,
						 al->indices,
						 al->numIndices * sizeof ( WORD ) );
			SafeDeleteEx ( al );
		}
		else
		{
			//MessageBox ( g_hWnd, lpName, "file erro", MB_OK );
		}
	}

	*/
	if ( FAILED ( ( *lpPhy )->ib->Unlock () ) )
		return false;

	// Load texture
	ReadFile ( f, &temp, sizeof (DWORD ), &bytes, 0 ) ;
	( *lpPhy )->lpTexName = new char[temp + 1];
	ReadFile ( f, ( *lpPhy )->lpTexName, temp, &bytes, 0 ) ;
	( *lpPhy )->lpTexName[temp] = '\0';

	// Create the texture
	if ( bTex)
	{
		C3Texture *tex;
		( *lpPhy )->nTex = Texture_Load ( &tex, ( *lpPhy )->lpTexName );
		if ( ( *lpPhy )->nTex == -1 )
			return false;
	}



	// bounding box
	ReadFile ( f, &( *lpPhy )->bboxMin, sizeof ( D3DXVECTOR3 ), &bytes, 0 ) ;
	ReadFile ( f, &( *lpPhy )->bboxMax, sizeof ( D3DXVECTOR3 ), &bytes, 0 ) ;
	ReadFile ( f, &( *lpPhy )->InitMatrix, sizeof ( D3DXMATRIXA16), &bytes, 0 ) ;
	
	// 用户自定义消息
	ReadFile ( f, &( *lpPhy )->dwTexRow, sizeof ( DWORD ), &bytes, 0 ) ;
	ReadFile ( f, &( *lpPhy )->Key.dwAlphas, sizeof ( DWORD ), &bytes, 0 );
	( *lpPhy )->Key.lpAlphas = new C3Frame[( *lpPhy )->Key.dwAlphas];
	ReadFile ( f,
			   ( *lpPhy )->Key.lpAlphas, 
			   sizeof ( C3Frame ) * ( *lpPhy )->Key.dwAlphas,
			   &bytes, 
			   0 );
	ReadFile ( f, &( *lpPhy )->Key.dwDraws, sizeof ( DWORD ), &bytes, 0 );
	( *lpPhy )->Key.lpDraws = new C3Frame[( *lpPhy )->Key.dwDraws];
	ReadFile ( f,
			   ( *lpPhy )->Key.lpDraws,
			   sizeof ( C3Frame ) * ( *lpPhy )->Key.dwDraws,
			   &bytes,
			   0 );
	ReadFile ( f, &( *lpPhy )->Key.dwChangeTexs, sizeof ( DWORD ), &bytes, 0 );
	( *lpPhy )->Key.lpChangeTexs = new C3Frame[( *lpPhy )->Key.dwChangeTexs];
	ReadFile ( f,
			   ( *lpPhy )->Key.lpChangeTexs,
			   sizeof ( C3Frame ) * ( *lpPhy )->Key.dwChangeTexs,
			   &bytes,
			   0 );
	
	char flag[4];
	// 12-20-2002 : STEP
	ReadFile ( f, flag, sizeof ( char ) * 4, &bytes, 0 );
	if ( flag[0] == 'S' &&
		 flag[1] == 'T' &&
		 flag[2] == 'E' &&
		 flag[3] == 'P' )
	{
		ReadFile ( f, &( *lpPhy )->uvstep.x, sizeof ( float ), &bytes, 0 );
		ReadFile ( f, &( *lpPhy )->uvstep.y, sizeof ( float ), &bytes, 0 );
	}
	else
		SetFilePointer ( f, -4, 0, FILE_CURRENT ) ;
	Phy_SetColor ( *lpPhy, 1, 1, 1, 1 ) ;
	return true ;
}

C3_CORE_DLL_API
BOOL Phy_Save ( char *lpName, C3Phy *lpPhy, 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] = 'P';
	chunk.byChunkID[1] = 'H';
	chunk.byChunkID[2] = 'Y';
	chunk.byChunkID[3] = ' ';
	chunk.dwChunkSize = 0;
	fwrite ( &chunk, sizeof ( chunk ), 1, file );

	// name
	DWORD length = strlen ( lpPhy->lpName );
	fwrite ( &length, sizeof ( DWORD ), 1, file );
	chunk.dwChunkSize += sizeof ( DWORD );

	fwrite ( lpPhy->lpName, sizeof ( char ), length, file );
	chunk.dwChunkSize += sizeof ( char ) * length;

	// blend
	fwrite ( &lpPhy->dwBlendCount, sizeof ( DWORD ), 1, file );
	chunk.dwChunkSize += sizeof ( DWORD );

	// vec count
	fwrite ( &lpPhy->dwNVecCount, sizeof ( DWORD ), 1, file );
	chunk.dwChunkSize += sizeof ( DWORD );
	fwrite ( &lpPhy->dwAVecCount, sizeof ( DWORD ), 1, file );
	chunk.dwChunkSize += sizeof ( DWORD );

	// vec
	/*if ( lpPhy->dwBlendCount == 0 )
	{
		if ( lpPhy->lpVB )
		{
			fwrite ( lpPhy->lpVB,
					 sizeof ( PhyVertex0 ),
					 lpPhy->dwVecCount,
					 file );
		}
		else
		if ( lpPhy->vb )
		{
			PhyVertex0 *vertex;
			if ( FAILED ( lpPhy->vb->Lock ( 0,
											0,
											( BYTE** )&vertex,
											D3DLOCK_NOSYSLOCK ) ) )
				return false;

			fwrite ( ( PhyVertex0* )vertex,
					  sizeof ( PhyVertex0 ),
					  lpPhy->dwVecCount,
					  file );

			if ( FAILED ( lpPhy->vb->Unlock () ) )
				return false;
		}
		chunk.dwChunkSize += sizeof ( PhyVertex0 ) * lpPhy->dwVecCount;
	}
	else
	if ( lpPhy->dwBlendCount == 2 )*/
	fwrite ( lpPhy->lpVB,
			 sizeof ( PhyVertex ),
			 lpPhy->dwNVecCount + lpPhy->dwAVecCount,
			 file );
	chunk.dwChunkSize += sizeof ( PhyVertex ) * ( lpPhy->dwNVecCount + 
lpPhy->dwAVecCount );

	// tri count
	fwrite ( &lpPhy->dwNTriCount, sizeof ( DWORD ), 1, file );
	chunk.dwChunkSize += sizeof ( DWORD );
	fwrite ( &lpPhy->dwATriCount, sizeof ( DWORD ), 1, file );
	chunk.dwChunkSize += sizeof ( DWORD );
	// tri
	if ( lpPhy->lpIB )
		fwrite ( lpPhy->lpIB, sizeof ( WORD ), ( lpPhy->dwNTriCount + 
lpPhy->dwATriCount ) * 3, file );
	else
	if ( lpPhy->ib )
	{
		WORD *tri;
		if ( FAILED ( lpPhy->ib->Lock ( 0,
										0,
										( BYTE** )&tri,
										D3DLOCK_NOSYSLOCK ) ) )
			return false;
		fwrite ( tri, sizeof ( WORD ), ( lpPhy->dwNTriCount + lpPhy->dwATriCount ) 
* 3, file );
		if ( FAILED ( lpPhy->ib->Unlock () ) )
			return false;
	}
	chunk.dwChunkSize += sizeof ( WORD ) * ( lpPhy->dwNTriCount + 
lpPhy->dwATriCount ) * 3;

	// texture
	length = strlen ( lpPhy->lpTexName );
	fwrite ( &length, sizeof ( DWORD ), 1, file );
	chunk.dwChunkSize += sizeof ( DWORD );

	fwrite ( lpPhy->lpTexName, sizeof ( char ), length, file );
	chunk.dwChunkSize += sizeof ( char ) * length;

	// bounding box
	fwrite ( &lpPhy->bboxMin, sizeof ( D3DXVECTOR3 ), 1, file );
	chunk.dwChunkSize += sizeof ( D3DXVECTOR3 );

	fwrite ( &lpPhy->bboxMax, sizeof ( D3DXVECTOR3 ), 1, file );
	chunk.dwChunkSize += sizeof ( D3DXVECTOR3 );

	fwrite ( &lpPhy->InitMatrix, sizeof ( D3DXMATRIXA16 ), 1, file );
	chunk.dwChunkSize += sizeof ( D3DXMATRIXA16 );

	// 用户自定义消息
	fwrite ( &lpPhy->dwTexRow, sizeof ( DWORD ), 1, file );
	chunk.dwChunkSize += sizeof ( DWORD );

	fwrite ( &lpPhy->Key.dwAlphas, sizeof ( DWORD ), 1, file );
	chunk.dwChunkSize += sizeof ( DWORD );

	fwrite ( lpPhy->Key.lpAlphas, sizeof ( C3Frame ), lpPhy->Key.dwAlphas, file 
);
	chunk.dwChunkSize += sizeof ( C3Frame ) * lpPhy->Key.dwAlphas;

	fwrite ( &lpPhy->Key.dwDraws, sizeof ( DWORD ), 1, file );
	chunk.dwChunkSize += sizeof ( DWORD );

	fwrite ( lpPhy->Key.lpDraws, sizeof ( C3Frame ), lpPhy->Key.dwDraws, file 
);
	chunk.dwChunkSize += sizeof ( C3Frame ) * lpPhy->Key.dwDraws;

	fwrite ( &lpPhy->Key.dwChangeTexs, sizeof ( DWORD ), 1, file );
	chunk.dwChunkSize += sizeof ( DWORD );

	fwrite ( lpPhy->Key.lpChangeTexs, sizeof ( C3Frame ), 
lpPhy->Key.dwChangeTexs, file );
	chunk.dwChunkSize += sizeof ( C3Frame ) * lpPhy->Key.dwChangeTexs;


	char flag[4];
	// 12-20-2002 : "STEP"
	flag[0] = 'S';
	flag[1] = 'T';
	flag[2] = 'E';
	flag[3] = 'P';

	fwrite ( flag, sizeof ( char ), 4, file );
	chunk.dwChunkSize += sizeof ( char ) * 4;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -