SDL2 游戏开发日志(二)

SDL2 游戏开发日志(二)

构建框架:场景,渲染。

渲染类

负责加载图片和渲染,它将可以添加到指定的【场景】中,当【场景】被【场景管理类】调用时,它将每一帧都被调用和更新。

#pragma once
#include <SDL.h>
#include <string>
using namespace std;

class Renderable{
protected:
	bool mIsDeleted;
	bool mIsVisible;
	int mLayer;
	SDL_Rect *mRenderRect;
	SDL_Rect *mClipRect;
	SDL_Texture *mTexture;
	int mTextureWidth, mTextureHeight;

public:
	Renderable() :mIsDeleted(false), mIsVisible(true){
		mTexture = NULL;
		mRenderRect = NULL;
		mClipRect = NULL;
		mLayer = 0;
	}
	virtual ~Renderable(){
		if (mTexture != NULL){
			SDL_DestroyTexture(mTexture);
			mTexture = NULL;
		}
		if (mRenderRect != NULL){
			delete mRenderRect;
			mRenderRect = NULL;
		}
		if (mClipRect != NULL){
			delete mClipRect;
			mClipRect = NULL;
		}
	}

	void SetLayer(int layer){
		mLayer = layer;
	}
	int GetLayer(){
		return mLayer;
	}
	virtual bool LoadTexture(string fileName);

	void SetPos(int x,int y){
		if (mRenderRect != NULL){
			mRenderRect->x = x;
			mRenderRect->y = y;
		}
	}

	bool IsDeleted(){
		return mIsDeleted;
	}

	void Delete(){
		mIsDeleted = true;
	}

	bool IsVisible(){
		return mIsVisible;
	}
	void SetVisible(bool visible){
		mIsVisible = visible;
	}

	virtual void Update(float time_step){}
	virtual void Render();

	int GetWidth(){
		return mTextureWidth;
	}
	int GetHeight(){
		return mTextureHeight;
	}
};

bool Renderable::LoadTexture(string fileName){
	bool bResult = false;
	if (mTexture != NULL){
		SDL_DestroyTexture(mTexture);
		mTexture = NULL;
	}
	SDL_Surface *surface = IMG_Load(fileName.c_str());
	if (surface != NULL){
		mTexture = SDL_CreateTextureFromSurface(theGame.getRenderer(), surface);
		if (mTexture != NULL){
			if (mRenderRect == NULL)
				mRenderRect = new SDL_Rect();
			mRenderRect->w = surface->w;
			mRenderRect->h = surface->h;
			mTextureWidth = surface->w;
			mTextureHeight = surface->h;
			bResult = true;
		}
		SDL_FreeSurface(surface);
	}
	return bResult;
}

void Renderable::Render(){	
	if (!mIsVisi
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值