From 9ecfeb196260a8775106e1a37aff556f4b19d333 Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Sun, 18 Jun 2017 20:37:45 +0800 Subject: [PATCH] Move almost all classes out --- MiniEngine.cpp | 856 ----------------------------------- MiniEngine.h | 216 +-------- SDLWrapper/ColorMode.cpp | 14 + SDLWrapper/Cursor.cpp | 1 + SDLWrapper/Cursor.h | 3 + SDLWrapper/ErrorViewer.cpp | 18 + SDLWrapper/ErrorViewer.h | 14 + SDLWrapper/Font.cpp | 10 +- SDLWrapper/Font.h | 11 +- SDLWrapper/IncludeAll.h | 11 + SDLWrapper/Log.cpp | 50 ++ SDLWrapper/Log.h | 13 + SDLWrapper/MessageBox.cpp | 66 +++ SDLWrapper/MessageBox.h | 45 ++ SDLWrapper/Music.cpp | 2 +- SDLWrapper/Music.h | 5 +- SDLWrapper/Noncopyable.h | 9 + SDLWrapper/Point.h | 1 + SDLWrapper/RGBA.h | 1 + SDLWrapper/RWOP.cpp | 48 ++ SDLWrapper/RWOP.h | 26 ++ SDLWrapper/Rect.cpp | 3 +- SDLWrapper/Rect.h | 2 +- SDLWrapper/Renderer.cpp | 35 ++ SDLWrapper/Renderer.h | 6 + SDLWrapper/SDLSystem.cpp | 243 ++++++++++ SDLWrapper/SDLSystem.h | 65 +++ SDLWrapper/SharedLibrary.cpp | 67 +++ SDLWrapper/SharedLibrary.h | 29 ++ SDLWrapper/Surface.cpp | 1 + SDLWrapper/Surface.h | 7 +- SDLWrapper/Texture.cpp | 13 +- SDLWrapper/Texture.h | 7 +- SDLWrapper/Timer.h | 5 +- SDLWrapper/Window.cpp | 92 ++++ SDLWrapper/Window.h | 7 +- SDLWrapper/begin_code.h | 3 - SDLWrapper/end_code.h | 2 - SDLWrapper/include.h | 6 + 39 files changed, 914 insertions(+), 1099 deletions(-) create mode 100644 SDLWrapper/ColorMode.cpp create mode 100644 SDLWrapper/ErrorViewer.cpp create mode 100644 SDLWrapper/ErrorViewer.h create mode 100644 SDLWrapper/IncludeAll.h create mode 100644 SDLWrapper/Log.cpp create mode 100644 SDLWrapper/Log.h create mode 100644 SDLWrapper/MessageBox.cpp create mode 100644 SDLWrapper/MessageBox.h create mode 100644 SDLWrapper/Noncopyable.h create mode 100644 SDLWrapper/RWOP.cpp create mode 100644 SDLWrapper/RWOP.h create mode 100644 SDLWrapper/SDLSystem.cpp create mode 100644 SDLWrapper/SDLSystem.h create mode 100644 SDLWrapper/SharedLibrary.cpp create mode 100644 SDLWrapper/SharedLibrary.h diff --git a/MiniEngine.cpp b/MiniEngine.cpp index 7b03851..687a572 100644 --- a/MiniEngine.cpp +++ b/MiniEngine.cpp @@ -17,862 +17,6 @@ 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; - } - } - - /// FIXME: return SDL_WindowFlags or Uint32 ? - Uint32 getSDLWindowFlagsFromWindowType(WindowType type) - { - switch(type) - { - case WindowType::FullScreen: - return SDL_WINDOW_FULLSCREEN; - case WindowType::OpenGL: - return SDL_WINDOW_OPENGL; - case WindowType::Shown: - return SDL_WINDOW_SHOWN; - case WindowType::Hidden: - return SDL_WINDOW_HIDDEN; - case WindowType::Borderless: - return SDL_WINDOW_BORDERLESS; - case WindowType::Resizable: - return SDL_WINDOW_RESIZABLE; - case WindowType::Minimized: - return SDL_WINDOW_MINIMIZED; - case WindowType::Maximized: - return SDL_WINDOW_MAXIMIZED; - case WindowType::InputGrabbed: - return SDL_WINDOW_INPUT_GRABBED; - case WindowType::InputFocus: - return SDL_WINDOW_INPUT_FOCUS; - case WindowType::MouseFocus: - return SDL_WINDOW_MOUSE_FOCUS; - case WindowType::FullScreenDesktop: - return SDL_WINDOW_FULLSCREEN_DESKTOP; - case WindowType::Foreign: - return SDL_WINDOW_FOREIGN; - case WindowType::AllowHighDPI: - return SDL_WINDOW_ALLOW_HIGHDPI; - case WindowType::MouseCapture: - return SDL_WINDOW_MOUSE_CAPTURE; - - #if _MINIENGINE_SDL_VERSION_ATLEAST(2,0,5) /// SDL 2.0.5 Required - case WindowType::AlwaysOnTop: - return SDL_WINDOW_ALWAYS_ON_TOP; - case WindowType::SkipTaskBar: - return SDL_WINDOW_SKIP_TASKBAR; - case WindowType::Utility: - return SDL_WINDOW_UTILITY; - case WindowType::ToolTip: - return SDL_WINDOW_TOOLTIP; - case WindowType::PopUpMenu: - return SDL_WINDOW_POPUP_MENU; - #endif // End of SDL2.0.5 Require - - default: - return 0;/// Return 0 on default. - } - } - - SystemCursorType getCursorTypeFromSDLSystemCursor(SDL_SystemCursor id) - { - switch(id) - { - case SDL_SYSTEM_CURSOR_ARROW: - return SystemCursorType::Arrow; - case SDL_SYSTEM_CURSOR_CROSSHAIR: - return SystemCursorType::CrossHair; - case SDL_SYSTEM_CURSOR_HAND: - return SystemCursorType::Hand; - case SDL_SYSTEM_CURSOR_IBEAM: - return SystemCursorType::Ibeam; - case SDL_SYSTEM_CURSOR_NO: - return SystemCursorType::No; - case SDL_SYSTEM_CURSOR_SIZEALL: - return SystemCursorType::SizeAll; - case SDL_SYSTEM_CURSOR_SIZENESW: - return SystemCursorType::SizeNESW; - case SDL_SYSTEM_CURSOR_SIZENS: - return SystemCursorType::SizeNS; - case SDL_SYSTEM_CURSOR_SIZENWSE: - return SystemCursorType::SizeNWSE; - case SDL_SYSTEM_CURSOR_SIZEWE: - return SystemCursorType::SizeWE; - case SDL_SYSTEM_CURSOR_WAIT: - return SystemCursorType::Wait; - case SDL_SYSTEM_CURSOR_WAITARROW: - return SystemCursorType::WaitArrow; - default:/// return SystemCursorType::Arrow on default. - return SystemCursorType::Arrow; - } - } - - SDL_SystemCursor getSDLSystemCursorFromSystemCursorType(SystemCursorType type) - { - switch(type) - { - case SystemCursorType::Arrow: - return SDL_SYSTEM_CURSOR_ARROW; - case SystemCursorType::CrossHair: - return SDL_SYSTEM_CURSOR_CROSSHAIR; - case SystemCursorType::Hand: - return SDL_SYSTEM_CURSOR_HAND; - case SystemCursorType::Ibeam: - return SDL_SYSTEM_CURSOR_IBEAM; - case SystemCursorType::No: - return SDL_SYSTEM_CURSOR_NO; - case SystemCursorType::SizeAll: - return SDL_SYSTEM_CURSOR_SIZEALL; - case SystemCursorType::SizeNESW: - return SDL_SYSTEM_CURSOR_SIZENESW; - case SystemCursorType::SizeNS: - return SDL_SYSTEM_CURSOR_SIZENS; - case SystemCursorType::SizeNWSE: - return SDL_SYSTEM_CURSOR_SIZENWSE; - case SystemCursorType::SizeWE: - return SDL_SYSTEM_CURSOR_SIZEWE; - case SystemCursorType::Wait: - return SDL_SYSTEM_CURSOR_WAIT; - case SystemCursorType::WaitArrow: - return SDL_SYSTEM_CURSOR_WAITARROW; - default:/// return SDL_SYSTEM_CURSOR_ARROW on default. - return SDL_SYSTEM_CURSOR_ARROW; - } - } - - int getTTFFontStyleFromFontStyle(FontStyle style) - { - switch(style) - { - case FontStyle::Bold: - return TTF_STYLE_BOLD; - case FontStyle::Italic: - return TTF_STYLE_ITALIC; - case FontStyle::Normal: - return TTF_STYLE_NORMAL; - case FontStyle::StrikeThrough: - return TTF_STYLE_STRIKETHROUGH; - case FontStyle::UnderLine: - return TTF_STYLE_UNDERLINE; - default: - return TTF_STYLE_NORMAL; - } - } - - std::vector getFontStyleVecFromMixedTTFFontStyle(int Mixed_TTF_Font_Style) - { - std::vector vec; - if(Mixed_TTF_Font_Style&TTF_STYLE_BOLD) - { - vec.push_back(FontStyle::Bold); - } - if(Mixed_TTF_Font_Style&TTF_STYLE_ITALIC) - { - vec.push_back(FontStyle::Italic); - } - if(Mixed_TTF_Font_Style&TTF_STYLE_STRIKETHROUGH) - { - vec.push_back(FontStyle::StrikeThrough); - } - if(Mixed_TTF_Font_Style&TTF_STYLE_UNDERLINE) - { - vec.push_back(FontStyle::UnderLine); - } - if(vec.empty()) - { - vec.push_back(FontStyle::Normal); - } - - return vec; - } - - SDL_RendererFlip getSDLRendererFlipFromFlipMode(FlipMode mode) - { - switch(mode) - { - case FlipMode::None: - return SDL_FLIP_NONE; - case FlipMode::Horizontal: - return SDL_FLIP_HORIZONTAL; - case FlipMode::Vertical: - return SDL_FLIP_VERTICAL; - default: - /// return non-flip mode on default. - return SDL_FLIP_NONE; - } - } - }/// End of namespace _internal - - - - - - ColorMode::ColorMode(int R, int G, int B) - { - r = R; - g = G; - b = B; - } - - ColorMode::ColorMode() - { - r = g = b = 0; - } - - - - void ErrorViewer::fetch() - { - str = SDL_GetError(); - } - - std::string ErrorViewer::getError() const - { - return str; - } - - const char * ErrorViewer::what() const throw() - { - return str.c_str(); - } - - // private - void RWOP::_set(SDL_RWops* p) - { - _op.reset(p,[](SDL_RWops* p){SDL_RWclose(p);}); - } - - // private - SDL_RWops* RWOP::_get() const - { - return _op.get(); - } - - void RWOP::_clear() - { - _op.reset(); - } - - RWOP::RWOP(FILE* fp,bool autoclose) - { - SDL_bool b=autoclose?SDL_TRUE:SDL_FALSE; - _set(SDL_RWFromFP(fp,b)); - } - - RWOP::RWOP(const std::string& filename,const std::string& openmode) - { - _set(SDL_RWFromFile(filename.c_str(),openmode.c_str())); - } - - RWOP::RWOP(const void* mem,int size) - { - _set(SDL_RWFromConstMem(mem,size)); - } - - RWOP::RWOP(void* mem,int size) - { - _set(SDL_RWFromMem(mem,size)); - } - - void RWOP::release() - { - _clear(); - } - - - - - - - - - - WindowMessageBoxButton::WindowMessageBoxButton() - { - _hitoption=0; - text="Button"; - callback=[](){}; - } - - WindowMessageBoxButton::WindowMessageBoxButton(const std::string& ButtonText,const std::function& CallbackFunc) : text(ButtonText) - { - _hitoption=0; - callback=CallbackFunc; - } - - void WindowMessageBoxButton::setHitAsEscape(bool enable) - { - _hitoption=enable?1:0; - } - - void WindowMessageBoxButton::setHitAsReturn(bool enable) - { - _hitoption=enable?2:0; - } - - bool WindowMessageBoxButton::isHitAsEscape() const - { - return _hitoption==1; - } - - bool WindowMessageBoxButton::isHitAsReturn() const - { - return _hitoption==2; - } - - WindowMessageBox::WindowMessageBox() - { - boxtype=MessageBoxType::Information; - } - - WindowMessageBox::WindowMessageBox(const std::string& Title,const std::string& Text,MessageBoxType BoxType,const std::function& DefaultCallback) : title(Title), text(Text) - { - boxtype=BoxType; - defaultcallback=DefaultCallback; - } - - void WindowMessageBox::addButton(const WindowMessageBoxButton& button) - { - _vec.push_back(button); - } - - int WindowMessageBox::getButtonNum() const - { - return _vec.size(); - } - - WindowMessageBoxButton& WindowMessageBox::getButton(int index) - { - return _vec.at(index); - } - - const WindowMessageBoxButton& WindowMessageBox::getButtonConst(int index) const - { - return _vec.at(index); - } - - - - - - - - // private - Uint32 Renderer::_rendertype_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; - } - - // private - int Renderer::_createRenderer_Real(Window& wnd,Uint32 flags) - { - SDL_Renderer* pSDLRnd=SDL_CreateRenderer(wnd._get(), -1, flags); - if(pSDLRnd!=nullptr) - { - _set(pSDLRnd); - return 0; - } - else - { - return -1; - } - } - - int Window::showSimpleMessageBox(MessageBoxType type,const std::string& Title,const std::string& Message) const - { - Uint32 flags=0; - switch(type) - { - case MessageBoxType::Error: - flags=SDL_MESSAGEBOX_ERROR; - break; - case MessageBoxType::Information: - flags=SDL_MESSAGEBOX_INFORMATION; - break; - case MessageBoxType::Warning: - flags=SDL_MESSAGEBOX_WARNING; - break; - } - return SDL_ShowSimpleMessageBox(flags,Title.c_str(),Message.c_str(),_get()); - } - - int Window::showMessageBox(const WindowMessageBox& box) const - { - SDL_MessageBoxData mboxdata; - mboxdata.title=box.title.c_str(); - mboxdata.message=box.text.c_str(); - mboxdata.window=_get(); - mboxdata.colorScheme=nullptr; - mboxdata.numbuttons=box.getButtonNum(); - SDL_MessageBoxButtonData* pButtonArr=(SDL_MessageBoxButtonData*)malloc(sizeof(SDL_MessageBoxButtonData)*(mboxdata.numbuttons)); - if(pButtonArr==nullptr) - { - /// Failed to malloc - return -2; - } - for(int i=0;i=1) - { - /// If any button is clicked, call the callback function associated with it. - if(box.getButtonConst(clickret-1).callback) - { - box.getButtonConst(clickret-1).callback(); - } - } - else - { - /// ... else, call the default callback - if(box.defaultcallback) box.defaultcallback(); - } - } - - /// Free allocated memory - free(pButtonArr); - - return ret; - } - - bool Window::isScreenKeyboardShown() - { - return SDL_IsScreenKeyboardShown(_get())==SDL_TRUE; - } - - - - void LogSystem::d(const char* fmt,...) - { - va_list ap; - va_start(ap,fmt); - SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION,SDL_LOG_PRIORITY_DEBUG,fmt,ap); - va_end(ap); - } - - void LogSystem::v(const char* fmt,...) - { - va_list ap; - va_start(ap,fmt); - SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION,SDL_LOG_PRIORITY_VERBOSE,fmt,ap); - va_end(ap); - } - - void LogSystem::e(const char* fmt,...) - { - va_list ap; - va_start(ap,fmt); - SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION,SDL_LOG_PRIORITY_ERROR,fmt,ap); - va_end(ap); - } - - void LogSystem::i(const char* fmt,...) - { - va_list ap; - va_start(ap,fmt); - SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION,SDL_LOG_PRIORITY_INFO,fmt,ap); - va_end(ap); - } - - void LogSystem::w(const char* fmt,...) - { - va_list ap; - va_start(ap,fmt); - SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION,SDL_LOG_PRIORITY_WARN,fmt,ap); - va_end(ap); - } - - void LogSystem::critical(const char* fmt,...) - { - va_list ap; - va_start(ap,fmt); - SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION,SDL_LOG_PRIORITY_CRITICAL,fmt,ap); - va_end(ap); - } - - //private - void* SharedLibrary::_get() const - { - return _obj.get(); - } - - //private - void SharedLibrary::_set(void* ptr) - { - _obj.reset(ptr,SDL_UnloadObject); - } - - //private - void SharedLibrary::_clear() - { - _obj.reset(); - } - - SharedLibrary::SharedLibrary() - { - _obj=nullptr; - } - - SharedLibrary::SharedLibrary(const std::string& Filename) - { - _obj=nullptr; - load(Filename); - } - - int SharedLibrary::load(const std::string& Filename) - { - if(_get()!=nullptr) return -1; /// Loaded - else - { - void* ptr=SDL_LoadObject(Filename.c_str()); - if(ptr) - { - _set(ptr); - return 0; /// Success - } - else return -2; /// Failed to load - } - } - - int SharedLibrary::unload() - { - if(_get()!=nullptr) - { - _clear(); - return 0; /// Success to unload - } - else return -1; /// Not Loaded. - } - - void* SharedLibrary::get(const std::string& FunctionName) const - { - if(_get()==nullptr) return nullptr; - else return SDL_LoadFunction(_get(),FunctionName.c_str()); - } - - void SharedLibrary::release() - { - _clear(); - } - - //static - int SDLSystem::SDLInit() - { - return SDL_Init(SDL_INIT_EVERYTHING); - } - - //static - void SDLSystem::SDLQuit() - { - SDL_Quit(); - } - - //static - int SDLSystem::IMGInit() - { - return IMG_Init(IMG_INIT_JPG | IMG_INIT_PNG); - } - - //static - void SDLSystem::IMGQuit() - { - IMG_Quit(); - } - - //static - int SDLSystem::TTFInit() - { - return TTF_Init(); - } - - //static - void SDLSystem::TTFQuit() - { - TTF_Quit(); - } - - //static - int SDLSystem::MixInit() - { - return Mix_Init(MIX_INIT_MP3); - } - - //static - void SDLSystem::MixQuit() - { - Mix_Quit(); - } - - //static - void SDLSystem::Init() - { - SDLInit(); - IMGInit(); - TTFInit(); - MixInit(); - } - - //static - void SDLSystem::Quit() - { - MixQuit(); - TTFQuit(); - IMGQuit(); - SDLQuit(); - } - - //static - void SDLSystem::Delay(int ms) - { - SDL_Delay(ms); - } - - //static - PowerState SDLSystem::GetPowerState() - { - SDL_PowerState ret=SDL_GetPowerInfo(NULL,NULL); - switch(ret) - { - case SDL_POWERSTATE_ON_BATTERY: - return PowerState::OnBattery; - case SDL_POWERSTATE_NO_BATTERY: - return PowerState::NoBattery; - case SDL_POWERSTATE_CHARGING: - return PowerState::Charging; - case SDL_POWERSTATE_CHARGED: - return PowerState::Charged; - - case SDL_POWERSTATE_UNKNOWN: - default: - return PowerState::Unknown; - } - } - - //static - int SDLSystem::GetPowerLifeLeft() - { - int i; - SDL_GetPowerInfo(&i,NULL); - return i; - } - - //static - int SDLSystem::GetPowerPrecentageLeft() - { - int i; - SDL_GetPowerInfo(NULL,&i); - return i; - } - - //static - Platform SDLSystem::GetPlatform() - { - std::string s(SDL_GetPlatform()); - if(s=="Windows") - { - return Platform::Windows; - } - else if(s=="Mac OS X") - { - return Platform::MacOS; - } - else if(s=="Linux") - { - return Platform::Linux; - } - else if(s=="iOS") - { - return Platform::iOS; - } - else if(s=="Android") - { - return Platform::Android; - } - else - { - return Platform::Unknown; - } - } - - //static - void SDLSystem::StartTextInput() - { - SDL_StartTextInput(); - } - - //static - bool SDLSystem::IsTextInputActive() - { - return SDL_IsTextInputActive()==SDL_TRUE; - } - - //static - void SDLSystem::StopTextInput() - { - SDL_StopTextInput(); - } - - //static - bool SDLSystem::HasScreenKeyboardSupport() - { - return SDL_HasScreenKeyboardSupport()==SDL_TRUE; - } - - //static - std::tuple SDLSystem::GetSDLCompileVersion() - { - SDL_version ver; - SDL_VERSION(&ver); - return std::make_tuple(ver.major,ver.minor,ver.patch); - } - - //static - std::tuple SDLSystem::GetSDLLinkedVersion() - { - SDL_version ver; - SDL_GetVersion(&ver); - return std::make_tuple(ver.major,ver.minor,ver.patch); - } - //static - std::tuple SDLSystem::GetIMGCompileVersion() - { - SDL_version ver; - SDL_IMAGE_VERSION(&ver); - return std::make_tuple(ver.major,ver.minor,ver.patch); - } - - //static - std::tuple SDLSystem::GetIMGLinkedVersion() - { - const SDL_version* ptr=IMG_Linked_Version(); - return std::make_tuple(ptr->major,ptr->minor,ptr->patch); - } - - //static - std::tuple SDLSystem::GetMixCompileVersion() - { - SDL_version ver; - SDL_MIXER_VERSION(&ver); - return std::make_tuple(ver.major,ver.minor,ver.patch); - } - - //static - std::tuple SDLSystem::GetMixLinkedVersion() - { - const SDL_version* ptr=Mix_Linked_Version(); - return std::make_tuple(ptr->major,ptr->minor,ptr->patch); - } - - //static - std::tuple SDLSystem::GetTTFCompileVersion() - { - SDL_version ver; - SDL_TTF_VERSION(&ver); - return std::make_tuple(ver.major,ver.minor,ver.patch); - } - - //static - std::tuple SDLSystem::GetTTFLinkedVersion() - { - const SDL_version* ptr=TTF_Linked_Version(); - return std::make_tuple(ptr->major,ptr->minor,ptr->patch); - } - - //static - int SDLSystem::GetCPUCount() - { - return SDL_GetCPUCount(); - } - - //static - int SDLSystem::GetCPUCacheLineSize() - { - return SDL_GetCPUCacheLineSize(); - } - - //static - int SDLSystem::GetSystemRAM() - { - return SDL_GetSystemRAM(); - } - - - - - struct StringEngine::impl { rapidxml::xml_document<> doc; diff --git a/MiniEngine.h b/MiniEngine.h index 6ba721f..c3dcaea 100644 --- a/MiniEngine.h +++ b/MiniEngine.h @@ -1,228 +1,14 @@ #pragma once -#include "MiniEngine_Config.h" #include #include #include -#include +#include "SDLWrapper/IncludeAll.h" -#define _MINIENGINE_SDL_VERSION_ATLEAST(X,Y,Z) SDL_VERSION_ATLEAST(X,Y,Z) namespace MiniEngine { - class NonCopyable - { - protected: - NonCopyable() = default; - ~NonCopyable() = default; - NonCopyable(const NonCopyable&) = delete; - NonCopyable& operator = (const NonCopyable&) = delete; - }; - - class ErrorViewer : public std::exception - { - public: - void fetch(); - std::string getError() const; - const char* what() const throw() override; - private: - 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; - - void release(); - private: - std::shared_ptr _op; - SDL_RWops* _get() const; - void _clear(); - void _set(SDL_RWops*); - friend class Surface; - friend class Renderer; - }; - - enum class BlendMode { None,Blend,Add,Mod }; - - - - enum class SystemCursorType - { - Arrow, Ibeam, CrossHair, - Wait, WaitArrow, - SizeNWSE, SizeNESW, SizeWE, SizeNS, SizeAll, - No, Hand - }; - - - enum class MessageBoxType { Error, Warning, Information }; - - enum class WindowType - { - FullScreen, OpenGL, Shown, Hidden, - Borderless, Resizable, Minimized, Maximized, - InputGrabbed, InputFocus, MouseFocus, - FullScreenDesktop, Foreign, AllowHighDPI, - MouseCapture, AlwaysOnTop, SkipTaskBar, - Utility, ToolTip, PopUpMenu - }; - - class WindowMessageBoxButton - { - public: - WindowMessageBoxButton(); - WindowMessageBoxButton(const std::string& ButtonText,const std::function& CallbackFunc=[](){}); - - std::string text; - std::function callback; - - /// Default: no hit option set. - void setHitAsReturn(bool); - void setHitAsEscape(bool); - - bool isHitAsReturn() const; - bool isHitAsEscape() const; - private: - int _hitoption; - }; - - class WindowMessageBox - { - public: - WindowMessageBox(); - WindowMessageBox(const std::string& Title,const std::string& Text,MessageBoxType BoxType=MessageBoxType::Information,const std::function& DefaultCallback=[](){}); - - MessageBoxType boxtype; - std::string title; - std::string text; - std::function defaultcallback; - - void addButton(const WindowMessageBoxButton& button); - int getButtonNum() const; - WindowMessageBoxButton& getButton(int index); - const WindowMessageBoxButton& getButtonConst(int index) const; - private: - std::vector _vec; - }; - - - - enum class RendererType { Software, Accelerated, PresentSync, TargetTexture }; - - enum class FlipMode { None, Horizontal, Vertical }; - - - - enum class FontStyle { Normal, Bold, Italic, UnderLine, StrikeThrough }; - enum class FontHint { Normal, Light, Mono, None , Error }; - - - enum class Platform { Unknown,Windows,MacOS,Linux,iOS,Android }; - enum class PowerState { Unknown,OnBattery,NoBattery,Charging,Charged }; - - class LogSystem - { - static void v(const char* fmt,...);/// Verbose - static void d(const char* fmt,...);/// Debug - static void i(const char* fmt,...);/// Information - static void w(const char* fmt,...);/// Warning - static void e(const char* fmt,...);/// Error - static void critical(const char* fmt,...);/// Critical - }; - - class SharedLibrary - { - public: - SharedLibrary(); - SharedLibrary(const std::string& Filename); - ~SharedLibrary()=default; - int load(const std::string& Filename); - int unload(); - - template - std::function get(const std::string& FunctionName) - { - return std::function(reinterpret_cast(get(FunctionName))); - } - - void* get(const std::string& FunctionName) const; - void release(); - private: - void* _get() const; - void _set(void*); - void _clear(); - std::shared_ptr _obj; - }; - - class SDLSystem - { - public: - static int SDLInit(); - static void SDLQuit(); - static int IMGInit(); - static void IMGQuit(); - static int TTFInit(); - static void TTFQuit(); - static int MixInit(); - static void MixQuit(); - - static void Init(); - static void Quit(); - - static void Delay(int ms); - - static PowerState GetPowerState(); - static int GetPowerLifeLeft(); - static int GetPowerPrecentageLeft(); - - static Platform GetPlatform(); - - static void StartTextInput(); - static bool IsTextInputActive(); - static void StopTextInput(); - - static bool HasScreenKeyboardSupport(); - - static std::tuple GetSDLCompileVersion(); - static std::tuple GetSDLLinkedVersion(); - - static std::tuple GetIMGCompileVersion(); - static std::tuple GetIMGLinkedVersion(); - - static std::tuple GetMixCompileVersion(); - static std::tuple GetMixLinkedVersion(); - - static std::tuple GetTTFCompileVersion(); - static std::tuple GetTTFLinkedVersion(); - - static int GetCPUCount(); - static int GetCPUCacheLineSize(); - /// RAM is calculated in MB. - static int GetSystemRAM(); - - class Android - { - public: - static std::string GetInternal(); - static bool ExternalAvaliable(); - static bool CanReadExternal(); - static bool CanWriteExternal(); - static std::string GetExternal(); - static void* GetJNIEnv(); - }; - }; - - - - class StringEngine { public: diff --git a/SDLWrapper/ColorMode.cpp b/SDLWrapper/ColorMode.cpp new file mode 100644 index 0000000..c148e2e --- /dev/null +++ b/SDLWrapper/ColorMode.cpp @@ -0,0 +1,14 @@ +#include "ColorMode.h" +#include "begin_code.h" +ColorMode::ColorMode(int R, int G, int B) +{ + r = R; + g = G; + b = B; +} + +ColorMode::ColorMode() +{ + r = g = b = 0; +} +#include "end_code.h" diff --git a/SDLWrapper/Cursor.cpp b/SDLWrapper/Cursor.cpp index 35f402c..30db39f 100644 --- a/SDLWrapper/Cursor.cpp +++ b/SDLWrapper/Cursor.cpp @@ -1,4 +1,5 @@ #include "Cursor.h" +#include "_caster.h" #include "begin_code.h" //private void Cursor::_set(SDL_Cursor* p) diff --git a/SDLWrapper/Cursor.h b/SDLWrapper/Cursor.h index 4016f7c..b1b381d 100644 --- a/SDLWrapper/Cursor.h +++ b/SDLWrapper/Cursor.h @@ -1,5 +1,8 @@ #pragma once #include "include.h" +#include "_SystemCursorType.h" +#include "Point.h" +#include "Surface.h" #include "begin_code.h" class Cursor { diff --git a/SDLWrapper/ErrorViewer.cpp b/SDLWrapper/ErrorViewer.cpp new file mode 100644 index 0000000..5a20006 --- /dev/null +++ b/SDLWrapper/ErrorViewer.cpp @@ -0,0 +1,18 @@ +#include "ErrorViewer.h" +#include "include.h" +#include "begin_code.h" +void ErrorViewer::fetch() +{ + str = SDL_GetError(); +} + +std::string ErrorViewer::getError() const +{ + return str; +} + +const char * ErrorViewer::what() const throw() +{ + return str.c_str(); +} +#include "end_code.h" diff --git a/SDLWrapper/ErrorViewer.h b/SDLWrapper/ErrorViewer.h new file mode 100644 index 0000000..465f6a2 --- /dev/null +++ b/SDLWrapper/ErrorViewer.h @@ -0,0 +1,14 @@ +#pragma once +#include +#include +#include "begin_code.h" +class ErrorViewer : public std::exception +{ +public: + void fetch(); + std::string getError() const; + const char* what() const throw() override; +private: + std::string str; +}; +#include "end_code.h" diff --git a/SDLWrapper/Font.cpp b/SDLWrapper/Font.cpp index 8c96ab4..75afcd6 100644 --- a/SDLWrapper/Font.cpp +++ b/SDLWrapper/Font.cpp @@ -1,21 +1,23 @@ #include "Font.h" +#include "_caster.h" #include "begin_code.h" +// private void Font::_set(TTF_Font* p) { _font.reset(p,TTF_CloseFont); } - +// private void Font::_clear() { _font.reset(); } - +// private TTF_Font* Font::_get() const { return _font.get(); } -Font::Font(std::string FontFileName, size_t size) throw(ErrorViewer) +Font::Font(const std::string& FontFileName, size_t size) throw(ErrorViewer) { if (use(FontFileName, size) != 0) { @@ -25,7 +27,7 @@ Font::Font(std::string FontFileName, size_t size) throw(ErrorViewer) } } -int Font::use(std::string FontFileName, size_t size) +int Font::use(const std::string& FontFileName, size_t size) { TTF_Font* temp = TTF_OpenFont(FontFileName.c_str(), size); if (temp == NULL) return -1; diff --git a/SDLWrapper/Font.h b/SDLWrapper/Font.h index 3a11edd..d2d9a24 100644 --- a/SDLWrapper/Font.h +++ b/SDLWrapper/Font.h @@ -1,12 +1,19 @@ #pragma once #include "include.h" +#include "_FontStyle.h" +#include "_FontHint.h" +#include "Rect.h" +#include "Surface.h" +#include "Texture.h" +#include "Renderer.h" +#include #include "begin_code.h" class Font { public: Font() = default; - Font(std::string FontFileName, size_t size) throw(ErrorViewer); - int use(std::string FontFileName, size_t size); + Font(const std::string& FontFileName, size_t size) throw(ErrorViewer); + int use(const std::string& FontFileName, size_t size); bool isReady() const; bool isNormal() const; diff --git a/SDLWrapper/IncludeAll.h b/SDLWrapper/IncludeAll.h new file mode 100644 index 0000000..094bec2 --- /dev/null +++ b/SDLWrapper/IncludeAll.h @@ -0,0 +1,11 @@ +#pragma once +#include "Rect.h" +#include "Point.h" +#include "Renderer.h" +#include "Texture.h" +#include "Surface.h" +#include "Window.h" +#include "Font.h" +#include "Music.h" +#include "Log.h" +#include "SDLSystem.h" diff --git a/SDLWrapper/Log.cpp b/SDLWrapper/Log.cpp new file mode 100644 index 0000000..75d21c2 --- /dev/null +++ b/SDLWrapper/Log.cpp @@ -0,0 +1,50 @@ +#include "Log.h" +#include "begin_code.h" +void LogSystem::d(const char* fmt,...) +{ + va_list ap; + va_start(ap,fmt); + SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION,SDL_LOG_PRIORITY_DEBUG,fmt,ap); + va_end(ap); +} + +void LogSystem::v(const char* fmt,...) +{ + va_list ap; + va_start(ap,fmt); + SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION,SDL_LOG_PRIORITY_VERBOSE,fmt,ap); + va_end(ap); +} + +void LogSystem::e(const char* fmt,...) +{ + va_list ap; + va_start(ap,fmt); + SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION,SDL_LOG_PRIORITY_ERROR,fmt,ap); + va_end(ap); +} + +void LogSystem::i(const char* fmt,...) +{ + va_list ap; + va_start(ap,fmt); + SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION,SDL_LOG_PRIORITY_INFO,fmt,ap); + va_end(ap); +} + +void LogSystem::w(const char* fmt,...) +{ + va_list ap; + va_start(ap,fmt); + SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION,SDL_LOG_PRIORITY_WARN,fmt,ap); + va_end(ap); +} + +void LogSystem::critical(const char* fmt,...) +{ + va_list ap; + va_start(ap,fmt); + SDL_LogMessageV(SDL_LOG_CATEGORY_APPLICATION,SDL_LOG_PRIORITY_CRITICAL,fmt,ap); + va_end(ap); +} +#include "end_code.h" diff --git a/SDLWrapper/Log.h b/SDLWrapper/Log.h new file mode 100644 index 0000000..c4c06cf --- /dev/null +++ b/SDLWrapper/Log.h @@ -0,0 +1,13 @@ +#pragma once +#include "include.h" +#include "begin_code.h" +class LogSystem +{ + static void v(const char* fmt,...);/// Verbose + static void d(const char* fmt,...);/// Debug + static void i(const char* fmt,...);/// Information + static void w(const char* fmt,...);/// Warning + static void e(const char* fmt,...);/// Error + static void critical(const char* fmt,...);/// Critical +}; +#include "end_code.h" diff --git a/SDLWrapper/MessageBox.cpp b/SDLWrapper/MessageBox.cpp new file mode 100644 index 0000000..2ad609d --- /dev/null +++ b/SDLWrapper/MessageBox.cpp @@ -0,0 +1,66 @@ +#include "MessageBox.h" +#include "begin_code.h" +WindowMessageBoxButton::WindowMessageBoxButton() +{ + _hitoption=0; + text="Button"; + callback=[]() {}; +} + +WindowMessageBoxButton::WindowMessageBoxButton(const std::string& ButtonText,const std::function& CallbackFunc) : text(ButtonText) +{ + _hitoption=0; + callback=CallbackFunc; +} + +void WindowMessageBoxButton::setHitAsEscape(bool enable) +{ + _hitoption=enable?1:0; +} + +void WindowMessageBoxButton::setHitAsReturn(bool enable) +{ + _hitoption=enable?2:0; +} + +bool WindowMessageBoxButton::isHitAsEscape() const +{ + return _hitoption==1; +} + +bool WindowMessageBoxButton::isHitAsReturn() const +{ + return _hitoption==2; +} + +WindowMessageBox::WindowMessageBox() +{ + boxtype=MessageBoxType::Information; +} + +WindowMessageBox::WindowMessageBox(const std::string& Title,const std::string& Text,MessageBoxType BoxType,const std::function& DefaultCallback) : title(Title), text(Text) +{ + boxtype=BoxType; + defaultcallback=DefaultCallback; +} + +void WindowMessageBox::addButton(const WindowMessageBoxButton& button) +{ + _vec.push_back(button); +} + +int WindowMessageBox::getButtonNum() const +{ + return _vec.size(); +} + +WindowMessageBoxButton& WindowMessageBox::getButton(int index) +{ + return _vec.at(index); +} + +const WindowMessageBoxButton& WindowMessageBox::getButtonConst(int index) const +{ + return _vec.at(index); +} +#include "end_code.h" diff --git a/SDLWrapper/MessageBox.h b/SDLWrapper/MessageBox.h new file mode 100644 index 0000000..49a7915 --- /dev/null +++ b/SDLWrapper/MessageBox.h @@ -0,0 +1,45 @@ +#pragma once +#include "include.h" +#include "_MessageBoxType.h" +#include +#include +#include +#include "begin_code.h" +class WindowMessageBoxButton +{ +public: + WindowMessageBoxButton(); + WindowMessageBoxButton(const std::string& ButtonText,const std::function& CallbackFunc=[]() {}); + + std::string text; + std::function callback; + + /// Default: no hit option set. + void setHitAsReturn(bool); + void setHitAsEscape(bool); + + bool isHitAsReturn() const; + bool isHitAsEscape() const; +private: + int _hitoption; +}; + +class WindowMessageBox +{ +public: + WindowMessageBox(); + WindowMessageBox(const std::string& Title,const std::string& Text,MessageBoxType BoxType=MessageBoxType::Information,const std::function& DefaultCallback=[]() {}); + + MessageBoxType boxtype; + std::string title; + std::string text; + std::function defaultcallback; + + void addButton(const WindowMessageBoxButton& button); + int getButtonNum() const; + WindowMessageBoxButton& getButton(int index); + const WindowMessageBoxButton& getButtonConst(int index) const; +private: + std::vector _vec; +}; +#include "end_code.h" diff --git a/SDLWrapper/Music.cpp b/SDLWrapper/Music.cpp index f0f3b71..6a98f8a 100644 --- a/SDLWrapper/Music.cpp +++ b/SDLWrapper/Music.cpp @@ -55,7 +55,7 @@ std::string MusicPlayer::GetDecoderName(int index) return std::string(Mix_GetMusicDecoder(index)); } -Music MusicPlayer::loadMusic(std::string Filename) throw(ErrorViewer) +Music MusicPlayer::loadMusic(const std::string& Filename) throw(ErrorViewer) { Mix_Music* temp = Mix_LoadMUS(Filename.c_str()); if (temp == nullptr) diff --git a/SDLWrapper/Music.h b/SDLWrapper/Music.h index 357d537..96efe4f 100644 --- a/SDLWrapper/Music.h +++ b/SDLWrapper/Music.h @@ -1,5 +1,8 @@ #pragma once #include "include.h" +#include +#include +#include "ErrorViewer.h" #include "begin_code.h" class AudioPlayer { @@ -39,7 +42,7 @@ public: static int GetDecoderNum(); static std::string GetDecoderName(int index); - Music loadMusic(std::string Filename) throw (ErrorViewer); + Music loadMusic(const std::string& Filename) throw (ErrorViewer); int play(Music music, int loops); void pause(); diff --git a/SDLWrapper/Noncopyable.h b/SDLWrapper/Noncopyable.h new file mode 100644 index 0000000..22dd719 --- /dev/null +++ b/SDLWrapper/Noncopyable.h @@ -0,0 +1,9 @@ +#pragma once +class NonCopyable +{ +protected: + NonCopyable() = default; + ~NonCopyable() = default; + NonCopyable(const NonCopyable&) = delete; + NonCopyable& operator = (const NonCopyable&) = delete; +}; diff --git a/SDLWrapper/Point.h b/SDLWrapper/Point.h index 786a2c7..eaa5841 100644 --- a/SDLWrapper/Point.h +++ b/SDLWrapper/Point.h @@ -1,5 +1,6 @@ #pragma once #include "include.h" +#include "Rect.h" #include "begin_code.h" class Point { diff --git a/SDLWrapper/RGBA.h b/SDLWrapper/RGBA.h index ff83c7e..f3bf58a 100644 --- a/SDLWrapper/RGBA.h +++ b/SDLWrapper/RGBA.h @@ -1,5 +1,6 @@ #pragma once #include "include.h" +#include "ColorMode.h" #include "begin_code.h" class RGBA diff --git a/SDLWrapper/RWOP.cpp b/SDLWrapper/RWOP.cpp new file mode 100644 index 0000000..52b9e0b --- /dev/null +++ b/SDLWrapper/RWOP.cpp @@ -0,0 +1,48 @@ +#include "RWOP.h" +#include "begin_code.h" +// private +void RWOP::_set(SDL_RWops* p) +{ + _op.reset(p,[](SDL_RWops* p) + { + SDL_RWclose(p); + }); +} + +// private +SDL_RWops* RWOP::_get() const +{ + return _op.get(); +} + +void RWOP::_clear() +{ + _op.reset(); +} + +RWOP::RWOP(FILE* fp,bool autoclose) +{ + SDL_bool b=autoclose?SDL_TRUE:SDL_FALSE; + _set(SDL_RWFromFP(fp,b)); +} + +RWOP::RWOP(const std::string& filename,const std::string& openmode) +{ + _set(SDL_RWFromFile(filename.c_str(),openmode.c_str())); +} + +RWOP::RWOP(const void* mem,int size) +{ + _set(SDL_RWFromConstMem(mem,size)); +} + +RWOP::RWOP(void* mem,int size) +{ + _set(SDL_RWFromMem(mem,size)); +} + +void RWOP::release() +{ + _clear(); +} +#include "end_code.h" diff --git a/SDLWrapper/RWOP.h b/SDLWrapper/RWOP.h new file mode 100644 index 0000000..0dd8418 --- /dev/null +++ b/SDLWrapper/RWOP.h @@ -0,0 +1,26 @@ +#pragma once +#include "include.h" +#include +#include +#include +#include "begin_code.h" +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; + + void release(); +private: + std::shared_ptr _op; + SDL_RWops* _get() const; + void _clear(); + void _set(SDL_RWops*); + friend class Surface; + friend class Renderer; +}; +#include "end_code.h" diff --git a/SDLWrapper/Rect.cpp b/SDLWrapper/Rect.cpp index 2785991..a835b3c 100644 --- a/SDLWrapper/Rect.cpp +++ b/SDLWrapper/Rect.cpp @@ -10,6 +10,7 @@ Rect::Rect(int X, int Y, int W, int H) h = H; } +// explicit Rect::Rect(const SDL_Rect& r):Rect(r.x,r.y,r.w,r.h) { @@ -30,7 +31,7 @@ SDL_Rect Rect::toSDLRect() const return r; } -bool Rect::isEmpty() +bool Rect::isEmpty() const { SDL_Rect r=toSDLRect(); return SDL_RectEmpty(&r)==SDL_TRUE; diff --git a/SDLWrapper/Rect.h b/SDLWrapper/Rect.h index b1da06c..016d5ef 100644 --- a/SDLWrapper/Rect.h +++ b/SDLWrapper/Rect.h @@ -11,7 +11,7 @@ public: explicit Rect(const SDL_Rect&); Rect(); SDL_Rect toSDLRect() const; - bool isEmpty(); + bool isEmpty() const; bool operator == (const Rect&) const; bool hasIntersection(const Rect&) const; Rect getIntersection(const Rect&) const; diff --git a/SDLWrapper/Renderer.cpp b/SDLWrapper/Renderer.cpp index 5194d00..3c2c9c9 100644 --- a/SDLWrapper/Renderer.cpp +++ b/SDLWrapper/Renderer.cpp @@ -1,4 +1,5 @@ #include "Renderer.h" +#include "_caster.h" #include "begin_code.h" //private @@ -414,4 +415,38 @@ int Renderer::GetDriversNum() return SDL_GetNumRenderDrivers(); } +// private +Uint32 Renderer::_rendertype_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; +} + +// private +int Renderer::_createRenderer_Real(Window& wnd,Uint32 flags) +{ + SDL_Renderer* pSDLRnd=SDL_CreateRenderer(wnd._get(), -1, flags); + if(pSDLRnd!=nullptr) + { + _set(pSDLRnd); + return 0; + } + else + { + return -1; + } +} + #include "end_code.h" diff --git a/SDLWrapper/Renderer.h b/SDLWrapper/Renderer.h index 580af67..93ac6ba 100644 --- a/SDLWrapper/Renderer.h +++ b/SDLWrapper/Renderer.h @@ -1,5 +1,11 @@ #pragma once #include "include.h" +#include "_RendererType.h" +#include "_FlipMode.h" +#include "Window.h" +#include "Surface.h" +#include "Texture.h" +#include #include "begin_code.h" class Renderer { diff --git a/SDLWrapper/SDLSystem.cpp b/SDLWrapper/SDLSystem.cpp new file mode 100644 index 0000000..738cfc6 --- /dev/null +++ b/SDLWrapper/SDLSystem.cpp @@ -0,0 +1,243 @@ +#include "SDLSystem.h" +#include "begin_code.h" +//static +int SDLSystem::SDLInit() +{ + return SDL_Init(SDL_INIT_EVERYTHING); +} + +//static +void SDLSystem::SDLQuit() +{ + SDL_Quit(); +} + +//static +int SDLSystem::IMGInit() +{ + return IMG_Init(IMG_INIT_JPG | IMG_INIT_PNG); +} + +//static +void SDLSystem::IMGQuit() +{ + IMG_Quit(); +} + +//static +int SDLSystem::TTFInit() +{ + return TTF_Init(); +} + +//static +void SDLSystem::TTFQuit() +{ + TTF_Quit(); +} + +//static +int SDLSystem::MixInit() +{ + return Mix_Init(MIX_INIT_MP3); +} + +//static +void SDLSystem::MixQuit() +{ + Mix_Quit(); +} + +//static +void SDLSystem::Init() +{ + SDLInit(); + IMGInit(); + TTFInit(); + MixInit(); +} + +//static +void SDLSystem::Quit() +{ + MixQuit(); + TTFQuit(); + IMGQuit(); + SDLQuit(); +} + +//static +void SDLSystem::Delay(int ms) +{ + SDL_Delay(ms); +} + +//static +PowerState SDLSystem::GetPowerState() +{ + SDL_PowerState ret=SDL_GetPowerInfo(NULL,NULL); + switch(ret) + { + case SDL_POWERSTATE_ON_BATTERY: + return PowerState::OnBattery; + case SDL_POWERSTATE_NO_BATTERY: + return PowerState::NoBattery; + case SDL_POWERSTATE_CHARGING: + return PowerState::Charging; + case SDL_POWERSTATE_CHARGED: + return PowerState::Charged; + + case SDL_POWERSTATE_UNKNOWN: + default: + return PowerState::Unknown; + } +} + +//static +int SDLSystem::GetPowerLifeLeft() +{ + int i; + SDL_GetPowerInfo(&i,NULL); + return i; +} + +//static +int SDLSystem::GetPowerPrecentageLeft() +{ + int i; + SDL_GetPowerInfo(NULL,&i); + return i; +} + +//static +Platform SDLSystem::GetPlatform() +{ + std::string s(SDL_GetPlatform()); + if(s=="Windows") + { + return Platform::Windows; + } + else if(s=="Mac OS X") + { + return Platform::MacOS; + } + else if(s=="Linux") + { + return Platform::Linux; + } + else if(s=="iOS") + { + return Platform::iOS; + } + else if(s=="Android") + { + return Platform::Android; + } + else + { + return Platform::Unknown; + } +} + +//static +void SDLSystem::StartTextInput() +{ + SDL_StartTextInput(); +} + +//static +bool SDLSystem::IsTextInputActive() +{ + return SDL_IsTextInputActive()==SDL_TRUE; +} + +//static +void SDLSystem::StopTextInput() +{ + SDL_StopTextInput(); +} + +//static +bool SDLSystem::HasScreenKeyboardSupport() +{ + return SDL_HasScreenKeyboardSupport()==SDL_TRUE; +} + +//static +std::tuple SDLSystem::GetSDLCompileVersion() +{ + SDL_version ver; + SDL_VERSION(&ver); + return std::make_tuple(ver.major,ver.minor,ver.patch); +} + +//static +std::tuple SDLSystem::GetSDLLinkedVersion() +{ + SDL_version ver; + SDL_GetVersion(&ver); + return std::make_tuple(ver.major,ver.minor,ver.patch); +} +//static +std::tuple SDLSystem::GetIMGCompileVersion() +{ + SDL_version ver; + SDL_IMAGE_VERSION(&ver); + return std::make_tuple(ver.major,ver.minor,ver.patch); +} + +//static +std::tuple SDLSystem::GetIMGLinkedVersion() +{ + const SDL_version* ptr=IMG_Linked_Version(); + return std::make_tuple(ptr->major,ptr->minor,ptr->patch); +} + +//static +std::tuple SDLSystem::GetMixCompileVersion() +{ + SDL_version ver; + SDL_MIXER_VERSION(&ver); + return std::make_tuple(ver.major,ver.minor,ver.patch); +} + +//static +std::tuple SDLSystem::GetMixLinkedVersion() +{ + const SDL_version* ptr=Mix_Linked_Version(); + return std::make_tuple(ptr->major,ptr->minor,ptr->patch); +} + +//static +std::tuple SDLSystem::GetTTFCompileVersion() +{ + SDL_version ver; + SDL_TTF_VERSION(&ver); + return std::make_tuple(ver.major,ver.minor,ver.patch); +} + +//static +std::tuple SDLSystem::GetTTFLinkedVersion() +{ + const SDL_version* ptr=TTF_Linked_Version(); + return std::make_tuple(ptr->major,ptr->minor,ptr->patch); +} + +//static +int SDLSystem::GetCPUCount() +{ + return SDL_GetCPUCount(); +} + +//static +int SDLSystem::GetCPUCacheLineSize() +{ + return SDL_GetCPUCacheLineSize(); +} + +//static +int SDLSystem::GetSystemRAM() +{ + return SDL_GetSystemRAM(); +} +#include "end_code.h" diff --git a/SDLWrapper/SDLSystem.h b/SDLWrapper/SDLSystem.h new file mode 100644 index 0000000..4a5422f --- /dev/null +++ b/SDLWrapper/SDLSystem.h @@ -0,0 +1,65 @@ +#pragma once +#include "include.h" +#include "_PowerState.h" +#include "_Platform.h" +#include +#include +#include "begin_code.h" +class SDLSystem +{ +public: + static int SDLInit(); + static void SDLQuit(); + static int IMGInit(); + static void IMGQuit(); + static int TTFInit(); + static void TTFQuit(); + static int MixInit(); + static void MixQuit(); + + static void Init(); + static void Quit(); + + static void Delay(int ms); + + static PowerState GetPowerState(); + static int GetPowerLifeLeft(); + static int GetPowerPrecentageLeft(); + + static Platform GetPlatform(); + + static void StartTextInput(); + static bool IsTextInputActive(); + static void StopTextInput(); + + static bool HasScreenKeyboardSupport(); + + static std::tuple GetSDLCompileVersion(); + static std::tuple GetSDLLinkedVersion(); + + static std::tuple GetIMGCompileVersion(); + static std::tuple GetIMGLinkedVersion(); + + static std::tuple GetMixCompileVersion(); + static std::tuple GetMixLinkedVersion(); + + static std::tuple GetTTFCompileVersion(); + static std::tuple GetTTFLinkedVersion(); + + static int GetCPUCount(); + static int GetCPUCacheLineSize(); + /// RAM is calculated in MB. + static int GetSystemRAM(); + + class Android + { + public: + static std::string GetInternal(); + static bool ExternalAvaliable(); + static bool CanReadExternal(); + static bool CanWriteExternal(); + static std::string GetExternal(); + static void* GetJNIEnv(); + }; +}; +#include "end_code.h" diff --git a/SDLWrapper/SharedLibrary.cpp b/SDLWrapper/SharedLibrary.cpp new file mode 100644 index 0000000..82bea93 --- /dev/null +++ b/SDLWrapper/SharedLibrary.cpp @@ -0,0 +1,67 @@ +#include "SharedLibrary.h" +#include "begin_code.h" +//private +void* SharedLibrary::_get() const +{ + return _obj.get(); +} + +//private +void SharedLibrary::_set(void* ptr) +{ + _obj.reset(ptr,SDL_UnloadObject); +} + +//private +void SharedLibrary::_clear() +{ + _obj.reset(); +} + +SharedLibrary::SharedLibrary() +{ + _obj=nullptr; +} + +SharedLibrary::SharedLibrary(const std::string& Filename) +{ + _obj=nullptr; + load(Filename); +} + +int SharedLibrary::load(const std::string& Filename) +{ + if(_get()!=nullptr) return -1; /// Loaded + else + { + void* ptr=SDL_LoadObject(Filename.c_str()); + if(ptr) + { + _set(ptr); + return 0; /// Success + } + else return -2; /// Failed to load + } +} + +int SharedLibrary::unload() +{ + if(_get()!=nullptr) + { + _clear(); + return 0; /// Success to unload + } + else return -1; /// Not Loaded. +} + +void* SharedLibrary::get(const std::string& FunctionName) const +{ + if(_get()==nullptr) return nullptr; + else return SDL_LoadFunction(_get(),FunctionName.c_str()); +} + +void SharedLibrary::release() +{ + _clear(); +} +#include "end_code.h" diff --git a/SDLWrapper/SharedLibrary.h b/SDLWrapper/SharedLibrary.h new file mode 100644 index 0000000..8b7b620 --- /dev/null +++ b/SDLWrapper/SharedLibrary.h @@ -0,0 +1,29 @@ +#pragma once +#include "include.h" +#include +#include +#include "begin_code.h" +class SharedLibrary +{ +public: + SharedLibrary(); + SharedLibrary(const std::string& Filename); + ~SharedLibrary()=default; + int load(const std::string& Filename); + int unload(); + + template + std::function get(const std::string& FunctionName) + { + return std::function(reinterpret_cast(get(FunctionName))); + } + + void* get(const std::string& FunctionName) const; + void release(); +private: + void* _get() const; + void _set(void*); + void _clear(); + std::shared_ptr _obj; +}; +#include "end_code.h" diff --git a/SDLWrapper/Surface.cpp b/SDLWrapper/Surface.cpp index e3514c2..50b8e4a 100644 --- a/SDLWrapper/Surface.cpp +++ b/SDLWrapper/Surface.cpp @@ -1,4 +1,5 @@ #include "Surface.h" +#include "_caster.h" #include "begin_code.h" //private void Surface::_set(SDL_Surface* p) diff --git a/SDLWrapper/Surface.h b/SDLWrapper/Surface.h index 90d5222..a83fcf6 100644 --- a/SDLWrapper/Surface.h +++ b/SDLWrapper/Surface.h @@ -1,8 +1,11 @@ #pragma once #include "include.h" +#include "_BlendMode.h" +#include "RGBA.h" +#include "Point.h" +#include "RWOP.h" +#include "ErrorViewer.h" #include "begin_code.h" - - class Surface { public: diff --git a/SDLWrapper/Texture.cpp b/SDLWrapper/Texture.cpp index 2c93331..6ac916e 100644 --- a/SDLWrapper/Texture.cpp +++ b/SDLWrapper/Texture.cpp @@ -1,4 +1,5 @@ #include "Texture.h" +#include "_caster.h" #include "begin_code.h" //private void Texture::_set(SDL_Texture* p) @@ -34,17 +35,17 @@ Texture::Texture() Rect Texture::getSize() { - return rect; + return _rect; } int Texture::getw() const { - return rect.w; + return _rect.w; } int Texture::geth() const { - return rect.h; + return _rect.h; } bool Texture::isReady() const @@ -112,10 +113,10 @@ void Texture::updateInfo() { if(_get()==nullptr) { - rect.x=rect.y=rect.w=rect.h=0; + _rect.x=_rect.y=_rect.w=_rect.h=0; } - SDL_QueryTexture(_get(), NULL, NULL, &rect.w, &rect.h); - rect.x = rect.y = 0; + SDL_QueryTexture(_get(), NULL, NULL, &_rect.w, &_rect.h); + _rect.x = _rect.y = 0; } void Texture::release() diff --git a/SDLWrapper/Texture.h b/SDLWrapper/Texture.h index 0f2963d..b023244 100644 --- a/SDLWrapper/Texture.h +++ b/SDLWrapper/Texture.h @@ -1,5 +1,10 @@ #pragma once #include "include.h" +#include "Rect.h" +#include "RGBA.h" +#include "ColorMode.h" +#include "_BlendMode.h" +#include #include "begin_code.h" class Texture @@ -33,7 +38,7 @@ private: void _set_no_delete(SDL_Texture*); void _clear(); SDL_Texture* _get() const; - Rect rect; + Rect _rect; friend class Renderer; }; diff --git a/SDLWrapper/Timer.h b/SDLWrapper/Timer.h index 09ebbd1..dc93aa8 100644 --- a/SDLWrapper/Timer.h +++ b/SDLWrapper/Timer.h @@ -1,5 +1,6 @@ #pragma once -#include "includes.h" +#include "include.h" +#include #include "begin_code.h" Uint32 _global_timer_executor(Uint32 interval,void* param); @@ -37,9 +38,7 @@ public: static void _delete_delegator(std::function* Delegator); private: - void _real_timer_call(SDL_TimerCallback callback,Uint32 interval,void* param); - SDL_TimerCallback _callback; Uint32 _interval; void* _param; diff --git a/SDLWrapper/Window.cpp b/SDLWrapper/Window.cpp index 89a62b6..d005824 100644 --- a/SDLWrapper/Window.cpp +++ b/SDLWrapper/Window.cpp @@ -1,4 +1,5 @@ #include "Window.h" +#include "_caster.h" #include "begin_code.h" //private void Window::_set(SDL_Window* p) @@ -155,4 +156,95 @@ void Window::release() { _clear(); } + +int Window::showSimpleMessageBox(MessageBoxType type,const std::string& Title,const std::string& Message) const +{ + Uint32 flags=0; + switch(type) + { + case MessageBoxType::Error: + flags=SDL_MESSAGEBOX_ERROR; + break; + case MessageBoxType::Information: + flags=SDL_MESSAGEBOX_INFORMATION; + break; + case MessageBoxType::Warning: + flags=SDL_MESSAGEBOX_WARNING; + break; + } + return SDL_ShowSimpleMessageBox(flags,Title.c_str(),Message.c_str(),_get()); +} + +int Window::showMessageBox(const WindowMessageBox& box) const +{ + SDL_MessageBoxData mboxdata; + mboxdata.title=box.title.c_str(); + mboxdata.message=box.text.c_str(); + mboxdata.window=_get(); + mboxdata.colorScheme=nullptr; + mboxdata.numbuttons=box.getButtonNum(); + SDL_MessageBoxButtonData* pButtonArr=(SDL_MessageBoxButtonData*)malloc(sizeof(SDL_MessageBoxButtonData)*(mboxdata.numbuttons)); + if(pButtonArr==nullptr) + { + /// Failed to malloc + return -2; + } + for(int i=0; i=1) + { + /// If any button is clicked, call the callback function associated with it. + if(box.getButtonConst(clickret-1).callback) + { + box.getButtonConst(clickret-1).callback(); + } + } + else + { + /// ... else, call the default callback + if(box.defaultcallback) box.defaultcallback(); + } + } + + /// Free allocated memory + free(pButtonArr); + + return ret; +} + +bool Window::isScreenKeyboardShown() +{ + return SDL_IsScreenKeyboardShown(_get())==SDL_TRUE; +} #include "end_code.h" diff --git a/SDLWrapper/Window.h b/SDLWrapper/Window.h index c5439ea..4e1a9e9 100644 --- a/SDLWrapper/Window.h +++ b/SDLWrapper/Window.h @@ -1,5 +1,10 @@ #pragma once -#include "includes.h" +#include "include.h" +#include "_WindowType.h" +#include "_MessageBoxType.h" +#include "ErrorViewer.h" +#include "MessageBox.h" +#include "Surface.h" #include "begin_code.h" class Window { diff --git a/SDLWrapper/begin_code.h b/SDLWrapper/begin_code.h index 349de4e..4dcbe64 100644 --- a/SDLWrapper/begin_code.h +++ b/SDLWrapper/begin_code.h @@ -1,5 +1,2 @@ namespace MiniEngine { - -inline namespace _SDLWrapper -{ diff --git a/SDLWrapper/end_code.h b/SDLWrapper/end_code.h index 1619da3..1f542f7 100644 --- a/SDLWrapper/end_code.h +++ b/SDLWrapper/end_code.h @@ -1,3 +1 @@ -} /// End of namespace MiniEngine::_SDLWrapper - } /// End of namespace MiniEngine diff --git a/SDLWrapper/include.h b/SDLWrapper/include.h index 22c740b..80376ce 100644 --- a/SDLWrapper/include.h +++ b/SDLWrapper/include.h @@ -6,3 +6,9 @@ #include #include #include + +/// Version Requiring Definition +#define _MINIENGINE_SDL_VERSION_ATLEAST(X,Y,Z) SDL_VERSION_ATLEAST(X,Y,Z) + +/// Deprecated Definition +#define _DECL_DEPRECATED [[deprecated]]