C++ 如何在SDL中定期调用函数?
我需要跟踪时间间隔,并在每次间隔结束时调用函数。我查阅了SDL关于C++ 如何在SDL中定期调用函数?,c++,timer,sdl,intervals,C++,Timer,Sdl,Intervals,我需要跟踪时间间隔,并在每次间隔结束时调用函数。我查阅了SDL关于SDL\u AddTimer的文档,但gcc抱怨我做得不对 那么,我该如何设置定期间隔,或者如何使用AddTimer SDL文档中的AddTimer示例我不清楚。gcc告诉我,我的回调函数中缺少参数,并且我的计时器在范围中不存在(但我不知道要声明什么)。以下是我曾经尝试过的: SDL_AddTimer(3000,changeMusic,NULL); Uint32 changeMusic(Uint32 interval, void
SDL\u AddTimer
的文档,但gcc抱怨我做得不对
那么,我该如何设置定期间隔,或者如何使用AddTimer
SDL文档中的AddTimer
示例我不清楚。gcc告诉我,我的回调函数中缺少参数,并且我的计时器在范围中不存在(但我不知道要声明什么)。以下是我曾经尝试过的:
SDL_AddTimer(3000,changeMusic,NULL);
Uint32 changeMusic(Uint32 interval, void *param){...
我想,如果经过的时间可以被3秒整除,那么函数就会运行,但最终以不稳定的频率激活
if(interval.getTicks()%3000==0){
changeMusic();
}
或者,如果倒计时为零,重置它并调用函数,但我不知道如何制作倒计时计时器
//something like this
cdTimer=(3000 to 0)
if(cdTimer==0){
cdTimer=(3000 to 0);
changeMusic();
}
从您的代码片段中,我非常确定您在调用SDL_AddTimer()之前没有声明函数,因此编译器认为它是错误的函数参数 有两种解决方案:
class Mylene
{
public:
... // other stuff goes here ...
static Uint32 ChangeMusic(Uint32 x, void *p)
{
Mylene *self = reinterpret_cast<Mylene *>(p);
self->doChangeMusic();
return 0;
}
... more stuff here, perhaps ...
};
Mylene mylene(...); // Note, must not go out of scope before the ChangeMusic is called.
// ... stuff ...
timer_id = SDL_AddTimer(3000, &Mylene::ChangeMusic, &mylene); // Passing the mylene object...
... Do other things here for some time ...
class Mylene
{
公众:
…//这里还有其他东西。。。
静态Uint32更改音乐(Uint32 x,void*p)
{
Mylene*self=重新解释铸件(p);
自我->音乐();
返回0;
}
…这里有更多的东西,也许。。。
};
Mylene Mylene(…);//注意,在调用ChangeMusic之前,不得超出范围。
// ... 东西
timer_id=SDL_AddTimer(3000,&Mylene::ChangeMusic,&Mylene);//正在传递mylene对象。。。
... 在这里做其他事情一段时间。。。
Q:gcc错误或警告是什么?Gah。这实际上只是一个订单问题。谢谢你的帮助!杰出的我想可能是——我自己也做过几次。我确实在学习,但慢慢地…;)