Click here to Skip to main content
15,881,787 members
Articles / Multimedia / DirectX

A Simple and Powerful Game Engine

Rate me:
Please Sign up or sign in to vote.
4.45/5 (17 votes)
23 Jul 20074 min read 91.5K   9.1K   74  
This article talks about a simple and powerful game engine to make game programming simpler
//SZM_Loader.cpp

/********************************************************

Description: SZM files loader.

Date: 1/7/2007

Auther: Shalom Keller

Contact: szkeller@gmail.com

Note:	If you find any bugs or you think that some thing 
		is wrong, Please send me an e-mail on it.

********************************************************/

#include <stdio.h>
#include "SZMLoader.h"
#include "SZM_Format.h"
#include "MyFile.h"
#include "externals.h"
////////////////////////////////////////////////////
//
////////////////////////////////////////////////////
#define TEXTURE_PATH "Textures\\"

#define SZM_SIGNETURE ' MZS'
#define SZM_MajorV '4000'
#define SZM_MinorV '3000'

////////////////////////////////////////////////////
//
////////////////////////////////////////////////////
DWORD dwAmountOfMemory;
////////////////////////////////////////////////////
//
////////////////////////////////////////////////////
BOOL CSZM_Loader::LoadSZM(char *szFileName,Object *pObject)
{
	dwAmountOfMemory=0;

	CMyFile SZMFile;
	SZM_File_header l_SZM_File_header;
	//----------------------------------------
	if(!SZMFile.Open(szFileName,0,0))
		return FALSE;
	//----------------------------------------
	if(!SZMFile.Read(&l_SZM_File_header,sizeof(SZM_File_header)))
		return FALSE;

	if(l_SZM_File_header.SZM_Sig!=SZM_SIGNETURE)
	{
		DisplayErrorMessage("CSZM_Loader::LoadSZM() -> SZM_SIGNETURE !SZM_SIGNETURE");
		return FALSE;
	}
	if(l_SZM_File_header.version1!=SZM_MajorV)
	{
		DisplayErrorMessage("CSZM_Loader::LoadSZM() -> SZM_MajorV != 4");
		return FALSE;
	}
	if(l_SZM_File_header.version2!=SZM_MinorV)
	{
		DisplayErrorMessage("CSZM_Loader::LoadSZM() -> SZM_MinorV != 2");
		return FALSE;
	}

	//Right after the header there is a DWORD that holds the number of meshes in the file.
	//----------------------------------------------------------
	if(!SZMFile.Read(&pObject->nMeshs,sizeof(DWORD)))
		return FALSE;

	//----------------------------------------
	pObject->pMeshs = new MyMesh[pObject->nMeshs];
	if(!pObject->pMeshs)
	{
		ShowWinError("CSZM_Loader::LoadSZM() -> Out Of Memory!");
		return FALSE;
	}
	dwAmountOfMemory+=(sizeof(MyMesh)*pObject->nMeshs);
	//----------------------------------------------------------

  for(DWORD loop=0;loop<pObject->nMeshs;loop++)
  {
	//----------------------------------------------------------
	//Mesh Name
	if(!SZMFile.Read(&pObject->pMeshs[loop].nMeshName,sizeof(DWORD)))
		return FALSE;

	pObject->pMeshs[loop].szMeshName = new char[pObject->pMeshs[loop].nMeshName];
	if(!pObject->pMeshs[loop].szMeshName)
	{
		ShowWinError("CSZM_Loader::LoadSZM() -> Out Of Memory!");
		return FALSE;
	}
	dwAmountOfMemory+=(pObject->pMeshs[loop].nMeshName);

	if(!SZMFile.Read(pObject->pMeshs[loop].szMeshName,pObject->pMeshs[loop].nMeshName))
		return FALSE;
	//----------------------------------------------------------
	//Texture Name

	if(!SZMFile.Read(&pObject->pMeshs[loop].nTextureFileName,sizeof(DWORD)))
		return FALSE;

	if(!pObject->pMeshs[loop].nTextureFileName)
	{
		pObject->pMeshs[loop].szTextureFileName=0;
	}
	else
	{
		DWORD strLen;
		pObject->pMeshs[loop].szTextureFileName = new char[pObject->pMeshs[loop].nTextureFileName+(sizeof(TEXTURE_PATH)-1)];
		if(!pObject->pMeshs[loop].szMeshName)
		{
			ShowWinError("CSZM_Loader::LoadSZM() -> Out Of Memory!");
			return FALSE;
		}
		dwAmountOfMemory+=(pObject->pMeshs[loop].nTextureFileName+(sizeof(TEXTURE_PATH)-1));

		strLen = strlen(TEXTURE_PATH);
		strcpy(pObject->pMeshs[loop].szTextureFileName,TEXTURE_PATH);

		if(!SZMFile.Read(&pObject->pMeshs[loop].szTextureFileName[strLen],pObject->pMeshs[loop].nTextureFileName))
			return FALSE;


		if(pObject->pMeshs[loop].nTextureFileName)
		{
			if(!SZMFile.Read(&pObject->pMeshs[loop].UOffsetData,sizeof(float)))
				return FALSE;
			if(!SZMFile.Read(&pObject->pMeshs[loop].VOffsetData,sizeof(float)))
				return FALSE;
			if(!SZMFile.Read(&pObject->pMeshs[loop].UTilingData,sizeof(float)))
				return FALSE;
			if(!SZMFile.Read(&pObject->pMeshs[loop].VTilingData,sizeof(float)))
				return FALSE;
		}

	}
	//----------------------------------------------------------
	//Vertexs
	if(!SZMFile.Read(&pObject->pMeshs[loop].nVertex,sizeof(DWORD)))
		return FALSE;

	pObject->pMeshs[loop].pVertex = new MyD3DVERTEX[pObject->pMeshs[loop].nVertex];
	if(!pObject->pMeshs[loop].szMeshName)
	{
		ShowWinError("CSZM_Loader::LoadSZM() -> Out Of Memory!");
		return FALSE;
	}
	dwAmountOfMemory+=(sizeof(MyD3DVERTEX)*pObject->pMeshs[loop].nVertex);

	if(!SZMFile.Read(pObject->pMeshs[loop].pVertex,sizeof(MyD3DVERTEX)*pObject->pMeshs[loop].nVertex))
		return FALSE;
	//----------------------------------------------------------
	//Matarial
	if(!SZMFile.Read(&pObject->pMeshs[loop].Material,sizeof(MyMaterial)))
		return FALSE;
	//----------------------------------------------------------
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<< -New (25/5/2007)
	//Read the Mesh Level In Tree
	if(!SZMFile.Read(&pObject->pMeshs[loop].dwLevelInTree,sizeof(DWORD)))
		return FALSE;
	//----------------------------------------------------------
	//Read the Mesh Number
	if(!SZMFile.Read(&pObject->pMeshs[loop].dwLevelInTree,sizeof(DWORD)))
		return FALSE;
	//----------------------------------------------------------
	//Read Bounding Box
	if(!SZMFile.Read(&pObject->pMeshs[loop].bHasBoundingBox,sizeof(BOOL)))
		return FALSE;

	if(pObject->pMeshs[loop].bHasBoundingBox)
	{
		if(!SZMFile.Read(&pObject->pMeshs[loop].BoundingBox,sizeof(Box)))
			return FALSE;
	}
	//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<< -END New (25/5/2007)


  }

  //dwAmountOfMemory holds the amount of memory that is needed by the loaded model.
  sprintf(g_Buffer,"dwAmountOfMemory=%u\n",dwAmountOfMemory);
  //AfxMessageBox(g_Buffer);

	//
	//Close File Handle.
	//
	SZMFile.Close();

	//----------------------------------------
	return TRUE;
}
////////////////////////////////////////////////////
//
////////////////////////////////////////////////////
BOOL CSZM_Loader::FreeObject(Object *pObject)
{

	//if(pObject->pVertexBuffers

	return TRUE;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Systems Engineer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions