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?

Edited 6 Years Ago by gusano79: typos

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

Attachments
#include "Timer.h"

Timer::Timer()
{
    baseTime = 0.0;
    PlatformInitialize();
}

double Timer::GetTime()
{
    return PlatformGetTime() - baseTime;
}

void Timer::Reset()
{
    baseTime = PlatformGetTime();
}

//----------------------------------------------------------------------------
// Platform-specific stuff follows:
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
#ifdef _WIN32 // Win32; use Windows QueryPerformanceFrequency API.
//----------------------------------------------------------------------------

#include <windows.h>
#include <time.h>
#include <math.h>

static bool initialized = false;
static BOOL highResolution = FALSE;
static LARGE_INTEGER hiResFrequency;

void Timer::PlatformInitialize()
{
    if(!initialized)
    {
        highResolution = QueryPerformanceFrequency(&hiResFrequency);
        initialized = true;
    }
}

double Timer::PlatformGetTime()
{
    if(highResolution)
    {
        LARGE_INTEGER time;

        if(QueryPerformanceCounter(&time))
        {
            return (double)time.QuadPart / hiResFrequency.QuadPart;
        }
        else
        {
            return NAN;
        }
    }
    else
    {
        return (double)clock() / CLOCKS_PER_SEC;
    }
}

//----------------------------------------------------------------------------
#else // Not Win32; use gettimeofday.
//----------------------------------------------------------------------------

#include <sys/time.h>

Timer::PlatformInitialize()
{
    // Nothing to initialize for gettimeofday;
}

double Timer::PlatformGetTime()
{
    timeval time;

    gettimeofday(&time, (void*)0);

    return time.tv_sec + (time.tv_usec / 1000000.0);
}

//----------------------------------------------------------------------------
#endif
//----------------------------------------------------------------------------
#ifndef TIMER_H
#define TIMER_H

class Timer
{
private:
    double baseTime;

    void PlatformInitialize();
    double PlatformGetTime();

public:
    Timer();

    double GetTime();
    void Reset();
};

#endif

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?

This question has already been answered. Start a new discussion instead.