📄 sdladdtimer.html
字号:
<HTML><HEAD><TITLE>SDL_AddTimer</TITLE><METANAME="GENERATOR"CONTENT="Modular DocBook HTML Stylesheet Version 1.76b+"><LINKREL="HOME"TITLE="SDL Library Documentation"HREF="index.html"><LINKREL="UP"TITLE="Time"HREF="time.html"><LINKREL="PREVIOUS"TITLE="SDL_Delay"HREF="sdldelay.html"><LINKREL="NEXT"TITLE="SDL_RemoveTimer"HREF="sdlremovetimer.html"></HEAD><BODYCLASS="REFENTRY"BGCOLOR="#FFF8DC"TEXT="#000000"LINK="#0000ee"VLINK="#551a8b"ALINK="#ff0000"><DIVCLASS="NAVHEADER"><TABLESUMMARY="Header navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><THCOLSPAN="3"ALIGN="center">SDL Library Documentation</TH></TR><TR><TDWIDTH="10%"ALIGN="left"VALIGN="bottom"><AHREF="sdldelay.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="80%"ALIGN="center"VALIGN="bottom"></TD><TDWIDTH="10%"ALIGN="right"VALIGN="bottom"><AHREF="sdlremovetimer.html"ACCESSKEY="N">Next</A></TD></TR></TABLE><HRALIGN="LEFT"WIDTH="100%"></DIV><H1><ANAME="SDLADDTIMER"></A>SDL_AddTimer</H1><DIVCLASS="REFNAMEDIV"><ANAME="AEN8482"></A><H2>Name</H2>SDL_AddTimer -- Add a timer which will call a callback after the specified number of milliseconds haselapsed.</DIV><DIVCLASS="REFSYNOPSISDIV"><ANAME="AEN8485"></A><H2>Synopsis</H2><DIVCLASS="FUNCSYNOPSIS"><ANAME="AEN8486"></A><P></P><PRECLASS="FUNCSYNOPSISINFO">#include "SDL.h"</PRE><P><CODE><CODECLASS="FUNCDEF">SDL_TimerID <BCLASS="FSFUNC">SDL_AddTimer</B></CODE>(Uint32 interval, SDL_NewTimerCallback callback, void *param);</CODE></P><P></P></DIV></DIV><DIVCLASS="REFSECT1"><ANAME="SDLNEWTIMERCALLBACK"></A><H2>Callback</H2><PRECLASS="PROGRAMLISTING">/* type definition for the "new" timer callback function */typedef Uint32 (*SDL_NewTimerCallback)(Uint32 interval, void *param);</PRE></DIV><DIVCLASS="REFSECT1"><ANAME="AEN8495"></A><H2>Description</H2><P>Adds a callback function to be run after the specified number ofmilliseconds has elapsed. The callback function is passed the currenttimer interval and the user supplied parameter from the<TTCLASS="FUNCTION">SDL_AddTimer</TT> call and returns the next timerinterval. If the returned value from the callback is the same as the onepassed in, the periodic alarm continues, otherwise a new alarm isscheduled.</P><P>To cancel a currently running timer call<AHREF="sdlremovetimer.html">SDL_RemoveTimer</A> with thetimer ID returned from<TTCLASS="FUNCTION">SDL_AddTimer</TT>.</P><P>The timer callback function may run in a different thread than yourmain program, and so shouldn't call any functions from within itself.You may always call <AHREF="sdlpushevent.html">SDL_PushEvent</A>, however.</P><P>The granularity of the timer is platform-dependent, but you should counton it being at least 10 ms as this is the most common number.This means that ifyou request a 16 ms timer, your callback will run approximately 20 mslater on an unloaded system. If you wanted to set a flag signalinga frame update at 30 frames per second (every 33 ms), you might set atimer for 30 ms (see example below).If you use this function, you need to pass <TTCLASS="LITERAL">SDL_INIT_TIMER</TT>to <AHREF="sdlinit.html">SDL_Init</A>.</P></DIV><DIVCLASS="REFSECT1"><ANAME="AEN8507"></A><H2>Return Value</H2><P>Returns an ID value for the added timer or<SPANCLASS="RETURNVALUE">NULL</SPAN> if there was an error.</P></DIV><DIVCLASS="REFSECT1"><ANAME="AEN8511"></A><H2>Examples</H2><P><PRECLASS="PROGRAMLISTING">my_timer_id = SDL_AddTimer((33/10)*10, my_callbackfunc, my_callback_param);</PRE></P></DIV><DIVCLASS="REFSECT1"><ANAME="AEN8515"></A><H2>See Also</H2><P><AHREF="sdlremovetimer.html"><TTCLASS="FUNCTION">SDL_RemoveTimer</TT></A>,<AHREF="sdlpushevent.html"><TTCLASS="FUNCTION">SDL_PushEvent</TT></A></P></DIV><DIVCLASS="NAVFOOTER"><HRALIGN="LEFT"WIDTH="100%"><TABLESUMMARY="Footer navigation table"WIDTH="100%"BORDER="0"CELLPADDING="0"CELLSPACING="0"><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top"><AHREF="sdldelay.html"ACCESSKEY="P">Prev</A></TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="index.html"ACCESSKEY="H">Home</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top"><AHREF="sdlremovetimer.html"ACCESSKEY="N">Next</A></TD></TR><TR><TDWIDTH="33%"ALIGN="left"VALIGN="top">SDL_Delay</TD><TDWIDTH="34%"ALIGN="center"VALIGN="top"><AHREF="time.html"ACCESSKEY="U">Up</A></TD><TDWIDTH="33%"ALIGN="right"VALIGN="top">SDL_RemoveTimer</TD></TR></TABLE></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -