From 3e2479e216bdcb149f33ad3b8928134544b4382f Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Fri, 5 May 2017 09:02:39 +0800 Subject: [PATCH 1/6] Add void timer function support. (without metaprogramming) --- MiniEngine.h | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/MiniEngine.h b/MiniEngine.h index 6412c3b..eca3106 100644 --- a/MiniEngine.h +++ b/MiniEngine.h @@ -467,11 +467,21 @@ namespace MiniEngine { public: Timer(); - /// Uint32 func(Uint32,void*) ... + + /// void func(Uint32,...) + template + Timer(Uint32 interval,VoidCallable&& vcallable,Args&&... args) : Timer() + { + auto realCall=[&](Uint32 ims)->Uint32{vcallable(ims,args...);return interval;}; + auto pfunc=new std::function(realCall); + _real_timer_call(_global_timer_executor,interval,pfunc); + } + + /// Uint32 func(Uint32,...) template 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(realCall); _real_timer_call(_global_timer_executor,interval,pfunc); } From a33a27dbb07c48ff4fd5a1f73d495596fb5cd9d3 Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Fri, 5 May 2017 17:28:11 +0800 Subject: [PATCH 2/6] Add More Event Handling Functions. Remove Debug Message in _global_timer_executor... --- MiniEngine.cpp | 1 - MiniEngine_Event.cpp | 15 +++++++++++++++ MiniEngine_Event.h | 5 ++++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/MiniEngine.cpp b/MiniEngine.cpp index 63c4618..9c8d33b 100644 --- a/MiniEngine.cpp +++ b/MiniEngine.cpp @@ -1404,7 +1404,6 @@ namespace MiniEngine /// Global Executor For class Timer Uint32 _global_timer_executor(Uint32 interval,void* param) { - printf("DEBUG: Global Timer Executor.\n"); auto p=reinterpret_cast*>(param); return (*p)(interval); } diff --git a/MiniEngine_Event.cpp b/MiniEngine_Event.cpp index 6f39595..d5ed6d8 100644 --- a/MiniEngine_Event.cpp +++ b/MiniEngine_Event.cpp @@ -20,6 +20,21 @@ int PushEvent(const Event& refEvent) return SDL_PushEvent(const_cast(&refEvent)); } +void PumpEvents() +{ + SDL_PumpEvents(); +} + +bool HasEvent(decltype(Event::type) EventType) +{ + return ( SDL_HasEvent(EventType)==SDL_TRUE ); +} + +bool HasEvent(decltype(Event::type) EventTypeMin,decltype(Event::type) EventTypeMax) +{ + return ( SDL_HasEvents(EventTypeMin,EventTypeMax)==SDL_TRUE ); +} + 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 4884bd5..8074765 100644 --- a/MiniEngine_Event.h +++ b/MiniEngine_Event.h @@ -9,6 +9,9 @@ int PollEvent(Event& refEvent); int WaitEvent(Event& refEvent); int WaitEventTimeout(Event& refEvent,int ms); int PushEvent(const Event& refEvent); +void PumpEvents(); +bool HasEvent(decltype(Event::type) EventType); +bool HasEvent(decltype(Event::type) EventTypeMin,decltype(Event::type) EventTypeMax); typedef struct { @@ -86,4 +89,4 @@ public: void run(); protected: int _timeout_ms; -}; \ No newline at end of file +}; From 7cab64132dfe7f5a48f7d27c53cf85004debf5ac Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Mon, 8 May 2017 14:17:42 +0800 Subject: [PATCH 3/6] Add more support for Event Handling. --- MiniEngine_Event.cpp | 5 +++++ MiniEngine_Event.h | 4 ++++ 2 files changed, 9 insertions(+) diff --git a/MiniEngine_Event.cpp b/MiniEngine_Event.cpp index d5ed6d8..b3e38db 100644 --- a/MiniEngine_Event.cpp +++ b/MiniEngine_Event.cpp @@ -40,6 +40,11 @@ bool operator == (const LooperID& a,const LooperID& b) 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() { _update=_running=true; diff --git a/MiniEngine_Event.h b/MiniEngine_Event.h index 8074765..0d000e5 100644 --- a/MiniEngine_Event.h +++ b/MiniEngine_Event.h @@ -12,6 +12,9 @@ int PushEvent(const Event& refEvent); void PumpEvents(); bool HasEvent(decltype(Event::type) EventType); bool HasEvent(decltype(Event::type) EventTypeMin,decltype(Event::type) EventTypeMax); +bool EnableEvent(decltype(Event::type) EventType); +bool DisableEvent(decltype(Event::type) EventType); +bool IsEventEnabled(decltype(Event::type) EventType); typedef struct { @@ -20,6 +23,7 @@ typedef struct }LooperID; bool operator == (const LooperID&,const LooperID&); +bool operator != (const LooperID&,const LooperID&); class Looper { From bb563d069d453bee1aacc0bbdcc9a55eadc45b0b Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Mon, 8 May 2017 15:42:54 +0800 Subject: [PATCH 4/6] Change decltype to typedef. --- MiniEngine_Event.cpp | 4 ++-- MiniEngine_Event.h | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/MiniEngine_Event.cpp b/MiniEngine_Event.cpp index b3e38db..fe2ea5d 100644 --- a/MiniEngine_Event.cpp +++ b/MiniEngine_Event.cpp @@ -25,12 +25,12 @@ void PumpEvents() SDL_PumpEvents(); } -bool HasEvent(decltype(Event::type) EventType) +bool HasEvent(_SDLEventType_ EventType) { return ( SDL_HasEvent(EventType)==SDL_TRUE ); } -bool HasEvent(decltype(Event::type) EventTypeMin,decltype(Event::type) EventTypeMax) +bool HasEvent(_SDLEventType_ EventTypeMin,_SDLEventType_ EventTypeMax) { return ( SDL_HasEvents(EventTypeMin,EventTypeMax)==SDL_TRUE ); } diff --git a/MiniEngine_Event.h b/MiniEngine_Event.h index 0d000e5..9a136db 100644 --- a/MiniEngine_Event.h +++ b/MiniEngine_Event.h @@ -4,21 +4,22 @@ #include typedef SDL_Event Event; +typedef decltype(Event::type) _SDLEventType_; int PollEvent(Event& refEvent); int WaitEvent(Event& refEvent); int WaitEventTimeout(Event& refEvent,int ms); int PushEvent(const Event& refEvent); void PumpEvents(); -bool HasEvent(decltype(Event::type) EventType); -bool HasEvent(decltype(Event::type) EventTypeMin,decltype(Event::type) EventTypeMax); -bool EnableEvent(decltype(Event::type) EventType); -bool DisableEvent(decltype(Event::type) EventType); -bool IsEventEnabled(decltype(Event::type) EventType); +bool HasEvent(_SDLEventType_ EventType); +bool HasEvent(_SDLEventType_ EventTypeMin,_SDLEventType_ EventTypeMax); +bool EnableEvent(_SDLEventType_ EventType); +bool DisableEvent(_SDLEventType_ EventType); +bool IsEventEnabled(_SDLEventType_ EventType); typedef struct { - decltype(Event::type) _type_id; + _SDLEventType_ _type_id; int _looper_cnt; }LooperID; @@ -28,7 +29,6 @@ bool operator != (const LooperID&,const LooperID&); class Looper { public: - typedef decltype(Event::type) _SDLEventType_; Looper(); /// If Callback does not return 0, then stop transferring this event. LooperID add(_SDLEventType_ event_type,const std::function& event_callback); From d8d77a5bd2dfcd2569cfdd2eba9e5c0dd2831606 Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Tue, 9 May 2017 10:53:02 +0800 Subject: [PATCH 5/6] Add more functions to class Rect --- MiniEngine.cpp | 45 ++++++++++++++++++++++++++++++++++++++++++++- MiniEngine.h | 8 +++++++- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/MiniEngine.cpp b/MiniEngine.cpp index 9c8d33b..4b23056 100644 --- a/MiniEngine.cpp +++ b/MiniEngine.cpp @@ -126,12 +126,17 @@ namespace MiniEngine h = H; } + Rect::Rect(const SDL_Rect& r):Rect(r.x,r.y,r.w,r.h) + { + + } + Rect::Rect() { x = y = w = h = 0; } - SDL_Rect Rect::toSDLRect() + SDL_Rect Rect::toSDLRect() const { SDL_Rect r; r.x = x; @@ -141,6 +146,44 @@ namespace MiniEngine 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) { x = X; diff --git a/MiniEngine.h b/MiniEngine.h index eca3106..387e007 100644 --- a/MiniEngine.h +++ b/MiniEngine.h @@ -33,8 +33,14 @@ namespace MiniEngine public: int x, y, w, h; Rect(int X, int Y, int W, int H); + Rect(const SDL_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 From d77c894adcf98ee9894eb4a9765d0b40908c3585 Mon Sep 17 00:00:00 2001 From: kiritow <1362050620@qq.com> Date: Thu, 11 May 2017 11:53:26 +0800 Subject: [PATCH 6/6] Add SharedLibrary Support --- MiniEngine.cpp | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ MiniEngine.h | 13 +++++++++++++ 2 files changed, 61 insertions(+) diff --git a/MiniEngine.cpp b/MiniEngine.cpp index 4b23056..283bcd6 100644 --- a/MiniEngine.cpp +++ b/MiniEngine.cpp @@ -1310,6 +1310,54 @@ namespace MiniEngine 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() { return SDL_Init(SDL_INIT_EVERYTHING); diff --git a/MiniEngine.h b/MiniEngine.h index 387e007..4f09bc4 100644 --- a/MiniEngine.h +++ b/MiniEngine.h @@ -429,6 +429,19 @@ namespace MiniEngine 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 { public: