Sign in to follow this  

How to create a simple time counter?

This topic is 4662 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Record the time at the start of your level using time(NULL), and then every frame get the current time. Subtract the start time from the current time, and format it.
Here's some source:

// At the start of your level, save this value somewhere (global or preferably a member varaible):
time_t m_timeStart = time(NULL);

// Every frame get the current time:
time_t timeCurr = time(NULL);
time_t timeTaken = timeCurr-m_timeStart;

// Format the time into a string:
int nHours = timeTaken/(60*60);
timeTaken -= (nHours*60*60);
int nMins = timeTaken/60;
timeTaken -= (nMins*60);
char szTime[16];
sprintf("%02d:%02d:%02d",nHours,nMins,timeTaken);

// Display szTime somehow (However you display text in SDL)

Share this post


Link to post
Share on other sites

#include <ctime>

class Timer
{
public:
Timer()
{
StartTime = clock();
}

int getHours()
{
return getMinutes()/60;
}

int getMinutes()
{
return getSeconds()/60;
}

int getSeconds()
{
return (clock()-StartTime)/1000;
}

private:
clock_t StartTime};




Thats just a rogh outline. I only just wrote it and I hven't tested it, but it should be enough to give you the general idea.

Share this post


Link to post
Share on other sites
Sorry. Wasn't really thinking.


class Timer
{
private:
clock_t StartTime;
enum TimeValues
{
MillisecsPerSec = 1000,
SecsPerMin = 60,
MinsPerHour = 60,
MillisecsPerMin = MillisecsPerSec*SecsPerMinute,
MillisecsPerHour= MillisecsPerMin*MinsPerHour
};

public:
Timer()
{
StartTime = clock();
}

int getHours()
{
return (clock()-StartTime)/MillisecsPerHour;
}

int getMinutes()
{
return (clock()-StartTime)/MillisecsPerMin;

}

int getSeconds()
{
return (clock()-StartTime)/MillisecsPerSec;
}


Share this post


Link to post
Share on other sites
I love you all!
Here is my class:

timer.hpp

#ifndef TIMER_HPP
#define TIMER_HPP

#include <time.h>

class C_Timer {
private:
clock_t startTime;
const unsigned int millisecsPerSec;
const unsigned int secsPerMin;
const unsigned int minsPerHour;
const unsigned int millisecsPerMin;
const unsigned int millisecsPerHour;
public:
C_Timer();
unsigned int getHours() const;
unsigned int getMinutes() const;
unsigned int getSeconds() const;
};


#endif




timer.cpp

#ifndef TIMER_HPP
#include "timer.hpp"
#endif


C_Timer::C_Timer() :
millisecsPerSec( 1000 ),
secsPerMin( 60 ),
minsPerHour( 60 ),
millisecsPerMin( millisecsPerSec * secsPerMin ),
millisecsPerHour( millisecsPerMin * minsPerHour ) {
startTime = clock();
}

unsigned int C_Timer::getHours() const {
return (clock() - startTime) / millisecsPerHour;
}

unsigned int C_Timer::getMinutes() const {
return (clock() - startTime) / millisecsPerMin;
}

unsigned int C_Timer::getSeconds() const {
return (clock() - startTime) / millisecsPerSec;
}



Thanks a lot!!!

Share this post


Link to post
Share on other sites

This topic is 4662 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this