Thread: Timing~

  1. #1
    Supar
    Guest

    Timing~

    Alright i have a question...i did search the boards...but it didnt give me quite what i needed...what i need is like to be able to assign a time to something...and still be able to do something...while that timer is going...like in a game...you are doing your move...but the 'computer' hits you because you've taken to long to do your turn...

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    In a single threaded program, the easiest way is to have your long block of code periodically check to see if it has taken to long, then interrupt itself. Not very accurate, but easy to implement, and platform independent.

    To do this properly requires asychronous techniques which tend to be operating system dependent.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Supar
    Guest
    Ah what is the 'timer' command? all ive used so far that deals with time is Sleep(x); and well srand(time(NULL));....but i think i understand what you are saying...yea wont be to accurate...but it could almost simulate a 'real time'...

  4. #4
    Unregistered
    Guest
    Use functions in time.h or ctime (depending on your compiler). Somehing like this:

    time_t starttime = time(NULL);
    time_t now;
    long diff = 0;
    const long someNumber = 1000;

    while(diff < someNumber)
    {
    //gamecode;
    now = time(NULL);
    diff = difftime(now, starttime);//look up exact prototype on your own
    }

    Or

    //gamecode
    now = time(NULL);
    if(difftime(now, starttime) < someNumber)
    {
    //continue current strategy;
    }
    else
    {
    starttime = now;
    //do something new;
    }

    etc. All types of ways to build it in, but all approximate.

  5. #5
    Supar
    Guest
    Hm...sorry...but heh...most of that didnt make sense to me...i used it...but if you could go through and explain what everything does...that would be great...always helps to know what you are doing~

Popular pages Recent additions subscribe to a feed