Merge pull request #11 from Kiritow/pre-merge

Update to v0.7
This commit is contained in:
Kirigaya Kazuto 2017-07-07 17:49:24 +08:00 committed by GitHub
commit edefaa6b2a
33 changed files with 774 additions and 247 deletions

4
.gitattributes vendored Normal file
View File

@ -0,0 +1,4 @@
*.c linguist-language=C++
*.cpp linguist-language=C++
*.h linguist-language=C++
*.hpp linguist-language=C++

View File

@ -74,31 +74,6 @@ namespace MiniEngine
delete pimpl; 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) bool GetScanKeyState(SDL_Scancode code)
{ {
return SDL_GetKeyboardState(NULL)[code]; return SDL_GetKeyboardState(NULL)[code];
@ -130,27 +105,3 @@ bool canexecute(std::string Path)
#else /// _MINIENGINE_HAS_UNISTD == 0 #else /// _MINIENGINE_HAS_UNISTD == 0
/// File Functions will be implied in platform specific source file. /// File Functions will be implied in platform specific source file.
#endif #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;
}

View File

@ -4,7 +4,6 @@
#include <functional> #include <functional>
#include "SDLWrapper/IncludeAll.h" #include "SDLWrapper/IncludeAll.h"
namespace MiniEngine namespace MiniEngine
{ {
class StringEngine class StringEngine
@ -24,10 +23,6 @@ namespace MiniEngine
impl* pimpl; impl* pimpl;
}; };
int SetClipboardText(const std::string& str);
std::string GetClipboardText();
bool HasClipboardText();
/// Experimental - For Experts: Use SDL ScanCode /// Experimental - For Experts: Use SDL ScanCode
bool GetScanKeyState(SDL_Scancode); bool GetScanKeyState(SDL_Scancode);
@ -39,13 +34,3 @@ bool canread(std::string Path);
bool canwrite(std::string Path); bool canwrite(std::string Path);
bool isexist(std::string Path); bool isexist(std::string Path);
bool canexecute(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();

View File

@ -1,5 +1,8 @@
#include "MiniEngine_Event.h" #include "MiniEngine_Event.h"
namespace MiniEngine
{
int PollEvent(Event& refEvent) int PollEvent(Event& refEvent)
{ {
return SDL_PollEvent(&refEvent); return SDL_PollEvent(&refEvent);
@ -166,17 +169,20 @@ void Looper::dispatch()
} }
void Looper::run() void Looper::run()
{ {
while(_running) do
{ {
if(_update)
{
updater();
_update=false;
}
while(!_update&&WaitEvent(_e)) while(!_update&&WaitEvent(_e))
{ {
dispatch(); dispatch();
} }
updater();
_update=false;
} }
while(_running);
} }
Event Looper::GetLastEvent() Event Looper::GetLastEvent()
{ {
@ -186,14 +192,10 @@ void Looper::needupdate()
{ {
_update=true; _update=true;
} }
void Looper::needstop()
{
_running=false;
}
void Looper::stop() void Looper::stop()
{ {
needstop(); _running=false;
needupdate(); _update=true;
} }
void Looper::reset() void Looper::reset()
{ {
@ -228,8 +230,15 @@ void Poller::reset()
void Poller::run() void Poller::run()
{ {
int pollret=1; int pollret=1;
while(_running)
do
{ {
if(_update)
{
updater();
_update=false;
}
while(!_update&&(pollret=PollEvent(_e))) while(!_update&&(pollret=PollEvent(_e)))
{ {
dispatch(); dispatch();
@ -237,12 +246,9 @@ 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 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(!pollret) idler();
if(_update)
{
updater();
_update=false;
}
} }
while(_running);
} }
LooperWithTime::LooperWithTime(int Timeout_ms) LooperWithTime::LooperWithTime(int Timeout_ms)
@ -263,8 +269,14 @@ int LooperWithTime::getTimeout() const
void LooperWithTime::run() void LooperWithTime::run()
{ {
int timeret = 1; int timeret = 1;
while (_running) do
{ {
if (_update)
{
updater();
_update = false;
}
while (!_update&&(timeret=WaitEventTimeout(_e, _timeout_ms))) while (!_update&&(timeret=WaitEventTimeout(_e, _timeout_ms)))
{ {
dispatch(); dispatch();
@ -272,10 +284,8 @@ void LooperWithTime::run()
/// 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 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 (!timeret) idler();
if (_update)
{
updater();
_update = false;
}
} }
while (_running);
} }
}/// End of namespace MiniEngine

View File

@ -4,6 +4,9 @@
#include <list> #include <list>
#include <functional> #include <functional>
namespace MiniEngine
{
typedef SDL_Event Event; typedef SDL_Event Event;
typedef decltype(Event::type) _SDLEventType_; typedef decltype(Event::type) _SDLEventType_;
@ -63,7 +66,6 @@ public:
void run(); void run();
Event GetLastEvent(); Event GetLastEvent();
void needupdate(); void needupdate();
void needstop();
void stop(); void stop();
void reset(); void reset();
@ -97,3 +99,5 @@ public:
protected: protected:
int _timeout_ms; int _timeout_ms;
}; };
}/// End of namespace MiniEngine

View File

@ -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 (SDL2.lib, SDL2main.lib, SDL2test.lib)
> SDL2 Image (SDL2_image.lib) > SDL2 Image (SDL2_image.lib)
> SDL2 Mixer (SDL2_mixer.lib) > SDL2 Mixer (SDL2_mixer.lib)
> SDL2 TTF (SDL2_ttf.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编译本项目可能会出现某些错误,目前还没有很好的解决办法. Windows-Visual Studio: 使用VS编译本项目可能会出现某些错误,目前还没有很好的解决办法.
C4droid: 长按编译键选择编译模式为Makefile. 选择编译目标为SDL2 Application. 修改程序名称为program_name(此处与makefile对应即可) C4droid: 使用Makefile Generator生成makefile文件. 选择编译目标为SDL2 Application. 修改程序名称为program_name(与makefile对应)
> 依赖库 > 依赖库
> C4droid本体 > C4droid本体
> GCC Plugin For C4droid > GCC Plugin For C4droid

View File

@ -3,6 +3,7 @@
#include "_SystemCursorType.h" #include "_SystemCursorType.h"
#include "Point.h" #include "Point.h"
#include "Surface.h" #include "Surface.h"
#include "__Plugin.h"
#include "begin_code.h" #include "begin_code.h"
class Cursor class Cursor
{ {
@ -26,5 +27,7 @@ private:
void _set_no_delete(SDL_Cursor*); void _set_no_delete(SDL_Cursor*);
SDL_Cursor* _get(); SDL_Cursor* _get();
void _clear(); void _clear();
friend class _internal::Plugin;
}; };
#include "end_code.h" #include "end_code.h"

View File

@ -215,7 +215,9 @@ Rect Font::sizeText(const std::string& Text) const throw (ErrorViewer)
if(TTF_SizeText(_get(),Text.c_str(),&w,&h)!=0) if(TTF_SizeText(_get(),Text.c_str(),&w,&h)!=0)
{ {
/// Something might be wrong /// Something might be wrong
throw ErrorViewer(); ErrorViewer e;
e.fetch();
throw e;
} }
return Rect(0,0,w,h); 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) if(TTF_SizeUTF8(_get(),Text.c_str(),&w,&h)!=0)
{ {
/// Something might be wrong /// Something might be wrong
throw ErrorViewer(); ErrorViewer e;
e.fetch();
throw e;
} }
return Rect(0,0,w,h); 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) if(TTF_SizeUNICODE(_get(),Text,&w,&h)!=0)
{ {
/// Something might be wrong /// Something might be wrong
throw ErrorViewer(); ErrorViewer e;
e.fetch();
throw e;
} }
return Rect(0,0,w,h); return Rect(0,0,w,h);
} }

View File

@ -7,6 +7,7 @@
#include "Texture.h" #include "Texture.h"
#include "Renderer.h" #include "Renderer.h"
#include <vector> #include <vector>
#include "__Plugin.h"
#include "begin_code.h" #include "begin_code.h"
class Font class Font
{ {
@ -118,5 +119,7 @@ private:
void _set(TTF_Font*); void _set(TTF_Font*);
void _clear(); void _clear();
TTF_Font* _get() const; TTF_Font* _get() const;
friend class _internal::Plugin;
}; };
#include "end_code.h" #include "end_code.h"

View File

@ -1,4 +1,5 @@
#include "Music.h" #include "Music.h"
#include "_caster.h"
#include "begin_code.h" #include "begin_code.h"
//private //private
void Music::_set(Mix_Music* p) void Music::_set(Mix_Music* p)
@ -21,6 +22,11 @@ Music::Music(const std::string& Filename)
_set(Mix_LoadMUS(Filename.c_str())); _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 bool Music::isReady() const
{ {
return (_get()!=nullptr); return (_get()!=nullptr);
@ -31,6 +37,11 @@ void Music::release()
_clear(); _clear();
} }
MusicType Music::getType() const
{
return _internal::getMusicTypeFromMixMusicType(Mix_GetMusicType(_get()));
}
//static //static
int MusicPlayer::GetDecoderNum() int MusicPlayer::GetDecoderNum()
{ {
@ -69,6 +80,12 @@ int MusicPlayer::stop()
return Mix_HaltMusic(); 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) int MusicPlayer::fadeIn(int loops, int ms)
{ {
return Mix_FadeInMusic(m._get(), loops, ms); return Mix_FadeInMusic(m._get(), loops, ms);

View File

@ -4,7 +4,10 @@
#include <string> #include <string>
#include "ErrorViewer.h" #include "ErrorViewer.h"
#include "Audio.h" #include "Audio.h"
#include "RWOP.h"
#include "_MusicType.h"
#include "__Noncopyable.h" #include "__Noncopyable.h"
#include "__Plugin.h"
#include "begin_code.h" #include "begin_code.h"
/// Forward Declaration /// Forward Declaration
class Music class Music
@ -12,14 +15,18 @@ class Music
public: public:
Music()=default; Music()=default;
Music(const std::string& Filename); Music(const std::string& Filename);
Music(const RWOP& rwop,MusicType musicType);
bool isReady() const; bool isReady() const;
void release(); void release();
MusicType getType() const;
private: private:
std::shared_ptr<Mix_Music> _music; std::shared_ptr<Mix_Music> _music;
void _set(Mix_Music*); void _set(Mix_Music*);
void _clear(); void _clear();
Mix_Music* _get() const; Mix_Music* _get() const;
friend class MusicPlayer; friend class MusicPlayer;
friend class _internal::Plugin;
}; };
class MusicPlayer : public AudioPlayer, public NonCopyable class MusicPlayer : public AudioPlayer, public NonCopyable
@ -35,6 +42,7 @@ public:
void rewind(); void rewind();
int setPosition(double second); int setPosition(double second);
int stop(); int stop();
int fadeIn(Music music,int loops,int ms);
int fadeIn(int loops, int ms); int fadeIn(int loops, int ms);
int fadeOut(int ms); int fadeOut(int ms);

View File

@ -3,6 +3,7 @@
#include <cstdio> #include <cstdio>
#include <string> #include <string>
#include <memory> #include <memory>
#include "__Plugin.h"
#include "begin_code.h" #include "begin_code.h"
class RWOP class RWOP
{ {
@ -22,5 +23,9 @@ private:
void _set(SDL_RWops*); void _set(SDL_RWops*);
friend class Surface; friend class Surface;
friend class Renderer; friend class Renderer;
friend class Sound;
friend class Music;
friend class _internal::Plugin;
}; };
#include "end_code.h" #include "end_code.h"

View File

@ -20,11 +20,57 @@ SDL_Renderer* Renderer::_get() const
return _rnd.get(); 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<RendererType> RendererFlags) throw (ErrorViewer) Renderer::Renderer(Window& wnd,std::initializer_list<RendererType> RendererFlags) throw (ErrorViewer)
{ {
if(createRenderer(wnd,RendererFlags)!=0) 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<RendererType> Ren
return _createRenderer_Real(wnd,flag); 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) int Renderer::setColor(const RGBA& pack)
{ {
return SDL_SetRenderDrawColor(_get(), pack.r, pack.g, pack.b, pack.a); return SDL_SetRenderDrawColor(_get(), pack.r, pack.g, pack.b, pack.a);
@ -141,6 +201,47 @@ int Renderer::drawLines(const std::vector<SDL_Point>& pointvec)
return drawLines(pointvec.data(),pointvec.size()); return drawLines(pointvec.data(),pointvec.size());
} }
int Renderer::fillRects(const std::vector<Rect>& rectvec)
{
std::vector<SDL_Rect> thisvec;
for(auto& rectref:rectvec)
{
thisvec.push_back(rectref.toSDLRect());
}
return fillRects(thisvec);
}
int Renderer::drawRects(const std::vector<Rect>& rectvec)
{
std::vector<SDL_Rect> thisvec;
for(auto& rectref:rectvec)
{
thisvec.push_back(rectref.toSDLRect());
}
return drawRects(thisvec);
}
int Renderer::drawPoints(const std::vector<Point>& pointvec)
{
std::vector<SDL_Point> thisvec;
for(auto& pointref:pointvec)
{
thisvec.push_back(pointref.toSDLPoint());
}
return drawPoints(thisvec);
}
int Renderer::drawLines(const std::vector<Point>& pointvec)
{
std::vector<SDL_Point> thisvec;
for(auto& pointref:pointvec)
{
thisvec.push_back(pointref.toSDLPoint());
}
return drawLines(thisvec);
}
int Renderer::setScale(float scaleX, float scaleY) int Renderer::setScale(float scaleX, float scaleY)
{ {
return SDL_RenderSetScale(_get(),scaleX,scaleY); return SDL_RenderSetScale(_get(),scaleX,scaleY);
@ -415,38 +516,4 @@ int Renderer::GetDriversNum()
return SDL_GetNumRenderDrivers(); 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" #include "end_code.h"

View File

@ -5,13 +5,17 @@
#include "Window.h" #include "Window.h"
#include "Surface.h" #include "Surface.h"
#include "Texture.h" #include "Texture.h"
#include "__Plugin.h"
#include <initializer_list> #include <initializer_list>
#include "begin_code.h" #include "begin_code.h"
class Renderer class Renderer
{ {
public: public:
Renderer() = default; Renderer() = default;
/// Create a Renderer associated with Window
Renderer(Window& wnd,std::initializer_list<RendererType> RendererFlags = { RendererType::Accelerated,RendererType::TargetTexture }) throw (ErrorViewer); Renderer(Window& wnd,std::initializer_list<RendererType> RendererFlags = { RendererType::Accelerated,RendererType::TargetTexture }) throw (ErrorViewer);
/// Create a software Renderer
Renderer(Surface& surf) throw (ErrorViewer);
~Renderer() = default; ~Renderer() = default;
/// If Renderer is current not ready, setRenderer will create Renderer. /// If Renderer is current not ready, setRenderer will create Renderer.
@ -31,6 +35,8 @@ public:
int createRenderer(Window& wnd,std::initializer_list<RendererType>); int createRenderer(Window& wnd,std::initializer_list<RendererType>);
int createSoftRenderer(Surface& surf);
int setColor(const RGBA& pack); int setColor(const RGBA& pack);
RGBA getColor() const; RGBA getColor() const;
int setBlendMode(BlendMode mode); int setBlendMode(BlendMode mode);
@ -56,6 +62,12 @@ public:
int drawPoints(const std::vector<SDL_Point>& pointvec); int drawPoints(const std::vector<SDL_Point>& pointvec);
int drawLines(const std::vector<SDL_Point>& pointvec); int drawLines(const std::vector<SDL_Point>& pointvec);
/// Slower Functions (Need Convert First, then call Experimental Functions.)
int fillRects(const std::vector<Rect>& rectvec);
int drawRects(const std::vector<Rect>& rectvec);
int drawPoints(const std::vector<Point>& pointvec);
int drawLines(const std::vector<Point>& pointvec);
int setScale(float scaleX,float scaleY); int setScale(float scaleX,float scaleY);
std::tuple<float,float> getScale() const; std::tuple<float,float> getScale() const;
@ -134,5 +146,7 @@ private:
void _set(SDL_Renderer*); void _set(SDL_Renderer*);
void _clear(); void _clear();
SDL_Renderer* _get() const; SDL_Renderer* _get() const;
friend class _internal::Plugin;
}; };
#include "end_code.h" #include "end_code.h"

View File

@ -1,69 +1,100 @@
#include "SDLSystem.h" #include "SDLSystem.h"
#include "_caster.h"
#include "begin_code.h" #include "begin_code.h"
//static // private
int SDLSystem::SDLInit() 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;
} }
//static Uint32 uret=0;
void SDLSystem::SDLQuit() uret=IMG_Init(img_flag);
if(uret!=img_flag) /// IMG_Init returns its parameter on success.
{ {
SDL_Quit(); ErrorViewer e;
e.fetch();
throw e;
} }
//static uret=Mix_Init(mix_flag);
int SDLSystem::IMGInit() if(uret!=mix_flag) /// Mix_Init returns its parameter on success.
{ {
return IMG_Init(IMG_INIT_JPG | IMG_INIT_PNG); ErrorViewer e;
e.fetch();
throw e;
} }
//static if(init_ttf)
void SDLSystem::IMGQuit()
{ {
IMG_Quit(); ret=TTF_Init();
if(ret!=0)
{
ErrorViewer e;
e.fetch();
throw e;
}
}
} }
//static SDLSystem::SDLSystem(const std::initializer_list<SDLInitFlag>& flag_sdl,
int SDLSystem::TTFInit() const std::initializer_list<IMGInitFlag>& flag_img,
const std::initializer_list<MixInitFlag>& flag_mix,
bool init_ttf ) throw (ErrorViewer)
{ {
return TTF_Init(); 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);
} }
//static try
void SDLSystem::TTFQuit() {
_init(sdl_flag,img_flag,mix_flag,init_ttf);
}
catch(ErrorViewer& e)
{
throw e;
}
}
SDLSystem::SDLSystem(Uint32 sdl_flag, Uint32 img_flag, Uint32 mix_flag, bool init_ttf) throw (ErrorViewer)
{
try
{
_init(sdl_flag,img_flag,mix_flag,init_ttf);
}
catch(ErrorViewer& e)
{
throw e;
}
}
SDLSystem::~SDLSystem()
{
if(TTF_WasInit())
{ {
TTF_Quit(); TTF_Quit();
} }
//static
int SDLSystem::MixInit()
{
return Mix_Init(MIX_INIT_MP3);
}
//static
void SDLSystem::MixQuit()
{
Mix_Quit(); Mix_Quit();
} IMG_Quit();
SDL_Quit();
//static
void SDLSystem::Init()
{
SDLInit();
IMGInit();
TTFInit();
MixInit();
}
//static
void SDLSystem::Quit()
{
MixQuit();
TTFQuit();
IMGQuit();
SDLQuit();
} }
//static //static
@ -240,4 +271,33 @@ int SDLSystem::GetSystemRAM()
{ {
return SDL_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" #include "end_code.h"

View File

@ -2,23 +2,26 @@
#include "include.h" #include "include.h"
#include "_PowerState.h" #include "_PowerState.h"
#include "_Platform.h" #include "_Platform.h"
#include "_SDLInitFlag.h"
#include "_IMGInitFlag.h"
#include "_MixInitFlag.h"
#include "__Noncopyable.h"
#include "__Nonmoveable.h"
#include "ErrorViewer.h"
#include <tuple> #include <tuple>
#include <string> #include <string>
#include "begin_code.h" #include "begin_code.h"
class SDLSystem
class SDLSystem : public NonCopyable, public NonMoveable
{ {
public: public:
static int SDLInit(); SDLSystem(const std::initializer_list<SDLInitFlag>& flag_sdl = {SDLInitFlag::All} ,
static void SDLQuit(); const std::initializer_list<IMGInitFlag>& flag_img = {IMGInitFlag::JPG,IMGInitFlag::PNG} ,
static int IMGInit(); const std::initializer_list<MixInitFlag>& flag_mix = {MixInitFlag::MP3} ,
static void IMGQuit(); bool init_ttf = true ) throw (ErrorViewer);
static int TTFInit(); /// Experimental Constructor
static void TTFQuit(); SDLSystem(Uint32 sdl_flag, Uint32 img_flag, Uint32 mix_flag, bool init_ttf) throw (ErrorViewer);
static int MixInit(); ~SDLSystem();
static void MixQuit();
static void Init();
static void Quit();
static void Delay(int ms); static void Delay(int ms);
@ -51,6 +54,10 @@ public:
/// RAM is calculated in MB. /// RAM is calculated in MB.
static int GetSystemRAM(); static int GetSystemRAM();
static int SetClipboardText(const std::string& str);
static std::string GetClipboardText();
static bool HasClipboardText();
class Android class Android
{ {
public: public:
@ -61,5 +68,9 @@ public:
static std::string GetExternal(); static std::string GetExternal();
static void* GetJNIEnv(); static void* GetJNIEnv();
}; };
private:
void _init(Uint32,Uint32,Uint32,bool) throw (ErrorViewer);
void _quit();
}; };
#include "end_code.h" #include "end_code.h"

View File

@ -2,6 +2,7 @@
#include "include.h" #include "include.h"
#include <string> #include <string>
#include <memory> #include <memory>
#include <functional>
#include "begin_code.h" #include "begin_code.h"
class SharedLibrary class SharedLibrary
{ {

View File

@ -21,6 +21,11 @@ Sound::Sound(const std::string& WAVFilename)
_set(Mix_LoadWAV(WAVFilename.c_str())); _set(Mix_LoadWAV(WAVFilename.c_str()));
} }
Sound::Sound(const RWOP& rwop)
{
_set(Mix_LoadWAV_RW(rwop._get(),0));
}
bool Sound::isReady() const bool Sound::isReady() const
{ {
return (_get()!=nullptr); return (_get()!=nullptr);

View File

@ -4,12 +4,15 @@
#include <string> #include <string>
#include "Audio.h" #include "Audio.h"
#include "ErrorViewer.h" #include "ErrorViewer.h"
#include "RWOP.h"
#include "__Plugin.h"
#include "begin_code.h" #include "begin_code.h"
class Sound class Sound
{ {
public: public:
Sound() = default; Sound() = default;
Sound(const std::string& WAVFilename); Sound(const std::string& WAVFilename);
Sound(const RWOP& rwop);
bool isReady() const; bool isReady() const;
void release(); void release();
private: private:
@ -19,6 +22,7 @@ private:
Mix_Chunk* _get() const; Mix_Chunk* _get() const;
friend class Channel; friend class Channel;
friend class _internal::Plugin;
}; };
class Channel class Channel

View File

@ -5,6 +5,7 @@
#include "Point.h" #include "Point.h"
#include "RWOP.h" #include "RWOP.h"
#include "ErrorViewer.h" #include "ErrorViewer.h"
#include "__Plugin.h"
#include "begin_code.h" #include "begin_code.h"
class Surface class Surface
{ {
@ -81,6 +82,8 @@ private:
friend class Renderer; friend class Renderer;
friend class Font; friend class Font;
friend class Cursor; friend class Cursor;
friend class _internal::Plugin;
}; };
#include "end_code.h" #include "end_code.h"

View File

@ -4,6 +4,7 @@
#include "RGBA.h" #include "RGBA.h"
#include "ColorMode.h" #include "ColorMode.h"
#include "_BlendMode.h" #include "_BlendMode.h"
#include "__Plugin.h"
#include <memory> #include <memory>
#include "begin_code.h" #include "begin_code.h"
@ -40,6 +41,8 @@ private:
SDL_Texture* _get() const; SDL_Texture* _get() const;
Rect _rect; Rect _rect;
friend class Renderer; friend class Renderer;
friend class _internal::Plugin;
}; };
#include "end_code.h" #include "end_code.h"

View File

@ -5,6 +5,7 @@
#include "ErrorViewer.h" #include "ErrorViewer.h"
#include "MessageBox.h" #include "MessageBox.h"
#include "Surface.h" #include "Surface.h"
#include "__Plugin.h"
#include "begin_code.h" #include "begin_code.h"
class Window class Window
{ {
@ -62,5 +63,6 @@ private:
SDL_Window* _get() const; SDL_Window* _get() const;
friend class Renderer; friend class Renderer;
friend class _internal::Plugin;
}; };
#include "end_code.h" #include "end_code.h"

11
SDLWrapper/_IMGInitFlag.h Normal file
View File

@ -0,0 +1,11 @@
#pragma once
#include "begin_code.h"
enum class IMGInitFlag
{
JPG,
PNG,
TIF,
WEBP,
ALL
};
#include "end_code.h"

12
SDLWrapper/_MixInitFlag.h Normal file
View File

@ -0,0 +1,12 @@
#pragma once
#include "begin_code.h"
enum class MixInitFlag
{
FLAC,
MOD,
MODPLUG,
MP3,
OGG,
FLUIDSYNTH
};
#include "end_code.h"

8
SDLWrapper/_MusicType.h Normal file
View File

@ -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"

14
SDLWrapper/_SDLInitFlag.h Normal file
View File

@ -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"

View File

@ -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"

36
SDLWrapper/__Plugin.h Normal file
View File

@ -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<typename T>
static decltype(auto) get(const T& obj)
{
return obj._get();
}
template<typename T,typename U>
static void set(T& obj,U&& value)
{
obj._set(value);
}
template<typename T>
static void clear(T& obj)
{
obj._clear();
}
template<typename T,typename U>
static void set_no_delete(T& obj,U&& value)
{
obj._set_no_delete(value);
}
};
}
#include "end_code.h"

View File

@ -215,5 +215,131 @@ SDL_RendererFlip getSDLRendererFlipFromFlipMode(FlipMode mode)
return SDL_FLIP_NONE; 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 }/// End of namespace _internal
#include "end_code.h" #include "end_code.h"

View File

@ -5,6 +5,10 @@
#include "_SystemCursorType.h" #include "_SystemCursorType.h"
#include "_FontStyle.h" #include "_FontStyle.h"
#include "_FlipMode.h" #include "_FlipMode.h"
#include "_SDLInitFlag.h"
#include "_IMGInitFlag.h"
#include "_MixInitFlag.h"
#include "_MusicType.h"
#include <vector> #include <vector>
#include "begin_code.h" #include "begin_code.h"
namespace _internal namespace _internal
@ -18,5 +22,10 @@ SDL_SystemCursor getSDLSystemCursorFromSystemCursorType(SystemCursorType type);
int getTTFFontStyleFromFontStyle(FontStyle style); int getTTFFontStyleFromFontStyle(FontStyle style);
std::vector<FontStyle> getFontStyleVecFromMixedTTFFontStyle(int Mixed_TTF_Font_Style); std::vector<FontStyle> getFontStyleVecFromMixedTTFFontStyle(int Mixed_TTF_Font_Style);
SDL_RendererFlip getSDLRendererFlipFromFlipMode(FlipMode mode); 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 }/// End of namespace _internal
#include "end_code.h" #include "end_code.h"

View File

@ -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)

160
makefile_c4gen.cpp Normal file
View File

@ -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 <string>
#include <functional>
/// Declaration
void FindFileRev(const std::string& dirname,const std::function<void(const std::string&)>& func);
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
char buff[1024];
int main()
{
printf("Generator: Detecting source files...\n");
/// Find files
vector<string> 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 <windows.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
void FindFileRev(const std::string& dirname,const std::function<void(const std::string&)>& 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 <dirent.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
void FindFileRev(const std::string& dirname,const std::function<void(const std::string&)>& 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

View File

@ -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)