Merge pull request #7 from Kiritow/pre-merge

Weekly Update
This commit is contained in:
Kirigaya Kazuto 2017-05-14 19:24:54 +08:00 committed by GitHub
commit 7c22fb544d
4 changed files with 154 additions and 8 deletions

View File

@ -126,12 +126,17 @@ namespace MiniEngine
h = H; h = H;
} }
Rect::Rect(const SDL_Rect& r):Rect(r.x,r.y,r.w,r.h)
{
}
Rect::Rect() Rect::Rect()
{ {
x = y = w = h = 0; x = y = w = h = 0;
} }
SDL_Rect Rect::toSDLRect() SDL_Rect Rect::toSDLRect() const
{ {
SDL_Rect r; SDL_Rect r;
r.x = x; r.x = x;
@ -141,6 +146,44 @@ namespace MiniEngine
return r; return r;
} }
bool Rect::isEmpty()
{
SDL_Rect r=toSDLRect();
return SDL_RectEmpty(&r)==SDL_TRUE;
}
bool Rect::operator == (const Rect& r) const
{
SDL_Rect a=toSDLRect(),b=r.toSDLRect();
return SDL_RectEquals(&a,&b)==SDL_TRUE;
}
bool Rect::hasIntersection(const Rect& r)
{
SDL_Rect a=toSDLRect(),b=r.toSDLRect();
return SDL_HasIntersection(&a,&b)==SDL_TRUE;
}
Rect Rect::getIntersection(const Rect& r)
{
SDL_Rect a=toSDLRect(),b=r.toSDLRect(),c;
if(SDL_IntersectRect(&a,&b,&c)==SDL_TRUE)
{
return Rect(c);
}
else
{
return Rect();
}
}
Rect Rect::getUnion(const Rect& r)
{
SDL_Rect a=toSDLRect(),b=r.toSDLRect(),c;
SDL_UnionRect(&a,&b,&c);//void
return Rect(c);
}
Point::Point(int X, int Y) Point::Point(int X, int Y)
{ {
x = X; x = X;
@ -1267,6 +1310,54 @@ namespace MiniEngine
va_end(ap); va_end(ap);
} }
SharedLibrary::SharedLibrary()
{
_obj=nullptr;
}
SharedLibrary::SharedLibrary(const std::string& Filename)
{
_obj=nullptr;
load(Filename);
}
SharedLibrary::~SharedLibrary()
{
if(_obj)
{
unload();
}
}
int SharedLibrary::load(const std::string& Filename)
{
if(_obj) return -1;
else
{
_obj=SDL_LoadObject(Filename.c_str());
if(_obj) return 0;
else return -2;
}
}
int SharedLibrary::unload()
{
if(_obj)
{
SDL_UnloadObject(_obj);
_obj=nullptr;
return 0;
}
else return -1;
}
void* SharedLibrary::get(const std::string& FunctionName)
{
if(!_obj) return nullptr;
else return SDL_LoadFunction(_obj,FunctionName.c_str());
}
int SDLSystem::SDLInit() int SDLSystem::SDLInit()
{ {
return SDL_Init(SDL_INIT_EVERYTHING); return SDL_Init(SDL_INIT_EVERYTHING);
@ -1404,7 +1495,6 @@ namespace MiniEngine
/// Global Executor For class Timer /// Global Executor For class Timer
Uint32 _global_timer_executor(Uint32 interval,void* param) Uint32 _global_timer_executor(Uint32 interval,void* param)
{ {
printf("DEBUG: Global Timer Executor.\n");
auto p=reinterpret_cast<std::function<Uint32(Uint32 interval)>*>(param); auto p=reinterpret_cast<std::function<Uint32(Uint32 interval)>*>(param);
return (*p)(interval); return (*p)(interval);
} }

View File

@ -33,8 +33,14 @@ namespace MiniEngine
public: public:
int x, y, w, h; int x, y, w, h;
Rect(int X, int Y, int W, int H); Rect(int X, int Y, int W, int H);
Rect(const SDL_Rect&);
Rect(); Rect();
SDL_Rect toSDLRect(); SDL_Rect toSDLRect() const;
bool isEmpty();
bool operator == (const Rect&) const;
bool hasIntersection(const Rect&);
Rect getIntersection(const Rect&);
Rect getUnion(const Rect&);
}; };
class Point class Point
@ -423,6 +429,19 @@ namespace MiniEngine
static void critical(const char* fmt,...);/// Critical static void critical(const char* fmt,...);/// Critical
}; };
class SharedLibrary
{
public:
SharedLibrary();
SharedLibrary(const std::string& Filename);
~SharedLibrary();
int load(const std::string& Filename);
int unload();
void* get(const std::string& FunctionName);
private:
void* _obj;
};
class SDLSystem class SDLSystem
{ {
public: public:
@ -467,11 +486,21 @@ namespace MiniEngine
{ {
public: public:
Timer(); Timer();
/// Uint32 func(Uint32,void*) ...
/// void func(Uint32,...)
template<typename VoidCallable,typename... Args>
Timer(Uint32 interval,VoidCallable&& vcallable,Args&&... args) : Timer()
{
auto realCall=[&](Uint32 ims)->Uint32{vcallable(ims,args...);return interval;};
auto pfunc=new std::function<Uint32(Uint32 interval)>(realCall);
_real_timer_call(_global_timer_executor,interval,pfunc);
}
/// Uint32 func(Uint32,...)
template<typename Callable,typename... Args> template<typename Callable,typename... Args>
Timer(Callable&& callable,Uint32 interval,Args&&... args) : Timer() Timer(Callable&& callable,Uint32 interval,Args&&... args) : Timer()
{ {
auto realCall=[&](Uint32 interval)->Uint32{return callable(interval,args...);}; auto realCall=[&](Uint32 ims)->Uint32{return callable(ims,args...);};
auto pfunc=new std::function<Uint32(Uint32 interval)>(realCall); auto pfunc=new std::function<Uint32(Uint32 interval)>(realCall);
_real_timer_call(_global_timer_executor,interval,pfunc); _real_timer_call(_global_timer_executor,interval,pfunc);
} }

View File

@ -20,11 +20,31 @@ int PushEvent(const Event& refEvent)
return SDL_PushEvent(const_cast<Event*>(&refEvent)); return SDL_PushEvent(const_cast<Event*>(&refEvent));
} }
void PumpEvents()
{
SDL_PumpEvents();
}
bool HasEvent(_SDLEventType_ EventType)
{
return ( SDL_HasEvent(EventType)==SDL_TRUE );
}
bool HasEvent(_SDLEventType_ EventTypeMin,_SDLEventType_ EventTypeMax)
{
return ( SDL_HasEvents(EventTypeMin,EventTypeMax)==SDL_TRUE );
}
bool operator == (const LooperID& a,const LooperID& b) bool operator == (const LooperID& a,const LooperID& b)
{ {
return a._type_id==b._type_id && a._looper_cnt==b._looper_cnt ; return a._type_id==b._type_id && a._looper_cnt==b._looper_cnt ;
} }
bool operator != (const LooperID& a,const LooperID& b)
{
return !(a==b);
}
Looper::Looper() Looper::Looper()
{ {
_update=_running=true; _update=_running=true;

View File

@ -4,24 +4,31 @@
#include <list> #include <list>
typedef SDL_Event Event; typedef SDL_Event Event;
typedef decltype(Event::type) _SDLEventType_;
int PollEvent(Event& refEvent); int PollEvent(Event& refEvent);
int WaitEvent(Event& refEvent); int WaitEvent(Event& refEvent);
int WaitEventTimeout(Event& refEvent,int ms); int WaitEventTimeout(Event& refEvent,int ms);
int PushEvent(const Event& refEvent); int PushEvent(const Event& refEvent);
void PumpEvents();
bool HasEvent(_SDLEventType_ EventType);
bool HasEvent(_SDLEventType_ EventTypeMin,_SDLEventType_ EventTypeMax);
bool EnableEvent(_SDLEventType_ EventType);
bool DisableEvent(_SDLEventType_ EventType);
bool IsEventEnabled(_SDLEventType_ EventType);
typedef struct typedef struct
{ {
decltype(Event::type) _type_id; _SDLEventType_ _type_id;
int _looper_cnt; int _looper_cnt;
}LooperID; }LooperID;
bool operator == (const LooperID&,const LooperID&); bool operator == (const LooperID&,const LooperID&);
bool operator != (const LooperID&,const LooperID&);
class Looper class Looper
{ {
public: public:
typedef decltype(Event::type) _SDLEventType_;
Looper(); Looper();
/// If Callback does not return 0, then stop transferring this event. /// If Callback does not return 0, then stop transferring this event.
LooperID add(_SDLEventType_ event_type,const std::function<int(Looper&,Event&)>& event_callback); LooperID add(_SDLEventType_ event_type,const std::function<int(Looper&,Event&)>& event_callback);
@ -86,4 +93,4 @@ public:
void run(); void run();
protected: protected:
int _timeout_ms; int _timeout_ms;
}; };