?? usingtimers.html
字號:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta http-equiv="Content-Language" content="zh-cn">
<title>使用SDL: 定時器</title>
</head>
<body bgcolor="#FFF8DC" text="#000000">
<TABLE>
<TR><!--#include file="../menu.tmpl" -->
<TD>
<p align="center">
[<a href="usingthreads.html">前一頁</a>]
<a href="toc.html"><font color="#8B0000">目錄</font> </a>
[<a href="usingendian.html">后一頁</a>]
</p>
<h1><font color="#8B0000">使用SDL</font></h1>
<h2>定時器</h2>
<table border="0" cellpadding="4">
<tr>
<td valign="top"><ul>
<li><strong>取得當前時間(毫秒級)</strong>
</li>
</ul>
<blockquote>
<p>調用SDL_GetTicks() 可以獲得從過去某個時刻到現在所經過的毫秒。</p>
</blockquote>
</td>
<td valign="top" width="300" bgcolor="#D3D3D3"><strong>提示:</strong><br>
總的來說,在游戲開發中基于時間,而不是幀率來移動對象要更好。這樣在高速系統上和在低速系統上,游戲的節奏是一致的。</td>
</tr>
</table>
<table border="0" cellpadding="50">
<tr>
<td valign="top"><font color="#000080"><strong>例程:</strong></font><strong>:
</strong><pre>
<font color="#0000FF">#define</font> TICK_INTERVAL 30
<font color="#008000">Uint32</font> TimeLeft(void)
{
<font color="#0000FF">static</font> <font color="#008000">Uint32</font> next_time = 0;
<font color="#008000">Uint32</font> now;
now = SDL_GetTicks();
<font color="#0000FF">if</font> ( next_time <= now ) {
next_time = now+TICK_INTERVAL;
<font color="#0000FF">return</font>(0);
}
<font color="#0000FF">return</font>(next_time-now);
}
</pre>
</td>
</tr>
</table>
<table border="0" cellpadding="4">
<tr>
<td valign="top"><ul>
<li><strong>等待一段時間(毫秒級) </strong></li>
</ul>
<blockquote>
<p>SDL_Delay() 可以實現毫秒級的等待。</p>
<p>由于SDL支持的操作系統是多任務的,所以無法保證你的程序能夠等待非常確切的一段時間。
等待相關的函數更多的是用于程序空閑一段時間,而不是在一個很確切的時刻被喚醒。 </p>
</blockquote>
</td>
<td valign="top" width="300" bgcolor="#D3D3D3"><strong>提示:</strong><br>
很多操作系統的調度時間片是10毫秒左右。調用SDL_Delay(1)將放棄當前的時間片。如果你的一個線程是很緊的循環,同時又要保持另一個線程(比如音頻)運行,這就很有用。
</td>
</tr>
</table>
<table border="0" cellpadding="50">
<tr>
<td valign="top"><font color="#000080"><strong>例程:</strong></font>
<pre>
{
<font color="#0000FF">while</font> ( game_running ) {
UpdateGameState();
SDL_Delay(TimeLeft());
}
}
</pre>
</td>
</tr>
</table>
<p align="center">
[<a href="usingthreads.html">前一頁</a>]
<a href="toc.html"><font color="#8B0000">目錄</font> </a>
[<a href="usingendian.html">后一頁</a>]
</p>
</TABLE>
</body>
</html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -