本篇是的
创建游戏内核(3)续篇,并且以
游戏程序流、状态处理机、进程管理器、数据包系统的实现中介绍的数据包系统的实现为基础,加入了两个函数,分别用于返回数据缓冲区的大小和指向缓冲区的指针。
下面是这个类的定义:
//==========================================================================
// Defines for data package.
//==========================================================================
class DATA_PACKAGE
{
protected:
void* _buf;
unsigned long _size;
public:
DATA_PACKAGE();
~DATA_PACKAGE();
void* Create(unsigned long size);
void Free();
BOOL Save(const char* filename);
void* Load(const char* filename, unsigned long* size);
void* Get_Ptr();
unsigned long Get_Size();
};
类的实现:
//-----------------------------------------------------------------------------
// Constructor, initialize buffer and size.
//-----------------------------------------------------------------------------
DATA_PACKAGE::DATA_PACKAGE()
{
_buf = NULL;
_size = 0;
}
//-----------------------------------------------------------------------------
// Destructor, free buffer.
//-----------------------------------------------------------------------------
DATA_PACKAGE::~DATA_PACKAGE()
{
Free();
}
//-----------------------------------------------------------------------------
// Re-allocate new buffer.
//-----------------------------------------------------------------------------
void* DATA_PACKAGE::Create(unsigned long size)
{
// free a previously created buffer
Free();
// allocate some memory and return a pointer
if((_buf = new char[_size = size]) == NULL)
return NULL;
memset(_buf, 0, _size);
return _buf;
}
//-----------------------------------------------------------------------------
// Free the allocated memory
//-----------------------------------------------------------------------------
void DATA_PACKAGE::Free()
{
delete _buf;
_buf = NULL;
_size = 0;
}
//-----------------------------------------------------------------------------
// Write buffer into file.
//-----------------------------------------------------------------------------
BOOL DATA_PACKAGE::Save(const char *filename)
{
FILE* fp;
// make sure there is something to write
if(_buf == NULL || _size == 0)
return FALSE;
// open file, write size and data.
if((fp = fopen(filename, "wb")) == NULL)
return FALSE;
fwrite(&_size, 1, 4, fp);
fwrite(_buf, 1, _size, fp);
fclose(fp);
return TRUE;
}
//-----------------------------------------------------------------------------
// Load data from file.
//-----------------------------------------------------------------------------
void* DATA_PACKAGE::Load(const char *filename, unsigned long *size)
{
FILE* fp;
// free a prior buffer
Free();
if((fp = fopen(filename, "rb")) == NULL)
return NULL;
// read in size and data
fread(&_size, 1, 4, fp);
if((_buf = new char[_size]) != NULL)
fread(_buf, 1, _size, fp);
fclose(fp);
// store size to return
if(size != NULL)
*size = _size;
// return pointer
return _buf;
}
//-----------------------------------------------------------------------------
// Get buffer pointer.
//-----------------------------------------------------------------------------
void* DATA_PACKAGE::Get_Ptr()
{
return _buf;
}
//-----------------------------------------------------------------------------
// Get buffer size.
//-----------------------------------------------------------------------------
unsigned long DATA_PACKAGE::Get_Size()
{
return _size;
}
测试代码:
点击下载源码和工程
/*****************************************************************************
PURPOSE:
Test for class DATA_PACKAGE.
*****************************************************************************/
#include "Core_System.h"
#pragma warning(disable : 4996)
class APP : public APPLICATION
{
private:
DATA_PACKAGE _data_package;
// a structure to contain a name
struct NAME
{
char name[32];
};
public:
BOOL Init()
{
DWORD size;
// create the data package (w/64 bytes) and get the pointer, casting it to an NAME structure type.
NAME* names = (NAME*) _data_package.Create(64);
// since there are 64 bytes total, and each name uses 32 bytes, then I can have 2 names stored.
strcpy(names[0].name, "loved");
strcpy(names[1].name, "day");
const char* filename = "names.data";
// save the names to disk
_data_package.Save(filename);
// load the names from disk, size will equal 64 when the load function returns.
names = (NAME*) _data_package.Load(filename, &size);
// display the names
MessageBox(NULL, names[0].name, "1st name", MB_OK);
MessageBox(NULL, names[1].name, "2nd name", MB_OK);
return TRUE;
}
};
int PASCAL WinMain(HINSTANCE inst, HINSTANCE, LPSTR cmd_line, int cmd_show)
{
APP app;
return app.Run();
}
运行截图: