插件:具有指定接口的一个独立的二进制代码,常以DLL形式存在,不过也可以为本地EXE,甚至远程代码,此时需要使用一些底层代码。
其好处在于,独立性极强,可以独立于引擎使用。
模块:基于引擎的核心概念,对于引擎功能的扩展和完善。需要严格保持同引擎的代码想关联。例如,C#所有的模块都是底层机制的一个模块。对于游戏引擎来说,每个具体的游戏就是引擎的一个模块。当然模块可能只是一个对于引擎的扩展,如:粒子系统模块就是引擎的一个下属子模块,通用游戏逻辑也是引擎的一个模块。模块之间存在强烈的代码关联性。模块也常以DLL形式存在。其完全基于引擎,并且不能独立引擎工作,可重用性低。
而基于组件的编程方式,可以说是集两种方式的优点于一身。现有的流行的组件编程方式如下:
1. 定义IDL实现对于组件的抽象;
2.实现IDL到C/C++(甚至可以是Java等)的映射;
3.在C/C++中实现实际组件。
4.组件使用者根据组件定义调用组件属性和方法。
下面为freesky engine的实际使用:
一 IDL的定义
(一)简介
IDL是一种类型和接口定义语言。具体细节可以参考CORBA标准(COM的IDL是纯接口定义)。
(二)关键字及说明
module
模块,类似于namespace,允许嵌套使用,但freeskyengine中只允许定义唯一module层。
标准系统类型
char wchar short int hyper float double string wstring
自定义类型
enum struct union interface class
特别描述说明
public private protected const
(三)语法
module mymodule{
...
}
const float pi = 3.14159f;
enum {
sunday = 0,
monday,
...
friday = 5,
saterday
};
union {
float _fv;
char[4] _cv;
};
struct point {
float x;
float y;
};
interface ILog {
void log( const string );
};
class person {
public int id;
public string name;
public virtual void sleep();
protected void smile();
};
class student : person {
public string school;
public virtual void gotoschool();
};
二 IDL的C/C++映射
namespace mymodule {
}
#ifndef pi
const float pi = 3.14159f;
#endif
enum {
sunday = 0,
monday,
...
friday = 5,
saterday
};
union {
float _fv;
char[4] _cv;
};
struct point {
float x;
float y;
};
interface ILog {
virtual void log( const string ) = 0;
};
class person {
public int id;
public string name;
public virtual void sleep();
protected void smile();
};
class student : public person {
public string school;
public virtual void gotoschool();
};
具体编译器代码在 这里
三 使用生成的C/C++头文件和cpp文件加入c/c++项目中编译,并补齐c/c++的实现函数部分。
四 客户端引用产生的c/c++头文件,并通过标准c/c++方式使用。
具体使用示例可以参考freeskyengine的实现。