mirror of
https://github.com/Kiritow/MiniEngine.git
synced 2024-03-22 13:11:22 +08:00
commit
7c22fb544d
|
@ -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;
|
||||
|
@ -1267,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);
|
||||
|
@ -1404,7 +1495,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<std::function<Uint32(Uint32 interval)>*>(param);
|
||||
return (*p)(interval);
|
||||
}
|
||||
|
|
35
MiniEngine.h
35
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
|
||||
|
@ -423,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:
|
||||
|
@ -467,11 +486,21 @@ namespace MiniEngine
|
|||
{
|
||||
public:
|
||||
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>
|
||||
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);
|
||||
_real_timer_call(_global_timer_executor,interval,pfunc);
|
||||
}
|
||||
|
|
|
@ -20,11 +20,31 @@ int PushEvent(const 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)
|
||||
{
|
||||
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;
|
||||
|
|
|
@ -4,24 +4,31 @@
|
|||
#include <list>
|
||||
|
||||
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(_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;
|
||||
|
||||
bool operator == (const LooperID&,const LooperID&);
|
||||
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<int(Looper&,Event&)>& event_callback);
|
||||
|
@ -86,4 +93,4 @@ public:
|
|||
void run();
|
||||
protected:
|
||||
int _timeout_ms;
|
||||
};
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue
Block a user