Thread: Timer for C++ console reflex game

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    28

    Timer for C++ console reflex game

    What's the most efficient way to implement a timer on a C++ console program? Any hope it would be portable to MFC Application? I need up to millisecond precision. This need is mainly for a reflex game I'll be coding, but it'll also be used for some other general purpose programs.

    Thanks in advance,

    Mariano Lopez-Gappa (20) from Argentina.

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    You may use the high resolution timer API functions:

    ::QueryPerformanceCounter(..)
    ::QueryPerformanceFrequency(..)

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    28
    i'm not familiar with this functions, what library should I include? does it work both in console and MFC application? would you briefly describe how they work?

    Thanks in advance,

    Mariano Lopez-Gappa

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    28
    I've run a little google search and found out some sort of explanation, but it seems that it would not work on lazy DOS PC's, like the 486dx2 I'm planning to code with...I should have said this earlier, shouldn't I? What do I do?

    Thx in advance

    Mariano Lopez-Gappa

  5. #5

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    28
    THX A LOT!!!! That's what I needed!!

    Just one more thing: this solved most of the problem, but should I place this:

    Code:
    time (&end);
    dif = difftime (end,start);
    inside a while? (this because events must be triggered at regular intervals) This question aims at efficiency of processor usage...

    Thx in advance!

    Mariano Lopez-Gappa

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    28
    shoot I just realized that this method won't go further than seconds, back to square 1...

  8. #8
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Well that's pretty crazy. I mean, is there even a DOS API like the WinAPI? I don't think so, I haven't been around long enough but that's like goddamn real mode and interrupts and wierd stuff. Are you kidding when you said that? I mean most of these API functions are backwards compatible to ~95 but past then it's like, whoa? clock(..) will have several ticks per second, but it's certainly not the millisecond timing you wanted.

  9. #9
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282
    Look at all the functions and your can work it out.
    But I did it for you anyway…
    You can make it a class so it can be used easier, but this should give you the idea.

    Code:
    #include <iostream>
    #include <ctime>
    #include <string>
    
    using namespace std;
    
    
    
    int main ()
    {
    
    	clock_t start;
    	clock_t end;
    	double total;
    	string garbage;
    
    	
    
    	cout << "Type in the word 'Switzerland': ";
    
    
    	start = clock();
    	cin >> garbage;
    	end = clock();
    
    
    	total = end-start;
    
    	cout << "It took you " << total << " ticks which in\n";
    	cout << "seconds is : " << total/CLOCKS_PER_SEC;
    
     
      return 0;
    }

  10. #10
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Well on this machine CLOCKS_PER_SEC is generously #define'd at 1000, but certainly other machines might be different?

  11. #11
    Registered User
    Join Date
    Nov 2005
    Posts
    28
    oh ok, Enahs code seems portable to DOS, I didn't know time would work further than seconds.

    PROBLEM SOLVED !!! THX TONTO AND ENAHS

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game Designer vs Game Programmer
    By the dead tree in forum Game Programming
    Replies: 8
    Last Post: 04-28-2005, 09:17 PM
  2. My best c++ console game
    By vasanth in forum C++ Programming
    Replies: 19
    Last Post: 03-06-2002, 03:26 AM
  3. Game Console in OpenGL
    By HellRaiZer in forum Game Programming
    Replies: 2
    Last Post: 02-27-2002, 12:52 AM
  4. My next 'dos console' game...
    By Leeman_s in forum C++ Programming
    Replies: 5
    Last Post: 11-09-2001, 06:08 AM
  5. Best game console
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-05-2001, 09:25 PM