I need to get milliseconds at one point, milliseconds at another point, and see how many milliseconds elapsed between those two points. I cannot use GetTickCount as I cannot include windows.h. How can I do this, perhaps using _ftime()?
Thanks!
I need to get milliseconds at one point, milliseconds at another point, and see how many milliseconds elapsed between those two points. I cannot use GetTickCount as I cannot include windows.h. How can I do this, perhaps using _ftime()?
Thanks!
"He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson
Try using clock() inside <ctime> (or <ctime.h>). On 99% of the computers, it returns milliseconds so you can do your stopwatch. CLOCKS_PER_SEC is a constant which tells you how many ticks are in one second (1000 on most compilers -- milliseconds).
Also, I guess it would help to know exactly what I'm trying to do. Basically I want to make sure one game cycle takes at least a certain number of milliseconds to run, so that it won't be zooming at ungodly frame rates on really fast computers.
Speedy5: I tried doing this:
But it didn't seem to work. Does my code say, "if the loop was done in less than 90 milliseconds, wait until 90 milliseconds have passed before looping again"?Code:while (loopity_loop) { clock_t f = clock(); /* ... all of the code ... */ while ((clock() - f) < 90) ; }
Thanks for any help!
"He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson
Well, that code is correct. This, for example, prints every second using the same technique you used. It works fine with me.
For what you're doing, its not a good idea. You're game should NEVER limit framerate. There are other techniques to figure zoom and things. Instead on basing your calculations on 'number of frames,' base them on time -- the milliseconds since boot (clock()). Thats always better than waiting 90 milliseconds. For example, if you're sprite has a speed of 5 tiles a second:Code:int _tmain(int argc, _TCHAR* argv[]) { clock_t time; cout << "Start" << endl; for (int i=0; i<5; i++) { time=clock(); // stuff here while (clock()-time<1000); cout << "Sec " << (i+1) << endl; } cout << "End" << endl; return 0; }
Instead of speed=5/FPS and add speed to the coordinates at every frame, do speed=5/sec and add speed that happens during(lasttime-currenttime). All you need to do is keep the time of the last frame and the current time in variables. Its all simple math if you think about it.