mirror of
https://github.com/Kiritow/MiniEngine.git
synced 2024-03-22 13:11:22 +08:00
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:
parent
2ecddc44e7
commit
f4c629393b
|
@ -164,7 +164,7 @@ namespace MiniEngine
|
||||||
{
|
{
|
||||||
auto p = toSDLPoint();
|
auto p = toSDLPoint();
|
||||||
auto r = rect.toSDLRect();
|
auto r = rect.toSDLRect();
|
||||||
return SDL_PointInRect(&p, &r);
|
return ( SDL_PointInRect(&p, &r) == SDL_TRUE );
|
||||||
}
|
}
|
||||||
|
|
||||||
ColorMode::ColorMode(int R, int G, int B)
|
ColorMode::ColorMode(int R, int G, int B)
|
||||||
|
@ -1582,12 +1582,12 @@ namespace MiniEngine
|
||||||
|
|
||||||
bool MusicPlayer::isPlaying()
|
bool MusicPlayer::isPlaying()
|
||||||
{
|
{
|
||||||
return Mix_PlayingMusic();
|
return (Mix_PlayingMusic() == 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MusicPlayer::isPaused()
|
bool MusicPlayer::isPaused()
|
||||||
{
|
{
|
||||||
return Mix_PausedMusic();
|
return (Mix_PausedMusic() == 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
int MusicPlayer::isFading()
|
int MusicPlayer::isFading()
|
||||||
|
|
|
@ -7,6 +7,9 @@
|
||||||
#include <SDL_image.h>
|
#include <SDL_image.h>
|
||||||
#include <SDL_ttf.h>
|
#include <SDL_ttf.h>
|
||||||
#include <SDL_mixer.h>
|
#include <SDL_mixer.h>
|
||||||
|
|
||||||
|
/// VC++ does not implied C++ exception. Use this to ignore compile warning on this.
|
||||||
|
#pragma warning (disable:4290)
|
||||||
#else
|
#else
|
||||||
/// CodeBlocks (MinGW Compiler)
|
/// CodeBlocks (MinGW Compiler)
|
||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
|
|
|
@ -34,67 +34,39 @@ Looper::Looper()
|
||||||
LooperID Looper::add(_SDLEventType_ event_type,const std::function<int(Looper&,Event&)>& event_callback)
|
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));
|
_evmap[event_type].push_front(std::make_pair(_loop_cnt,event_callback));
|
||||||
|
return _getNextID(event_type);
|
||||||
LooperID id;
|
|
||||||
id._looper_cnt=_loop_cnt;
|
|
||||||
id._type_id=event_type;
|
|
||||||
|
|
||||||
++_loop_cnt;
|
|
||||||
|
|
||||||
return id;
|
|
||||||
}
|
}
|
||||||
LooperID Looper::add(_SDLEventType_ event_type,const std::function<int(Event&)>& event_callback)
|
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);}));
|
_evmap[event_type].push_front(std::make_pair(_loop_cnt,[=](Looper& lp,Event& ev)->int{return event_callback(ev);}));
|
||||||
|
return _getNextID(event_type);
|
||||||
LooperID id;
|
|
||||||
id._looper_cnt=_loop_cnt;
|
|
||||||
id._type_id=event_type;
|
|
||||||
|
|
||||||
++_loop_cnt;
|
|
||||||
|
|
||||||
return id;
|
|
||||||
}
|
}
|
||||||
LooperID Looper::add(_SDLEventType_ event_type,const std::function<int(Looper&)>& event_callback)
|
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);}));
|
_evmap[event_type].push_front(std::make_pair(_loop_cnt,[=](Looper& lp,Event& ev)->int{return event_callback(lp);}));
|
||||||
|
return _getNextID(event_type);
|
||||||
LooperID id;
|
|
||||||
id._looper_cnt=_loop_cnt;
|
|
||||||
id._type_id=event_type;
|
|
||||||
|
|
||||||
++_loop_cnt;
|
|
||||||
|
|
||||||
return id;
|
|
||||||
}
|
}
|
||||||
LooperID Looper::add(_SDLEventType_ event_type,const std::function<int()>& event_callback)
|
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();}));
|
_evmap[event_type].push_front(std::make_pair(_loop_cnt,[=](Looper& lp,Event& ev)->int{return event_callback();}));
|
||||||
|
return _getNextID(event_type);
|
||||||
LooperID id;
|
|
||||||
id._looper_cnt=_loop_cnt;
|
|
||||||
id._type_id=event_type;
|
|
||||||
|
|
||||||
++_loop_cnt;
|
|
||||||
|
|
||||||
return id;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
LooperID Looper::add(_SDLEventType_ event_type,const std::function<void(Looper&,Event&)>& event_callback)
|
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)
|
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)
|
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)
|
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)
|
LooperID Looper::operator + (const std::pair<_SDLEventType_,std::function<int(Looper&,Event&)>>& event_callback)
|
||||||
|
@ -202,6 +174,16 @@ void Looper::reset()
|
||||||
updater=[](){};
|
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()
|
Poller::Poller()
|
||||||
{
|
{
|
||||||
idler=[](){};
|
idler=[](){};
|
||||||
|
@ -223,7 +205,7 @@ void Poller::run()
|
||||||
dispatch();
|
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(!pollret) idler();
|
||||||
if(_update)
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -60,6 +60,7 @@ public:
|
||||||
std::function<void()> updater;
|
std::function<void()> updater;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
LooperID _getNextID(const _SDLEventType_ & event_type);
|
||||||
Event _e;
|
Event _e;
|
||||||
bool _running,_update;
|
bool _running,_update;
|
||||||
std::map<_SDLEventType_,std::list<std::pair<int,std::function<int(Looper&,Event&)>>>> _evmap;
|
std::map<_SDLEventType_,std::list<std::pair<int,std::function<int(Looper&,Event&)>>>> _evmap;
|
||||||
|
@ -75,3 +76,14 @@ public:
|
||||||
|
|
||||||
std::function<void()> idler;
|
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;
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user