CEGUI中的Sample程序库模型

CEGUI中的sample的例子给的很全。很有研究价值。而且wiki主页上的很多都是基于sample程序库的例子,所以sample库的整天框架还是有必要搞清楚的。

下面是一个模型程序,不过基本上把sample的整体框架说明了:

#include <iostream> #include <string> #include <vector> #include <ctime> using namespace std; #define SAFE_DELETE(p) {if(p){delete p; p = 0;}} //代表整个CEGUI窗口系统 class System{ private: System() : text("") {} ~System(){} string text; public: static System* getSingleton(){ static System *singleton = new System; return singleton; } void RenderGUI(){ cout << text <<endl; } void setText(string tex){ text = tex; } }; //前置申明 class Sample; class BaseApplication { public: virtual bool excute(Sample* sampleApp) = 0; }; class OpenGLBaseApplication : public BaseApplication { public: virtual bool excute(Sample *sampleApp); }; class DXBaseApplication : public BaseApplication { public: virtual bool excute(Sample *sampleApp); }; class OrgeBaseApplication : public BaseApplication { public: virtual bool excute(Sample *sampleApp); }; //选择器类 class RenderModeSelector { public: enum RenderMode{ OpenGL, DX, Orge }; void setMode(RenderMode m){ mode = m; } RenderMode getMode(){ return mode; }; private: RenderMode mode; }; //Sample定义 class Sample { public: Sample() : sampleApp(0), selecter(0) { srand( (unsigned)time( NULL ) ); } ~Sample() { SAFE_DELETE(sampleApp); SAFE_DELETE(selecter); } //给子类改写接口 virtual bool InitialiseSample() = 0; virtual bool cleanSample() = 0; virtual bool Initialise(){ selecter = new RenderModeSelector(); //selecter->setMode( rand() ); //selecter->getMode() switch( rand()%3 ) { case RenderModeSelector::OpenGL: sampleApp = new OpenGLBaseApplication; break; case RenderModeSelector::DX: sampleApp = new DXBaseApplication; break; case RenderModeSelector::Orge: sampleApp = new OrgeBaseApplication; break; } //核心函数: 动态选择 if( sampleApp->excute(this) ) return true; return false; } virtual void cleanup(){ cout << "exit..." <<endl; } virtual int run(){ if(Initialise()) cleanup(); return 0; } private: BaseApplication *sampleApp; RenderModeSelector *selecter; }; //在一个cpp中,所以必须调整类和成员的申明和定义顺序 bool OpenGLBaseApplication::excute(Sample *sampleApp) { //接受Sample继承体系中对窗口System的修改 sampleApp->InitialiseSample(); //这里:绘制窗口 System::getSingleton()->RenderGUI(); //这里:场景绘制 cout << "OpenGL render..." << endl; return true; } bool DXBaseApplication::excute(Sample *sampleApp) { sampleApp->InitialiseSample(); //这里:绘制窗口 System::getSingleton()->RenderGUI(); //这里:场景绘制 cout << "DX render..." << endl; return true; } bool OrgeBaseApplication::excute(Sample *sampleApp) { sampleApp->InitialiseSample(); //这里:绘制窗口 System::getSingleton()->RenderGUI(); //这里:场景绘制 cout << "Orge render..." << endl; return true; } //自己定义的sample class MySample : public Sample { public: //重写initialiseSample函数只是改变system窗口的一些属性... //核心窗口显示,渲染不会改变, bool InitialiseSample(){ //改变system属性 System::getSingleton()->setText("My Sample pV_Vq"); return true; } bool cleanSample(){ //;;; return true; } }; int main() { //OOP封装思想 MySample app; return app.run(); }

只是熟悉CEGUI,那么就可以继承与CEGuiSample类,重写里面的initialise函数。

我是比较习惯使用glut库,sample中还实现了将glut封装。

你可能感兴趣的:(框架,String,null,delete,System,Class)