diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..e709388 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,4 @@ +*.c linguist-language=C++ +*.cpp linguist-language=C++ +*.h linguist-language=C++ +*.hpp linguist-language=C++ diff --git a/MiniEngine.cpp b/MiniEngine.cpp index 687a572..eca5768 100644 --- a/MiniEngine.cpp +++ b/MiniEngine.cpp @@ -74,31 +74,6 @@ 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; - } - bool GetScanKeyState(SDL_Scancode code) { return SDL_GetKeyboardState(NULL)[code]; @@ -130,27 +105,3 @@ bool canexecute(std::string Path) #else /// _MINIENGINE_HAS_UNISTD == 0 /// File Functions will be implied in platform specific source file. #endif - -int _miniengine_argc; -char** _miniengine_argv; - - /// Default Setup Code -int main(int argc, char* argv[]) -{ - _miniengine_argc=argc; - _miniengine_argv=argv; - MiniEngine::SDLSystem::Init(); - int ret = AppMain(); - MiniEngine::SDLSystem::Quit(); - return ret; -} - -int GetArgc() -{ - return _miniengine_argc; -} - -char** GetArgv() -{ - return _miniengine_argv; -} diff --git a/MiniEngine.h b/MiniEngine.h index 6604e92..0a7ce45 100644 --- a/MiniEngine.h +++ b/MiniEngine.h @@ -4,7 +4,6 @@ #include #include "SDLWrapper/IncludeAll.h" - namespace MiniEngine { class StringEngine @@ -24,10 +23,6 @@ namespace MiniEngine impl* pimpl; }; - int SetClipboardText(const std::string& str); - std::string GetClipboardText(); - bool HasClipboardText(); - /// Experimental - For Experts: Use SDL ScanCode bool GetScanKeyState(SDL_Scancode); @@ -39,13 +34,3 @@ bool canread(std::string Path); bool canwrite(std::string Path); bool isexist(std::string Path); bool canexecute(std::string Path); - - /// Your Program Should Start Here -int AppMain(); - -/// MiniEngine Provides main -int main(int argc,char* argv[]); - -/// MiniEngine Provided API: Get Start Parameters -int GetArgc(); -char** GetArgv(); diff --git a/MiniEngine_Event.cpp b/MiniEngine_Event.cpp index d3e8720..671b02a 100644 --- a/MiniEngine_Event.cpp +++ b/MiniEngine_Event.cpp @@ -1,5 +1,8 @@ #include "MiniEngine_Event.h" +namespace MiniEngine +{ + int PollEvent(Event& refEvent) { return SDL_PollEvent(&refEvent); @@ -64,22 +67,22 @@ Looper::Looper() LooperID Looper::add(_SDLEventType_ event_type,const std::function& event_callback) { _evmap[event_type].push_front(std::make_pair(_loop_cnt,event_callback)); - return _getNextID(event_type); + return _getNextID(event_type); } LooperID Looper::add(_SDLEventType_ event_type,const std::function& event_callback) { _evmap[event_type].push_front(std::make_pair(_loop_cnt,[=](Looper& lp,Event& ev)->int{return event_callback(ev);})); - return _getNextID(event_type); + return _getNextID(event_type); } LooperID Looper::add(_SDLEventType_ event_type,const std::function& event_callback) { _evmap[event_type].push_front(std::make_pair(_loop_cnt,[=](Looper& lp,Event& ev)->int{return event_callback(lp);})); - return _getNextID(event_type); + return _getNextID(event_type); } LooperID Looper::add(_SDLEventType_ event_type,const std::function& event_callback) { _evmap[event_type].push_front(std::make_pair(_loop_cnt,[=](Looper& lp,Event& ev)->int{return event_callback();})); - return _getNextID(event_type); + return _getNextID(event_type); } LooperID Looper::add(_SDLEventType_ event_type,const std::function& event_callback) @@ -96,7 +99,7 @@ LooperID Looper::add(_SDLEventType_ event_type,const std::function& event_callback) { - return add(event_type,std::function([=](Looper& lp,Event& ev)->int{event_callback();return 0;})); + return add(event_type,std::function([=](Looper& lp,Event& ev)->int{event_callback(); return 0;})); } LooperID Looper::operator + (const std::pair<_SDLEventType_,std::function>& event_callback) @@ -137,10 +140,10 @@ LooperID Looper::operator + (const std::pair<_SDLEventType_,std::functionfirst==looperid._looper_cnt) { @@ -166,17 +169,20 @@ void Looper::dispatch() } void Looper::run() { - while(_running) + do { + if(_update) + { + updater(); + _update=false; + } + while(!_update&&WaitEvent(_e)) { dispatch(); } - - updater(); - - _update=false; } + while(_running); } Event Looper::GetLastEvent() { @@ -186,14 +192,10 @@ void Looper::needupdate() { _update=true; } -void Looper::needstop() -{ - _running=false; -} void Looper::stop() { - needstop(); - needupdate(); + _running=false; + _update=true; } void Looper::reset() { @@ -206,30 +208,37 @@ void Looper::reset() LooperID Looper::_getNextID(const _SDLEventType_& event_type) { - LooperID id; - id._looper_cnt = _loop_cnt; - id._type_id = event_type; + LooperID id; + id._looper_cnt = _loop_cnt; + id._type_id = event_type; - ++_loop_cnt; - return id; + ++_loop_cnt; + return id; } Poller::Poller() { - idler=[](){}; + idler=[]() {}; } void Poller::reset() { Looper::reset(); - idler=[](){}; + idler=[]() {}; } void Poller::run() { int pollret=1; - while(_running) + + do { + if(_update) + { + updater(); + _update=false; + } + while(!_update&&(pollret=PollEvent(_e))) { dispatch(); @@ -237,45 +246,46 @@ void Poller::run() /// If pollret is not 0 (new event requests update), or pollret is 0 (No New Event) but Idle function requests update, then call updater. if(!pollret) idler(); - if(_update) - { - updater(); - _update=false; - } + } + while(_running); } LooperWithTime::LooperWithTime(int Timeout_ms) { - _timeout_ms = Timeout_ms; + _timeout_ms = Timeout_ms; } void LooperWithTime::setTimeout(int ms) { - _timeout_ms = ms; + _timeout_ms = ms; } int LooperWithTime::getTimeout() const { - return _timeout_ms; + return _timeout_ms; } void LooperWithTime::run() { - int timeret = 1; - while (_running) - { - while (!_update&&(timeret=WaitEventTimeout(_e, _timeout_ms))) - { - dispatch(); - } + int timeret = 1; + do + { + if (_update) + { + updater(); + _update = false; + } - /// If timeret is not 0 (new event request update), or timeret is 0 (Time out) but Idle function requests update, then call updater. - if (!timeret) idler(); - if (_update) - { - updater(); - _update = false; - } - } + while (!_update&&(timeret=WaitEventTimeout(_e, _timeout_ms))) + { + dispatch(); + } + + /// If timeret is not 0 (new event request update), or timeret is 0 (Time out) but Idle function requests update, then call updater. + if (!timeret) idler(); + } + while (_running); } + +}/// End of namespace MiniEngine diff --git a/MiniEngine_Event.h b/MiniEngine_Event.h index b3c5ed4..e329f32 100644 --- a/MiniEngine_Event.h +++ b/MiniEngine_Event.h @@ -4,6 +4,9 @@ #include #include +namespace MiniEngine +{ + typedef SDL_Event Event; typedef decltype(Event::type) _SDLEventType_; @@ -63,7 +66,6 @@ public: void run(); Event GetLastEvent(); void needupdate(); - void needstop(); void stop(); void reset(); @@ -97,3 +99,5 @@ public: protected: int _timeout_ms; }; + +}/// End of namespace MiniEngine diff --git a/README.md b/README.md index 0c4457b..03bc527 100644 --- a/README.md +++ b/README.md @@ -9,16 +9,18 @@ C++编写的SDL2引擎. ### 编译说明 -Windows: 请使用Codeblocks 16.01(推荐)载入所有.cpp文件.接下来Codeblocks会完成其余的工作. +Windows/Linux: 请使用Codeblocks 16.01(推荐)载入所有.cpp文件.接下来Codeblocks会完成其余的工作. > 依赖库 > SDL2 (SDL2.lib, SDL2main.lib, SDL2test.lib) > SDL2 Image (SDL2_image.lib) > SDL2 Mixer (SDL2_mixer.lib) > SDL2 TTF (SDL2_ttf.lib) +Linux Codeblocks PPA 参见: [Code::Blocks Release Builds](https://launchpad.net/~damien-moore/+archive/ubuntu/codeblocks-stable) + Windows-Visual Studio: 使用VS编译本项目可能会出现某些错误,目前还没有很好的解决办法. -C4droid: 长按编译键选择编译模式为Makefile. 选择编译目标为SDL2 Application. 修改程序名称为program_name(此处与makefile对应即可) +C4droid: 使用Makefile Generator生成makefile文件. 选择编译目标为SDL2 Application. 修改程序名称为program_name(与makefile对应) > 依赖库 > C4droid本体 > GCC Plugin For C4droid diff --git a/SDLWrapper/Cursor.h b/SDLWrapper/Cursor.h index b1b381d..6b45469 100644 --- a/SDLWrapper/Cursor.h +++ b/SDLWrapper/Cursor.h @@ -3,6 +3,7 @@ #include "_SystemCursorType.h" #include "Point.h" #include "Surface.h" +#include "__Plugin.h" #include "begin_code.h" class Cursor { @@ -26,5 +27,7 @@ private: void _set_no_delete(SDL_Cursor*); SDL_Cursor* _get(); void _clear(); + + friend class _internal::Plugin; }; #include "end_code.h" diff --git a/SDLWrapper/Font.cpp b/SDLWrapper/Font.cpp index 75afcd6..55682ac 100644 --- a/SDLWrapper/Font.cpp +++ b/SDLWrapper/Font.cpp @@ -215,7 +215,9 @@ Rect Font::sizeText(const std::string& Text) const throw (ErrorViewer) if(TTF_SizeText(_get(),Text.c_str(),&w,&h)!=0) { /// Something might be wrong - throw ErrorViewer(); + ErrorViewer e; + e.fetch(); + throw e; } return Rect(0,0,w,h); } @@ -226,7 +228,9 @@ Rect Font::sizeUTF8(const std::string& Text) const throw (ErrorViewer) if(TTF_SizeUTF8(_get(),Text.c_str(),&w,&h)!=0) { /// Something might be wrong - throw ErrorViewer(); + ErrorViewer e; + e.fetch(); + throw e; } return Rect(0,0,w,h); } @@ -237,7 +241,9 @@ Rect Font::sizeUnicode(const uint16_t* Text) const throw (ErrorViewer) if(TTF_SizeUNICODE(_get(),Text,&w,&h)!=0) { /// Something might be wrong - throw ErrorViewer(); + ErrorViewer e; + e.fetch(); + throw e; } return Rect(0,0,w,h); } diff --git a/SDLWrapper/Font.h b/SDLWrapper/Font.h index d2d9a24..1587746 100644 --- a/SDLWrapper/Font.h +++ b/SDLWrapper/Font.h @@ -7,6 +7,7 @@ #include "Texture.h" #include "Renderer.h" #include +#include "__Plugin.h" #include "begin_code.h" class Font { @@ -118,5 +119,7 @@ private: void _set(TTF_Font*); void _clear(); TTF_Font* _get() const; + + friend class _internal::Plugin; }; #include "end_code.h" diff --git a/SDLWrapper/Music.cpp b/SDLWrapper/Music.cpp index 9bdae9b..8e9410b 100644 --- a/SDLWrapper/Music.cpp +++ b/SDLWrapper/Music.cpp @@ -1,4 +1,5 @@ #include "Music.h" +#include "_caster.h" #include "begin_code.h" //private void Music::_set(Mix_Music* p) @@ -21,6 +22,11 @@ Music::Music(const std::string& Filename) _set(Mix_LoadMUS(Filename.c_str())); } +Music::Music(const RWOP& rwop,MusicType musicType) +{ + _set(Mix_LoadMUSType_RW(rwop._get(),_internal::getMixMusicTypeFromMusicType(musicType),0)); +} + bool Music::isReady() const { return (_get()!=nullptr); @@ -31,6 +37,11 @@ void Music::release() _clear(); } +MusicType Music::getType() const +{ + return _internal::getMusicTypeFromMixMusicType(Mix_GetMusicType(_get())); +} + //static int MusicPlayer::GetDecoderNum() { @@ -69,6 +80,12 @@ int MusicPlayer::stop() return Mix_HaltMusic(); } +int MusicPlayer::fadeIn(Music music, int loops, int ms) +{ + m=music; + return fadeIn(loops,ms); +} + int MusicPlayer::fadeIn(int loops, int ms) { return Mix_FadeInMusic(m._get(), loops, ms); diff --git a/SDLWrapper/Music.h b/SDLWrapper/Music.h index 7966886..1d870a6 100644 --- a/SDLWrapper/Music.h +++ b/SDLWrapper/Music.h @@ -4,7 +4,10 @@ #include #include "ErrorViewer.h" #include "Audio.h" +#include "RWOP.h" +#include "_MusicType.h" #include "__Noncopyable.h" +#include "__Plugin.h" #include "begin_code.h" /// Forward Declaration class Music @@ -12,14 +15,18 @@ class Music public: Music()=default; Music(const std::string& Filename); + Music(const RWOP& rwop,MusicType musicType); bool isReady() const; void release(); + MusicType getType() const; private: std::shared_ptr _music; void _set(Mix_Music*); void _clear(); Mix_Music* _get() const; + friend class MusicPlayer; + friend class _internal::Plugin; }; class MusicPlayer : public AudioPlayer, public NonCopyable @@ -35,6 +42,7 @@ public: void rewind(); int setPosition(double second); int stop(); + int fadeIn(Music music,int loops,int ms); int fadeIn(int loops, int ms); int fadeOut(int ms); diff --git a/SDLWrapper/RWOP.h b/SDLWrapper/RWOP.h index 0dd8418..bebefb3 100644 --- a/SDLWrapper/RWOP.h +++ b/SDLWrapper/RWOP.h @@ -3,6 +3,7 @@ #include #include #include +#include "__Plugin.h" #include "begin_code.h" class RWOP { @@ -22,5 +23,9 @@ private: void _set(SDL_RWops*); friend class Surface; friend class Renderer; + friend class Sound; + friend class Music; + + friend class _internal::Plugin; }; #include "end_code.h" diff --git a/SDLWrapper/Renderer.cpp b/SDLWrapper/Renderer.cpp index 3c2c9c9..00664e0 100644 --- a/SDLWrapper/Renderer.cpp +++ b/SDLWrapper/Renderer.cpp @@ -20,11 +20,57 @@ SDL_Renderer* Renderer::_get() const return _rnd.get(); } +// 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; + } +} + +// 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; +} + Renderer::Renderer(Window& wnd,std::initializer_list RendererFlags) throw (ErrorViewer) { if(createRenderer(wnd,RendererFlags)!=0) { - throw ErrorViewer(); + ErrorViewer e; + e.fetch(); + throw e; + } +} + +Renderer::Renderer(Surface& surf) throw (ErrorViewer) +{ + if(createSoftRenderer(surf)!=0) + { + ErrorViewer e; + e.fetch(); + throw e; } } @@ -38,6 +84,20 @@ int Renderer::createRenderer(Window& wnd,std::initializer_list Ren return _createRenderer_Real(wnd,flag); } +int Renderer::createSoftRenderer(Surface& surf) +{ + SDL_Renderer* pRnd=SDL_CreateSoftwareRenderer(surf._get()); + if(pRnd!=nullptr) + { + _set(pRnd); + return 0; + } + else + { + return -1; + } +} + int Renderer::setColor(const RGBA& pack) { return SDL_SetRenderDrawColor(_get(), pack.r, pack.g, pack.b, pack.a); @@ -141,6 +201,47 @@ int Renderer::drawLines(const std::vector& pointvec) return drawLines(pointvec.data(),pointvec.size()); } +int Renderer::fillRects(const std::vector& rectvec) +{ + std::vector thisvec; + for(auto& rectref:rectvec) + { + thisvec.push_back(rectref.toSDLRect()); + } + return fillRects(thisvec); +} + +int Renderer::drawRects(const std::vector& rectvec) +{ + std::vector thisvec; + for(auto& rectref:rectvec) + { + thisvec.push_back(rectref.toSDLRect()); + } + return drawRects(thisvec); +} + +int Renderer::drawPoints(const std::vector& pointvec) +{ + std::vector thisvec; + for(auto& pointref:pointvec) + { + thisvec.push_back(pointref.toSDLPoint()); + } + return drawPoints(thisvec); +} + +int Renderer::drawLines(const std::vector& pointvec) +{ + std::vector thisvec; + for(auto& pointref:pointvec) + { + thisvec.push_back(pointref.toSDLPoint()); + } + return drawLines(thisvec); +} + + int Renderer::setScale(float scaleX, float scaleY) { return SDL_RenderSetScale(_get(),scaleX,scaleY); @@ -415,38 +516,4 @@ 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 93ac6ba..be95dfc 100644 --- a/SDLWrapper/Renderer.h +++ b/SDLWrapper/Renderer.h @@ -5,13 +5,17 @@ #include "Window.h" #include "Surface.h" #include "Texture.h" +#include "__Plugin.h" #include #include "begin_code.h" class Renderer { public: Renderer() = default; + /// Create a Renderer associated with Window Renderer(Window& wnd,std::initializer_list RendererFlags = { RendererType::Accelerated,RendererType::TargetTexture }) throw (ErrorViewer); + /// Create a software Renderer + Renderer(Surface& surf) throw (ErrorViewer); ~Renderer() = default; /// If Renderer is current not ready, setRenderer will create Renderer. @@ -31,6 +35,8 @@ public: int createRenderer(Window& wnd,std::initializer_list); + int createSoftRenderer(Surface& surf); + int setColor(const RGBA& pack); RGBA getColor() const; int setBlendMode(BlendMode mode); @@ -56,6 +62,12 @@ public: int drawPoints(const std::vector& pointvec); int drawLines(const std::vector& pointvec); + /// Slower Functions (Need Convert First, then call Experimental Functions.) + int fillRects(const std::vector& rectvec); + int drawRects(const std::vector& rectvec); + int drawPoints(const std::vector& pointvec); + int drawLines(const std::vector& pointvec); + int setScale(float scaleX,float scaleY); std::tuple getScale() const; @@ -134,5 +146,7 @@ private: void _set(SDL_Renderer*); void _clear(); SDL_Renderer* _get() const; + + friend class _internal::Plugin; }; #include "end_code.h" diff --git a/SDLWrapper/SDLSystem.cpp b/SDLWrapper/SDLSystem.cpp index 738cfc6..bb3febe 100644 --- a/SDLWrapper/SDLSystem.cpp +++ b/SDLWrapper/SDLSystem.cpp @@ -1,69 +1,100 @@ #include "SDLSystem.h" +#include "_caster.h" #include "begin_code.h" -//static -int SDLSystem::SDLInit() +// private +void SDLSystem::_init(Uint32 sdl_flag, Uint32 img_flag, Uint32 mix_flag, bool init_ttf) throw (ErrorViewer) { - return SDL_Init(SDL_INIT_EVERYTHING); + int ret=SDL_Init(sdl_flag); + if(ret!=0) + { + ErrorViewer e; + e.fetch(); + throw e; + } + + Uint32 uret=0; + uret=IMG_Init(img_flag); + if(uret!=img_flag) /// IMG_Init returns its parameter on success. + { + ErrorViewer e; + e.fetch(); + throw e; + } + + uret=Mix_Init(mix_flag); + if(uret!=mix_flag) /// Mix_Init returns its parameter on success. + { + ErrorViewer e; + e.fetch(); + throw e; + } + + if(init_ttf) + { + ret=TTF_Init(); + if(ret!=0) + { + ErrorViewer e; + e.fetch(); + throw e; + } + } } -//static -void SDLSystem::SDLQuit() +SDLSystem::SDLSystem(const std::initializer_list& flag_sdl, + const std::initializer_list& flag_img, + const std::initializer_list& flag_mix, + bool init_ttf ) throw (ErrorViewer) { - SDL_Quit(); + Uint32 sdl_flag=0; + for(auto& v:flag_sdl) + { + sdl_flag |= _internal::getUint32FromSDLInitFlag(v); + } + int img_flag=0; + for(auto& v:flag_img) + { + img_flag |= _internal::getIntFromIMGInitFlag(v); + } + int mix_flag=0; + for(auto& v:flag_mix) + { + mix_flag |= _internal::getIntFromMixInitFlag(v); + } + + try + { + _init(sdl_flag,img_flag,mix_flag,init_ttf); + } + catch(ErrorViewer& e) + { + throw e; + } } -//static -int SDLSystem::IMGInit() +SDLSystem::SDLSystem(Uint32 sdl_flag, Uint32 img_flag, Uint32 mix_flag, bool init_ttf) throw (ErrorViewer) { - return IMG_Init(IMG_INIT_JPG | IMG_INIT_PNG); + try + { + _init(sdl_flag,img_flag,mix_flag,init_ttf); + } + catch(ErrorViewer& e) + { + throw e; + } } -//static -void SDLSystem::IMGQuit() -{ - IMG_Quit(); -} -//static -int SDLSystem::TTFInit() +SDLSystem::~SDLSystem() { - return TTF_Init(); -} + if(TTF_WasInit()) + { + TTF_Quit(); + } -//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(); + IMG_Quit(); + SDL_Quit(); } //static @@ -240,4 +271,33 @@ int SDLSystem::GetSystemRAM() { return SDL_GetSystemRAM(); } + +//static +int SDLSystem::SetClipboardText(const std::string& str) +{ + return SDL_SetClipboardText(str.c_str()); +} + +//static +std::string SDLSystem::GetClipboardText() +{ + char* pstr=SDL_GetClipboardText(); + if(pstr==nullptr) + { + return std::string(); + } + else + { + std::string s(pstr); + SDL_free(pstr); + return s; + } +} + +//static +bool SDLSystem::HasClipboardText() +{ + return SDL_HasClipboardText()==SDL_TRUE; +} + #include "end_code.h" diff --git a/SDLWrapper/SDLSystem.h b/SDLWrapper/SDLSystem.h index 4a5422f..67ff90e 100644 --- a/SDLWrapper/SDLSystem.h +++ b/SDLWrapper/SDLSystem.h @@ -2,23 +2,26 @@ #include "include.h" #include "_PowerState.h" #include "_Platform.h" +#include "_SDLInitFlag.h" +#include "_IMGInitFlag.h" +#include "_MixInitFlag.h" +#include "__Noncopyable.h" +#include "__Nonmoveable.h" +#include "ErrorViewer.h" #include #include #include "begin_code.h" -class SDLSystem + +class SDLSystem : public NonCopyable, public NonMoveable { 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(); + SDLSystem(const std::initializer_list& flag_sdl = {SDLInitFlag::All} , + const std::initializer_list& flag_img = {IMGInitFlag::JPG,IMGInitFlag::PNG} , + const std::initializer_list& flag_mix = {MixInitFlag::MP3} , + bool init_ttf = true ) throw (ErrorViewer); + /// Experimental Constructor + SDLSystem(Uint32 sdl_flag, Uint32 img_flag, Uint32 mix_flag, bool init_ttf) throw (ErrorViewer); + ~SDLSystem(); static void Delay(int ms); @@ -51,6 +54,10 @@ public: /// RAM is calculated in MB. static int GetSystemRAM(); + static int SetClipboardText(const std::string& str); + static std::string GetClipboardText(); + static bool HasClipboardText(); + class Android { public: @@ -61,5 +68,9 @@ public: static std::string GetExternal(); static void* GetJNIEnv(); }; + +private: + void _init(Uint32,Uint32,Uint32,bool) throw (ErrorViewer); + void _quit(); }; #include "end_code.h" diff --git a/SDLWrapper/SharedLibrary.h b/SDLWrapper/SharedLibrary.h index 8b7b620..5438e41 100644 --- a/SDLWrapper/SharedLibrary.h +++ b/SDLWrapper/SharedLibrary.h @@ -2,6 +2,7 @@ #include "include.h" #include #include +#include #include "begin_code.h" class SharedLibrary { diff --git a/SDLWrapper/Sound.cpp b/SDLWrapper/Sound.cpp index 5852cd2..768f7bd 100644 --- a/SDLWrapper/Sound.cpp +++ b/SDLWrapper/Sound.cpp @@ -21,6 +21,11 @@ Sound::Sound(const std::string& WAVFilename) _set(Mix_LoadWAV(WAVFilename.c_str())); } +Sound::Sound(const RWOP& rwop) +{ + _set(Mix_LoadWAV_RW(rwop._get(),0)); +} + bool Sound::isReady() const { return (_get()!=nullptr); diff --git a/SDLWrapper/Sound.h b/SDLWrapper/Sound.h index b1f677c..9cb9043 100644 --- a/SDLWrapper/Sound.h +++ b/SDLWrapper/Sound.h @@ -4,12 +4,15 @@ #include #include "Audio.h" #include "ErrorViewer.h" +#include "RWOP.h" +#include "__Plugin.h" #include "begin_code.h" class Sound { public: Sound() = default; Sound(const std::string& WAVFilename); + Sound(const RWOP& rwop); bool isReady() const; void release(); private: @@ -19,6 +22,7 @@ private: Mix_Chunk* _get() const; friend class Channel; + friend class _internal::Plugin; }; class Channel diff --git a/SDLWrapper/Surface.h b/SDLWrapper/Surface.h index a83fcf6..3fce1dc 100644 --- a/SDLWrapper/Surface.h +++ b/SDLWrapper/Surface.h @@ -5,6 +5,7 @@ #include "Point.h" #include "RWOP.h" #include "ErrorViewer.h" +#include "__Plugin.h" #include "begin_code.h" class Surface { @@ -81,6 +82,8 @@ private: friend class Renderer; friend class Font; friend class Cursor; + + friend class _internal::Plugin; }; #include "end_code.h" diff --git a/SDLWrapper/Texture.h b/SDLWrapper/Texture.h index b023244..3c0dcfc 100644 --- a/SDLWrapper/Texture.h +++ b/SDLWrapper/Texture.h @@ -4,6 +4,7 @@ #include "RGBA.h" #include "ColorMode.h" #include "_BlendMode.h" +#include "__Plugin.h" #include #include "begin_code.h" @@ -40,6 +41,8 @@ private: SDL_Texture* _get() const; Rect _rect; friend class Renderer; + + friend class _internal::Plugin; }; #include "end_code.h" diff --git a/SDLWrapper/Window.h b/SDLWrapper/Window.h index 4e1a9e9..644bd0f 100644 --- a/SDLWrapper/Window.h +++ b/SDLWrapper/Window.h @@ -5,6 +5,7 @@ #include "ErrorViewer.h" #include "MessageBox.h" #include "Surface.h" +#include "__Plugin.h" #include "begin_code.h" class Window { @@ -62,5 +63,6 @@ private: SDL_Window* _get() const; friend class Renderer; + friend class _internal::Plugin; }; #include "end_code.h" diff --git a/SDLWrapper/_IMGInitFlag.h b/SDLWrapper/_IMGInitFlag.h new file mode 100644 index 0000000..ed9085e --- /dev/null +++ b/SDLWrapper/_IMGInitFlag.h @@ -0,0 +1,11 @@ +#pragma once +#include "begin_code.h" +enum class IMGInitFlag +{ + JPG, + PNG, + TIF, + WEBP, + ALL +}; +#include "end_code.h" diff --git a/SDLWrapper/_MixInitFlag.h b/SDLWrapper/_MixInitFlag.h new file mode 100644 index 0000000..8f70ef6 --- /dev/null +++ b/SDLWrapper/_MixInitFlag.h @@ -0,0 +1,12 @@ +#pragma once +#include "begin_code.h" +enum class MixInitFlag +{ + FLAC, + MOD, + MODPLUG, + MP3, + OGG, + FLUIDSYNTH +}; +#include "end_code.h" diff --git a/SDLWrapper/_MusicType.h b/SDLWrapper/_MusicType.h new file mode 100644 index 0000000..7a11c5a --- /dev/null +++ b/SDLWrapper/_MusicType.h @@ -0,0 +1,8 @@ +#pragma once +#include "include.h" +#include "begin_code.h" +enum class MusicType +{ + None,CMD,WAV,MOD,MID,OGG,MP3,MP3MAD,FLAC,MODPLUG +}; +#include "end_code.h" diff --git a/SDLWrapper/_SDLInitFlag.h b/SDLWrapper/_SDLInitFlag.h new file mode 100644 index 0000000..dfa67df --- /dev/null +++ b/SDLWrapper/_SDLInitFlag.h @@ -0,0 +1,14 @@ +#pragma once +#include "begin_code.h" +enum class SDLInitFlag +{ + Timer, + Audio, + Video, /**< SDL_INIT_VIDEO implies SDL_INIT_EVENTS */ + Joystick, /**< SDL_INIT_JOYSTICK implies SDL_INIT_EVENTS */ + Haptic, + GameController, /**< SDL_INIT_GAMECONTROLLER implies SDL_INIT_JOYSTICK */ + Events, + All +}; +#include "end_code.h" diff --git a/SDLWrapper/__Nonmoveable.h b/SDLWrapper/__Nonmoveable.h new file mode 100644 index 0000000..0d5bca4 --- /dev/null +++ b/SDLWrapper/__Nonmoveable.h @@ -0,0 +1,11 @@ +#pragma once +#include "begin_code.h" +class NonMoveable +{ +public: + NonMoveable()=default; + ~NonMoveable()=default; + NonMoveable(NonMoveable&&) =delete; + NonMoveable& operator = (NonMoveable&&)=delete; +}; +#include "end_code.h" diff --git a/SDLWrapper/__Plugin.h b/SDLWrapper/__Plugin.h new file mode 100644 index 0000000..c9430ff --- /dev/null +++ b/SDLWrapper/__Plugin.h @@ -0,0 +1,36 @@ +#pragma once +#include "begin_code.h" +namespace _internal +{ + +/// This is just an empty class. +/// Most wrapper class regard this class as a friend class. +/// You can get/set raw pointers through this class. +class Plugin +{ +public: + template + static decltype(auto) get(const T& obj) + { + return obj._get(); + } + template + static void set(T& obj,U&& value) + { + obj._set(value); + } + template + static void clear(T& obj) + { + obj._clear(); + } + + template + static void set_no_delete(T& obj,U&& value) + { + obj._set_no_delete(value); + } +}; + +} +#include "end_code.h" diff --git a/SDLWrapper/_caster.cpp b/SDLWrapper/_caster.cpp index df00204..7db951c 100644 --- a/SDLWrapper/_caster.cpp +++ b/SDLWrapper/_caster.cpp @@ -215,5 +215,131 @@ SDL_RendererFlip getSDLRendererFlipFromFlipMode(FlipMode mode) return SDL_FLIP_NONE; } } + +Uint32 getUint32FromSDLInitFlag(SDLInitFlag flag) +{ + switch(flag) + { + case SDLInitFlag::Timer: + return SDL_INIT_TIMER; + case SDLInitFlag::Audio: + return SDL_INIT_AUDIO; + case SDLInitFlag::Video: + return SDL_INIT_VIDEO; + case SDLInitFlag::Joystick: + return SDL_INIT_JOYSTICK; + case SDLInitFlag::Haptic: + return SDL_INIT_HAPTIC; + case SDLInitFlag::GameController: + return SDL_INIT_GAMECONTROLLER; + case SDLInitFlag::Events: + return SDL_INIT_EVENTS; + case SDLInitFlag::All: + return SDL_INIT_EVERYTHING; + default: + return 0; /// Return 0 by default. (Fix warning) + } +} + + +int getIntFromIMGInitFlag(IMGInitFlag flag) +{ + switch(flag) + { + case IMGInitFlag::JPG: + return IMG_INIT_JPG; + case IMGInitFlag::PNG: + return IMG_INIT_PNG; + case IMGInitFlag::TIF: + return IMG_INIT_TIF; + case IMGInitFlag::WEBP: + return IMG_INIT_WEBP; + case IMGInitFlag::ALL: + return ( IMG_INIT_JPG | IMG_INIT_PNG | IMG_INIT_TIF | IMG_INIT_WEBP ); + default: + return 0; /// Return 0 by default. (Fix warning) + } +} + +int getIntFromMixInitFlag(MixInitFlag flag) +{ + switch(flag) + { + case MixInitFlag::FLAC: + return MIX_INIT_FLAC; + case MixInitFlag::MOD: + return MIX_INIT_MOD; + case MixInitFlag::MODPLUG: + return MIX_INIT_MODPLUG; + case MixInitFlag::MP3: + return MIX_INIT_MP3; + case MixInitFlag::OGG: + return MIX_INIT_OGG; + case MixInitFlag::FLUIDSYNTH: + return MIX_INIT_FLUIDSYNTH; + default: + return 0; /// Return 0 by default. (Fix warning) + } +} + +MusicType getMusicTypeFromMixMusicType(Mix_MusicType type) +{ + switch(type) + { + case MUS_NONE: + return MusicType::None; + case MUS_CMD: + return MusicType::CMD; + case MUS_WAV: + return MusicType::WAV; + case MUS_MOD: + return MusicType::MOD; + case MUS_MID: + return MusicType::MID; + case MUS_OGG: + return MusicType::OGG; + case MUS_MP3: + return MusicType::MP3; + case MUS_MP3_MAD: + return MusicType::MP3MAD; + case MUS_FLAC: + return MusicType::FLAC; + case MUS_MODPLUG: + return MusicType::MODPLUG; + default: + return MusicType::None; + } +} + +Mix_MusicType getMixMusicTypeFromMusicType(MusicType type) +{ + switch(type) + { + case MusicType::None: + return MUS_NONE; + case MusicType::CMD: + return MUS_CMD; + case MusicType::WAV: + return MUS_WAV; + case MusicType::MOD: + return MUS_MOD; + case MusicType::MID: + return MUS_MID; + case MusicType::OGG: + return MUS_OGG; + case MusicType::MP3: + return MUS_MP3; + case MusicType::MP3MAD: + return MUS_MP3_MAD; + case MusicType::FLAC: + return MUS_FLAC; + case MusicType::MODPLUG: + return MUS_MODPLUG; + default: + return MUS_NONE; + } +} + + }/// End of namespace _internal #include "end_code.h" diff --git a/SDLWrapper/_caster.h b/SDLWrapper/_caster.h index 646fa35..4583215 100644 --- a/SDLWrapper/_caster.h +++ b/SDLWrapper/_caster.h @@ -5,6 +5,10 @@ #include "_SystemCursorType.h" #include "_FontStyle.h" #include "_FlipMode.h" +#include "_SDLInitFlag.h" +#include "_IMGInitFlag.h" +#include "_MixInitFlag.h" +#include "_MusicType.h" #include #include "begin_code.h" namespace _internal @@ -18,5 +22,10 @@ SDL_SystemCursor getSDLSystemCursorFromSystemCursorType(SystemCursorType type); int getTTFFontStyleFromFontStyle(FontStyle style); std::vector getFontStyleVecFromMixedTTFFontStyle(int Mixed_TTF_Font_Style); SDL_RendererFlip getSDLRendererFlipFromFlipMode(FlipMode mode); +Uint32 getUint32FromSDLInitFlag(SDLInitFlag flag); +int getIntFromIMGInitFlag(IMGInitFlag flag); +int getIntFromMixInitFlag(MixInitFlag flag); +MusicType getMusicTypeFromMixMusicType(Mix_MusicType); +Mix_MusicType getMixMusicTypeFromMusicType(MusicType); }/// End of namespace _internal #include "end_code.h" diff --git a/makefile_c4 b/makefile_c4 deleted file mode 100644 index 507ec04..0000000 --- a/makefile_c4 +++ /dev/null @@ -1,14 +0,0 @@ -CXXFLAGS = -std=c++14 -Wall -O2 -D__C4DROID__ -Iinclude -LDFLAGS = -LDLIBS = -lSDL2_image -lSDL2_net -ltiff -ljpeg -lpng -lz -lSDL2_ttf -lfreetype -lSDL2_mixer -lSDL2_test -lsmpeg2 -lvorbisfile -lvorbis -logg -lstdc++ -lSDL2 -lEGL -lGLESv1_CM -lGLESv2 -landroid -Wl,--no-undefined -shared - -PROG = program_name -OBJS = MiniEngine.o MiniEngine_Android.o MiniEngine_Event.o MiniEngine_Widget.o sqlite/sqlite3.o MiniEngine_SQLite.o - -all: $(PROG) - -$(PROG): $(OBJS) - $(CXX) $(CXXFLAGS) $(LDFLAGS) $(LDLIBS) -o $@ $(OBJS) `sdl2-config --cflags --libs` - -clean: - rm -f $(PROG) $(OBJS) \ No newline at end of file diff --git a/makefile_c4gen.cpp b/makefile_c4gen.cpp new file mode 100644 index 0000000..14b2219 --- /dev/null +++ b/makefile_c4gen.cpp @@ -0,0 +1,160 @@ +/// makefile_c4gen.cpp +/// Under MIT License. Part of MiniEngine Project. +/// You can run this code to generate a makefile for C4droid. + +#include +#include +/// Declaration +void FindFileRev(const std::string& dirname,const std::function& func); + +#include +#include +#include +#include +#include +#include +using namespace std; + +char buff[1024]; +int main() +{ + printf("Generator: Detecting source files...\n"); + /// Find files + vector objvec; + const string toFindCpp=".cpp"; + const string toFindC=".c"; + const string toAppendObj=".o"; + auto replaceEnd=[](const string& source,const string& replaceFrom,const string& replaceTo)->string + { + return source.substr(0,source.size()-replaceFrom.size()).append(replaceTo); + }; + auto endWith=[](const string& text,const string& testEndWith)->bool + { + return (text.substr(text.size()-testEndWith.size())==testEndWith); + }; + FindFileRev(".",[&](const std::string& name) + { + if(endWith(name,toFindCpp)) + { + objvec.push_back(replaceEnd(name,toFindCpp,toAppendObj)); + } + else if(endWith(name,toFindC)) + { + objvec.push_back(replaceEnd(name,toFindC,toAppendObj)); + } + }); + printf("Generator: Excluding files...\n"); + objvec.erase(remove_if(objvec.begin(),objvec.end(),[](const string& objname)->bool + { + return ( /// Beginning of excluding list + (objname.find("makefile_")!=string::npos && objname.find("gen.")!=string::npos) || + (objname.find("_Windows.")!=string::npos) + ); + }),objvec.end()); + printf("Generator: Generating makefile...\n"); + FILE* fp=fopen("makefile","w"); + fprintf(fp, +"CFLAGS = -Wall -s -O2 -D__LINUX__ -Iinclude -fPIC\n" +"CXXFLAGS = -std=c++14 -Wall -s -O2 -D__C4DROID__ -Iinclude\n" +"LDFLAGS =\n" +"LDLIBS = -lSDL2_image -lSDL2_net -ltiff -ljpeg -lpng -lz -lSDL2_ttf -lfreetype -lSDL2_mixer " +"-lSDL2_test -lsmpeg2 -lvorbisfile -lvorbis -logg -lstdc++ -lSDL2 -lEGL -lGLESv1_CM -lGLESv2 " +"-landroid -Wl,--no-undefined -shared\n" +"PROG = program_name\n" +"OBJS = "); + for(auto& obj:objvec) + { + fprintf(fp,"%s ",obj.c_str()); + } + fprintf(fp,"\n"); + fprintf(fp, +"all: $(PROG)\n" +"\n" +"$(PROG): $(OBJS)\n" +"\t$(CXX) $(CXXFLAGS) $(LDFLAGS) $(LDLIBS) -o $@ $(OBJS) `sdl2-config --cflags --libs`\n" +"\n" +"clean:\n" +"\trm -f $(PROG) $(OBJS)\n"); + fclose(fp); + + printf("Generator: Generation Finished.\n"); + return 0; +} + +/// Implement +#if defined(_MSC_VER) || defined(_WIN32) /// VS or Windows +#include +#include +#include +#include +void FindFileRev(const std::string& dirname,const std::function& func) +{ + std::string patternString; + if(dirname[dirname.size()-1]!='\\') + { + patternString=dirname+"\\*"; + } + else + { + patternString=dirname+"*"; + } + + WIN32_FIND_DATA fnd; + HANDLE hand=FindFirstFile(patternString.c_str(),&fnd); + if(hand!=INVALID_HANDLE_VALUE) + { + do + { + std::string fullname=dirname+fnd.cFileName; + if(fnd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) + { + fullname.append("\\"); + FindFileRev(fullname,func); + } + else + { + func(fullname); + } + } + while(FindNextFile(hand,&fnd)); + FindClose(hand); + } +} +#else /// Linux-like +#include +#include +#include +#include + +void FindFileRev(const std::string& dirname,const std::function& func) +{ + DIR* Dir = NULL; + struct dirent* file = NULL; + std::string curDir; + if (dirname[dirname.size()-1] != '/') + { + curDir=dirname+"/"; + } + else + { + curDir=dirname; + } + + if ((Dir = opendir(curDir.c_str())) == NULL) + { + return ; + } + while ((file = readdir(Dir)) != nullptr) + { + if (file->d_type == DT_REG) + { + func(curDir + file->d_name); + } + else if (file->d_type == DT_DIR && strcmp(file->d_name, ".") != 0 && strcmp(file->d_name, "..") != 0) + { + FindFileRev(curDir + file->d_name,func); + } + } + closedir(Dir); +} +#endif diff --git a/makefile_linux b/makefile_linux deleted file mode 100644 index 85a1c68..0000000 --- a/makefile_linux +++ /dev/null @@ -1,14 +0,0 @@ -CXXFLAGS = -std=c++14 -Wall -O2 -D__LINUX__ -Iinclude -LDFLAGS = -LDLIBS = -lstdc++ -lSDL2_image -lSDL2_net -lSDL2_ttf -lSDL2_mixer -lSDL2_test -lSDL2 -shared - -PROG = program_name -OBJS = MiniEngine.o MiniEngine_Android.o MiniEngine_Event.o MiniEngine_Widget.o sqlite/sqlite3.o MiniEngine_SQLite.o - -all: $(PROG) - -$(PROG): $(OBJS) - $(CXX) $(CXXFLAGS) $(LDFLAGS) $(LDLIBS) -o $@ $(OBJS) `sdl2-config --cflags --libs` - -clean: - rm -f $(PROG) $(OBJS)