Countdown.. 3.. 2.. 1?

This is a discussion on Countdown.. 3.. 2.. 1? within the C++ Programming forums, part of the General Programming Boards category; I wanna make a game where you have a time limit to do something... is there a way to have ...

  1. #1
    drdroid
    Guest

    Question Countdown.. 3.. 2.. 1?

    I wanna make a game where you have a time limit to do something... is there a way to have a clock going on in the background without multithreading? I know this is somewhat related to a previous question I had but this question differs to the point where I needed to ask it... oh and I couldn't find it on the boards either... so the question again is....


    I wanna make a game where you have a time limit to do something... is there a way to have a clock going on in the background without multithreading? If so, how?

  2. #2
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    Cool Looked?

    Has anyone looked here yet?

  3. #3
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    no.
    hello, internet!

  4. #4
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    ...

    lol, just checking

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,641
    Just keep checking the clock() (include time.h), and divide by 1000 to get seconds. You can do so by sprinkling the function throughout the program, but that's messy. Threads are much better, more accurate, and fairly simple to set up. That's my suggestion...
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  6. #6
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    dos?

    but can you multithread in dos programs?

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,641
    Of course. I believe the function originally used was beginthread. But since console proggies these days are merely Windows programs in disguise, you'd be better off using CreateThread().

    Try this:


    Code:
    
    /*
    
    Keep these in mind...
    
    BOOL SuspendThread(HANDLE thread);
    BOOL ResumeThread(HANDLE thread);
    BOOL SetThreadPriority(HANDLE thread, int priority); 
    int GetThreadPriority(HANDLE thread);
    
    THREAD_PRIORITY_IDLE
    THREAD_PRIORITY_LOWEST
    THREAD_PRIORITY_BELOW_NORMAL
    THREAD_PRIORITY_NORMAL
    THREAD_PRIORITY_ABOVE_NORMAL
    THREAD_PRIORITY_HIGHEST
    THREAD_PRIORITY_TIME_CRITICAL
    
    */
    
    
    
    #define THREAD HANDLE
    #define THREADFUNC DWORD WINAPI
    
    
    
    THREAD
     NewThread( DWORD func(LPVOID), LPVOID params = NULL, DWORD *id = NULL)
    {
      static DWORD fakeIt = 0;
    
      if(id == NULL)
       {
        fakeIt++;
        id = &fakeIt;
       }
      return CreateThread(NULL, 0, func, (LPVOID)params, 0, id);
    }
    
    
    
    
    THREAD
     NewThreadWaiting( DWORD func(LPVOID), LPVOID params, DWORD *id = NULL)
    {
      static DWORD fakeIt = 0;
    
      if(id == NULL)
       {
        fakeIt++;
        id = &fakeIt;
       }
      return CreateThread(NULL, 0, func, params, CREATE_SUSPENDED, id);
    }
    
    
    
    
    
    bool ThreadIsActive(THREAD t)
    {
     DWORD status = 0;
    
     GetExitCodeThread(t, &status);
    
     if(status == STILL_ACTIVE)
     return true;
    
     CloseHandle(t);
    
     return false;
    }

    About threads:


    When you create a thread there will be a couple of things to do/remember:

    1) Define the function to pass to "NewThread()"

    2) A thread will not hold a console program "open", thus at the end of the program, be sure to wait for the thread before exiting.

    3) There is no guarantee when a thread begins, exits, and how long it gets to execute each time slice.

    4) When in doubt, USE A critical section, mutex, or semaphore to protect shared data.

    Here's a sample app:


    Code:
    
    THREADFUNC ThreadFunc( void *param ) //...the parameter is mandatory, even if unused... 
    {
     printf("\n I live!!!");
     
     printf("\n Message: %s", (param == NULL) ? "none" : (char*)param);
    
     Sleep(5000);
    
     printf("\n I'm dying!!!");
    }
    
    
    int main()
    {
     char msg[] = "Thread Two Gets A Message.";
    
     THREAD thread = NewThread( ThreadFunc );
     
     THREAD alert = NewThread( ThreadFunc, (void*)msg);
     
     while( ThreadIsActive(thread) );    //...wait around...
     
     while( ThreadIsActive(alert) );    //...wait around...
     
     return 0;
    }
    Have fun!
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  8. #8
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    Question declaration syntax error

    it gives me a declaration syntax error for the following:

    Code:
    THREADFUNC ThreadFunc( void *param ) //...the parameter is mandatory, even if unused...

  9. #9
    Sebastiani
    Guest
    Did you include windows.h?

    If so, try:

    DWORD WINAPI ThreadFunc(void* param);

    Otherwise, I'm not sure. What IDE(compiler) do you use?

  10. #10
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    Borland

    Borland

  11. #11
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    I found this to be a nice overview of threads.. http://blacksun.box.sk/tutorials.php?id=150 hopefully you can get something out of it.

  12. #12
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    multithreading shouldn't be necessary. Games typically have to check the time EVERY time through their animation loop. This is so they can correctly time the movements. Since you will need to check the time in the loop anyway you might as well check to see if time has expired too.

  13. #13
    . Driveway's Avatar
    Join Date
    May 2002
    Posts
    469
    Did he say it was a game?

  14. #14
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297

    Re: Countdown.. 3.. 2.. 1?

    Originally posted by drdroid
    I wanna make a game....
    yup

  15. #15
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Yes, read the first post.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Day countdown
    By putty88 in forum C Programming
    Replies: 5
    Last Post: 02-15-2009, 05:55 PM
  2. Countdown
    By Zagaberoo in forum C++ Programming
    Replies: 3
    Last Post: 09-26-2004, 05:26 PM
  3. Replies: 4
    Last Post: 08-13-2003, 07:25 PM
  4. delay()?/sleep()?/need for countdown/
    By Robin Hood in forum C++ Programming
    Replies: 2
    Last Post: 07-19-2002, 04:04 PM
  5. Whats wrong with my countdown program?
    By Golden Bunny in forum C++ Programming
    Replies: 3
    Last Post: 04-23-2002, 01:15 PM

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