Add more functions to class Surface

This commit is contained in:
Kirigaya Kazuto 2017-05-23 13:05:09 +08:00
parent 1b2ba7b2d8
commit 79b97736b4
2 changed files with 144 additions and 52 deletions

View File

@ -376,7 +376,7 @@ namespace MiniEngine
} }
// private // private
SDL_RWops* RWOP::_get() SDL_RWops* RWOP::_get() const
{ {
return _op.get(); return _op.get();
} }
@ -412,17 +412,20 @@ namespace MiniEngine
_clear(); _clear();
} }
void Surface::_set(SDL_Surface* p)//private //private
void Surface::_set(SDL_Surface* p)
{ {
_surf.reset(p,SDL_FreeSurface); _surf.reset(p,SDL_FreeSurface);
} }
void Surface::_clear()//private //private
void Surface::_clear()
{ {
_surf.reset(); _surf.reset();
} }
SDL_Surface* Surface::_get()//private //private
SDL_Surface* Surface::_get() const
{ {
return _surf.get(); return _surf.get();
} }
@ -432,6 +435,115 @@ namespace MiniEngine
return _surf; return _surf;
} }
Surface::Surface(int width,int height,int depth,int Rmask,int Gmask,int Bmask,int Amask) throw(ErrorViewer)
{
if(create(width,height,depth,Rmask,Gmask,Bmask,Amask)!=0)
{
ErrorViewer e;
e.fetch();
throw e;
}
}
Surface::Surface(int width,int height,int depth,RGBA maskPack) throw (ErrorViewer)
: Surface(width,height,depth,maskPack.r,maskPack.g,maskPack.b,maskPack.a)
{
}
Surface::Surface(int width,int height,int depth,Uint32 surfaceFormat) throw(ErrorViewer)
{
if(create(width,height,depth,surfaceFormat)!=0)
{
ErrorViewer e;
e.fetch();
throw e;
}
}
Surface::Surface(const std::string& filename) throw(ErrorViewer)
{
if(load(filename)!=0)
{
ErrorViewer e;
e.fetch();
throw e;
}
}
Surface::Surface(const RWOP& rwop) throw (ErrorViewer)
{
if(load(rwop)!=0)
{
ErrorViewer e;
e.fetch();
throw e;
}
}
int Surface::load(const std::string& filename)
{
SDL_Surface* temp=IMG_Load(filename.c_str());
if(temp==nullptr)
{
return -1;
}
else
{
_set(temp);
return 0;
}
}
int Surface::load(const RWOP& rwop)
{
SDL_Surface* temp=IMG_Load_RW(rwop._get(),0);
if(temp==nullptr)
{
return -1;
}
else
{
_set(temp);
return 0;
}
}
int Surface::create(int width,int height,int depth,int Rmask,int Gmask,int Bmask,int Amask)
{
SDL_Surface* temp=SDL_CreateRGBSurface(0,width,height,depth,Rmask,Gmask,Bmask,Amask);
if(temp==nullptr)
{
return -1;
}
else
{
_set(temp);
return 0;
}
}
int Surface::create(int width,int height,int depth,Uint32 surfaceFormat)
{
/// FIXME: This Function is available from SDL2.0.5. But the linker report a undefined reference.
/*
SDL_Surface* temp=SDL_CreateRGBSurfaceWithFormat(0,width,height,depth,surfaceFormat);
if(temp==nullptr)
{
return -1;
}
else
{
_set(temp);
return 0;
}
*/
SDL_SetError("[MiniEngine] SDL_CreateRGBSurfaceWithFormat Not Linked.");
return -1;
}
int Surface::getw() int Surface::getw()
{ {
if(_get()!=nullptr) if(_get()!=nullptr)
@ -588,19 +700,9 @@ namespace MiniEngine
SDL_UnlockSurface(_get()); SDL_UnlockSurface(_get());
} }
//static bool Surface::isReady() const
Surface Surface::createSurface(int width,int height,int depth,int Rmask,int Gmask,int Bmask,int Amask) throw(ErrorViewer)
{ {
SDL_Surface* temp=SDL_CreateRGBSurface(0,width,height,depth,Rmask,Gmask,Bmask,Amask); return _get()!=nullptr;
if(temp==nullptr)
{
ErrorViewer e;
e.fetch();
throw e;
}
Surface surf;
surf._set(temp);
return surf;
} }
void Surface::release() void Surface::release()
@ -608,6 +710,12 @@ namespace MiniEngine
_clear(); _clear();
} }
/// Experimental
SDL_Surface* Surface::getRawPointer()
{
return _get();
}
void Texture::_set(SDL_Texture* p)//private void Texture::_set(SDL_Texture* p)//private
{ {
_text.reset(p,SDL_DestroyTexture); _text.reset(p,SDL_DestroyTexture);
@ -871,34 +979,6 @@ namespace MiniEngine
return SDL_RenderCopyEx(_get(),t._get(),pR1,pR2,angle,pPoint,flip); return SDL_RenderCopyEx(_get(),t._get(),pR1,pR2,angle,pPoint,flip);
} }
Surface Renderer::loadSurface(std::string FileName) throw(ErrorViewer)
{
Surface surf;
SDL_Surface* temp = IMG_Load(FileName.c_str());
if (temp == nullptr)
{
ErrorViewer e;
e.fetch();
throw e;
}
surf._set(temp);
return surf;
}
Surface Renderer::loadSurfaceRW(RWOP rwop) throw (ErrorViewer)
{
Surface surf;
SDL_Surface* temp=IMG_Load_RW(rwop._get(),0);
if(temp==nullptr)
{
ErrorViewer e;
e.fetch();
throw e;
}
surf._set(temp);
return surf;
}
Texture Renderer::render(Surface surf) throw(ErrorViewer) Texture Renderer::render(Surface surf) throw(ErrorViewer)
{ {
Texture t; Texture t;

View File

@ -84,9 +84,10 @@ namespace MiniEngine
void release(); void release();
private: private:
std::shared_ptr<SDL_RWops> _op; std::shared_ptr<SDL_RWops> _op;
SDL_RWops* _get(); SDL_RWops* _get() const;
void _clear(); void _clear();
void _set(SDL_RWops*); void _set(SDL_RWops*);
friend class Surface;
friend class Renderer; friend class Renderer;
}; };
@ -95,7 +96,20 @@ namespace MiniEngine
class Surface class Surface
{ {
public: public:
Surface()=default;
Surface(int width,int height,int depth,int Rmask,int Gmask,int Bmask,int Amask) throw(ErrorViewer);
Surface(int width,int height,int depth,RGBA colorPack) throw(ErrorViewer);
Surface(int width,int height,int depth,Uint32 surfaceFormat) throw(ErrorViewer);
Surface(const std::string& filename) throw(ErrorViewer);
Surface(const RWOP& rwop) throw(ErrorViewer);
~Surface() = default; ~Surface() = default;
int load(const std::string& filename);
int load(const RWOP& rwop);
int create(int width,int height,int depth,int Rmask,int Gmask,int Bmask,int Amask);
int create(int width,int height,int depth,Uint32 surfaceFormat);
int savePNG(const std::string& filename); int savePNG(const std::string& filename);
int getw(); int getw();
int geth(); int geth();
@ -126,16 +140,16 @@ namespace MiniEngine
int lock(); int lock();
void unlock(); void unlock();
static Surface createSurface(int width,int height,int depth,int Rmask,int Gmask,int Bmask,int Amask) throw(ErrorViewer); bool isReady() const;
void release(); void release();
protected:
Surface() = default; /// Experimental : Get SDL_Surface Pointer and then do anything you want!
SDL_Surface* getRawPointer();
private: private:
std::shared_ptr<SDL_Surface> _surf; std::shared_ptr<SDL_Surface> _surf;
void _set(SDL_Surface*); void _set(SDL_Surface*);
void _clear(); void _clear();
SDL_Surface* _get(); SDL_Surface* _get() const;
std::shared_ptr<SDL_Surface>& _getex(); std::shared_ptr<SDL_Surface>& _getex();
friend class Window; friend class Window;
@ -208,8 +222,6 @@ namespace MiniEngine
int supercopy(Texture t,bool srcfull,Rect src,bool dstfull,Rect dst,double angle,bool haspoint,Point center,FlipMode mode); int supercopy(Texture t,bool srcfull,Rect src,bool dstfull,Rect dst,double angle,bool haspoint,Point center,FlipMode mode);
Surface loadSurface(std::string FileName) throw(ErrorViewer);
Surface loadSurfaceRW(RWOP rwop) throw(ErrorViewer);
Texture render(Surface surf) throw (ErrorViewer); Texture render(Surface surf) throw (ErrorViewer);
Texture loadTexture(std::string FileName) throw(ErrorViewer); Texture loadTexture(std::string FileName) throw(ErrorViewer);
Texture loadTextureRW(RWOP rwop) throw(ErrorViewer); Texture loadTextureRW(RWOP rwop) throw(ErrorViewer);