渲染树的简单实现(链表实现)

//对象的创建初始化,挂到链表上去
//实现了简单的内存管理

class CCSprite {
private:
	static CCSprite* head;
	CCSprite* next;
public:
	CCSprite(){}
	static CCSprite* create() {
		CCSprite *sprite = new CCSprite;
		if (sprite && sprite->init()) {  
			sprite->autoRelease();
			return sprite;
		}
		else {
			delete sprite;
			sprite = nullptr;
			exit(-1);
		}
		
	}

	bool init() {
		cout << "init sprite" << endl;
		return true;
	}

	static void renderTree() {
		CCSprite* t = head;
		while (t) {

			t = t->next;
		}
	}

	void autoRelease() {   //内存挂到链表上
		this->next = head;
		head = this;
	}

};

CCSprite* CCSprite::head = nullptr;

int main() {

	CCSprite* ps = CCSprite::create();   //利用渲染树生成精灵
	CCSprite* ps2 = CCSprite::create();
	CCSprite* ps3 = CCSprite::create();
	CCSprite* ps4 = CCSprite::create();

	system("PAUSE");
}

多态实现不同的类对象连接到渲染树上(利用父类指针的赋值兼容)

#include 
#include 
using namespace std;

class RenderShape { // 抽象基类
public:
	virtual void show() = 0;
	bool init(int x,int y) {  //初始化函数
		_x = x;
		_y = y;

		return true;
	}
	
	static void rendershapeList() { 
		RenderShape* t = head;
		while (t) {
			t->show();   //通过父类的指针覆写子类
			t = t->next;
		}
	}

protected:
	int _x;
	int _y;

	static RenderShape* head;
	RenderShape* next;
};
RenderShape * RenderShape::head = nullptr;

class Rect :public RenderShape
{
public:
	static Rect* create(int x,int y,int w,int l) {
		Rect* prect = new Rect;
		if (prect && prect->init(x, y, w, l)) {
			prect->autoRelease();
			return prect;
		}
		else {
			delete prect;
			prect = nullptr;
		}
	}

	
	void show() override {
		cout << "draw rect from" << "(" << _x << "," << _y << ")" <<","<<_w<<","<<_l<next = head;
			head = this;
	}

protected:
	int _w;
	int _l;
};

class Circle :public RenderShape
{
public:
	void show() override {
		cout << "draw circle from" << "(" << _x << "," << _y << ")" <<"||"<<_r<< endl;
	}

	static Circle* create(int x,int y,int r) {
		Circle* pCircle = new Circle;
		if (pCircle && pCircle->init(x, y, r)) {
			pCircle->autoRelease();
			return pCircle;
		}
		else {
			delete pCircle;
			pCircle = nullptr;
		}
	}

	bool init(int x, int y, int r) {
		RenderShape::init(x, y);
		_r = r;
		return true;
	}
	
	void autoRelease() {
		this->next = head;
		head = this;
	}

protected:
	int _r;
};

class Ellipse :public RenderShape
{
public:
	void show() override {
		cout << "draw Ellipse from" << "(" << _x << "," << _y << ")" << "||" << _l <<" "<<_s<< endl;
	}

	static Ellipse* create(int x, int y, int l,int s) {
		Ellipse* pEllipse = new Ellipse;
		if (pEllipse && pEllipse->init(x, y, l, s)) {
			pEllipse->autoRelease();
			return pEllipse;
		}
		else {
			delete pEllipse;
			pEllipse = nullptr;
		}
	}

	bool init(int x, int y, int l,int s) {
		RenderShape::init(x, y);
		_l = l;
		_s = s;
		return true;
	}

	void autoRelease() {
		this->next = head;
		head = this;
	}
protected:
	int _l;
	int _s;
};



int main() {

	Rect *pr1 = Rect::create(1, 2, 3, 4);
	Rect *pr2 = Rect::create(5, 2, 7, 4);

	Circle *pc1 = Circle::create(5, 3, 10);
	Ellipse *pe1 = Ellipse::create(5, 12, 15, 90);

	RenderShape::rendershapeList();  //渲染链
	system("PAUSE");
}

 

你可能感兴趣的:(游戏引擎架构原理)