Click here to Skip to main content
15,896,359 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.7K   9.1K   74  
This article talks about a simple and powerful game engine to make game programming simpler
// MyFile.cpp 

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

Description: Game File Handeling

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 "MyFile.h"


////////////////////////////////////////////////////
//
////////////////////////////////////////////////////
BOOL DisplayErrorMessage(char *szMessage)
{
//	assert(0);
	MessageBox(0,
		szMessage,
		"ERROR DisplayErrorMessage",
		MB_OK|MB_ICONERROR);

	if(::MessageBox(0,
		"An Unexpected Error Happened, Continue?",
		"Error",
		MB_YESNO|MB_ICONQUESTION)==IDYES)
			return TRUE;

	ASSERT(0);


	return TRUE;
}
//////////////////////////////////////////////
//
//////////////////////////////////////////////
BOOL CMyFile::Open(char *FileName,BOOL bWrite,BOOL bNew)
{


	DWORD Flags=GENERIC_READ;
	DWORD CreationFlags=OPEN_EXISTING;

	if(bWrite)Flags|=GENERIC_WRITE;
	if(bNew)CreationFlags=CREATE_ALWAYS;

	hFile=CreateFile(FileName,
		Flags,
		FILE_SHARE_READ|FILE_SHARE_WRITE,
		NULL,
		CreationFlags,
		FILE_ATTRIBUTE_NORMAL,
		NULL);

	if(hFile == INVALID_HANDLE_VALUE)
	{
		ShowWinError("CMyFile::Open()\r\n");
		return FALSE;
	}

	return TRUE;
}
//////////////////////////////////////////////
//
//////////////////////////////////////////////
BOOL CMyFile::OpenTemp(char *FileName,BOOL bWrite,BOOL bNew)
{


	DWORD Flags=GENERIC_READ;
	DWORD CreationFlags=OPEN_EXISTING;

	if(bWrite)Flags|=GENERIC_WRITE;
	if(bNew)CreationFlags=CREATE_ALWAYS;

	hFile=CreateFile(FileName,
		Flags,
		0,
		NULL,
		CreationFlags,
		FILE_ATTRIBUTE_HIDDEN,
		NULL);

	if(hFile == INVALID_HANDLE_VALUE)
	{
		dwErrorCode=::GetLastError();
		return FALSE;
	}



	return TRUE;
}
//////////////////////////////////////////////
//
//////////////////////////////////////////////
BOOL CMyFile::Read(void *pBuffer,DWORD dwLength)
{

	DWORD dwBytesReaden;

	::ReadFile(hFile,
		pBuffer,
		dwLength,
		&dwBytesReaden,
		NULL);

	if(dwBytesReaden!=dwLength)
	{
		ShowWinError("CMyFile::Read()\r\n");
		return FALSE;
	}

	return TRUE;
}
//////////////////////////////////////////////
//
//////////////////////////////////////////////
BOOL CMyFile::Write(void *pBuffer,DWORD dwLength)
{


	DWORD dwBytesWritten;

	::WriteFile(hFile,
		pBuffer,
		dwLength,
		&dwBytesWritten,
		NULL);


	if(dwBytesWritten!=dwLength)
	{
		ShowWinError("CMyFile::Write()\r\n");
		return FALSE;
	}

	return TRUE;
}
//////////////////////////////////////////////
//
//////////////////////////////////////////////
BOOL CMyFile::Close()
{

	if(!CloseHandle(hFile))
	{
		ShowWinError("CMyFile::Close()\r\n");
		return FALSE;
	}

	return TRUE;
}
//////////////////////////////////////////////
//	
//////////////////////////////////////////////
BOOL CMyFile::GetFileSize(DWORD &dwFileSize)
{

	dwFileSize=::GetFileSize(hFile,0);
	if(dwFileSize==0xFFFFFFFF)
	{
		ShowWinError("CMyFile::GetFileSize()\r\n");
		return FALSE;
	}
	
	return TRUE;;

}
//////////////////////////////////////////////
//	Origin:
//	FILE_BEGIN
//	FILE_CURRENT
//	FILE_END 
//////////////////////////////////////////////
int CMyFile::fSeek(int Offset,int Origin)
{
BOOL ret;
	ret=::SetFilePointer(hFile,Offset,0,Origin);
	if(ret==0xFFFFFFFF)
	{
		ShowWinError("CMyFile::fSeek()\r\n");
		return FALSE;
	}

	return TRUE;
}
//////////////////////////////////////////////
//	BytsLeft = 4GB x SpaceLeftHighPart + SpaceLeftLowPart;
//////////////////////////////////////////////
int CMyFile::GetDiskFreeSpace(const char *szDirectoryName,
							  DWORD &SpaceLeftLowPart,DWORD &SpaceLeftHighPart)
{

	ULARGE_INTEGER FreeBytesAvailableToCaller;
	ULARGE_INTEGER TotalNumberOfBytes; 
	ULARGE_INTEGER TotalNumberOfFreeBytes ;

	if(!GetDiskFreeSpaceEx(szDirectoryName,
		&FreeBytesAvailableToCaller,
		&TotalNumberOfBytes,
		&TotalNumberOfFreeBytes))
	{
		dwErrorCode=::GetLastError();
		return FALSE;
	}

	SpaceLeftLowPart=FreeBytesAvailableToCaller.LowPart;
	SpaceLeftHighPart=FreeBytesAvailableToCaller.HighPart;

	return TRUE;
}
//////////////////////////////////////////////
//	
//////////////////////////////////////////////
void CMyFile::SetHandle(HANDLE newHandle)
{

	hFile=newHandle;

}
//////////////////////////////////////////////
//	
//////////////////////////////////////////////
BOOL CMyFile::GetFilePointerPos(long &iFilePointerPos)
{
	BOOL ret;
	ret=::SetFilePointer(hFile,0,0,FILE_CURRENT);
	if(ret==0xFFFFFFFF)
	{
		dwErrorCode=::GetLastError();
		return FALSE;
	}

	iFilePointerPos=ret;

	return TRUE;
}
////////////////////////////////////////////////////
//
////////////////////////////////////////////////////
void ShowWinError(char *szError)
{
	char Buffer[1000];
	char OutBuffer[1000];
	BYTE width=0;
	DWORD flags;
	flags = FORMAT_MESSAGE_MAX_WIDTH_MASK &width;
	flags |= FORMAT_MESSAGE_FROM_SYSTEM;
	flags |= FORMAT_MESSAGE_IGNORE_INSERTS;
	DWORD dwLastError = ::GetLastError();


	if(!dwLastError)
	{
		::MessageBox(0,szError,"ERROR",MB_ICONERROR|MB_OK);
//		ASSERT(0);
		return;
	}

	 ::FormatMessage(flags,
		 NULL,
		 dwLastError,
		 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
		 OutBuffer,
		 1000,
		 NULL);

	 sprintf(Buffer,"%s\r\nErrorCode=%d\r\nError Description = %s",
		 szError,dwLastError,OutBuffer);

	::MessageBox(0,Buffer,"ERROR",MB_ICONERROR|MB_OK);
//	ASSERT(0);
}

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