Ok, i have my code:

int seconds;
	clock_t clock();
	seconds = clock()/CLOCKS_PER_SEC;
			
	if (seconds == 8){
			FIVE.SetPositionX(-1);
			}
//....
if ((AdventureGame.keypressed[DIK_F]) && (WOO.positionX == -10)){
		
		//reset clock() here
		FIVE.SetPositionX(-5);}

the clock starts when the program is run and after 8 secs, FIVE moves position.
When i press F, i want FIVE to reset its position [which it does], and to reset the clock(), so that after 8 secs, FIVE will move again.
Anyway to do this?
Using Windows 7, visual studio.

Make a simple timer class to simplify stuff. For example :

#include <iostream>
#include <ctime>

using namespace std;

class ClockTimer{
public:
	typedef time_t TimeType;
private:
 TimeType startTime;
public:
 ClockTimer(): startTime(clock()){
 }
 TimeType elapsed(){
    return clock() - startTime;
 }
 TimeType elapsedInSec(){
   return  TimeType(elapsed()/float(CLOCKS_PER_SEC));
 }
 void start(){
    startTime = clock();
 }
 void reset(){
    startTime = clock();
 }
};

void wait(unsigned long sec){
	ClockTimer timer;
	timer.start();
	while(timer.elapsedInSec() < sec) continue;	
}
int main(){
	cout << "Loading...\n";
	wait(5); //seconds
	cout << "Done\n";
	
	ClockTimer timer;
	timer.start();
	while(true){
		if(timer.elapsedInSec() > 1 ){			
			cout << "moving character\n";
			timer.reset();
		}
	}
}

the clock starts when the program is run and after 8 secs, FIVE moves position.
When i press F, i want FIVE to reset its position [which it does], and to reset the clock(), so that after 8 secs, FIVE will move again.
Anyway to do this?
Using Windows 7, visual studio.

Short answer is you can't reset clock() . It always returns the ticks elapsed since the program started.

What you can do is keep around some sort of "seconds since last time FIVE moved" variable, and reset it to the current number of ticks when you want to reset the clock. It might look like this:

int seconds;
clock_t clock();

// This keeps track of the start time for your 8-second timer
clock_t timerStart = clock();

// I added this to keep track of whether or not we're using the 8-second timer.
// This way, we can stop the timer when it triggers
bool timerOn = true;

while(true) // I'm assuming your code has this in a loop somewhere...
{
  // Only check the timer if it's "on"
  if(timerOn)
  {
    // Track only the number of seconds since timerStart
    seconds = (clock() - timerStart) / CLOCKS_PER_SEC;
		
    // Changed "==" to ">=" here; this catches instances where one check ends up
    // at 7 seconds, but the loop takes so long that the next one reads 9 seconds
    if (seconds >= 8)
    {
      FIVE.SetPositionX(-1);

      // Turning off the timer here; we don't want FIVE to keep moving. Right?
      // That's how I understood your intent, anyway
      timerOn = false;
    }
  }

  //....

  if((AdventureGame.keypressed[DIK_F]) && (WOO.positionX == -10)
  {
    // Reset clock() here--actually, just track a new start time
    timerStart = clock();

    // Turn the timer back on
    timerOn = true;

    FIVE.SetPositionX(-5);}
  }
}

It's a little cluttered because I didn't put all of this into a timer class or anything; does this make sense?

Make a simple timer class to simplify stuff. For example :

Beat me to it! =)

This is better; the timer details don't distract from your main loop.

Here's a timer class of mine that's put together a little differently. On Win32 platforms, it uses the Windows high-resolution timer if it's available, or clock() if it's not. On non-Win32 platforms, it compiles to use gettimeofday() .

Also of possible interest: clock vs gettimeofday

Short answer is you can't reset clock() . It always returns the ticks elapsed since the program started.

What you can do is keep around some sort of "seconds since last time FIVE moved" variable, and reset it to the current number of ticks when you want to reset the clock. It might look like this:

int seconds;
clock_t clock();

// This keeps track of the start time for your 8-second timer
clock_t timerStart = clock();

// I added this to keep track of whether or not we're using the 8-second timer.
// This way, we can stop the timer when it triggers
bool timerOn = true;

while(true) // I'm assuming your code has this in a loop somewhere...
{
  // Only check the timer if it's "on"
  if(timerOn)
  {
    // Track only the number of seconds since timerStart
    seconds = (clock() - timerStart) / CLOCKS_PER_SEC;
		
    // Changed "==" to ">=" here; this catches instances where one check ends up
    // at 7 seconds, but the loop takes so long that the next one reads 9 seconds
    if (seconds >= 8)
    {
      FIVE.SetPositionX(-1);

      // Turning off the timer here; we don't want FIVE to keep moving. Right?
      // That's how I understood your intent, anyway
      timerOn = false;
    }
  }

  //....

  if((AdventureGame.keypressed[DIK_F]) && (WOO.positionX == -10)
  {
    // Reset clock() here--actually, just track a new start time
    timerStart = clock();

    // Turn the timer back on
    timerOn = true;

    FIVE.SetPositionX(-5);}
  }
}

It's a little cluttered because I didn't put all of this into a timer class or anything; does this make sense?

Hi, i have tried this, but can not get it to work.
From what im reading, doesn't clock() and timerStart have the same value? so it will not start?

Hi, i have tried this, but can not get it to work.
From what im reading, doesn't clock() and timerStart have the same value? so it will not start?

Read about clock() . It's not really a "timer"--all it does is tell you what the time is when you call it. In order to find out how much time has passed, you have to call clock() again, and compare the two values.

From my (admittedly not super-clear) example:

clock_t timerStart = clock();

This happens right before the main loop. It tells us what time it was when the loop started.

Then, in the loop:

// Track only the number of seconds since timerStart
seconds = (clock() - timerStart) / CLOCKS_PER_SEC;

...figures out how many seconds since the loop started.

The call to clock() here happens every time through the loop, so it's possible that for the first time--or more, depending on how fast the loop is--it might return the same as the initial call to clock() . Eventually, though, enough time will pass and we're looking at the time elapsed since we assigned timerStart originally.

Then, when it's time to start counting again:

// Reset clock() here--actually, just track a new start time
timerStart = clock();

..reassigns timerStart , so we're measuring from the moment clock() gets called for the assignment--this effectively resets the timer.

So we've built a simple timer from a "what time is it?" library function. The timer classes are doing the same thing, just in an encapsulated, object-oriented sort of way.

Does that help any?

Thanks, got it to work now :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.