作者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