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

最低0.47元/天 解锁文章
1458





