Skip to content

Commit 7c22fb5

Browse files
authored
Merge pull request #7 from Kiritow/pre-merge
Weekly Update
2 parents 9d9ba52 + d77c894 commit 7c22fb5

4 files changed

Lines changed: 154 additions & 8 deletions

File tree

MiniEngine.cpp

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,17 @@ namespace MiniEngine
126126
h = H;
127127
}
128128

129+
Rect::Rect(const SDL_Rect& r):Rect(r.x,r.y,r.w,r.h)
130+
{
131+
132+
}
133+
129134
Rect::Rect()
130135
{
131136
x = y = w = h = 0;
132137
}
133138

134-
SDL_Rect Rect::toSDLRect()
139+
SDL_Rect Rect::toSDLRect() const
135140
{
136141
SDL_Rect r;
137142
r.x = x;
@@ -141,6 +146,44 @@ namespace MiniEngine
141146
return r;
142147
}
143148

149+
bool Rect::isEmpty()
150+
{
151+
SDL_Rect r=toSDLRect();
152+
return SDL_RectEmpty(&r)==SDL_TRUE;
153+
}
154+
155+
bool Rect::operator == (const Rect& r) const
156+
{
157+
SDL_Rect a=toSDLRect(),b=r.toSDLRect();
158+
return SDL_RectEquals(&a,&b)==SDL_TRUE;
159+
}
160+
161+
bool Rect::hasIntersection(const Rect& r)
162+
{
163+
SDL_Rect a=toSDLRect(),b=r.toSDLRect();
164+
return SDL_HasIntersection(&a,&b)==SDL_TRUE;
165+
}
166+
167+
Rect Rect::getIntersection(const Rect& r)
168+
{
169+
SDL_Rect a=toSDLRect(),b=r.toSDLRect(),c;
170+
if(SDL_IntersectRect(&a,&b,&c)==SDL_TRUE)
171+
{
172+
return Rect(c);
173+
}
174+
else
175+
{
176+
return Rect();
177+
}
178+
}
179+
180+
Rect Rect::getUnion(const Rect& r)
181+
{
182+
SDL_Rect a=toSDLRect(),b=r.toSDLRect(),c;
183+
SDL_UnionRect(&a,&b,&c);//void
184+
return Rect(c);
185+
}
186+
144187
Point::Point(int X, int Y)
145188
{
146189
x = X;
@@ -1267,6 +1310,54 @@ namespace MiniEngine
12671310
va_end(ap);
12681311
}
12691312

1313+
SharedLibrary::SharedLibrary()
1314+
{
1315+
_obj=nullptr;
1316+
}
1317+
1318+
SharedLibrary::SharedLibrary(const std::string& Filename)
1319+
{
1320+
_obj=nullptr;
1321+
load(Filename);
1322+
}
1323+
1324+
SharedLibrary::~SharedLibrary()
1325+
{
1326+
if(_obj)
1327+
{
1328+
unload();
1329+
}
1330+
}
1331+
1332+
int SharedLibrary::load(const std::string& Filename)
1333+
{
1334+
if(_obj) return -1;
1335+
else
1336+
{
1337+
_obj=SDL_LoadObject(Filename.c_str());
1338+
if(_obj) return 0;
1339+
else return -2;
1340+
}
1341+
}
1342+
1343+
int SharedLibrary::unload()
1344+
{
1345+
if(_obj)
1346+
{
1347+
SDL_UnloadObject(_obj);
1348+
_obj=nullptr;
1349+
return 0;
1350+
}
1351+
else return -1;
1352+
}
1353+
1354+
void* SharedLibrary::get(const std::string& FunctionName)
1355+
{
1356+
if(!_obj) return nullptr;
1357+
else return SDL_LoadFunction(_obj,FunctionName.c_str());
1358+
}
1359+
1360+
12701361
int SDLSystem::SDLInit()
12711362
{
12721363
return SDL_Init(SDL_INIT_EVERYTHING);
@@ -1404,7 +1495,6 @@ namespace MiniEngine
14041495
/// Global Executor For class Timer
14051496
Uint32 _global_timer_executor(Uint32 interval,void* param)
14061497
{
1407-
printf("DEBUG: Global Timer Executor.\n");
14081498
auto p=reinterpret_cast<std::function<Uint32(Uint32 interval)>*>(param);
14091499
return (*p)(interval);
14101500
}

MiniEngine.h

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,14 @@ namespace MiniEngine
3333
public:
3434
int x, y, w, h;
3535
Rect(int X, int Y, int W, int H);
36+
Rect(const SDL_Rect&);
3637
Rect();
37-
SDL_Rect toSDLRect();
38+
SDL_Rect toSDLRect() const;
39+
bool isEmpty();
40+
bool operator == (const Rect&) const;
41+
bool hasIntersection(const Rect&);
42+
Rect getIntersection(const Rect&);
43+
Rect getUnion(const Rect&);
3844
};
3945

4046
class Point
@@ -423,6 +429,19 @@ namespace MiniEngine
423429
static void critical(const char* fmt,...);/// Critical
424430
};
425431

432+
class SharedLibrary
433+
{
434+
public:
435+
SharedLibrary();
436+
SharedLibrary(const std::string& Filename);
437+
~SharedLibrary();
438+
int load(const std::string& Filename);
439+
int unload();
440+
void* get(const std::string& FunctionName);
441+
private:
442+
void* _obj;
443+
};
444+
426445
class SDLSystem
427446
{
428447
public:
@@ -467,11 +486,21 @@ namespace MiniEngine
467486
{
468487
public:
469488
Timer();
470-
/// Uint32 func(Uint32,void*) ...
489+
490+
/// void func(Uint32,...)
491+
template<typename VoidCallable,typename... Args>
492+
Timer(Uint32 interval,VoidCallable&& vcallable,Args&&... args) : Timer()
493+
{
494+
auto realCall=[&](Uint32 ims)->Uint32{vcallable(ims,args...);return interval;};
495+
auto pfunc=new std::function<Uint32(Uint32 interval)>(realCall);
496+
_real_timer_call(_global_timer_executor,interval,pfunc);
497+
}
498+
499+
/// Uint32 func(Uint32,...)
471500
template<typename Callable,typename... Args>
472501
Timer(Callable&& callable,Uint32 interval,Args&&... args) : Timer()
473502
{
474-
auto realCall=[&](Uint32 interval)->Uint32{return callable(interval,args...);};
503+
auto realCall=[&](Uint32 ims)->Uint32{return callable(ims,args...);};
475504
auto pfunc=new std::function<Uint32(Uint32 interval)>(realCall);
476505
_real_timer_call(_global_timer_executor,interval,pfunc);
477506
}

MiniEngine_Event.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,31 @@ int PushEvent(const Event& refEvent)
2020
return SDL_PushEvent(const_cast<Event*>(&refEvent));
2121
}
2222

23+
void PumpEvents()
24+
{
25+
SDL_PumpEvents();
26+
}
27+
28+
bool HasEvent(_SDLEventType_ EventType)
29+
{
30+
return ( SDL_HasEvent(EventType)==SDL_TRUE );
31+
}
32+
33+
bool HasEvent(_SDLEventType_ EventTypeMin,_SDLEventType_ EventTypeMax)
34+
{
35+
return ( SDL_HasEvents(EventTypeMin,EventTypeMax)==SDL_TRUE );
36+
}
37+
2338
bool operator == (const LooperID& a,const LooperID& b)
2439
{
2540
return a._type_id==b._type_id && a._looper_cnt==b._looper_cnt ;
2641
}
2742

43+
bool operator != (const LooperID& a,const LooperID& b)
44+
{
45+
return !(a==b);
46+
}
47+
2848
Looper::Looper()
2949
{
3050
_update=_running=true;

MiniEngine_Event.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,31 @@
44
#include <list>
55

66
typedef SDL_Event Event;
7+
typedef decltype(Event::type) _SDLEventType_;
78

89
int PollEvent(Event& refEvent);
910
int WaitEvent(Event& refEvent);
1011
int WaitEventTimeout(Event& refEvent,int ms);
1112
int PushEvent(const Event& refEvent);
13+
void PumpEvents();
14+
bool HasEvent(_SDLEventType_ EventType);
15+
bool HasEvent(_SDLEventType_ EventTypeMin,_SDLEventType_ EventTypeMax);
16+
bool EnableEvent(_SDLEventType_ EventType);
17+
bool DisableEvent(_SDLEventType_ EventType);
18+
bool IsEventEnabled(_SDLEventType_ EventType);
1219

1320
typedef struct
1421
{
15-
decltype(Event::type) _type_id;
22+
_SDLEventType_ _type_id;
1623
int _looper_cnt;
1724
}LooperID;
1825

1926
bool operator == (const LooperID&,const LooperID&);
27+
bool operator != (const LooperID&,const LooperID&);
2028

2129
class Looper
2230
{
2331
public:
24-
typedef decltype(Event::type) _SDLEventType_;
2532
Looper();
2633
/// If Callback does not return 0, then stop transferring this event.
2734
LooperID add(_SDLEventType_ event_type,const std::function<int(Looper&,Event&)>& event_callback);
@@ -86,4 +93,4 @@ class LooperWithTime : public Poller
8693
void run();
8794
protected:
8895
int _timeout_ms;
89-
};
96+
};

0 commit comments

Comments
 (0)