1 <HTML 2 ><HEAD 3 ><TITLE 4 >Time Examples</TITLE 5 ><META 6 NAME="GENERATOR" 7 CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+ 8 "><LINK 9 REL="HOME" 10 TITLE="SDL Library Documentation" 11 HREF="index.html"><LINK 12 REL="UP" 13 TITLE="Examples" 14 HREF="guideexamples.html"><LINK 15 REL="PREVIOUS" 16 TITLE="CDROM Examples" 17 HREF="guidecdromexamples.html"><LINK 18 REL="NEXT" 19 TITLE="SDL Reference" 20 HREF="reference.html"></HEAD 21 ><BODY 22 CLASS="SECT1" 23 BGCOLOR="#FFF8DC" 24 TEXT="#000000" 25 LINK="#0000ee" 26 VLINK="#551a8b" 27 ALINK="#ff0000" 28 ><DIV 29 CLASS="NAVHEADER" 30 ><TABLE 31 SUMMARY="Header navigation table" 32 WIDTH="100%" 33 BORDER="0" 34 CELLPADDING="0" 35 CELLSPACING="0" 36 ><TR 37 ><TH 38 COLSPAN="3" 39 ALIGN="center" 40 >SDL Library Documentation</TH 41 ></TR 42 ><TR 43 ><TD 44 WIDTH="10%" 45 ALIGN="left" 46 VALIGN="bottom" 47 ><A 48 HREF="guidecdromexamples.html" 49 ACCESSKEY="P" 50 >Prev</A 51 ></TD 52 ><TD 53 WIDTH="80%" 54 ALIGN="center" 55 VALIGN="bottom" 56 >Chapter 4. Examples</TD 57 ><TD 58 WIDTH="10%" 59 ALIGN="right" 60 VALIGN="bottom" 61 ><A 62 HREF="reference.html" 63 ACCESSKEY="N" 64 >Next</A 65 ></TD 66 ></TR 67 ></TABLE 68 ><HR 69 ALIGN="LEFT" 70 WIDTH="100%"></DIV 71 ><DIV 72 CLASS="SECT1" 73 ><H1 74 CLASS="SECT1" 75 ><A 76 NAME="GUIDETIMEEXAMPLES" 77 ></A 78 >Time Examples</H1 79 ><P 80 ></P 81 ><DIV 82 CLASS="SECT2" 83 ><H2 84 CLASS="SECT2" 85 ><A 86 NAME="AEN412" 87 ></A 88 >Time based game loop</H2 89 ><P 90 ><PRE 91 CLASS="PROGRAMLISTING" 92 >#define TICK_INTERVAL 30 93 94 static Uint32 next_time; 95 96 Uint32 time_left(void) 97 { 98 Uint32 now; 99 100 now = SDL_GetTicks(); 101 if(next_time <= now) 102 return 0; 103 else 104 return next_time - now; 105 } 106 107 108 /* main game loop */ 109 110 next_time = SDL_GetTicks() + TICK_INTERVAL; 111 while ( game_running ) { 112 update_game_state(); 113 SDL_Delay(time_left()); 114 next_time += TICK_INTERVAL; 115 } </PRE 116 ></P 117 ></DIV 118 ></DIV 119 ><DIV 120 CLASS="NAVFOOTER" 121 ><HR 122 ALIGN="LEFT" 123 WIDTH="100%"><TABLE 124 SUMMARY="Footer navigation table" 125 WIDTH="100%" 126 BORDER="0" 127 CELLPADDING="0" 128 CELLSPACING="0" 129 ><TR 130 ><TD 131 WIDTH="33%" 132 ALIGN="left" 133 VALIGN="top" 134 ><A 135 HREF="guidecdromexamples.html" 136 ACCESSKEY="P" 137 >Prev</A 138 ></TD 139 ><TD 140 WIDTH="34%" 141 ALIGN="center" 142 VALIGN="top" 143 ><A 144 HREF="index.html" 145 ACCESSKEY="H" 146 >Home</A 147 ></TD 148 ><TD 149 WIDTH="33%" 150 ALIGN="right" 151 VALIGN="top" 152 ><A 153 HREF="reference.html" 154 ACCESSKEY="N" 155 >Next</A 156 ></TD 157 ></TR 158 ><TR 159 ><TD 160 WIDTH="33%" 161 ALIGN="left" 162 VALIGN="top" 163 >CDROM Examples</TD 164 ><TD 165 WIDTH="34%" 166 ALIGN="center" 167 VALIGN="top" 168 ><A 169 HREF="guideexamples.html" 170 ACCESSKEY="U" 171 >Up</A 172 ></TD 173 ><TD 174 WIDTH="33%" 175 ALIGN="right" 176 VALIGN="top" 177 >SDL Reference</TD 178 ></TR 179 ></TABLE 180 ></DIV 181 ></BODY 182 ></HTML 183 >