From 0629ceadba208744fc9f79b62d1a27cae63c7a4b Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Sun, 18 Jun 2017 17:00:08 +0800 Subject: [PATCH] Move Rect and Surface out of main file --- MiniEngine.cpp | 418 ---------------------------------------- MiniEngine.h | 93 --------- SDLWrapper/Rect.cpp | 71 +++++++ SDLWrapper/Rect.h | 21 ++ SDLWrapper/Surface.cpp | 358 ++++++++++++++++++++++++++++++++++ SDLWrapper/Surface.h | 83 ++++++++ SDLWrapper/begin_code.h | 5 + SDLWrapper/end_code.h | 3 + SDLWrapper/include.h | 8 + 9 files changed, 549 insertions(+), 511 deletions(-) create mode 100644 SDLWrapper/Rect.cpp create mode 100644 SDLWrapper/Rect.h create mode 100644 SDLWrapper/Surface.cpp create mode 100644 SDLWrapper/Surface.h create mode 100644 SDLWrapper/begin_code.h create mode 100644 SDLWrapper/end_code.h create mode 100644 SDLWrapper/include.h diff --git a/MiniEngine.cpp b/MiniEngine.cpp index a422b5f..0514212 100644 --- a/MiniEngine.cpp +++ b/MiniEngine.cpp @@ -234,71 +234,7 @@ namespace MiniEngine } }/// End of namespace _internal - Rect::Rect(int X, int Y, int W, int H) - { - x = X; - y = Y; - w = W; - h = H; - } - Rect::Rect(const SDL_Rect& r):Rect(r.x,r.y,r.w,r.h) - { - - } - - Rect::Rect() - { - x = y = w = h = 0; - } - - SDL_Rect Rect::toSDLRect() const - { - SDL_Rect r; - r.x = x; - r.y = y; - r.w = w; - r.h = h; - return r; - } - - bool Rect::isEmpty() - { - SDL_Rect r=toSDLRect(); - return SDL_RectEmpty(&r)==SDL_TRUE; - } - - bool Rect::operator == (const Rect& r) const - { - SDL_Rect a=toSDLRect(),b=r.toSDLRect(); - return SDL_RectEquals(&a,&b)==SDL_TRUE; - } - - bool Rect::hasIntersection(const Rect& r) const - { - SDL_Rect a=toSDLRect(),b=r.toSDLRect(); - return SDL_HasIntersection(&a,&b)==SDL_TRUE; - } - - Rect Rect::getIntersection(const Rect& r) const - { - SDL_Rect a=toSDLRect(),b=r.toSDLRect(),c; - if(SDL_IntersectRect(&a,&b,&c)==SDL_TRUE) - { - return Rect(c); - } - else - { - return Rect(); - } - } - - Rect Rect::getUnion(const Rect& r) const - { - SDL_Rect a=toSDLRect(),b=r.toSDLRect(),c; - SDL_UnionRect(&a,&b,&c);//void - return Rect(c); - } Point::Point(int X, int Y) { @@ -432,361 +368,7 @@ namespace MiniEngine _clear(); } - //private - void Surface::_set(SDL_Surface* p) - { - _surf.reset(p,SDL_FreeSurface); - } - //private - void Surface::_clear() - { - _surf.reset(); - } - - //private - SDL_Surface* Surface::_get() const - { - return _surf.get(); - } - - //private - void Surface::_set_no_delete(SDL_Surface* p) - { - _surf.reset(p,[](SDL_Surface*){}); - } - - Surface::Surface(int width,int height,int depth,int Rmask,int Gmask,int Bmask,int Amask) throw(ErrorViewer) - { - if(createAs(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(createAs(width,height,depth,surfaceFormat)!=0) - { - ErrorViewer e; - e.fetch(); - throw e; - } - } - - Surface::Surface(const std::string& filename) throw(ErrorViewer) - { - if(loadAs(filename)!=0) - { - ErrorViewer e; - e.fetch(); - throw e; - } - } - - Surface::Surface(const RWOP& rwop) throw (ErrorViewer) - { - if(loadAs(rwop)!=0) - { - ErrorViewer e; - e.fetch(); - throw e; - } - } - - //static - Surface Surface::load(const std::string& filename) - { - Surface s; - s.loadAs(filename); - return s; - } - - //static - Surface Surface::load(const RWOP& rwop) - { - Surface s; - s.loadAs(rwop); - return s; - } - - //static - Surface Surface::create(int width, int height, int depth, int Rmask, int Gmask, int Bmask, int Amask) - { - Surface s; - s.create(width,height,depth,Rmask,Gmask,Bmask,Amask); - return s; - } - - //static - Surface Surface::create(int width, int height, int depth, Uint32 surfaceFormat) - { - Surface s; - s.create(width,height,depth,surfaceFormat); - return s; - } - - - int Surface::loadAs(const std::string& filename) - { - SDL_Surface* temp=IMG_Load(filename.c_str()); - if(temp==nullptr) - { - return -1; - } - else - { - _set(temp); - return 0; - } - } - - int Surface::loadAs(const RWOP& rwop) - { - SDL_Surface* temp=IMG_Load_RW(rwop._get(),0); - if(temp==nullptr) - { - return -1; - } - else - { - _set(temp); - return 0; - } - } - - int Surface::createAs(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::createAs(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() const - { - if(_get()!=nullptr) - { - return _get()->w; - } - else - { - /// return -1 on null surface pointer - return -1; - } - } - - int Surface::geth() const - { - if(_get()!=nullptr) - { - return _get()->h; - } - else - { - /// return -1 on null surface pointer - return -1; - } - } - - BlendMode Surface::getBlendMode() const - { - SDL_BlendMode temp; - /// FIXME: return value are ignored. - SDL_GetSurfaceBlendMode(_get(),&temp); - return _internal::getBlendModeFromSDLBlendMode(temp); - } - - int Surface::setBlendMode(BlendMode mode) - { - return SDL_SetSurfaceBlendMode(_get(),_internal::getSDLBlendModeFromBlendMode(mode)); - } - - int Surface::savePNG(const std::string& filename) const - { - return IMG_SavePNG(_get(),filename.c_str()); - } - - int Surface::blit(const Surface& s,const Rect& src,const Rect& dst) - { - SDL_Rect rsrc=src.toSDLRect(); - SDL_Rect rdst=dst.toSDLRect(); - return SDL_BlitSurface(s._get(),&rsrc,_get(),&rdst); - } - - int Surface::blitTo(const Surface& s,const Rect& dst) - { - SDL_Rect rdst=dst.toSDLRect(); - return SDL_BlitSurface(s._get(),NULL,_get(),&rdst); - } - - int Surface::blitTo(const Surface& s,const Point& lupoint) - { - return blitTo(s,Rect(lupoint.x,lupoint.y,s.getw(),s.geth())); - } - - int Surface::blitFill(const Surface& s,const Rect& src) - { - SDL_Rect rsrc=src.toSDLRect(); - return SDL_BlitSurface(s._get(),&rsrc,_get(),NULL); - } - - int Surface::blitFullFill(const Surface& s) - { - return SDL_BlitSurface(s._get(),NULL,_get(),NULL); - } - - int Surface::blitScaled(const Surface& s,const Rect& src,const Rect& dst) - { - SDL_Rect rsrc=src.toSDLRect(); - SDL_Rect rdst=dst.toSDLRect(); - return SDL_BlitScaled(s._get(),&rsrc,_get(),&rdst); - } - - int Surface::blitScaledTo(const Surface& s,const Rect& dst) - { - SDL_Rect rdst=dst.toSDLRect(); - return SDL_BlitScaled(s._get(),NULL,_get(),&rdst); - } - - int Surface::blitScaledTo(const Surface& s,const Point& lupoint) - { - return blitScaledTo(s,Rect(lupoint.x,lupoint.y,s.getw(),s.geth())); - } - - int Surface::blitScaledFill(const Surface& s,const Rect& src) - { - SDL_Rect rsrc=src.toSDLRect(); - return SDL_BlitScaled(s._get(),&rsrc,_get(),NULL); - } - - int Surface::blitScaledFullFill(const Surface& s) - { - return SDL_BlitScaled(s._get(),NULL,_get(),NULL); - } - - int Surface::setClipRect(const Rect& clipRect) - { - auto m=clipRect.toSDLRect(); - return (SDL_SetClipRect(_get(),&m) == SDL_TRUE) ? 0 : -1; - } - - Rect Surface::getClipRect() const - { - SDL_Rect rect; - SDL_GetClipRect(_get(),&rect); - return Rect(rect); - } - - void Surface::disableClipping() - { - SDL_SetClipRect(_get(),NULL); - } - - int Surface::setAlphaMode(int alpha) - { - return SDL_SetSurfaceAlphaMod(_get(),alpha); - } - - int Surface::getAlphaMode() const - { - Uint8 al; - SDL_GetSurfaceAlphaMod(_get(),&al); - return al; - } - - int Surface::setColorMode(ColorMode mode) - { - return SDL_SetSurfaceColorMod(_get(),mode.r,mode.g,mode.b); - } - - ColorMode Surface::getColorMode() const - { - Uint8 r,g,b; - SDL_GetSurfaceColorMod(_get(),&r,&g,&b); - ColorMode mode; - mode.r=r; - mode.g=g; - mode.b=b; - return mode; - } - - void Surface::setRGBA(const RGBA& pack) - { - setColorMode(pack.toColorMode()); - setAlphaMode(pack.a); - } - - RGBA Surface::getRGBA() const - { - return RGBA(getColorMode(),getAlphaMode()); - } - - bool Surface::mustlock() const - { - return SDL_MUSTLOCK(_get()); - } - - int Surface::lock() - { - return SDL_LockSurface(_get()); - } - - void Surface::unlock() - { - SDL_UnlockSurface(_get()); - } - - bool Surface::isReady() const - { - return _get()!=nullptr; - } - - void Surface::release() - { - _clear(); - } - - /// Experimental (Not constant) - SDL_Surface* Surface::getRawPointer() - { - return _get(); - } //private void Texture::_set(SDL_Texture* p) diff --git a/MiniEngine.h b/MiniEngine.h index c7a257a..c8fa871 100644 --- a/MiniEngine.h +++ b/MiniEngine.h @@ -9,22 +9,6 @@ namespace MiniEngine { - - class Rect - { - public: - int x, y, w, h; - Rect(int X, int Y, int W, int H); - explicit Rect(const SDL_Rect&); - Rect(); - SDL_Rect toSDLRect() const; - bool isEmpty(); - bool operator == (const Rect&) const; - bool hasIntersection(const Rect&) const; - Rect getIntersection(const Rect&) const; - Rect getUnion(const Rect&) const; - }; - class Point { public: @@ -95,83 +79,6 @@ namespace MiniEngine enum class BlendMode { None,Blend,Add,Mod }; - class Surface - { - 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; - - /// static functions - static Surface load(const std::string& filename); - static Surface load(const RWOP& rwop); - static Surface create(int width,int height,int depth,int Rmask,int Gmask,int Bmask,int Amask); - static Surface create(int width,int height,int depth,Uint32 surfaceFormat); - - /// xxxAs will clear the current surface if loaded or created successfully. - int loadAs(const std::string& filename); - int loadAs(const RWOP& rwop); - int createAs(int width,int height,int depth,int Rmask,int Gmask,int Bmask,int Amask); - int createAs(int width,int height,int depth,Uint32 surfaceFormat); - - int savePNG(const std::string& filename) const; - int getw() const; - int geth() const; - BlendMode getBlendMode() const; - int setBlendMode(BlendMode mode); - - /// Rendering functions. Copy an external surface to this surface. So it has no constant attribute. - int blit(const Surface& s,const Rect& src,const Rect& dst); - int blitTo(const Surface& t, const Rect& dst); - int blitTo(const Surface& t, const Point& lupoint); - int blitFill(const Surface& t, const Rect& src); - int blitFullFill(const Surface& t); - - int blitScaled(const Surface& s,const Rect& src,const Rect& dst); - int blitScaledTo(const Surface& t, const Rect& dst); - int blitScaledTo(const Surface& t, const Point& lupoint); - int blitScaledFill(const Surface& t, const Rect& src); - int blitScaledFullFill(const Surface& t); - - int setClipRect(const Rect& clipRect); - Rect getClipRect() const; - void disableClipping(); - - int setAlphaMode(int alpha); - int getAlphaMode() const; - - ColorMode getColorMode() const; - int setColorMode(ColorMode mode); - RGBA getRGBA() const; - void setRGBA(const RGBA& pack); - - bool mustlock() const; - int lock(); - void unlock(); - - bool isReady() const; - void release(); - - /// Experimental : Get SDL_Surface Pointer and then do anything you want! - /// In case you can do anything with this pointer, this function should not has constant attribute. - SDL_Surface* getRawPointer(); - private: - std::shared_ptr _surf; - void _set(SDL_Surface*); - void _set_no_delete(SDL_Surface*); - void _clear(); - SDL_Surface* _get() const; - - friend class Window; - friend class Renderer; - friend class Font; - friend class Cursor; - }; - class Texture { public: diff --git a/SDLWrapper/Rect.cpp b/SDLWrapper/Rect.cpp new file mode 100644 index 0000000..2785991 --- /dev/null +++ b/SDLWrapper/Rect.cpp @@ -0,0 +1,71 @@ +#include "Rect.h" + +#include "begin_code.h" + +Rect::Rect(int X, int Y, int W, int H) +{ + x = X; + y = Y; + w = W; + h = H; +} + +Rect::Rect(const SDL_Rect& r):Rect(r.x,r.y,r.w,r.h) +{ + +} + +Rect::Rect() +{ + x = y = w = h = 0; +} + +SDL_Rect Rect::toSDLRect() const +{ + SDL_Rect r; + r.x = x; + r.y = y; + r.w = w; + r.h = h; + return r; +} + +bool Rect::isEmpty() +{ + SDL_Rect r=toSDLRect(); + return SDL_RectEmpty(&r)==SDL_TRUE; +} + +bool Rect::operator == (const Rect& r) const +{ + SDL_Rect a=toSDLRect(),b=r.toSDLRect(); + return SDL_RectEquals(&a,&b)==SDL_TRUE; +} + +bool Rect::hasIntersection(const Rect& r) const +{ + SDL_Rect a=toSDLRect(),b=r.toSDLRect(); + return SDL_HasIntersection(&a,&b)==SDL_TRUE; +} + +Rect Rect::getIntersection(const Rect& r) const +{ + SDL_Rect a=toSDLRect(),b=r.toSDLRect(),c; + if(SDL_IntersectRect(&a,&b,&c)==SDL_TRUE) + { + return Rect(c); + } + else + { + return Rect(); + } +} + +Rect Rect::getUnion(const Rect& r) const +{ + SDL_Rect a=toSDLRect(),b=r.toSDLRect(),c; + SDL_UnionRect(&a,&b,&c);//void + return Rect(c); +} + +#include "end_code.h" diff --git a/SDLWrapper/Rect.h b/SDLWrapper/Rect.h new file mode 100644 index 0000000..b1da06c --- /dev/null +++ b/SDLWrapper/Rect.h @@ -0,0 +1,21 @@ +#pragma once +#include "include.h" + +#include "begin_code.h" + +class Rect +{ +public: + int x, y, w, h; + Rect(int X, int Y, int W, int H); + explicit Rect(const SDL_Rect&); + Rect(); + SDL_Rect toSDLRect() const; + bool isEmpty(); + bool operator == (const Rect&) const; + bool hasIntersection(const Rect&) const; + Rect getIntersection(const Rect&) const; + Rect getUnion(const Rect&) const; +}; + +#include "end_code.h" diff --git a/SDLWrapper/Surface.cpp b/SDLWrapper/Surface.cpp new file mode 100644 index 0000000..e3514c2 --- /dev/null +++ b/SDLWrapper/Surface.cpp @@ -0,0 +1,358 @@ +#include "Surface.h" +#include "begin_code.h" +//private +void Surface::_set(SDL_Surface* p) +{ + _surf.reset(p,SDL_FreeSurface); +} + +//private +void Surface::_clear() +{ + _surf.reset(); +} + +//private +SDL_Surface* Surface::_get() const +{ + return _surf.get(); +} + +//private +void Surface::_set_no_delete(SDL_Surface* p) +{ + _surf.reset(p,[](SDL_Surface*) {}); +} + +Surface::Surface(int width,int height,int depth,int Rmask,int Gmask,int Bmask,int Amask) throw(ErrorViewer) +{ + if(createAs(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(createAs(width,height,depth,surfaceFormat)!=0) + { + ErrorViewer e; + e.fetch(); + throw e; + } +} + +Surface::Surface(const std::string& filename) throw(ErrorViewer) +{ + if(loadAs(filename)!=0) + { + ErrorViewer e; + e.fetch(); + throw e; + } +} + +Surface::Surface(const RWOP& rwop) throw (ErrorViewer) +{ + if(loadAs(rwop)!=0) + { + ErrorViewer e; + e.fetch(); + throw e; + } +} + +//static +Surface Surface::load(const std::string& filename) +{ + Surface s; + s.loadAs(filename); + return s; +} + +//static +Surface Surface::load(const RWOP& rwop) +{ + Surface s; + s.loadAs(rwop); + return s; +} + +//static +Surface Surface::create(int width, int height, int depth, int Rmask, int Gmask, int Bmask, int Amask) +{ + Surface s; + s.create(width,height,depth,Rmask,Gmask,Bmask,Amask); + return s; +} + +//static +Surface Surface::create(int width, int height, int depth, Uint32 surfaceFormat) +{ + Surface s; + s.create(width,height,depth,surfaceFormat); + return s; +} + + +int Surface::loadAs(const std::string& filename) +{ + SDL_Surface* temp=IMG_Load(filename.c_str()); + if(temp==nullptr) + { + return -1; + } + else + { + _set(temp); + return 0; + } +} + +int Surface::loadAs(const RWOP& rwop) +{ + SDL_Surface* temp=IMG_Load_RW(rwop._get(),0); + if(temp==nullptr) + { + return -1; + } + else + { + _set(temp); + return 0; + } +} + +int Surface::createAs(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::createAs(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() const +{ + if(_get()!=nullptr) + { + return _get()->w; + } + else + { + /// return -1 on null surface pointer + return -1; + } +} + +int Surface::geth() const +{ + if(_get()!=nullptr) + { + return _get()->h; + } + else + { + /// return -1 on null surface pointer + return -1; + } +} + +BlendMode Surface::getBlendMode() const +{ + SDL_BlendMode temp; + /// FIXME: return value are ignored. + SDL_GetSurfaceBlendMode(_get(),&temp); + return _internal::getBlendModeFromSDLBlendMode(temp); +} + +int Surface::setBlendMode(BlendMode mode) +{ + return SDL_SetSurfaceBlendMode(_get(),_internal::getSDLBlendModeFromBlendMode(mode)); +} + +int Surface::savePNG(const std::string& filename) const +{ + return IMG_SavePNG(_get(),filename.c_str()); +} + +int Surface::blit(const Surface& s,const Rect& src,const Rect& dst) +{ + SDL_Rect rsrc=src.toSDLRect(); + SDL_Rect rdst=dst.toSDLRect(); + return SDL_BlitSurface(s._get(),&rsrc,_get(),&rdst); +} + +int Surface::blitTo(const Surface& s,const Rect& dst) +{ + SDL_Rect rdst=dst.toSDLRect(); + return SDL_BlitSurface(s._get(),NULL,_get(),&rdst); +} + +int Surface::blitTo(const Surface& s,const Point& lupoint) +{ + return blitTo(s,Rect(lupoint.x,lupoint.y,s.getw(),s.geth())); +} + +int Surface::blitFill(const Surface& s,const Rect& src) +{ + SDL_Rect rsrc=src.toSDLRect(); + return SDL_BlitSurface(s._get(),&rsrc,_get(),NULL); +} + +int Surface::blitFullFill(const Surface& s) +{ + return SDL_BlitSurface(s._get(),NULL,_get(),NULL); +} + +int Surface::blitScaled(const Surface& s,const Rect& src,const Rect& dst) +{ + SDL_Rect rsrc=src.toSDLRect(); + SDL_Rect rdst=dst.toSDLRect(); + return SDL_BlitScaled(s._get(),&rsrc,_get(),&rdst); +} + +int Surface::blitScaledTo(const Surface& s,const Rect& dst) +{ + SDL_Rect rdst=dst.toSDLRect(); + return SDL_BlitScaled(s._get(),NULL,_get(),&rdst); +} + +int Surface::blitScaledTo(const Surface& s,const Point& lupoint) +{ + return blitScaledTo(s,Rect(lupoint.x,lupoint.y,s.getw(),s.geth())); +} + +int Surface::blitScaledFill(const Surface& s,const Rect& src) +{ + SDL_Rect rsrc=src.toSDLRect(); + return SDL_BlitScaled(s._get(),&rsrc,_get(),NULL); +} + +int Surface::blitScaledFullFill(const Surface& s) +{ + return SDL_BlitScaled(s._get(),NULL,_get(),NULL); +} + +int Surface::setClipRect(const Rect& clipRect) +{ + auto m=clipRect.toSDLRect(); + return (SDL_SetClipRect(_get(),&m) == SDL_TRUE) ? 0 : -1; +} + +Rect Surface::getClipRect() const +{ + SDL_Rect rect; + SDL_GetClipRect(_get(),&rect); + return Rect(rect); +} + +void Surface::disableClipping() +{ + SDL_SetClipRect(_get(),NULL); +} + +int Surface::setAlphaMode(int alpha) +{ + return SDL_SetSurfaceAlphaMod(_get(),alpha); +} + +int Surface::getAlphaMode() const +{ + Uint8 al; + SDL_GetSurfaceAlphaMod(_get(),&al); + return al; +} + +int Surface::setColorMode(ColorMode mode) +{ + return SDL_SetSurfaceColorMod(_get(),mode.r,mode.g,mode.b); +} + +ColorMode Surface::getColorMode() const +{ + Uint8 r,g,b; + SDL_GetSurfaceColorMod(_get(),&r,&g,&b); + ColorMode mode; + mode.r=r; + mode.g=g; + mode.b=b; + return mode; +} + +void Surface::setRGBA(const RGBA& pack) +{ + setColorMode(pack.toColorMode()); + setAlphaMode(pack.a); +} + +RGBA Surface::getRGBA() const +{ + return RGBA(getColorMode(),getAlphaMode()); +} + +bool Surface::mustlock() const +{ + return SDL_MUSTLOCK(_get()); +} + +int Surface::lock() +{ + return SDL_LockSurface(_get()); +} + +void Surface::unlock() +{ + SDL_UnlockSurface(_get()); +} + +bool Surface::isReady() const +{ + return _get()!=nullptr; +} + +void Surface::release() +{ + _clear(); +} + +/// Experimental (Not constant) +SDL_Surface* Surface::getRawPointer() +{ + return _get(); +} +#include "end_code.h" diff --git a/SDLWrapper/Surface.h b/SDLWrapper/Surface.h new file mode 100644 index 0000000..90d5222 --- /dev/null +++ b/SDLWrapper/Surface.h @@ -0,0 +1,83 @@ +#pragma once +#include "include.h" +#include "begin_code.h" + + +class Surface +{ +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; + + /// static functions + static Surface load(const std::string& filename); + static Surface load(const RWOP& rwop); + static Surface create(int width,int height,int depth,int Rmask,int Gmask,int Bmask,int Amask); + static Surface create(int width,int height,int depth,Uint32 surfaceFormat); + + /// xxxAs will clear the current surface if loaded or created successfully. + int loadAs(const std::string& filename); + int loadAs(const RWOP& rwop); + int createAs(int width,int height,int depth,int Rmask,int Gmask,int Bmask,int Amask); + int createAs(int width,int height,int depth,Uint32 surfaceFormat); + + int savePNG(const std::string& filename) const; + int getw() const; + int geth() const; + BlendMode getBlendMode() const; + int setBlendMode(BlendMode mode); + + /// Rendering functions. Copy an external surface to this surface. So it has no constant attribute. + int blit(const Surface& s,const Rect& src,const Rect& dst); + int blitTo(const Surface& t, const Rect& dst); + int blitTo(const Surface& t, const Point& lupoint); + int blitFill(const Surface& t, const Rect& src); + int blitFullFill(const Surface& t); + + int blitScaled(const Surface& s,const Rect& src,const Rect& dst); + int blitScaledTo(const Surface& t, const Rect& dst); + int blitScaledTo(const Surface& t, const Point& lupoint); + int blitScaledFill(const Surface& t, const Rect& src); + int blitScaledFullFill(const Surface& t); + + int setClipRect(const Rect& clipRect); + Rect getClipRect() const; + void disableClipping(); + + int setAlphaMode(int alpha); + int getAlphaMode() const; + + ColorMode getColorMode() const; + int setColorMode(ColorMode mode); + RGBA getRGBA() const; + void setRGBA(const RGBA& pack); + + bool mustlock() const; + int lock(); + void unlock(); + + bool isReady() const; + void release(); + + /// Experimental : Get SDL_Surface Pointer and then do anything you want! + /// In case you can do anything with this pointer, this function should not has constant attribute. + SDL_Surface* getRawPointer(); +private: + std::shared_ptr _surf; + void _set(SDL_Surface*); + void _set_no_delete(SDL_Surface*); + void _clear(); + SDL_Surface* _get() const; + + friend class Window; + friend class Renderer; + friend class Font; + friend class Cursor; +}; + +#include "end_code.h" diff --git a/SDLWrapper/begin_code.h b/SDLWrapper/begin_code.h new file mode 100644 index 0000000..349de4e --- /dev/null +++ b/SDLWrapper/begin_code.h @@ -0,0 +1,5 @@ +namespace MiniEngine +{ + +inline namespace _SDLWrapper +{ diff --git a/SDLWrapper/end_code.h b/SDLWrapper/end_code.h new file mode 100644 index 0000000..1619da3 --- /dev/null +++ b/SDLWrapper/end_code.h @@ -0,0 +1,3 @@ +} /// End of namespace MiniEngine::_SDLWrapper + +} /// End of namespace MiniEngine diff --git a/SDLWrapper/include.h b/SDLWrapper/include.h new file mode 100644 index 0000000..22c740b --- /dev/null +++ b/SDLWrapper/include.h @@ -0,0 +1,8 @@ +#pragma once + +/// Include SDL Library Headers. +#include +#undef main +#include +#include +#include