From 08a858acf00cb26685a056f4931bbc60eac9a90c Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Mon, 22 May 2017 18:27:56 +0800 Subject: [PATCH 01/25] Add Clipboard Support --- MiniEngine.cpp | 25 +++++++++++++++++++++++++ MiniEngine.h | 4 ++++ 2 files changed, 29 insertions(+) diff --git a/MiniEngine.cpp b/MiniEngine.cpp index e2b9b17..3592dce 100644 --- a/MiniEngine.cpp +++ b/MiniEngine.cpp @@ -2074,6 +2074,31 @@ namespace MiniEngine delete pimpl; } + int SetClipboardText(const std::string& str) + { + return SDL_SetClipboardText(str.c_str()); + } + + std::string GetClipboardText() + { + char* pstr=SDL_GetClipboardText(); + if(pstr==nullptr) + { + return std::string(); + } + else + { + std::string s(pstr); + SDL_free(pstr); + return s; + } + } + + bool HasClipboardText() + { + return SDL_HasClipboardText()==SDL_TRUE; + } + }/// End of namespace MiniEngine /// The Following Functions are not avaliable in Visual Studio diff --git a/MiniEngine.h b/MiniEngine.h index 44338c5..6420e6d 100644 --- a/MiniEngine.h +++ b/MiniEngine.h @@ -650,6 +650,10 @@ namespace MiniEngine impl* pimpl; }; + int SetClipboardText(const std::string& str); + std::string GetClipboardText(); + bool HasClipboardText(); + }/// End of namespace MiniEngine std::string UTF8ToGBK(std::string UTF8String); From 14c31689be6a54654bd4e2bb1fbb197a02bf2bc6 Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Mon, 22 May 2017 18:47:28 +0800 Subject: [PATCH 02/25] Add more support about keyboard. Now class Window has default constructor. --- MiniEngine.cpp | 21 +++++++++++++++++++++ MiniEngine.h | 28 ++++++++++++++++++---------- 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/MiniEngine.cpp b/MiniEngine.cpp index 3592dce..6610d61 100644 --- a/MiniEngine.cpp +++ b/MiniEngine.cpp @@ -1243,6 +1243,11 @@ namespace MiniEngine return SDL_ShowSimpleMessageBox(flags,Title.c_str(),Message.c_str(),_get()); } + bool Window::isScreenKeyboardShown() + { + return SDL_IsScreenKeyboardShown(_get())==SDL_TRUE; + } + void Font::_set(TTF_Font* p) { _font.reset(p,TTF_CloseFont); @@ -1728,11 +1733,22 @@ namespace MiniEngine SDL_StartTextInput(); } + bool SDLSystem::IsTextInputActive() + { + return SDL_IsTextInputActive()==SDL_TRUE; + } + void SDLSystem::StopTextInput() { SDL_StopTextInput(); } + //static + bool SDLSystem::HasScreenKeyboardSupport() + { + return SDL_HasScreenKeyboardSupport()==SDL_TRUE; + } + /// Global Executor For class Timer Uint32 _global_timer_executor(Uint32 interval,void* param) { @@ -2099,6 +2115,11 @@ namespace MiniEngine return SDL_HasClipboardText()==SDL_TRUE; } + bool GetScanKeyState(SDL_Scancode code) + { + return SDL_GetKeyboardState(NULL)[code]; + } + }/// End of namespace MiniEngine /// The Following Functions are not avaliable in Visual Studio diff --git a/MiniEngine.h b/MiniEngine.h index 6420e6d..1641f8b 100644 --- a/MiniEngine.h +++ b/MiniEngine.h @@ -273,6 +273,7 @@ namespace MiniEngine class Window { public: + Window()=default; Window(std::string Title, int Width, int Height, std::initializer_list RendererFlags = { RendererType::Accelerated,RendererType::TargetTexture }, std::initializer_list WindowFlags = {WindowType::Shown} , @@ -281,15 +282,15 @@ namespace MiniEngine void setRenderer(RendererType Type) { - _internal_rndflagcalc=0; - _setRenderer(Type); + int flagcalc=0; + _setRenderer(flagcalc,Type); } template void setRenderer(RendererType Type,Args&&... args) { - _internal_rndflagcalc=0; - _setRenderer(Type,std::forward(args...)); + int flagcalc=0; + _setRenderer(flagcalc,Type,std::forward(args...)); } void setRenderer(std::initializer_list); @@ -324,23 +325,24 @@ namespace MiniEngine _DECL_DEPRECATED Surface getSurface(); + bool isScreenKeyboardShown(); + void release(); protected: template - void _setRenderer(RendererType Type,Args&&... args) + void _setRenderer(int& refcalc,RendererType Type,Args&&... args) { - _internal_rndflagcalc|=_render_caster(Type); + refcalc|=_render_caster(Type); _setRenderer(args...); } - void _setRenderer(RendererType Type) + void _setRenderer(int& refcalc,RendererType Type) { - _internal_rndflagcalc|=_render_caster(Type); - _setRenderer_Real(_internal_rndflagcalc); + refcalc|=_render_caster(Type); + _setRenderer_Real(refcalc); } private: void _setRenderer_Real(Uint32 flags); - Uint32 _internal_rndflagcalc; Uint32 _render_caster(RendererType); std::shared_ptr _wnd; @@ -489,8 +491,11 @@ namespace MiniEngine static Platform GetPlatform(); static void StartTextInput(); + static bool IsTextInputActive(); static void StopTextInput(); + bool HasScreenKeyboardSupport(); + class Android { public: @@ -654,6 +659,9 @@ namespace MiniEngine std::string GetClipboardText(); bool HasClipboardText(); + /// Experimental - For Experts: Use SDL ScanCode + bool GetScanKeyState(SDL_Scancode); + }/// End of namespace MiniEngine std::string UTF8ToGBK(std::string UTF8String); From 52cb15482ed6115f2d83204e1073f2ec55e00ee8 Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Mon, 22 May 2017 19:07:26 +0800 Subject: [PATCH 03/25] More Event Functions --- MiniEngine_Event.cpp | 10 ++++++++++ MiniEngine_Event.h | 2 ++ 2 files changed, 12 insertions(+) diff --git a/MiniEngine_Event.cpp b/MiniEngine_Event.cpp index fe2ea5d..d3e8720 100644 --- a/MiniEngine_Event.cpp +++ b/MiniEngine_Event.cpp @@ -35,6 +35,16 @@ bool HasEvent(_SDLEventType_ EventTypeMin,_SDLEventType_ EventTypeMax) return ( SDL_HasEvents(EventTypeMin,EventTypeMax)==SDL_TRUE ); } +_SDLEventType_ RegisterEvent(int howMuch) +{ + return SDL_RegisterEvents(howMuch); +} + +bool IsValidEventType(_SDLEventType_ EventType) +{ + return (EventType > SDL_FIRSTEVENT) && (EventType < SDL_LASTEVENT); +} + bool operator == (const LooperID& a,const LooperID& b) { return a._type_id==b._type_id && a._looper_cnt==b._looper_cnt ; diff --git a/MiniEngine_Event.h b/MiniEngine_Event.h index 0f44f23..b3c5ed4 100644 --- a/MiniEngine_Event.h +++ b/MiniEngine_Event.h @@ -17,6 +17,8 @@ bool HasEvent(_SDLEventType_ EventTypeMin,_SDLEventType_ EventTypeMax); bool EnableEvent(_SDLEventType_ EventType); bool DisableEvent(_SDLEventType_ EventType); bool IsEventEnabled(_SDLEventType_ EventType); +_SDLEventType_ RegisterEvent(int howMuch); +bool IsValidEventType(_SDLEventType_ EventType); typedef struct { From 1b2ba7b2d8e8c316a74ddff03e077bfa943c30d6 Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Mon, 22 May 2017 19:17:02 +0800 Subject: [PATCH 04/25] Update class Cursor. Fix compile bug in class Window --- MiniEngine.cpp | 20 ++++++++++---------- MiniEngine.h | 9 +++++---- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/MiniEngine.cpp b/MiniEngine.cpp index 6610d61..bc3f8a3 100644 --- a/MiniEngine.cpp +++ b/MiniEngine.cpp @@ -983,33 +983,30 @@ namespace MiniEngine return _cur.get(); } + //private void Cursor::_clear() { _cur.reset(); } - //static - Cursor Cursor::CreateCursor(Surface surf,Point hotspot) + Cursor::Cursor(Surface surf,Point hotspot) { Cursor ns; SDL_Cursor* cursor=SDL_CreateColorCursor(surf._get(),hotspot.x,hotspot.y); ns._set(cursor); - return ns; } - //static - Cursor Cursor::CreateSystemCursor(SystemCursorType type) + Cursor::Cursor(SystemCursorType type) { Cursor ns; ns._set(SDL_CreateSystemCursor(_internal::getSDLSystemCursorFromSystemCursorType(type))); - return ns; } //static Cursor Cursor::GetActiveCursor() { Cursor ns; - ns._set(SDL_GetCursor()); + ns._set_no_delete(SDL_GetCursor()); return ns; } @@ -1017,7 +1014,7 @@ namespace MiniEngine Cursor Cursor::GetDefaultCursor() { Cursor ns; - ns._set(SDL_GetDefaultCursor()); + ns._set_no_delete(SDL_GetDefaultCursor()); return ns; } @@ -1028,7 +1025,7 @@ namespace MiniEngine } //static - void Cursor::show(bool Settings) + void Cursor::setShow(bool Settings) { SDL_ShowCursor(Settings?SDL_ENABLE:SDL_DISABLE); } @@ -1040,7 +1037,10 @@ namespace MiniEngine void Cursor::activate() { - SDL_SetCursor(_get()); + if(_get()!=nullptr) + { + SDL_SetCursor(_get()); + } } void Window::_set(SDL_Window* p) diff --git a/MiniEngine.h b/MiniEngine.h index 1641f8b..20de1aa 100644 --- a/MiniEngine.h +++ b/MiniEngine.h @@ -238,13 +238,14 @@ namespace MiniEngine class Cursor { public: - static Cursor CreateSystemCursor(SystemCursorType); - static Cursor CreateCursor(Surface surf,Point hotspot={0,0}); + Cursor()=default; + Cursor(SystemCursorType); + Cursor(Surface surf,Point hotspot={0,0}); static Cursor GetActiveCursor(); static Cursor GetDefaultCursor(); - static void show(bool); + static void setShow(bool); static bool isShow(); void activate(); @@ -333,7 +334,7 @@ namespace MiniEngine void _setRenderer(int& refcalc,RendererType Type,Args&&... args) { refcalc|=_render_caster(Type); - _setRenderer(args...); + _setRenderer(refcalc,args...); } void _setRenderer(int& refcalc,RendererType Type) From 1e8a0d6155b2edf1dc6cc1bdd749325e258933bb Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Tue, 23 May 2017 12:26:36 +0800 Subject: [PATCH 05/25] Add SDLTest lib support --- MiniEngine_Test.cpp | 37 +++++++++++++++++++++++++++++++++++++ MiniEngine_Test.h | 15 +++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 MiniEngine_Test.cpp create mode 100644 MiniEngine_Test.h diff --git a/MiniEngine_Test.cpp b/MiniEngine_Test.cpp new file mode 100644 index 0000000..8a36cb1 --- /dev/null +++ b/MiniEngine_Test.cpp @@ -0,0 +1,37 @@ +#include "MiniEngine_Test.h" +#include +#include + +namespace MiniEngine +{ + +namespace Test +{ + +void GetMD5Raw(unsigned char* buffer,unsigned int bufferLen,unsigned char* outbuff) +{ + SDLTest_Md5Context ct; + SDLTest_Md5Init(&ct); + SDLTest_Md5Update(&ct,buffer,bufferLen); + SDLTest_Md5Final(&ct); + memcpy(outbuff,ct.digest,16); +} + +std::string GetMD5(unsigned char* buffer,unsigned int bufferLen) +{ + unsigned char buff[16]; + char tmp[8]; + GetMD5Raw(buffer,bufferLen,buff); + std::string str; + for(int i=0;i<16;i++) + { + sprintf(tmp,"%02x",buff[i]); + str.append(tmp); + } + return str; +} + + +}/// End of namespace MiniEngine::Test + +}/// End of namespace MiniEngine diff --git a/MiniEngine_Test.h b/MiniEngine_Test.h new file mode 100644 index 0000000..879fa90 --- /dev/null +++ b/MiniEngine_Test.h @@ -0,0 +1,15 @@ +#pragma once +#include + +namespace MiniEngine +{ + +namespace Test +{ + +std::string GetMD5(unsigned char* buffer,unsigned int bufferLen); +void GetMD5Raw(unsigned char* buffer,unsigned int bufferLen,unsigned char* outbuff); + +}/// End of namespace MiniEngine::Test + +}/// End of namespace MiniEngine From 79b97736b443ea865579300168f0111c207f4c6c Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Tue, 23 May 2017 13:05:09 +0800 Subject: [PATCH 06/25] Add more functions to class Surface --- MiniEngine.cpp | 168 ++++++++++++++++++++++++++++++++++++------------- MiniEngine.h | 28 ++++++--- 2 files changed, 144 insertions(+), 52 deletions(-) diff --git a/MiniEngine.cpp b/MiniEngine.cpp index bc3f8a3..37d5156 100644 --- a/MiniEngine.cpp +++ b/MiniEngine.cpp @@ -376,7 +376,7 @@ namespace MiniEngine } // private - SDL_RWops* RWOP::_get() + SDL_RWops* RWOP::_get() const { return _op.get(); } @@ -412,17 +412,20 @@ namespace MiniEngine _clear(); } - void Surface::_set(SDL_Surface* p)//private + //private + void Surface::_set(SDL_Surface* p) { _surf.reset(p,SDL_FreeSurface); } - void Surface::_clear()//private + //private + void Surface::_clear() { _surf.reset(); } - SDL_Surface* Surface::_get()//private + //private + SDL_Surface* Surface::_get() const { return _surf.get(); } @@ -432,6 +435,115 @@ namespace MiniEngine 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() { if(_get()!=nullptr) @@ -588,19 +700,9 @@ namespace MiniEngine SDL_UnlockSurface(_get()); } - //static - Surface Surface::createSurface(int width,int height,int depth,int Rmask,int Gmask,int Bmask,int Amask) throw(ErrorViewer) + bool Surface::isReady() const { - SDL_Surface* temp=SDL_CreateRGBSurface(0,width,height,depth,Rmask,Gmask,Bmask,Amask); - if(temp==nullptr) - { - ErrorViewer e; - e.fetch(); - throw e; - } - Surface surf; - surf._set(temp); - return surf; + return _get()!=nullptr; } void Surface::release() @@ -608,6 +710,12 @@ namespace MiniEngine _clear(); } + /// Experimental + SDL_Surface* Surface::getRawPointer() + { + return _get(); + } + void Texture::_set(SDL_Texture* p)//private { _text.reset(p,SDL_DestroyTexture); @@ -871,34 +979,6 @@ namespace MiniEngine 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 t; diff --git a/MiniEngine.h b/MiniEngine.h index 20de1aa..822015e 100644 --- a/MiniEngine.h +++ b/MiniEngine.h @@ -84,9 +84,10 @@ namespace MiniEngine void release(); private: std::shared_ptr _op; - SDL_RWops* _get(); + SDL_RWops* _get() const; void _clear(); void _set(SDL_RWops*); + friend class Surface; friend class Renderer; }; @@ -95,7 +96,20 @@ namespace MiniEngine 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; + + 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 getw(); int geth(); @@ -126,16 +140,16 @@ namespace MiniEngine int lock(); 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(); - protected: - Surface() = default; + + /// Experimental : Get SDL_Surface Pointer and then do anything you want! + SDL_Surface* getRawPointer(); private: std::shared_ptr _surf; void _set(SDL_Surface*); void _clear(); - SDL_Surface* _get(); + SDL_Surface* _get() const; std::shared_ptr& _getex(); 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); - Surface loadSurface(std::string FileName) throw(ErrorViewer); - Surface loadSurfaceRW(RWOP rwop) throw(ErrorViewer); Texture render(Surface surf) throw (ErrorViewer); Texture loadTexture(std::string FileName) throw(ErrorViewer); Texture loadTextureRW(RWOP rwop) throw(ErrorViewer); From f2d60629a09c64f9ca93355fde89b1bece11eb79 Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Tue, 23 May 2017 22:31:57 +0800 Subject: [PATCH 07/25] Use new UTF8-GBK converter. --- MiniEngine_Windows.cpp | 125 ++++++++++++++--------------------------- 1 file changed, 42 insertions(+), 83 deletions(-) diff --git a/MiniEngine_Windows.cpp b/MiniEngine_Windows.cpp index b07522e..a383a3b 100644 --- a/MiniEngine_Windows.cpp +++ b/MiniEngine_Windows.cpp @@ -3,118 +3,77 @@ using namespace std; #include -//GBK编码转换到UTF8编码 -int _GBKToUTF8(unsigned char * lpGBKStr,unsigned char * lpUTF8Str,int nUTF8StrLen) +void _utf8_to_gb(const char* src, char* dst, int len) { - wchar_t * lpUnicodeStr = NULL; - int nRetLen = 0; - - if(!lpGBKStr) //如果GBK字符串为NULL则出错退出 - return 0; - - nRetLen = MultiByteToWideChar(CP_ACP,0,(char *)lpGBKStr,-1,NULL,0); //获取转换到Unicode编码后所需要的字符空间长度 - lpUnicodeStr = new WCHAR[nRetLen + 1]; //为Unicode字符串空间 - nRetLen = ::MultiByteToWideChar(CP_ACP,0,(char *)lpGBKStr,-1,lpUnicodeStr,nRetLen); //转换到Unicode编码 - if(!nRetLen) //转换失败则出错退出 - return 0; - - nRetLen = ::WideCharToMultiByte(CP_UTF8,0,lpUnicodeStr,-1,NULL,0,NULL,0); //获取转换到UTF8编码后所需要的字符空间长度 - - if(!lpUTF8Str) //输出缓冲区为空则返回转换后需要的空间大小 - { - if(lpUnicodeStr) - delete []lpUnicodeStr; - return nRetLen; + int ret = 0; + WCHAR* strA; + int i= MultiByteToWideChar(CP_UTF8, 0, src, -1, NULL, 0); + if (i <= 0) { + printf("ERROR."); + return; + } + strA = (WCHAR*)malloc(i * 2); + MultiByteToWideChar(CP_UTF8, 0, src, -1, strA, i); + i = WideCharToMultiByte(CP_ACP, 0, strA, -1, NULL, 0, NULL, NULL); + if (len >= i) { + ret = WideCharToMultiByte(CP_ACP, 0, strA, -1, dst, i, NULL, NULL); + dst[i] = 0; + } + if (ret <= 0) { + free(strA); + return; } - if(nUTF8StrLen < nRetLen) //如果输出缓冲区长度不够则退出 - { - if(lpUnicodeStr) - delete []lpUnicodeStr; - return 0; - } - - nRetLen = ::WideCharToMultiByte(CP_UTF8,0,lpUnicodeStr,-1,(char *)lpUTF8Str,nUTF8StrLen,NULL,NULL); //转换到UTF8编码 - - if(lpUnicodeStr) - delete []lpUnicodeStr; - - return nRetLen; + free( strA ); } -// UTF8编码转换到GBK编码 -int _UTF8ToGBK(unsigned char * lpUTF8Str,unsigned char * lpGBKStr,int nGBKStrLen) +void _gb_to_utf8(const char* src, char* dst, int len) { - wchar_t * lpUnicodeStr = NULL; - int nRetLen = 0; - - if(!lpUTF8Str) //如果UTF8字符串为NULL则出错退出 - return 0; - - nRetLen = ::MultiByteToWideChar(CP_UTF8,0,(char *)lpUTF8Str,-1,NULL,0); //获取转换到Unicode编码后所需要的字符空间长度 - lpUnicodeStr = new WCHAR[nRetLen + 1]; //为Unicode字符串空间 - nRetLen = ::MultiByteToWideChar(CP_UTF8,0,(char *)lpUTF8Str,-1,lpUnicodeStr,nRetLen); //转换到Unicode编码 - if(!nRetLen) //转换失败则出错退出 - return 0; - - nRetLen = ::WideCharToMultiByte(CP_ACP,0,lpUnicodeStr,-1,NULL,0,NULL,NULL); //获取转换到GBK编码后所需要的字符空间长度 - - if(!lpGBKStr) //输出缓冲区为空则返回转换后需要的空间大小 - { - if(lpUnicodeStr) - delete []lpUnicodeStr; - return nRetLen; + int ret = 0; + WCHAR* strA; + int i= MultiByteToWideChar(CP_ACP, 0, src, -1, NULL, 0); + if (i <= 0) { + printf("ERROR."); + return; + } + strA = (WCHAR*)malloc(i * 2); + MultiByteToWideChar(CP_ACP, 0, src, -1, strA, i); + i = WideCharToMultiByte(CP_UTF8, 0, strA, -1, NULL, 0, NULL, NULL); + if (len >= i) { + ret = WideCharToMultiByte(CP_UTF8, 0, strA, -1, dst, i, NULL, NULL); + dst[i] = 0; } - if(nGBKStrLen < nRetLen) //如果输出缓冲区长度不够则退出 - { - if(lpUnicodeStr) - delete []lpUnicodeStr; - return 0; + if (ret <= 0) { + free(strA); + return; } - - nRetLen = ::WideCharToMultiByte(CP_ACP,0,lpUnicodeStr,-1,(char *)lpGBKStr,nRetLen,NULL,NULL); //转换到GBK编码 - - if(lpUnicodeStr) - delete []lpUnicodeStr; - - return nRetLen; + free(strA); } - string UTF8ToGBK(string UTF8String) { int sz=UTF8String.size()*2/3+256; - auto utf8str=new unsigned char[sz]; - memset(utf8str,0,sz); - memcpy(utf8str,UTF8String.c_str(),UTF8String.size()); - - auto gbkstr=new unsigned char[sz]; + auto gbkstr=new char[sz]; memset(gbkstr,0,sz); - _UTF8ToGBK(utf8str,gbkstr,sz); + _utf8_to_gb(UTF8String.c_str(),gbkstr,sz); string s((char*)gbkstr); delete[] gbkstr; - delete[] utf8str; return s; } string GBKToUTF8(string GBKString) { int sz=GBKString.size()*3/2+32; - auto gbkstr=new unsigned char[sz]; - memset(gbkstr,0,sz); - memcpy(gbkstr,GBKString.c_str(),GBKString.size()); - - auto utf8str=new unsigned char[sz]; + auto utf8str=new char[sz]; memset(utf8str,0,sz); - _GBKToUTF8(gbkstr,utf8str,sz); + _gb_to_utf8(GBKString.c_str(),utf8str,sz); - string s((char*)utf8str); + string s(utf8str); - delete[] gbkstr; delete[] utf8str; return s; } From 337694bd89b52e94fda358ff8c0f2c19fd896004 Mon Sep 17 00:00:00 2001 From: Kiritow_Bar <1362050620@qq.com> Date: Wed, 24 May 2017 02:57:23 +0800 Subject: [PATCH 08/25] Fix compile error --- MiniEngine_Windows.cpp | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/MiniEngine_Windows.cpp b/MiniEngine_Windows.cpp index a383a3b..d929338 100644 --- a/MiniEngine_Windows.cpp +++ b/MiniEngine_Windows.cpp @@ -3,14 +3,13 @@ using namespace std; #include -void _utf8_to_gb(const char* src, char* dst, int len) +int _utf8_to_gb(const char* src, char* dst, int len) { int ret = 0; WCHAR* strA; int i= MultiByteToWideChar(CP_UTF8, 0, src, -1, NULL, 0); if (i <= 0) { - printf("ERROR."); - return; + return -1; } strA = (WCHAR*)malloc(i * 2); MultiByteToWideChar(CP_UTF8, 0, src, -1, strA, i); @@ -21,20 +20,19 @@ void _utf8_to_gb(const char* src, char* dst, int len) } if (ret <= 0) { free(strA); - return; + return -2; } - free( strA ); + return 0; } -void _gb_to_utf8(const char* src, char* dst, int len) +int _gb_to_utf8(const char* src, char* dst, int len) { int ret = 0; WCHAR* strA; int i= MultiByteToWideChar(CP_ACP, 0, src, -1, NULL, 0); if (i <= 0) { - printf("ERROR."); - return; + return -1; } strA = (WCHAR*)malloc(i * 2); MultiByteToWideChar(CP_ACP, 0, src, -1, strA, i); @@ -46,17 +44,22 @@ void _gb_to_utf8(const char* src, char* dst, int len) if (ret <= 0) { free(strA); - return; + return -2; } free(strA); + return 0; } + string UTF8ToGBK(string UTF8String) { int sz=UTF8String.size()*2/3+256; auto gbkstr=new char[sz]; memset(gbkstr,0,sz); - _utf8_to_gb(UTF8String.c_str(),gbkstr,sz); + if(_utf8_to_gb(UTF8String.c_str(),gbkstr,sz)!=0) + { + return "[MiniEngine] UT8ToGBK Convert Failed"; + } string s((char*)gbkstr); @@ -70,7 +73,10 @@ string GBKToUTF8(string GBKString) auto utf8str=new char[sz]; memset(utf8str,0,sz); - _gb_to_utf8(GBKString.c_str(),utf8str,sz); + if(_gb_to_utf8(GBKString.c_str(),utf8str,sz)!=0) + { + return "[MiniEngine] GBKToUTF8 Convert Failed"; + } string s(utf8str); From 709e67a40fe8dd5ab92c3b6aa86f2414e2ab6d31 Mon Sep 17 00:00:00 2001 From: Kiritow_Bar <1362050620@qq.com> Date: Wed, 24 May 2017 03:09:27 +0800 Subject: [PATCH 09/25] Fix compile error on C4 --- MiniEngine.cpp | 5 +++++ MiniEngine_Android.cpp | 4 ++-- MiniEngine_SQLite.h | 1 + 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/MiniEngine.cpp b/MiniEngine.cpp index 37d5156..4bcc1a1 100644 --- a/MiniEngine.cpp +++ b/MiniEngine.cpp @@ -86,6 +86,9 @@ namespace MiniEngine return SDL_WINDOW_ALLOW_HIGHDPI; case WindowType::MouseCapture: return SDL_WINDOW_MOUSE_CAPTURE; + + /// The following value are not defined on C4. + #ifndef __C4DROID__ case WindowType::AlwaysOnTop: return SDL_WINDOW_ALWAYS_ON_TOP; case WindowType::SkipTaskBar: @@ -96,6 +99,8 @@ namespace MiniEngine return SDL_WINDOW_TOOLTIP; case WindowType::PopUpMenu: return SDL_WINDOW_POPUP_MENU; + #endif // __C4DROID__ + default: return 0;/// Return 0 on default. } diff --git a/MiniEngine_Android.cpp b/MiniEngine_Android.cpp index d45630f..fba67c6 100644 --- a/MiniEngine_Android.cpp +++ b/MiniEngine_Android.cpp @@ -7,12 +7,12 @@ namespace MiniEngine #if defined(__ANDROID__) && __ANDROID__ std::string SDLSystem::Android::GetInternal() { - return string(SDL_AndroidGetInternalStoragePath()); + return std::string(SDL_AndroidGetInternalStoragePath()); } std::string SDLSystem::Android::GetExternal() { - return string(SDL_AndroidGetExternalStoragePath()); + return std::string(SDL_AndroidGetExternalStoragePath()); } bool SDLSystem::Android::CanReadExternal() diff --git a/MiniEngine_SQLite.h b/MiniEngine_SQLite.h index cc3a507..575bf0f 100644 --- a/MiniEngine_SQLite.h +++ b/MiniEngine_SQLite.h @@ -2,6 +2,7 @@ #include "sqlite/sqlite3.h" #include #include +#include namespace MiniEngine { From d8e2bb9e2b01e75ed4a00473cc99fe1f20ea6a92 Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Wed, 24 May 2017 18:17:29 +0800 Subject: [PATCH 10/25] Add Surface compare function. Build Notice!: I got a link error while compiling the full project. The link error is "undefined reference to SDL_GetRGBA" and "undefined reference to SDL_snprintf"... Currently there is one way to solve it : Link SDL2test.lib first (Before SDL2 and SDL2main) --- MiniEngine_Test.cpp | 4 ++++ MiniEngine_Test.h | 2 ++ 2 files changed, 6 insertions(+) diff --git a/MiniEngine_Test.cpp b/MiniEngine_Test.cpp index 8a36cb1..9e9db92 100644 --- a/MiniEngine_Test.cpp +++ b/MiniEngine_Test.cpp @@ -31,6 +31,10 @@ std::string GetMD5(unsigned char* buffer,unsigned int bufferLen) return str; } +int CompareSurface(const Surface& surface1, const Surface& surface2, int allowableError) +{ + return SDLTest_CompareSurfaces(surface1.getRawPointer(),surface2.getRawPointer(),allowableError); +} }/// End of namespace MiniEngine::Test diff --git a/MiniEngine_Test.h b/MiniEngine_Test.h index 879fa90..3d6389a 100644 --- a/MiniEngine_Test.h +++ b/MiniEngine_Test.h @@ -1,4 +1,5 @@ #pragma once +#include "MiniEngine.h" #include namespace MiniEngine @@ -9,6 +10,7 @@ namespace Test std::string GetMD5(unsigned char* buffer,unsigned int bufferLen); void GetMD5Raw(unsigned char* buffer,unsigned int bufferLen,unsigned char* outbuff); +int CompareSurface(const Surface& surface1,const Surface& surface2,int allowableError); }/// End of namespace MiniEngine::Test From 782254470121cb6aeefc19467c77bfe92438f9f7 Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Thu, 25 May 2017 18:34:59 +0800 Subject: [PATCH 11/25] Add More Functions to class Surface --- MiniEngine.cpp | 20 +++++++++++++++++++- MiniEngine.h | 6 +++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/MiniEngine.cpp b/MiniEngine.cpp index 4bcc1a1..334c184 100644 --- a/MiniEngine.cpp +++ b/MiniEngine.cpp @@ -651,6 +651,24 @@ namespace MiniEngine 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); @@ -716,7 +734,7 @@ namespace MiniEngine } /// Experimental - SDL_Surface* Surface::getRawPointer() + SDL_Surface* Surface::getRawPointer() const { return _get(); } diff --git a/MiniEngine.h b/MiniEngine.h index 822015e..54200c9 100644 --- a/MiniEngine.h +++ b/MiniEngine.h @@ -128,6 +128,10 @@ namespace MiniEngine int blitScaledFill(Surface t, Rect src); int blitScaledFullFill(Surface t); + int setClipRect(const Rect& clipRect); + Rect getClipRect() const; + void disableClipping(); + int setAlphaMode(int alpha); int getAlphaMode(); @@ -144,7 +148,7 @@ namespace MiniEngine void release(); /// Experimental : Get SDL_Surface Pointer and then do anything you want! - SDL_Surface* getRawPointer(); + SDL_Surface* getRawPointer() const; private: std::shared_ptr _surf; void _set(SDL_Surface*); From d450ce863628291f7d07b95d0a5a2a5c6ff92ca3 Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Wed, 31 May 2017 22:17:30 +0800 Subject: [PATCH 12/25] Add support for set/get window opacity --- MiniEngine.cpp | 14 +++++++++++++- MiniEngine.h | 5 ++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/MiniEngine.cpp b/MiniEngine.cpp index 334c184..0ed176d 100644 --- a/MiniEngine.cpp +++ b/MiniEngine.cpp @@ -1156,7 +1156,7 @@ namespace MiniEngine _wnd.reset(); } - SDL_Window* Window::_get() + SDL_Window* Window::_get() const { return _wnd.get(); } @@ -1254,6 +1254,18 @@ namespace MiniEngine return (SDL_GetWindowGrab(_get())==SDL_TRUE)?true:false; } + int Window::setOpacity(float opacity) + { + return SDL_SetWindowOpacity(_get(),opacity); + } + + float Window::getOpacity() const + { + float op=-1; + SDL_GetWindowOpacity(_get(),&op); + return op; + } + void Window::setResizable(bool resizable) { //SDL_SetWindowResizable(_get(), resizable?SDL_TRUE:SDL_FALSE); diff --git a/MiniEngine.h b/MiniEngine.h index 54200c9..24caaf3 100644 --- a/MiniEngine.h +++ b/MiniEngine.h @@ -328,6 +328,9 @@ namespace MiniEngine void setGrab(bool); bool getGrab(); + int setOpacity(float opacity); + float getOpacity() const; + void setResizable(bool resizable); /// Use UTF8 in Title and Message please. @@ -365,7 +368,7 @@ namespace MiniEngine std::shared_ptr _wnd; void _set(SDL_Window*); void _clear(); - SDL_Window* _get(); + SDL_Window* _get() const; Renderer winrnd; }; From d8e8deb03121f132596a4fb48d05f8955e9dc83b Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Wed, 31 May 2017 22:30:10 +0800 Subject: [PATCH 13/25] Add template-like get method in class SharedLibrary --- MiniEngine.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/MiniEngine.h b/MiniEngine.h index 24caaf3..3379096 100644 --- a/MiniEngine.h +++ b/MiniEngine.h @@ -478,6 +478,13 @@ namespace MiniEngine ~SharedLibrary(); 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); void release(); private: From 7cecae5695eb6996a3489304d0f3f17cc4a29a23 Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Thu, 1 Jun 2017 22:50:17 +0800 Subject: [PATCH 14/25] Fix Compile error on low level SDL2. --- MiniEngine.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++++--- MiniEngine.h | 10 +++++++++- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/MiniEngine.cpp b/MiniEngine.cpp index 0ed176d..c7d30de 100644 --- a/MiniEngine.cpp +++ b/MiniEngine.cpp @@ -87,8 +87,7 @@ namespace MiniEngine case WindowType::MouseCapture: return SDL_WINDOW_MOUSE_CAPTURE; - /// The following value are not defined on C4. - #ifndef __C4DROID__ + #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: @@ -99,7 +98,7 @@ namespace MiniEngine return SDL_WINDOW_TOOLTIP; case WindowType::PopUpMenu: return SDL_WINDOW_POPUP_MENU; - #endif // __C4DROID__ + #endif // End of SDL2.0.5 Require default: return 0;/// Return 0 on default. @@ -1256,14 +1255,22 @@ namespace MiniEngine int Window::setOpacity(float opacity) { + #if _MINIENGINE_SDL_VERSION_ATLEAST(2,0,5) return SDL_SetWindowOpacity(_get(),opacity); + #else + return -1; + #endif } float Window::getOpacity() const { + #if _MINIENGINE_SDL_VERSION_ATLEAST(2,0,5) float op=-1; SDL_GetWindowOpacity(_get(),&op); return op; + #else + return -1; + #endif } void Window::setResizable(bool resizable) @@ -1719,46 +1726,55 @@ namespace MiniEngine _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(); @@ -1767,6 +1783,7 @@ namespace MiniEngine MixInit(); } + //static void SDLSystem::Quit() { MixQuit(); @@ -1775,11 +1792,13 @@ namespace MiniEngine SDLQuit(); } + //static void SDLSystem::Delay(int ms) { SDL_Delay(ms); } + //static PowerState SDLSystem::GetPowerState() { SDL_PowerState ret=SDL_GetPowerInfo(NULL,NULL); @@ -1800,6 +1819,7 @@ namespace MiniEngine } } + //static int SDLSystem::GetPowerLifeLeft() { int i; @@ -1807,6 +1827,7 @@ namespace MiniEngine return i; } + //static int SDLSystem::GetPowerPrecentageLeft() { int i; @@ -1814,6 +1835,7 @@ namespace MiniEngine return i; } + //static Platform SDLSystem::GetPlatform() { std::string s(SDL_GetPlatform()); @@ -1843,16 +1865,19 @@ namespace MiniEngine } } + //static void SDLSystem::StartTextInput() { SDL_StartTextInput(); } + //static bool SDLSystem::IsTextInputActive() { return SDL_IsTextInputActive()==SDL_TRUE; } + //static void SDLSystem::StopTextInput() { SDL_StopTextInput(); @@ -1864,6 +1889,22 @@ namespace MiniEngine return SDL_HasScreenKeyboardSupport()==SDL_TRUE; } + //static + std::tuple SDLSystem::getCompileVersion() + { + SDL_version ver; + SDL_VERSION(&ver); + return std::make_tuple(ver.major,ver.minor,ver.patch); + } + + //static + std::tuple SDLSystem::getLinkedVersion() + { + SDL_version ver; + SDL_GetVersion(&ver); + return std::make_tuple(ver.major,ver.minor,ver.patch); + } + /// Global Executor For class Timer Uint32 _global_timer_executor(Uint32 interval,void* param) { diff --git a/MiniEngine.h b/MiniEngine.h index 3379096..31d4849 100644 --- a/MiniEngine.h +++ b/MiniEngine.h @@ -5,6 +5,8 @@ #include #include +#define _MINIENGINE_SDL_VERSION_ATLEAST(X,Y,Z) SDL_VERSION_ATLEAST(X,Y,Z) + namespace MiniEngine { @@ -328,6 +330,9 @@ namespace MiniEngine void setGrab(bool); bool getGrab(); + /// SDL2.0.5 Required. + /// If MiniEngine Runs on a lower version of SDL, + /// setOpacity() and getOpacity() always returns -1 int setOpacity(float opacity); float getOpacity() const; @@ -521,7 +526,10 @@ namespace MiniEngine static bool IsTextInputActive(); static void StopTextInput(); - bool HasScreenKeyboardSupport(); + static bool HasScreenKeyboardSupport(); + + static std::tuple getCompileVersion(); + static std::tuple getLinkedVersion(); class Android { From e6624b8238d7dcf46d394e335a812e40b8a5e079 Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Mon, 5 Jun 2017 14:07:57 +0800 Subject: [PATCH 15/25] class Font supports Unicode now. --- MiniEngine.cpp | 128 ++++++++++++++++++++++++++++++++++++------------- MiniEngine.h | 67 +++++++++++++++----------- 2 files changed, 134 insertions(+), 61 deletions(-) diff --git a/MiniEngine.cpp b/MiniEngine.cpp index c7d30de..5e12dfa 100644 --- a/MiniEngine.cpp +++ b/MiniEngine.cpp @@ -171,47 +171,47 @@ namespace MiniEngine } } - int getTTFFontStyleFromFontStyle(Font::Style style) + int getTTFFontStyleFromFontStyle(FontStyle style) { switch(style) { - case Font::Style::Bold: + case FontStyle::Bold: return TTF_STYLE_BOLD; - case Font::Style::Italic: + case FontStyle::Italic: return TTF_STYLE_ITALIC; - case Font::Style::Normal: + case FontStyle::Normal: return TTF_STYLE_NORMAL; - case Font::Style::StrikeThrough: + case FontStyle::StrikeThrough: return TTF_STYLE_STRIKETHROUGH; - case Font::Style::UnderLine: + case FontStyle::UnderLine: return TTF_STYLE_UNDERLINE; default: return TTF_STYLE_NORMAL; } } - std::vector getFontStyleVecFromMixedTTFFontStyle(int Mixed_TTF_Font_Style) + std::vector getFontStyleVecFromMixedTTFFontStyle(int Mixed_TTF_Font_Style) { - std::vector vec; + std::vector vec; if(Mixed_TTF_Font_Style&TTF_STYLE_BOLD) { - vec.push_back(Font::Style::Bold); + vec.push_back(FontStyle::Bold); } if(Mixed_TTF_Font_Style&TTF_STYLE_ITALIC) { - vec.push_back(Font::Style::Italic); + vec.push_back(FontStyle::Italic); } if(Mixed_TTF_Font_Style&TTF_STYLE_STRIKETHROUGH) { - vec.push_back(Font::Style::StrikeThrough); + vec.push_back(FontStyle::StrikeThrough); } if(Mixed_TTF_Font_Style&TTF_STYLE_UNDERLINE) { - vec.push_back(Font::Style::UnderLine); + vec.push_back(FontStyle::UnderLine); } if(vec.empty()) { - vec.push_back(Font::Style::Normal); + vec.push_back(FontStyle::Normal); } return vec; @@ -1380,7 +1380,7 @@ namespace MiniEngine _font.reset(); } - TTF_Font* Font::_get() + TTF_Font* Font::_get() const { return _font.get(); } @@ -1403,32 +1403,32 @@ namespace MiniEngine return 0; } - bool Font::isReady() + bool Font::isReady() const { return (_get() != nullptr); } - bool Font::isNormal() + bool Font::isNormal() const { return !(TTF_GetFontStyle(_get())); } - bool Font::isBold() + bool Font::isBold() const { return (TTF_GetFontStyle(_get()) & TTF_STYLE_BOLD ); } - bool Font::isItalic() + bool Font::isItalic() const { return (TTF_GetFontStyle(_get()) & TTF_STYLE_ITALIC ); } - bool Font::isUnderLine() + bool Font::isUnderLine() const { return (TTF_GetFontStyle(_get()) & TTF_STYLE_UNDERLINE ); } - bool Font::isStrikeThrough() + bool Font::isStrikeThrough() const { return (TTF_GetFontStyle(_get()) & TTF_STYLE_STRIKETHROUGH ); } @@ -1475,18 +1475,18 @@ namespace MiniEngine TTF_SetFontStyle(_get(),Style); } - int Font::_style_caster(Style style) + int Font::_style_caster(FontStyle style) { return _internal::getTTFFontStyleFromFontStyle(style); } - std::vector Font::getFontStyles() + std::vector Font::getFontStyles() const { int styles=TTF_GetFontStyle(_get()); return _internal::getFontStyleVecFromMixedTTFFontStyle(styles); } - Rect Font::sizeText(const std::string& Text) throw (ErrorViewer) + Rect Font::sizeText(const std::string& Text) const throw (ErrorViewer) { int w=0,h=0; if(TTF_SizeText(_get(),Text.c_str(),&w,&h)!=0) @@ -1497,7 +1497,7 @@ namespace MiniEngine return Rect(0,0,w,h); } - Rect Font::sizeUTF8(const std::string& Text) throw (ErrorViewer) + Rect Font::sizeUTF8(const std::string& Text) const throw (ErrorViewer) { int w=0,h=0; if(TTF_SizeUTF8(_get(),Text.c_str(),&w,&h)!=0) @@ -1508,70 +1508,109 @@ namespace MiniEngine return Rect(0,0,w,h); } + Rect Font::sizeUnicode(const uint16_t* Text) const throw (ErrorViewer) + { + int w=0,h=0; + if(TTF_SizeUNICODE(_get(),Text,&w,&h)!=0) + { + /// Something might be wrong + throw ErrorViewer(); + } + return Rect(0,0,w,h); + } + /// rendering surfaces... - Surface Font::renderText(std::string Text,RGBA fg) + Surface Font::renderText(std::string Text,RGBA fg) const { Surface surf; surf._set(TTF_RenderText_Blended(_get(), Text.c_str(), fg.toSDLColor())); return surf; } - Surface Font::renderTextWrapped(std::string Text, RGBA fg, int WrapLength) + Surface Font::renderTextWrapped(std::string Text, RGBA fg, size_t WrapLength) const { Surface surf; surf._set(TTF_RenderText_Blended_Wrapped(_get(), Text.c_str(), fg.toSDLColor(), WrapLength)); return surf; } - Surface Font::renderTextShaded(std::string Text, RGBA fg,RGBA bg) + Surface Font::renderTextShaded(std::string Text, RGBA fg,RGBA bg) const { Surface surf; surf._set(TTF_RenderText_Shaded(_get(), Text.c_str(), fg.toSDLColor(), bg.toSDLColor())); return surf; } - Surface Font::renderTextSolid(std::string Text,RGBA fg) + Surface Font::renderTextSolid(std::string Text,RGBA fg) const { Surface surf; surf._set(TTF_RenderText_Solid(_get(), Text.c_str(), fg.toSDLColor())); return surf; } - Surface Font::renderUTF8(std::string Text,RGBA fg) + Surface Font::renderUTF8(std::string Text,RGBA fg) const { Surface surf; surf._set(TTF_RenderUTF8_Blended(_get(), Text.c_str(), fg.toSDLColor())); return surf; } - Surface Font::renderUTF8Wrapped(std::string Text, RGBA fg, int WrapLength) + Surface Font::renderUTF8Wrapped(std::string Text, RGBA fg, size_t WrapLength) const { Surface surf; surf._set(TTF_RenderUTF8_Blended_Wrapped(_get(), Text.c_str(), fg.toSDLColor(), WrapLength)); return surf; } - Surface Font::renderUTF8Shaded(std::string Text, RGBA fg,RGBA bg) + Surface Font::renderUTF8Shaded(std::string Text, RGBA fg,RGBA bg) const { Surface surf; surf._set(TTF_RenderUTF8_Shaded(_get(), Text.c_str(), fg.toSDLColor(), bg.toSDLColor())); return surf; } - Surface Font::renderUTF8Solid(std::string Text,RGBA fg) + Surface Font::renderUTF8Solid(std::string Text,RGBA fg) const { Surface surf; surf._set(TTF_RenderUTF8_Solid(_get(), Text.c_str(), fg.toSDLColor())); return surf; } + Surface Font::renderUnicode(const uint16_t* Text, RGBA fg) const + { + Surface surf; + surf._set(TTF_RenderUNICODE_Blended(_get(),Text,fg.toSDLColor())); + return surf; + } + + Surface Font::renderUnicodeWrapped(const uint16_t* Text, RGBA fg, size_t WrapLength) const + { + Surface surf; + surf._set(TTF_RenderUNICODE_Blended_Wrapped(_get(),Text,fg.toSDLColor(),WrapLength)); + return surf; + } + + Surface Font::renderUnicodeShaded(const uint16_t* Text, RGBA fg, RGBA bg) const + { + Surface surf; + surf._set(TTF_RenderUNICODE_Shaded(_get(),Text,fg.toSDLColor(),bg.toSDLColor())); + return surf; + } + + Surface Font::renderUnicodeSolid(const uint16_t* Text, RGBA fg) const + { + Surface surf; + surf._set(TTF_RenderUNICODE_Solid(_get(),Text,fg.toSDLColor())); + return surf; + } + /// rendering textures... Texture Font::renderText(Renderer rnd, std::string Text, RGBA fg) { return rnd.render(renderText(Text,fg)); } - Texture Font::renderTextWrapped(Renderer rnd, std::string Text, RGBA fg, int WrapLength) + Texture Font::renderTextWrapped(Renderer rnd, std::string Text, RGBA fg, size_t WrapLength) { return rnd.render(renderTextWrapped(Text,fg,WrapLength)); } @@ -1591,7 +1630,7 @@ namespace MiniEngine return rnd.render(renderUTF8(Text,fg)); } - Texture Font::renderUTF8Wrapped(Renderer rnd, std::string Text, RGBA fg, int WrapLength) + Texture Font::renderUTF8Wrapped(Renderer rnd, std::string Text, RGBA fg, size_t WrapLength) { return rnd.render(renderUTF8Wrapped(Text,fg,WrapLength)); } @@ -1606,6 +1645,27 @@ namespace MiniEngine return rnd.render(renderUTF8Solid(Text,fg)); } + + Texture Font::renderUnicode(Renderer rnd, const uint16_t* Text, RGBA fg) const + { + return rnd.render(renderUnicode(Text,fg)); + } + + Texture Font::renderUnicodeWrapped(Renderer rnd, const uint16_t* Text, RGBA fg, size_t WrapLength) const + { + return rnd.render(renderUnicodeWrapped(Text,fg,WrapLength)); + } + + Texture Font::renderUnicodeShaded(Renderer rnd, const uint16_t* Text, RGBA fg, RGBA bg) const + { + return rnd.render(renderUnicodeShaded(Text,fg,bg)); + } + + Texture Font::renderUnicodeSolid(Renderer rnd, const uint16_t* Text, RGBA fg) const + { + return rnd.render(renderUnicodeSolid(Text,fg)); + } + void Font::release() { _clear(); diff --git a/MiniEngine.h b/MiniEngine.h index 31d4849..f1b3266 100644 --- a/MiniEngine.h +++ b/MiniEngine.h @@ -378,21 +378,21 @@ namespace MiniEngine Renderer winrnd; }; + enum class FontStyle { Normal, Bold, Italic, UnderLine, StrikeThrough }; + 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(); + bool isReady() const; - bool isNormal(); - bool isBold(); - bool isItalic(); - bool isUnderLine(); - bool isStrikeThrough(); + bool isNormal() const; + bool isBold() const; + bool isItalic() const; + bool isUnderLine() const; + bool isStrikeThrough() const; void setNormal(); void setBold(bool); @@ -401,65 +401,78 @@ namespace MiniEngine void setStrikeThrough(bool); template - void setFontStyle(Style style,Args&&... args) + void setFontStyle(FontStyle style,Args&&... args) { int fontcalc=0; _setFontStyle(fontcalc,style,args...); } - void setFontStyle(Style style) + void setFontStyle(FontStyle style) { int fontcalc=0; _setFontStyle(fontcalc,style); } - std::vector