diff --git a/MiniEngine.cpp b/MiniEngine.cpp index 0dc49e1..a9b235a 100644 --- a/MiniEngine.cpp +++ b/MiniEngine.cpp @@ -129,6 +129,32 @@ namespace MiniEngine return str.c_str(); } + RWOP::RWOP(FILE* fp,bool autoclose) + { + SDL_bool b=autoclose?SDL_TRUE:SDL_FALSE; + op.reset(SDL_RWFromFP(fp,b),[](SDL_RWops* p){SDL_RWclose(p);}); + } + + RWOP::RWOP(const std::string& filename,const std::string& openmode) + { + op.reset(SDL_RWFromFile(filename.c_str(),openmode.c_str()),[](SDL_RWops* p){SDL_RWclose(p);}); + } + + RWOP::RWOP(const void* mem,int size) + { + op.reset(SDL_RWFromConstMem(mem,size),[](SDL_RWops* p){SDL_RWclose(p);}); + } + + RWOP::RWOP(void* mem,int size) + { + op.reset(SDL_RWFromMem(mem,size),[](SDL_RWops* p){SDL_RWclose(p);}); + } + + int Surface::savePNG(const std::string& filename) + { + return IMG_SavePNG(surf.get(),filename.c_str()); + } + Texture::Texture() { updateInfo(); @@ -435,10 +461,10 @@ namespace MiniEngine void Window::setRenderer(std::initializer_list RendererFlags) { - int flag = 0; + Uint32 flag = 0; for (auto v : RendererFlags) { - flag |= static_cast(v); + flag |= _render_caster(v); } _setRenderer_Real(flag); } @@ -533,6 +559,24 @@ namespace MiniEngine return s; } + Uint32 Window::_render_caster(RendererType Type) + { + switch(Type) + { + case RendererType::Accelerated: + return SDL_RENDERER_ACCELERATED; + case RendererType::PresentSync: + return SDL_RENDERER_PRESENTVSYNC; + case RendererType::Software: + return SDL_RENDERER_SOFTWARE; + case RendererType::TargetTexture: + return SDL_RENDERER_TARGETTEXTURE; + } + + /// If an error occurs, return 0 by default. + return 0; + } + void Window::_setRenderer_Real(Uint32 flags) { winrnd.rnd.reset(SDL_CreateRenderer(wnd.get(), -1, flags), SDL_DestroyRenderer); @@ -579,6 +623,31 @@ namespace MiniEngine return (font.get() != nullptr); } + void Font::_real_setFontStyle(int Style) + { + TTF_SetFontStyle(font.get(),Style); + } + + int Font::_style_caster(Style style) + { + switch(style) + { + case Style::Bold: + return TTF_STYLE_BOLD; + case Style::Italic: + return TTF_STYLE_ITALIC; + case Style::Normal: + return TTF_STYLE_NORMAL; + case Style::StrikeThrough: + return TTF_STYLE_STRIKETHROUGH; + case Style::UnderLine: + return TTF_STYLE_UNDERLINE; + } + + /// If an error occurs, return 0 instead of -1. + return 0; + } + Texture Font::renderText(Renderer rnd, std::string Text, RGBA fg) { Surface surf; diff --git a/MiniEngine.h b/MiniEngine.h index c1864f7..96db246 100644 --- a/MiniEngine.h +++ b/MiniEngine.h @@ -72,6 +72,19 @@ namespace MiniEngine std::string str; }; + class RWOP + { + public: + RWOP(FILE* fp,bool autoclose); + RWOP(const std::string& filename,const std::string& openmode); + RWOP(const void* mem,int size); + RWOP(void* mem,int size); + RWOP()=default; + ~RWOP()=default; + private: + std::shared_ptr op; + }; + enum class BlendMode { None = SDL_BLENDMODE_NONE, @@ -84,6 +97,7 @@ namespace MiniEngine { public: ~Surface() = default; + int savePNG(const std::string& filename); protected: Surface() = default; private: @@ -124,10 +138,10 @@ namespace MiniEngine enum class RendererType { - Software = SDL_RENDERER_SOFTWARE, - Accelerated = SDL_RENDERER_ACCELERATED, - PresentSync = SDL_RENDERER_PRESENTVSYNC, - TargetTexture = SDL_RENDERER_TARGETTEXTURE + Software, + Accelerated, + PresentSync, + TargetTexture }; enum class FlipMode { None, Horizontal, Vertical }; @@ -135,6 +149,7 @@ namespace MiniEngine class Renderer { public: + Renderer() = default; int setColor(RGBA pack); RGBA getColor(); int setBlendMode(BlendMode mode); @@ -163,7 +178,6 @@ namespace MiniEngine Texture loadTexture(std::string FileName) throw(ErrorViewer); Texture createTexture(int Width, int Height) throw(ErrorViewer); - Renderer() = default; bool isReady(); private: std::shared_ptr rnd; @@ -178,7 +192,20 @@ namespace MiniEngine Window(std::string Title, int Width, int Height, std::initializer_list RendererFlags = { RendererType::Accelerated,RendererType::TargetTexture }) throw(ErrorViewer); Renderer getRenderer() const; - void setRenderer(std::initializer_list RendererFlags); + void setRenderer(RendererType Type) + { + _internal_rndflagcalc=0; + _setRenderer(Type); + } + + template + void setRenderer(RendererType Type,Args&&... args) + { + _internal_rndflagcalc=0; + _setRenderer(Type,std::forward(args...)); + } + + void setRenderer(std::initializer_list); Rect getSize(); void setSize(Rect sizeRect); @@ -207,8 +234,23 @@ namespace MiniEngine _DECL_DEPRECATED Surface getSurface(); + protected: + template + void _setRenderer(RendererType Type,Args&&... args) + { + _internal_rndflagcalc|=_render_caster(Type); + _setRenderer(args...); + } + + void _setRenderer(RendererType Type) + { + _internal_rndflagcalc|=_render_caster(Type); + _setRenderer_Real(_internal_rndflagcalc); + } private: void _setRenderer_Real(Uint32 flags); + Uint32 _internal_rndflagcalc; + Uint32 _render_caster(RendererType); std::shared_ptr wnd; Renderer winrnd; }; @@ -216,10 +258,27 @@ namespace MiniEngine class Font { public: + enum class Style { Normal, Bold, Italic, UnderLine, StrikeThrough }; + Font() = default; Font(std::string FontFileName, int size) throw(ErrorViewer); int use(std::string FontFileName, int size); bool isReady(); + + template + void setFontStyle(Style style,Args&&... args) + { + _internal_fontcalc=0; + _setFontStyle(style,std::forward(args...)); + } + + void setFontStyle(Style style) + { + _real_setFontStyle(_style_caster(style)); + } + + std::tuple