Advertisement

Delay Problems using GetTickCount()

Started by August 18, 2001 11:31 AM
8 comments, last by misterags 23 years, 6 months ago
Okay, I am programming in DOS and always have problems w/ delays when I shouldn''t be. Here''s the code I am haveing problem with... I am using MS VC++; void main() { cout <<"Gamedev Rocks.\n"; int Time = GetTickCount(); while(GetTickCount() - Time < 6000) {} } Now, when I do this, it does the delay first, then displays "Gamedev Rocks". So the delay works fine, but it goes out of turn. What''s wrong? -Misterags™
Output is buffered for efficiency. To ''flush'' the buffer, you do this:

cout << "Gamedev Rocks.\n" << flush;

''flush'' is called a manipulator, because it manipulates the stream (cout). This ensures that the buffer gets emptied before the program moves on. For ''cout'', this means the text should appear on the screen instantly.

However - before you go and do that - there is another manipulator that is useful to you, called ''endl''. endl is basically ''\n'' and flush combined. So, you could do this instead:

cout <<"Gamedev Rocks." << endl;

for the same result as the first line of code I posted.
Advertisement
Thanks bro! I always used endl but just thought it was a different way of doing \n incase you weren''t in Parenthases, and I had heard of flush but didn''t know what it did. I haven''t tried it yet, but I will now. Thanks again for your time, dude.

-misterags
quote:
Original post by misterags
Okay, I am programming in DOS...


Sorry for being pedantic, but you're not programming for DOS. You're programming for the Win32 Console. There is a relatively large difference, even though they end up looking the same when you run them in Windows.

[Resist Windows XP's Invasive Production Activation Technology!]

Edited by - Null and Void on August 18, 2001 9:12:42 PM
Sorry to interrupt you guys, but since it was mentioned... How ''accurate'' is GetTickCount? I know it has a limit as it wraps around after 50 days or so, and I also know it''s accurate down to 1000th of a second, but how much error does it have, a lot or little? Are there any alternatives to time keeping in games under Windows, which is just as simple to use? I''m wondering what everyone uses for time keeping in your games. Is GetTickCount the "standard" method used in most games?
Actually, PugPenguin, GetTickCount() is not very accurate. It is limited to the resolution of the system timer. For games, you should use timeGetTime() or a performance timer.

Edited by - Midnight Coder on August 19, 2001 3:30:53 AM
Advertisement
timeGetTime() has problems as well..

I used it and I experienced choppy animation. I logged the time passed to a text file...It looked something like this:

10 ms
10 ms
10 ms
20 ms
10 ms
10 ms
10 ms
20 ms
...
...

Then I switched to the HighPerformance Timer and output was the same each frame and my animation was smooth..

But if you are a beginner then don´t worry too much about it yet.



-- Look at you hacker. A Pathetic creature of meat and bone, panting and sweating as you run through my corridors. How can you challenge a perfect, immortal machine?--

"Yeah, I''ve seen people w/ ''so-called'' lives. They are petty and thoughtless." - Nazrix
-------------Ban KalvinB !
The thing is that each frame "staggers" in my game. If I let it run at full speed, it''s sort of ok but still, the time it takes to render each frame varies slightly (but, that slight difference is annoying) so scrolling is jerky. If I use GetTickCount to regulate it at a certain pace, again it staggers. I believe thats because the resolution of the timer isn''t good enough or have significant error in clocking it such that I can see the jerking motion.

Any ideas on a lot more accurate time keeping technique under windows would be appreciated.
i thinks this code is less acurate, but
defining a

bool tshot;

and a timer:

SetTimer(hwnd, TIMER_ID, 1000/30, NULL); // 30 fps

in windowproc:
  case WM_TIMER:		{			switch(wparam)			{			case TIMER:				{					fpsshot=true;				} break;			default: break;			}; //eo_switch			return(0);		} break; //eo_timer  


now, where you want to time, do this:
  if(fpsshot)	{	fpsshot=false;        show_frame	}        else        {        prepare_next_frame; //but with very light code        };  



i hope any of this made sense to you guys,
if you try it, tell me if it works better.
(probably won''t)















Hugo Ferreira
UniteK Future
"Concentrate, and you can see it. If you see it, then it is possible. If it is possible, you can Achieve it."
the WM_TIMER message is a low priority message, that means it might get bumped out of the queue if a more important message is recieved (or at least delayed). A more important message would be something like WM_PAINT, a few others as well.

I think your best bet is performance counters (check out QueryPerformanceCounter() in MSDN). I''ve heard of people having problems with performance counters (errors and so forth) but no where near as much as with GetTickCount() and also much less than timeGetTime(). Just be careful, because not all hardware has a performance counter (though I''m sure most newish hardware would) so maybe have a fallback to timeGetTime() if QueryPerformanceCounter() fails.


War Worlds - A 3D Real-Time Strategy game in development.

This topic is closed to new replies.

Advertisement