作者crazytea (............)
看板C_and_CPP
标题[问题] 我应该直接问音乐游戏的计时器该怎麽做 QQ
时间Sat Apr 25 21:18:48 2009
其实前面的码表问题本来是希望能够自己解决,
没想到问的词不达意还无法解决问题 囧
所以就直接问了!
音乐游戏一定要藉由精准的时间判定来播放botton
该在几秒案就该在几秒按
虽然拍子跟秒数之间有公式可以算(bpm)
例如:150 bpm (即 1 分钟 150拍 )
但是算到後来一拍的时间也是几百 ms
要如何计算从音乐开始播放後
每隔固定时间(拍子)播放botton
是我现在的瓶颈~~
我有下载跳舞机模拟器( stepMania ) 的原始码来看,
但是由於程式过於庞大,
目前只针对timeGetTime()寻找整个专案
找到相关程式码如下:
static bool g_bTimerInitialized;
static DWORD g_iStartTime;
static void InitTimer()
{
if( g_bTimerInitialized )
return;
g_bTimerInitialized = true;
timeBeginPeriod( 1 );
g_iStartTime = timeGetTime();
}
int64_t ArchHooks::GetMicrosecondsSinceStart( bool bAccurate )
{
if( !g_bTimerInitialized )
InitTimer();
int64_t ret = (timeGetTime() - g_iStartTime) * int64_t(1000);
if( bAccurate )
{
ret = FixupTimeIfLooped( ret );
ret = FixupTimeIfBackwards( ret );
}
return ret;
}
uint64_t ArchHooks::FixupTimeIfLooped( uint64_t usecs )
{
static uint64_t last = 0;
static uint64_t offset_us = 0;
/* The time has wrapped if the last time was very high and the current time is
very low. */
const uint64_t i32BitMaxMs = uint64_t(1) << 32;
const uint64_t i32BitMaxUs = i32BitMaxMs*1000;
const uint64_t one_day = uint64_t(24*60*60)*1000000;
if( last > (i32BitMaxUs-one_day) && usecs < one_day )
offset_us += i32BitMaxUs;
last = usecs;
return usecs + offset_us;
}
uint64_t ArchHooks::FixupTimeIfBackwards( uint64_t usecs )
{
static uint64_t last = 0;
static uint64_t offset_us = 0;
if( usecs < last )
{
/* The time has moved backwards. Increase the offset by the amount we moved.
*/
offset_us += last - usecs;
}
last = usecs;
return usecs + offset_us;
}
还是搞不懂他在干啥?
或是有更好的方法吗?
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 192.192.154.40
1F:→ MOONRAKER:我记得是我回你的嘛,那你是嫌我的回答不对就对了(火大) 04/25 21:58
2F:→ MOONRAKER:音乐开始的时候记录一个{开始时间}=timeGetTime() 04/25 21:59
3F:→ MOONRAKER:以後你需要知道经过时间的时候就用 04/25 21:59
4F:→ MOONRAKER:timeGetTime()-{开始时间}就得到时距 04/25 22:00
5F:→ MOONRAKER:唔,第一行好像弄错了 04/25 22:16
6F:→ crazytea:如果我想在固定时距做事可以吗? 04/25 22:20
7F:→ crazytea:我知道取得时距的方式 但是固定时距做事就.... 04/25 22:22
8F:推 VictorTom:以前用BCB有Timer元件的时候我就直接用Timer就行了.... 04/25 22:23
9F:→ VictorTom:没有Timer, 想得到的就是跑回圈用M大提的func一直取时间 04/25 22:24
10F:→ VictorTom:时间差满足条件就做事, 做完就等下一次时间差够大这样. 04/25 22:25
11F:→ VictorTom:只是, 要确保每一轮要做的事耗时不会超过时间间隔XD 04/25 22:25