游戏.数据包程序
2009-04-28 09:57
288 查看
/**************************************************
game Data Storage Demo code http://blow.csdn.net/chinayaosir **************************************************/
//0.头文件
#include <windows.h>
#include <stdio.h>
//1.数据包类定义
class cDataPackage
{
protected:
void *m_Buf;
unsigned long m_Size;
public:
cDataPackage() { m_Buf = NULL; m_Size = 0; }
~cDataPackage() { Free(); }
void *Create(unsigned long Size) {
Free();
return (m_Buf = (void*)new char[(m_Size = Size)]);
}
void Free() { delete m_Buf; m_Buf = NULL; m_Size = 0; }
BOOL Save(char *Filename)
{
FILE *fp;
if(m_Buf != NULL && m_Size) {
if((fp=fopen(Filename, "wb")) != NULL) {
fwrite(&m_Size, 1, 4, fp);
fwrite(m_Buf, 1, m_Size, fp);
fclose(fp);
return TRUE;
}
}
return FALSE;
}
void *Load(char *Filename, unsigned long *Size)
{
FILE *fp;
Free();
if((fp=fopen(Filename, "rb"))!=NULL) {
// Read in size and data
fread(&m_Size, 1, 4, fp);
if((m_Buf = (void*)new char[m_Size]) != NULL)
fread(m_Buf, 1, m_Size, fp);
fclose(fp);
// Store size to return
if(Size != NULL)
*Size = m_Size;
return m_Buf;
}
return NULL;
}
};
// 一个包含名字的测试结构sName
typedef struct {
char Name[32];
} sName;
int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, /
LPSTR szCmdLine, int nCmdShow)
{
cDataPackage DP;
DWORD Size;
// Create the data package (w/128 bytes) and get the
// pointer, casting it to an sName structure type.
sName *Names = (sName*)DP.Create(128);
// Since there are 128 bytes total, and each name uses 32 bytes,
//then I can have 4 names stored.
strcpy(Names[0].Name, "zhangsan");
strcpy(Names[1].Name, "lishi");
strcpy(Names[2].Name, "wanger");
strcpy(Names[3].Name, "chinayaosir");
// Save the names to disk and free the data buffer
DP.Save("names.dat");
DP.Free();
// Load the names from disk. Size will equal 128
// when the load function returns.
Names = (sName*)DP.Load("names.dat", &Size);
// Display the names
MessageBox(NULL, Names[0].Name, "the 1 Name ", MB_OK);
MessageBox(NULL, Names[1].Name, "the 2 Name ", MB_OK);
MessageBox(NULL, Names[2].Name, "the 3 Name ", MB_OK);
MessageBox(NULL, Names[3].Name, "the 4 Name ", MB_OK);
// Free up the data package
DP.Free();
return 0;
}
/*
run value:生成一个names.dat文件,并存有4个名字,然后用对话框显示结果
*/
game Data Storage Demo code http://blow.csdn.net/chinayaosir **************************************************/
//0.头文件
#include <windows.h>
#include <stdio.h>
//1.数据包类定义
class cDataPackage
{
protected:
void *m_Buf;
unsigned long m_Size;
public:
cDataPackage() { m_Buf = NULL; m_Size = 0; }
~cDataPackage() { Free(); }
void *Create(unsigned long Size) {
Free();
return (m_Buf = (void*)new char[(m_Size = Size)]);
}
void Free() { delete m_Buf; m_Buf = NULL; m_Size = 0; }
BOOL Save(char *Filename)
{
FILE *fp;
if(m_Buf != NULL && m_Size) {
if((fp=fopen(Filename, "wb")) != NULL) {
fwrite(&m_Size, 1, 4, fp);
fwrite(m_Buf, 1, m_Size, fp);
fclose(fp);
return TRUE;
}
}
return FALSE;
}
void *Load(char *Filename, unsigned long *Size)
{
FILE *fp;
Free();
if((fp=fopen(Filename, "rb"))!=NULL) {
// Read in size and data
fread(&m_Size, 1, 4, fp);
if((m_Buf = (void*)new char[m_Size]) != NULL)
fread(m_Buf, 1, m_Size, fp);
fclose(fp);
// Store size to return
if(Size != NULL)
*Size = m_Size;
return m_Buf;
}
return NULL;
}
};
// 一个包含名字的测试结构sName
typedef struct {
char Name[32];
} sName;
int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev, /
LPSTR szCmdLine, int nCmdShow)
{
cDataPackage DP;
DWORD Size;
// Create the data package (w/128 bytes) and get the
// pointer, casting it to an sName structure type.
sName *Names = (sName*)DP.Create(128);
// Since there are 128 bytes total, and each name uses 32 bytes,
//then I can have 4 names stored.
strcpy(Names[0].Name, "zhangsan");
strcpy(Names[1].Name, "lishi");
strcpy(Names[2].Name, "wanger");
strcpy(Names[3].Name, "chinayaosir");
// Save the names to disk and free the data buffer
DP.Save("names.dat");
DP.Free();
// Load the names from disk. Size will equal 128
// when the load function returns.
Names = (sName*)DP.Load("names.dat", &Size);
// Display the names
MessageBox(NULL, Names[0].Name, "the 1 Name ", MB_OK);
MessageBox(NULL, Names[1].Name, "the 2 Name ", MB_OK);
MessageBox(NULL, Names[2].Name, "the 3 Name ", MB_OK);
MessageBox(NULL, Names[3].Name, "the 4 Name ", MB_OK);
// Free up the data package
DP.Free();
return 0;
}
/*
run value:生成一个names.dat文件,并存有4个名字,然后用对话框显示结果
*/
相关文章推荐
- 游戏程序流、状态处理机、进程管理器、数据包系统的实现
- 游戏程序流、状态处理机、进程管理器、数据包系统的实现
- 编写设定游戏按键的小程序
- 利用双计算机调试游戏程序
- cocos2d-x游戏发布交叉编译成安卓程序(二)交叉编译
- python---Trivia游戏(问答题程序)
- 网络游戏中的数据包设计与定义(初论)
- 【腾讯GAD暑期训练营游戏程序班】游戏中的特效系统作业说明文档
- 在串口通讯程序中处理数据包
- 【Android游戏开发之十】(优化处理)详细剖析Android Traceview 效率检视工具!分析程序运行速度!并讲解两种创建SDcard方式!
- 公司新年网页抽奖程序 数字抽奖游戏JS特效
- 用c语言实现模拟用户登录程序以及猜数字游戏
- 今天编译了DEVCPP+ALLEGRO的第一个程序,写游戏的确很强
- 关于西游记游戏程序代码感想
- 【Android游戏开发之十】(优化处理)详细剖析Android Traceview效率检视工具,分析程序运行速度!并讲解两种创建SDcard方式!
- 基于Android平台的网络游戏对战系统项目可执行程序及源代码
- 用Eclipse开发J2ME手机游戏 第四讲 创建经典的HelloWorld程序
- 13周,1题:利用Java Swing技术设计一个鼠标点击熟读比赛游戏程序。
- Silverlight 游戏开发小技巧:透明背景的Silverlight程序
- 猴子选大王游戏程序