Something like GetTickCount()

This is a discussion on Something like GetTickCount() within the C++ Programming forums, part of the General Programming Boards category; I need to get milliseconds at one point, milliseconds at another point, and see how many milliseconds elapsed between those ...

  1. #1
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    Something like GetTickCount()

    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

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    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).

  3. #3
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    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:

    Code:
    while (loopity_loop) {
    
    clock_t f = clock();
    
    /*
        ... all of the code ...
    */
    
    while ((clock() - f) < 90)
          ;
    }
    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"?

    Thanks for any help!
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Well, that code is correct. This, for example, prints every second using the same technique you used. It works fine with me.

    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;
    }
    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:

    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Sleep and GetTickCount().
    By IamBMF in forum C Programming
    Replies: 13
    Last Post: 02-03-2008, 08:25 AM
  2. GetTickCount()
    By ElastoManiac in forum Windows Programming
    Replies: 3
    Last Post: 12-17-2005, 02:49 PM
  3. timeGetTime(), getSystemTime(), GetTickCount()
    By Shadow12345 in forum Windows Programming
    Replies: 1
    Last Post: 11-02-2002, 09:35 AM
  4. Time.h and GetTickCount()
    By SlimDady in forum C++ Programming
    Replies: 4
    Last Post: 04-12-2002, 09:50 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21