Trouble with the canonical game loop
Frank C. Wrote:I'm just going by the Apple docs for CFAbsoluteTimeGetCurrent: http://developer.apple.com/documentation...rence.html
That's why I use mach_time as well. I got slapped on the wrist for this a while back, using the unix time functions (which, BTW, I noticed id's Wolfenstein on iPhone uses).
I too check for negative deltas since I accidentally used a float for a time calc instead of a double one time. It blew the precision like once every 50 minutes or so (can't recall the exact time), and froze the game (seemingly, except it was just a large -delta so nothing would move forward), and was a challenging bug to track down.
I usually use following cross platform time function:
I don't know if gettimeofday() would be affected by the user setting date/time or network time updates, never got to really test that bit
Code:
static inline double SystemReferenceTime(void)
{
#if defined(__APPLE__)
AbsoluteTime atime = UpTime();
Nanoseconds nsecs = AbsoluteToNanoseconds(atime);
double time = UnsignedWideToUInt64(nsecs);
return time*1.0E-9;
#elif defined(WIN32)
static LARGE_INTEGER pfreq;
static int whichTimer = 0;
switch(whichTimer)
{
case 0:
{
int result = QueryPerformanceFrequency(&pfreq);
if (!result)
{
whichTimer = 2;
return GetTickCount()*1.0E-3;
}
whichTimer = 1;
}
case 1:
{
LARGE_INTEGER pcount;
QueryPerformanceCounter(&pcount);
return (double)pcount.QuadPart/(double)pfreq.QuadPart;
}
case 2:
default:
return GetTickCount()*1.0E-3;
};
// return GetTickCount()*0.001;
#elif defined(_POSIX__) || defined(WINDOWS)
struct timeval tod;
gettimeofday(&tod, NULL);
return tod.tv_sec + tod.tv_usec * 1.0E-6;
#else
#error SystemReferenceTime() not implemented for this platform
#endif
}I don't know if gettimeofday() would be affected by the user setting date/time or network time updates, never got to really test that bit
Well gettimeofday isn't reliably monotonic on the Mac, I know that much
Frank C. Wrote:I'm just going by the Apple docs for CFAbsoluteTimeGetCurrent: http://developer.apple.com/documentation...rence.html
I also experienced negative deltas first hand on the iPhone (though that may have been a bug in an early OS). But either way IMHO, it can't hurt to check for and fix negative deltas even if they should never happen.
Interesting. I was under the impression that CFAbsoluteTime was just a wrapper over mach_absolute_time. I guess that may not be the case.
I've switched to using mach time, and still get the strange jerky animation.
I've posted a demo app here, with the three game loop implementations: http://shamyl.zakariya.net/etc/gl/TerrainDemo.zip
(Note -- drive with arrow keys, unflip with the delete key, and if you have a wacom tablet plugged in, unplug it first! )
On my Oct 2006 MBP and spanking new Mac Pro I get smooth animation on all three game loops, until I turn off foliage ( the toggle on the top left ). Then the framerate stays the same, but the animation gets very jerky.
I assume that the foliage takes just long enough to draw that whatever problem is occurring in my gameloop is "smoothed out".
I've posted a demo app here, with the three game loop implementations: http://shamyl.zakariya.net/etc/gl/TerrainDemo.zip
(Note -- drive with arrow keys, unflip with the delete key, and if you have a wacom tablet plugged in, unplug it first! )
On my Oct 2006 MBP and spanking new Mac Pro I get smooth animation on all three game loops, until I turn off foliage ( the toggle on the top left ). Then the framerate stays the same, but the animation gets very jerky.
I assume that the foliage takes just long enough to draw that whatever problem is occurring in my gameloop is "smoothed out".
I'm assuming "SPS" steps-per-second? If so, there's something wrong with "Another Jake's" loop...
A more meaningful statistic to debug jerky animation would be steps-per-frame - hate to sound like a broken record but frame interpolation is what ya need here.
On a side note, your demo worked fine with my Wacom tablet pluged in.
A more meaningful statistic to debug jerky animation would be steps-per-frame - hate to sound like a broken record but frame interpolation is what ya need here.
On a side note, your demo worked fine with my Wacom tablet pluged in.
Frank C. Wrote:I'm assuming "SPS" steps-per-second? If so, there's something wrong with "Another Jake's" loop...
I know -- I mentioned that earlier on.
Quote:A more meaningful statistic to debug jerky animation would be steps-per-frame - hate to sound like a broken record but frame interpolation is what ya need here.
I won't argue against interpolation -- it's a good thing. But I'd rather figure out what's wrong with my gameloop first, then add interpolation later.

Quote:On a side note, your demo worked fine with my Wacom tablet pluged in.
I assume you've not installed Wacom's drivers then?
I also had issues with inexplicable jerkiness when using Alex's game loop back when I was using BlitzMax. Since then, I have changed my method to something simpler that uses the same idea. You only need three things: a minimum time step for your physics, an overflow bucket, and dt.
Code:
step = 1/120.0
bucket = 0.0
# ...
def update(dt):
bucket += dt
while bucket >= step:
bucket -= step
# ...do stuff...My web site - Games, music, Python stuff
TomorrowPlusX Wrote:I won't argue against interpolation -- it's a good thing. But I'd rather figure out what's wrong with my gameloop first, then add interpolation later.It's just as likely there's nothing wrong with the loop, and the problem is that frames aren't interpolated

TomorrowPlusX Wrote:I assume you've not installed Wacom's drivers then?They're installed - the preference pane says version 6.1.0-7. I recall updating the driver not too long ago.
Hmm... Crashes on startup here: Mac Mini Intel Core Duo 1.83 ghz, 512 RAM, 64 shared VRAM(Intel GMA 950) etc...
From the console:
And some of the crash log:
I'm guessing this is not an unexpected crash(i.e no GMA support), but thought I'd post.
From the console:
Code:
8/10/09 4:45:32 PM com.apple.launchd[84] ([0x0-0xa10a1].org.zakariya.Terrain[1525]) Exited abnormally: Bus errorAnd some of the crash log:
Code:
Process: Terrain [1525]
Path: /Users/lincolngreen/Downloads/TerrainDemo/Terrain.app/Contents/MacOS/Terrain
Identifier: org.zakariya.Terrain
Version: ??? (1.0)
Code Type: X86 (Native)
Parent Process: launchd [84]
Date/Time: 2009-08-10 16:45:06.760 -0500
OS Version: Mac OS X 10.5.7 (9J61)
Report Version: 6
Anonymous UUID: 1BE6D722-B312-41AF-A4A1-40DC17C2861F
Exception Type: EXC_BAD_ACCESS (SIGBUS)
Exception Codes: KERN_PROTECTION_FAILURE at 0x0000000000000000
Crashed Thread: 0
Thread 0 Crashed:
0 ??? 0000000000 0 + 0
1 org.zakariya.sgf 0x007d120f sgf::glsl::ShaderProgram::ShaderProgram(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::map<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > const&) + 701
2 org.zakariya.sgf 0x008b3df0 sgf::CompositeShader2D::CompositeShader2D(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) + 228
3 org.zakariya.sgf 0x008146d1 sgf::ui::Hud::Hud(sgf::DisplayDelegate*) + 485
4 org.zakariya.sgf 0x007bcfb3 sgf::DisplayDelegate::glContextCreated() + 113
5 org.zakariya.sgf 0x0085e53d -[SGFOpenGLView update] + 325
6 com.apple.AppKit 0x954c422c -[NSView _drawRect:clip:] + 3853
7 com.apple.AppKit 0x954c2d23 -[NSView _recursiveDisplayAllDirtyWithLockFocus:visRect:] + 1050
8 com.apple.AppKit 0x954c30ba -[NSView _recursiveDisplayAllDirtyWithLockFocus:visRect:] + 1969
9 com.apple.AppKit 0x954c1679 -[NSView _recursiveDisplayRectIfNeededIgnoringOpacity:isVisibleRect:rectIsVisibleRectForView:topView:] + 759
10 com.apple.AppKit 0x954c0fbb -[NSThemeFrame _recursiveDisplayRectIfNeededIgnoringOpacity:isVisibleRect:rectIsVisibleRectForView:topView:] + 306
11 com.apple.AppKit 0x954bdadf -[NSView _displayRectIgnoringOpacity:isVisibleRect:rectIsVisibleRectForView:] + 3090
12 com.apple.AppKit 0x953fe4b3 -[NSView displayIfNeeded] + 933
13 com.apple.AppKit 0x953fe061 -[NSWindow displayIfNeeded] + 189
14 com.apple.AppKit 0x953fde84 _handleWindowNeedsDisplay + 436
15 com.apple.CoreFoundation 0x9145c942 __CFRunLoopDoObservers + 466
16 com.apple.CoreFoundation 0x9145dc9c CFRunLoopRunSpecific + 844
17 com.apple.CoreFoundation 0x9145ec78 CFRunLoopRunInMode + 88
18 com.apple.HIToolbox 0x906b928c RunCurrentEventLoopInMode + 283
19 com.apple.HIToolbox 0x906b8fde ReceiveNextEventCommon + 175
20 com.apple.HIToolbox 0x906b8f19 BlockUntilNextEventMatchingListInMode + 106
21 com.apple.AppKit 0x953fbd0d _DPSNextEvent + 657
22 com.apple.AppKit 0x953fb5c0 -[NSApplication nextEventMatchingMask:untilDate:inMode:dequeue:] + 128
23 com.apple.AppKit 0x953f45fb -[NSApplication run] + 795
24 org.zakariya.sgf 0x0085ca62 SGFApplicationMain(sgf::Application*, int, char const**) + 700
25 org.zakariya.Terrain 0x000022cc main + 82
26 org.zakariya.Terrain 0x0000223e start + 54
Thread 1:
0 libSystem.B.dylib 0x9008946e __semwait_signal + 10
1 libSystem.B.dylib 0x900b3dcd pthread_cond_wait$UNIX2003 + 73
2 libGLProgrammability.dylib 0x93a10b32 glvmDoWork + 162
3 libSystem.B.dylib 0x900b3155 _pthread_start + 321
4 libSystem.B.dylib 0x900b3012 thread_start + 34
Thread 0 crashed with X86 Thread State (32-bit):
eax: 0x00000002 ebx: 0x007d0d4a ecx: 0x00000000 edx: 0x00cc9720
edi: 0xbfffe00c esi: 0x009482c0 ebp: 0xbfffde78 esp: 0xbfffddec
ss: 0x0000001f efl: 0x00010202 eip: 0x00000000 cs: 0x00000017
ds: 0x0000001f es: 0x0000001f fs: 0x00000000 gs: 0x00000037
cr2: 0x00000000
Binary Images:
0x1000 - 0x49ff7 +org.zakariya.Terrain ??? (1.0) <f0553cc54a940c7685c0ceb7b76d852b> /Users/lincolngreen/Downloads/TerrainDemo/Terrain.app/Contents/MacOS/Terrain
0x119000 - 0x125fff com.apple.iokit.IOHIDLib 1.5.5 (1.5.5) <2a9407a7c9aa27d571f1118aa02b3248> /System/Library/Extensions/IOHIDFamily.kext/Contents/PlugIns/IOHIDLib.plugin/Contents/MacOS/IOHIDLib
0x137000 - 0x153ff7 GLRendererFloat ??? (???) <927b7d5ce6a7c21fdc761f6f29cdf4ee> /System/Library/Frameworks/OpenGL.framework/Versions/A/Resources/GLRendererFloat.bundle/GLRendererFloat
0x1ce000 - 0x227ff7 com.apple.driver.AppleIntelGMA950GLDriver 1.5.44 (5.4.4) <eff61d519cdb9eb0af377cc0bd046987> /System/Library/Extensions/AppleIntelGMA950GLDriver.bundle/Contents/MacOS/AppleIntelGMA950GLDriver
0x3dc000 - 0x60cfef +org.zakariya.sre ??? (1.0) <6a7a8138b720a9aa5a365b20c29227eb>I'm guessing this is not an unexpected crash(i.e no GMA support), but thought I'd post.
Yeah -- no gma support. Sorry chief. I don't know what the minimum is, but I'm going with ATI x1600 -- which is what I have and which is barely able to run my shaders.
Frank C. Wrote:I'm assuming "SPS" steps-per-second? If so, there's something wrong with "Another Jake's" loop...
Well, it's not "my" loop exactly, since it's derived from DoG's. However, I did neglect to test that the tick rate was as-advertised. I just dropped it in and wanted to see if it worked on the end-result side of things. I didn't run it through anything that requires stable integration, so that might very well be an issue.
FWIW, I haven't changed my "main" code-base timing algorithm over to DoG's yet since I need to do more testing and tweaking with it, so I'm still using the one I derived from ThemsAllTook's.
I wish I had something more than a GMA to work with so I could see what's going on too.
Well, I've decided I'm going to try a slightly different path. I'm going to give a stab ad using a displaylink thread to perform rendering, and keep my game logic updates on the main thread, driven by a timer at 120hz.
Does anybody have any experience with display link? I should probably start a new thread for this discussion...
Does anybody have any experience with display link? I should probably start a new thread for this discussion...
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| iPhone game loop | agreendev | 2 | 3,709 |
Jul 27, 2010 11:32 AM Last Post: AnotherJake |
|
| Proper game update loop in Cocoa? | LoneIgadzra | 13 | 5,712 |
May 30, 2008 05:49 AM Last Post: ThemsAllTook |
|
| Controlling Game Loop | Nick | 2 | 2,853 |
Jul 21, 2007 07:12 PM Last Post: Nick |
|
| Good Game Loop with Physics Units | blobbo | 6 | 4,050 |
Oct 14, 2005 02:59 AM Last Post: unknown |
|
| Game Loop in Cocoa | robmcq | 9 | 5,361 |
Sep 27, 2005 10:49 AM Last Post: akb825 |
|

