From 085463c8732951078299c4b46c4c96bf60b70a7e Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Wed, 12 Apr 2017 09:22:15 +0800 Subject: [PATCH] Add BlendMode caster --- MiniEngine.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++++++---- MiniEngine.h | 20 +++++------------ 2 files changed, 59 insertions(+), 19 deletions(-) diff --git a/MiniEngine.cpp b/MiniEngine.cpp index 85b3d4e..739cdb3 100644 --- a/MiniEngine.cpp +++ b/MiniEngine.cpp @@ -17,6 +17,41 @@ namespace MiniEngine { + namespace _internal + { + BlendMode getBlendModeFromSDLBlendMode(SDL_BlendMode mode) + { + switch(mode) + { + case SDL_BLENDMODE_ADD: + return BlendMode::Add; + case SDL_BLENDMODE_BLEND: + return BlendMode::Blend; + case SDL_BLENDMODE_MOD: + return BlendMode::Mod; + case SDL_BLENDMODE_NONE: + default:/// return BlendMode::None on default. + return BlendMode::None; + } + } + + SDL_BlendMode getSDLBlendModeFromBlendMode(BlendMode mode) + { + switch(mode) + { + case BlendMode::Add: + return SDL_BLENDMODE_ADD; + case BlendMode::Blend: + return SDL_BLENDMODE_BLEND; + case BlendMode::Mod: + return SDL_BLENDMODE_MOD; + case BlendMode::None: + default:/// return SDL_BLENDMODE_NONE on default. + return SDL_BLENDMODE_NONE; + } + } + }/// End of namespace _internal + Rect::Rect(int X, int Y, int W, int H) { x = X; @@ -213,6 +248,19 @@ namespace MiniEngine } } + BlendMode Surface::getBlendMode() + { + 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) { return IMG_SavePNG(_get(),filename.c_str()); @@ -262,14 +310,14 @@ namespace MiniEngine int Texture::setBlendMode(BlendMode mode) { - return SDL_SetTextureBlendMode(_get(), static_cast(mode)); + return SDL_SetTextureBlendMode(_get(), _internal::getSDLBlendModeFromBlendMode(mode)); } BlendMode Texture::getBlendMode() { SDL_BlendMode temp; SDL_GetTextureBlendMode(_get(), &temp); - return static_cast(temp); + return _internal::getBlendModeFromSDLBlendMode(temp); } /// Alpha: 0: Transparent 255: opaque @@ -356,16 +404,18 @@ namespace MiniEngine int Renderer::setBlendMode(BlendMode mode) { - return SDL_SetRenderDrawBlendMode(_get(), static_cast(mode)); + return SDL_SetRenderDrawBlendMode(_get(), _internal::getSDLBlendModeFromBlendMode(mode)); } BlendMode Renderer::getBlendMode() { SDL_BlendMode temp; SDL_GetRenderDrawBlendMode(_get(), &temp); - return static_cast(temp); + return _internal::getBlendModeFromSDLBlendMode(temp); } + + int Renderer::setTarget(Texture & t) { return SDL_SetRenderTarget(_get(), t._get()); diff --git a/MiniEngine.h b/MiniEngine.h index 65e5c88..4c2ed00 100644 --- a/MiniEngine.h +++ b/MiniEngine.h @@ -89,13 +89,7 @@ namespace MiniEngine friend class Renderer; }; - enum class BlendMode - { - None = SDL_BLENDMODE_NONE, - Blend = SDL_BLENDMODE_BLEND, - Add = SDL_BLENDMODE_ADD, - Mod = SDL_BLENDMODE_MOD - }; + enum class BlendMode { None,Blend,Add,Mod }; class Surface { @@ -104,6 +98,8 @@ namespace MiniEngine int savePNG(const std::string& filename); int getw(); int geth(); + BlendMode getBlendMode(); + int setBlendMode(BlendMode mode); protected: Surface() = default; private: @@ -150,13 +146,7 @@ namespace MiniEngine friend class Renderer; }; - enum class RendererType - { - Software, - Accelerated, - PresentSync, - TargetTexture - }; + enum class RendererType { Software, Accelerated, PresentSync, TargetTexture }; enum class FlipMode { None, Horizontal, Vertical }; @@ -204,7 +194,7 @@ namespace MiniEngine friend class Window; }; - enum class MessageBoxType { Error,Warning,Information }; + enum class MessageBoxType { Error, Warning, Information }; class Window {