Fix compile warning generated by VC.

Add LooperWithTime.
Fix Bug: Change Lambda Capture from "&" (Keep Reference) to "=" (Copy).

Before this fix, the following code will cause a runtime error:

int AppMain()
{
	Window wnd("Title", 1280, 768);
	Renderer rnd = wnd.getRenderer();

	Looper lp;
	lp + make_pair(SDL_QUIT, []() {printf("Quit!\n");});
	lp.run();

	return 0;
}

Now, the bug is fixed~!
This commit is contained in:
Kirigaya Kazuto 2017-05-02 22:38:37 +08:00
parent 2ecddc44e7
commit f4c629393b
4 changed files with 75 additions and 43 deletions

View File

@ -164,7 +164,7 @@ namespace MiniEngine
{
auto p = toSDLPoint();
auto r = rect.toSDLRect();
return SDL_PointInRect(&p, &r);
return ( SDL_PointInRect(&p, &r) == SDL_TRUE );
}
ColorMode::ColorMode(int R, int G, int B)
@ -1582,12 +1582,12 @@ namespace MiniEngine
bool MusicPlayer::isPlaying()
{
return Mix_PlayingMusic();
return (Mix_PlayingMusic() == 1);
}
bool MusicPlayer::isPaused()
{
return Mix_PausedMusic();
return (Mix_PausedMusic() == 1);
}
int MusicPlayer::isFading()

View File

@ -7,6 +7,9 @@
#include <SDL_image.h>
#include <SDL_ttf.h>
#include <SDL_mixer.h>
/// VC++ does not implied C++ exception. Use this to ignore compile warning on this.
#pragma warning (disable:4290)
#else
/// CodeBlocks (MinGW Compiler)
#include <SDL2/SDL.h>

View File

@ -34,67 +34,39 @@ Looper::Looper()
LooperID Looper::add(_SDLEventType_ event_type,const std::function<int(Looper&,Event&)>& event_callback)
{
_evmap[event_type].push_front(std::make_pair(_loop_cnt,event_callback));
LooperID id;
id._looper_cnt=_loop_cnt;
id._type_id=event_type;
++_loop_cnt;
return id;
return _getNextID(event_type);
}
LooperID Looper::add(_SDLEventType_ event_type,const std::function<int(Event&)>& event_callback)
{
_evmap[event_type].push_front(std::make_pair(_loop_cnt,[&](Looper& lp,Event& ev)->int{return event_callback(ev);}));
LooperID id;
id._looper_cnt=_loop_cnt;
id._type_id=event_type;
++_loop_cnt;
return id;
_evmap[event_type].push_front(std::make_pair(_loop_cnt,[=](Looper& lp,Event& ev)->int{return event_callback(ev);}));
return _getNextID(event_type);
}
LooperID Looper::add(_SDLEventType_ event_type,const std::function<int(Looper&)>& event_callback)
{
_evmap[event_type].push_front(std::make_pair(_loop_cnt,[&](Looper& lp,Event& ev)->int{return event_callback(lp);}));
LooperID id;
id._looper_cnt=_loop_cnt;
id._type_id=event_type;
++_loop_cnt;
return id;
_evmap[event_type].push_front(std::make_pair(_loop_cnt,[=](Looper& lp,Event& ev)->int{return event_callback(lp);}));
return _getNextID(event_type);
}
LooperID Looper::add(_SDLEventType_ event_type,const std::function<int()>& event_callback)
{
_evmap[event_type].push_front(std::make_pair(_loop_cnt,[&](Looper& lp,Event& ev)->int{return event_callback();}));
LooperID id;
id._looper_cnt=_loop_cnt;
id._type_id=event_type;
++_loop_cnt;
return id;
_evmap[event_type].push_front(std::make_pair(_loop_cnt,[=](Looper& lp,Event& ev)->int{return event_callback();}));
return _getNextID(event_type);
}
LooperID Looper::add(_SDLEventType_ event_type,const std::function<void(Looper&,Event&)>& event_callback)
{
return add(event_type,std::function<int(Looper&,Event&)>([&](Looper& lp,Event& ev)->int{event_callback(lp,ev); return 0;}));
return add(event_type,std::function<int(Looper&,Event&)>([=](Looper& lp,Event& ev)->int{event_callback(lp,ev); return 0;}));
}
LooperID Looper::add(_SDLEventType_ event_type,const std::function<void(Event&)>& event_callback)
{
return add(event_type,std::function<int(Looper&,Event&)>([&](Looper& lp,Event& ev)->int{event_callback(ev); return 0;}));
return add(event_type,std::function<int(Looper&,Event&)>([=](Looper& lp,Event& ev)->int{event_callback(ev); return 0;}));
}
LooperID Looper::add(_SDLEventType_ event_type,const std::function<void(Looper&)>& event_callback)
{
return add(event_type,std::function<int(Looper&,Event&)>([&](Looper& lp,Event& ev)->int{event_callback(lp); return 0;}));
return add(event_type,std::function<int(Looper&,Event&)>([=](Looper& lp,Event& ev)->int{event_callback(lp); return 0;}));
}
LooperID Looper::add(_SDLEventType_ event_type,const std::function<void()>& event_callback)
{
return add(event_type,std::function<int(Looper&,Event&)>([&](Looper& lp,Event& ev)->int{event_callback();return 0;}));
return add(event_type,std::function<int(Looper&,Event&)>([=](Looper& lp,Event& ev)->int{event_callback();return 0;}));
}
LooperID Looper::operator + (const std::pair<_SDLEventType_,std::function<int(Looper&,Event&)>>& event_callback)
@ -202,6 +174,16 @@ void Looper::reset()
updater=[](){};
}
LooperID Looper::_getNextID(const _SDLEventType_& event_type)
{
LooperID id;
id._looper_cnt = _loop_cnt;
id._type_id = event_type;
++_loop_cnt;
return id;
}
Poller::Poller()
{
idler=[](){};
@ -223,7 +205,7 @@ void Poller::run()
dispatch();
}
/// If pollret is not 0 (new event requests update), or pollret is 0 (No New Event) but Idle function requests update, then call update.
/// 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(_update)
{
@ -232,3 +214,38 @@ void Poller::run()
}
}
}
LooperWithTime::LooperWithTime(int Timeout_ms)
{
_timeout_ms = Timeout_ms;
}
void LooperWithTime::setTimeout(int ms)
{
_timeout_ms = ms;
}
int LooperWithTime::getTimeout() const
{
return _timeout_ms;
}
void LooperWithTime::run()
{
int timeret = 1;
while (_running)
{
while (!_update&&(timeret=WaitEventTimeout(_e, _timeout_ms)))
{
dispatch();
}
/// 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 (_update)
{
updater();
_update = false;
}
}
}

View File

@ -60,6 +60,7 @@ public:
std::function<void()> updater;
protected:
LooperID _getNextID(const _SDLEventType_ & event_type);
Event _e;
bool _running,_update;
std::map<_SDLEventType_,std::list<std::pair<int,std::function<int(Looper&,Event&)>>>> _evmap;
@ -75,3 +76,14 @@ public:
std::function<void()> idler;
};
class LooperWithTime : public Poller
{
public:
LooperWithTime(int Timeout_ms=10);
void setTimeout(int ms);
int getTimeout() const;
void run();
protected:
int _timeout_ms;
};