[Tux4kids-commits] r1193 - tuxmath/branches/lan/src
David Bruce
dbruce-guest at alioth.debian.org
Mon Jul 13 02:54:30 UTC 2009
Author: dbruce-guest
Date: 2009-07-13 02:54:28 +0000 (Mon, 13 Jul 2009)
New Revision: 1193
Added:
tuxmath/branches/lan/src/throttle.c
tuxmath/branches/lan/src/throttle.h
Log:
added small loop-throttling function
Added: tuxmath/branches/lan/src/throttle.c
===================================================================
--- tuxmath/branches/lan/src/throttle.c (rev 0)
+++ tuxmath/branches/lan/src/throttle.c 2009-07-13 02:54:28 UTC (rev 1193)
@@ -0,0 +1,41 @@
+/*
+* C Implementation: network.c
+*
+* Description: A simple function that uses SDL_Delay() to keep
+* loops from eating all available CPU.
+
+*
+* Author: David Bruce, and the TuxMath team, (C) 2009
+* Developers list: <tuxmath-devel at lists.sourceforge.net>
+*
+* Copyright: See COPYING file that comes with this distribution. (Briefly, GNU GPL).
+*/
+
+
+#include "SDL.h"
+
+void Throttle(int loop_msec)
+{
+ static Uint32 now_t, last_t; //These will be zero first time through
+ int wait_t;
+
+ //Target loop time must be between 0 and 100 msec:
+ if(loop_msec < 0)
+ loop_msec = 0;
+ if(loop_msec > 100)
+ loop_msec = 100;
+
+ //See if we need to wait:
+ now_t = SDL_GetTicks();
+ if (now_t < (last_t + loop_msec))
+ {
+ wait_t = (last_t + loop_msec) - now_t;
+ //Avoid problem if we somehow wrap past uint32 size (at 49.7 days!)
+ if(wait_t < 0)
+ wait_t = 0;
+ if(wait_t > loop_msec)
+ wait_t = loop_msec;
+ SDL_Delay(wait_t);
+ }
+ last_t = SDL_GetTicks();
+}
Added: tuxmath/branches/lan/src/throttle.h
===================================================================
--- tuxmath/branches/lan/src/throttle.h (rev 0)
+++ tuxmath/branches/lan/src/throttle.h 2009-07-13 02:54:28 UTC (rev 1193)
@@ -0,0 +1,20 @@
+/*
+
+ throttle.h
+
+ Description: A simple function that uses SDL_Delay() to keep loops from eating all available
+ CPU
+ Author: David Bruce and the TuxMath team, (C) 2009
+
+ Copyright: See COPYING file that comes with this distribution (briefly, GNU GPL version 2 or later)
+
+*/
+
+#ifndef THROTTLE_H
+#define THROTTLE_H
+// This simple function uses SDL_Delay() to wait to return until 'loop_msec'
+// milliseconds after it returned the last time. Per SDL docs, the granularity
+// is likely no better than 10 msec
+void Throttle(int loop_msec);
+
+#endif
More information about the Tux4kids-commits
mailing list