Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. make a timer

make a timer

Scheduled Pinned Locked Moved C / C++ / MFC
questiongame-devhelp
9 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hasani2007
    wrote on last edited by
    #1

    Hello I want to make a timer in maze.The timer should countdown to reach 0 and if the player doesn't reach to end he/she lose the game. I want to denote that if player reach to end timer should stopped working. How can I do it? Thanks anyone help me.

    R M K S A 5 Replies Last reply
    0
    • H hasani2007

      Hello I want to make a timer in maze.The timer should countdown to reach 0 and if the player doesn't reach to end he/she lose the game. I want to denote that if player reach to end timer should stopped working. How can I do it? Thanks anyone help me.

      R Offline
      R Offline
      Rajesh R Subramanian
      wrote on last edited by
      #2

      I hope that you have the code for your "maze". What kind of application is it? Assuming your main thread has a message pump, you could run a timer on a separate thread, and have it post a message to the main thread after the time is over. There may be other messages to be processed in the queue, but then again, Windows isn't an RTOS...

      Workout progress:
      Current arm size: 14.4in
      Desired arm size: 18in
      Next Target: 15.4in by Dec 2010

      Current training method: HIT

      C 1 Reply Last reply
      0
      • R Rajesh R Subramanian

        I hope that you have the code for your "maze". What kind of application is it? Assuming your main thread has a message pump, you could run a timer on a separate thread, and have it post a message to the main thread after the time is over. There may be other messages to be processed in the queue, but then again, Windows isn't an RTOS...

        Workout progress:
        Current arm size: 14.4in
        Desired arm size: 18in
        Next Target: 15.4in by Dec 2010

        Current training method: HIT

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #3

        Rajesh R Subramanian wrote:

        but then again, Windows isn't an RTOS...

        Too bad, sir: I need to kick out the player with the nanosecond precision... :-D :laugh: :-D

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        1 Reply Last reply
        0
        • H hasani2007

          Hello I want to make a timer in maze.The timer should countdown to reach 0 and if the player doesn't reach to end he/she lose the game. I want to denote that if player reach to end timer should stopped working. How can I do it? Thanks anyone help me.

          M Offline
          M Offline
          Maximilien
          wrote on last edited by
          #4

          Create a secondary thread with a CTimer and a countdown counter; I don't think this is much difficult. At each "pulse" (either compare delay or Sleep), send a message to the main application thread to display the time left and when the countdown is finished, send a message to the main application to display "You FAIL". Max.

          Watched code never compiles.

          S 1 Reply Last reply
          0
          • M Maximilien

            Create a secondary thread with a CTimer and a countdown counter; I don't think this is much difficult. At each "pulse" (either compare delay or Sleep), send a message to the main application thread to display the time left and when the countdown is finished, send a message to the main application to display "You FAIL". Max.

            Watched code never compiles.

            S Offline
            S Offline
            Stephen Hewitt
            wrote on last edited by
            #5

            Why bother with a secondary thread if you're going to use window messages to inform the main thread? A vanilla timer set using SetTimer[^] is quite capable of achieving the same result more efficiently.

            Steve

            1 Reply Last reply
            0
            • H hasani2007

              Hello I want to make a timer in maze.The timer should countdown to reach 0 and if the player doesn't reach to end he/she lose the game. I want to denote that if player reach to end timer should stopped working. How can I do it? Thanks anyone help me.

              K Offline
              K Offline
              KarstenK
              wrote on last edited by
              #6

              a plain overview from a pro: http://www.mvps.org/directx/articles/selecting_timer_functions.htm[^] this should interest you http://msdn.microsoft.com/en-us/magazine/cc163996.aspx[^]

              Press F1 for help or google it. Greetings from Germany

              1 Reply Last reply
              0
              • H hasani2007

                Hello I want to make a timer in maze.The timer should countdown to reach 0 and if the player doesn't reach to end he/she lose the game. I want to denote that if player reach to end timer should stopped working. How can I do it? Thanks anyone help me.

                S Offline
                S Offline
                Software_Developer
                wrote on last edited by
                #7

                Sir, Here is your timer hasani2007. Its in 'dirty c code', clean it up. set the variable count_down_time_in_secs in seconds for desired count down time. The Code is self explanatory. 1 minute = 60 secs 5 minutes = 60x5 secs 30 minutes =60x30 secs 1 hour = 60x60 secs

                #include <stdio.h>
                #include <time.h>

                int main ()
                {

                unsigned int x\_hours=0;
                unsigned int x\_minutes=0;
                unsigned int x\_seconds=0;
                unsigned int x\_milliseconds=0;
                unsigned int totaltime=0,count\_down\_time\_in\_secs=0,time\_left=0;
                
                clock\_t x\_startTime,x\_countTime;
                count\_down\_time\_in\_secs=10;  // 1 minute is 60, 1 hour is 3600
                
                
                x\_startTime=clock();  // start clock
                time\_left=count\_down\_time\_in\_secs-x\_seconds;   // update timer
                
                while (time\_left>0) 
                {
                	x\_countTime=clock(); // update timer difference
                	x\_milliseconds=x\_countTime-x\_startTime;
                	x\_seconds=(x\_milliseconds/(CLOCKS\_PER\_SEC))-(x\_minutes\*60);
                	x\_minutes=(x\_milliseconds/(CLOCKS\_PER\_SEC))/60;
                	x\_hours=x\_minutes/60;
                
                
                 
                
                	time\_left=count\_down\_time\_in\_secs-x\_seconds; // subtract to get difference 
                
                
                	printf( "\\nYou have %d seconds left ( %d ) count down timer by TopCoder 
                

                ",time_left,count_down_time_in_secs);
                }

                printf( "\\n\\n\\nTime's out\\n\\n\\n");
                

                return 0;
                }

                . . . TopCoder

                S 1 Reply Last reply
                0
                • S Software_Developer

                  Sir, Here is your timer hasani2007. Its in 'dirty c code', clean it up. set the variable count_down_time_in_secs in seconds for desired count down time. The Code is self explanatory. 1 minute = 60 secs 5 minutes = 60x5 secs 30 minutes =60x30 secs 1 hour = 60x60 secs

                  #include <stdio.h>
                  #include <time.h>

                  int main ()
                  {

                  unsigned int x\_hours=0;
                  unsigned int x\_minutes=0;
                  unsigned int x\_seconds=0;
                  unsigned int x\_milliseconds=0;
                  unsigned int totaltime=0,count\_down\_time\_in\_secs=0,time\_left=0;
                  
                  clock\_t x\_startTime,x\_countTime;
                  count\_down\_time\_in\_secs=10;  // 1 minute is 60, 1 hour is 3600
                  
                  
                  x\_startTime=clock();  // start clock
                  time\_left=count\_down\_time\_in\_secs-x\_seconds;   // update timer
                  
                  while (time\_left>0) 
                  {
                  	x\_countTime=clock(); // update timer difference
                  	x\_milliseconds=x\_countTime-x\_startTime;
                  	x\_seconds=(x\_milliseconds/(CLOCKS\_PER\_SEC))-(x\_minutes\*60);
                  	x\_minutes=(x\_milliseconds/(CLOCKS\_PER\_SEC))/60;
                  	x\_hours=x\_minutes/60;
                  
                  
                   
                  
                  	time\_left=count\_down\_time\_in\_secs-x\_seconds; // subtract to get difference 
                  
                  
                  	printf( "\\nYou have %d seconds left ( %d ) count down timer by TopCoder 
                  

                  ",time_left,count_down_time_in_secs);
                  }

                  printf( "\\n\\n\\nTime's out\\n\\n\\n");
                  

                  return 0;
                  }

                  . . . TopCoder

                  S Offline
                  S Offline
                  Stephen Hewitt
                  wrote on last edited by
                  #8

                  A busy loop is perhaps not the best way to go about things.

                  Steve

                  1 Reply Last reply
                  0
                  • H hasani2007

                    Hello I want to make a timer in maze.The timer should countdown to reach 0 and if the player doesn't reach to end he/she lose the game. I want to denote that if player reach to end timer should stopped working. How can I do it? Thanks anyone help me.

                    A Offline
                    A Offline
                    Aescleal
                    wrote on last edited by
                    #9

                    Hi Folks, nothing like coming late to the party but as no one has mentioned the way most games are written I thought I'd stick me oar in... In any game the timing is worked into the main dispatch loop. Every cycle around your dispatch loop you grab input, you update the timer, evolve the rest of the system based on how much time has elapsed and then render the game world according to what's happens. It looks something like this:

                    unsigned last_time = get_current_time_in_ms();

                    while( true )
                    {
                    /*
                    Record who's got keys pressed, where the mouse is, network stuff
                    */

                    update\_IO\_state();
                    
                    /\*
                        Sort out how much time has elapsed and how much time we have to evolve the game state over
                     \*/
                    
                    unsigned time\_now = get\_current\_time\_in\_ms();
                    unsigned ms\_this\_loop = time\_now - last\_time;
                    last\_time = time\_now;
                    
                    /\*
                        Change the state of all the game objects
                     \*/
                    
                    evolve( ms\_this\_loop );
                    
                    /\*
                        Display the changes
                     \*/
                    
                    render();
                    

                    }

                    All the functions take some additional parameters depending on how you store your game state. The key point of this lot is that the game hammers along flat out - the thread this is on will eat a CPU. It churns out frames as often as it can and slows down when there's something system intensive happening. Oh, and if you do your IO on another thread make sure everything is synchronised properly or something weird WILL happen, possibly not on your computer but on your first customer's computer. Hope that helps, Cheers, Ash

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups