本篇是创建游戏内核(3)【接口与实现分离版】的续篇,关于该内核的细节说明请参考创建游戏内核(4),这个版本主要是按照功能划分模块的思想,并严格按照接口与实现相分离的原则来写的,没有用面向对象的思想来写,没有继承没有多态。大家可以对比两个版本,比较优劣。
给用户的接口:
/*************************************************************************
PURPOSE:
Interface for data read and write from file.
*************************************************************************/
#ifndef _CORE_DATA_H_
#define _CORE_DATA_H_
typedef int BOOL;
void* data_create(unsigned long size);
void data_destroy(void* data);
BOOL data_save(const char* filename, const void* data, unsigned long size);
BOOL data_load(const char* filename, void* data, unsigned long* size);
#endif
接口的实现:
/*************************************************************************
PURPOSE:
Implenents for data read and write from file.
*************************************************************************/
#include <stdio.h>
#include <malloc.h>
#include <memory.h>
#include "core_data.h"
#pragma warning(disable : 4996)
#define NULL 0
#define TRUE 1
#define FALSE 0
//-----------------------------------------------------------------------------
// Create data object.
//-----------------------------------------------------------------------------
void* data_create(unsigned long size)
{
void* data = malloc(size);
if(data)
memset(data, 0, size);
return data;
}
//-----------------------------------------------------------------------------
// Destroy data object
//-----------------------------------------------------------------------------
void data_destroy(void* data)
{
free(data);
data = NULL;
}
//-----------------------------------------------------------------------------
// Write data into file.
//-----------------------------------------------------------------------------
BOOL data_save(const char* filename, const void* data, unsigned long size)
{
FILE* fp;
if(data == 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(data, 1, size, fp);
fclose(fp);
return TRUE;
}
//-----------------------------------------------------------------------------
// Load data from file.
//-----------------------------------------------------------------------------
BOOL data_load(const char* filename, void* data, unsigned long* size)
{
FILE* fp;
if((fp = fopen(filename, "rb")) == NULL)
return FALSE;
// read in size and data
unsigned long _size;
char* _data;
fread(&_size, 1, 4, fp);
if((_data = (char*) malloc(_size)) == NULL)
return FALSE;
fread(_data, 1, _size, fp);
fclose(fp);
data = _data;
// store size in return
if(size != NULL)
*size = _size;
return TRUE;
}
测试代码:
/*************************************************************************
PURPOSE:
Test for manager stack.
*************************************************************************/
#include "core_common.h"
#include "core_framework.h"
#include "core_data.h"
// a structure to contain a name
struct NAME
{
char name[32];
};
BOOL game_init()
{
const int _data_size = 64;
// create the data package (w/64 bytes) and get the pointer, casting it to an NAME structure type.
NAME* _names = (NAME*) data_create(_data_size);
// since there are 64 bytes total, and each name uses 32 bytes, then I can have 2 names stored.
strcpy(_names[0].name, "name1");
strcpy(_names[1].name, "name2");
const char* _filename = "names.data";
// save the names to disk
data_save(_filename, _names, _data_size);
DWORD _load_size;
// load the names from disk, size will equal 64 when the load function returns.
data_load(_filename, _names, &_load_size);
// display the names
MessageBox(NULL, _names[0].name, "1st name", MB_OK);
MessageBox(NULL, _names[1].name, "2nd name", MB_OK);
return TRUE;
}
BOOL game_frame()
{
return TRUE;
}
BOOL game_shutdown()
{
return TRUE;
}
//--------------------------------------------------------------------------------
// Main function, routine entry.
//--------------------------------------------------------------------------------
int WINAPI WinMain(HINSTANCE inst, HINSTANCE pre_inst, LPSTR cmd_line, int cmd_show)
{
if(! build_window(inst, "MainClass", "MainWindow", WS_OVERLAPPEDWINDOW, 0, 0, 800, 600))
return FALSE;
run_game(game_init, game_frame, game_shutdown);
return 0;
}
点击下载源码和工程