Skip to main content
gamedev.net
🔒 Locked

Help With my timer class

Started by Ademan555 Jan 2, 2004 at 2:38 AM 18 replies 1.7k views
Original Post
Ademan555
Ademan555
i have two test programs for my timer class, here they are
#include <iostream>
#include <conio.h>
#include "DCTIMER.h"
DWORD CTIMER::TicksPerS=0;

int main()
{
	CTIMER timer;
	DWORD Time=0;
	char bleh=''d'';
	timer.InitTimers();
	while (bleh!=''q'')
	{
		bleh=getch();
		std::cout<<timer.GetPassed(0)<<''\n'';
	}
	return 0;
}

//GetPassed function (in DCTIMER.h)

DWORD CTIMER::GetPassedMS()
{
	__int64 Temp=0;
	DWORD Passed;
	if (QueryPerformanceCounter((LARGE_INTEGER*)&Temp))		//if Query Successful

	{
		Temp/=(CTIMER::TicksPerS*1000);						//convert to MS

		Passed=Temp-CTIMER::LASTCALL;						//Get time passed

		CTIMER::LASTCALL=Temp;								//store Temp in LASTCALL

		return Passed;
	}
	return FALSE;
}

//Init Timers Function


void CTIMER::InitTimers()
{
	LARGE_INTEGER Temp;
	Temp.QuadPart=0;
	if (QueryPerformanceFrequency(&Temp))//Get Ticks Per Second

	CTIMER::TicksPerS=Temp.LowPart;		 //Store in Class

}


this one seems to work great save for the fact taht it seems a bit slow, and the first number it gives me is something around 4234546 secondly... this doesnt have any output at all
#include <iostream>
#include <conio.h>
#include "c:\\documents and settings\\dan\\my documents\\visual studio projects\\life as it should be\\DCTIMER.h"
DWORD CTIMER::TicksPerS=0;

int main()
{
	CTIMER timer;
	DWORD Time=0;
	char bleh=''d'';
	timer.InitTimers();
	while (bleh!=''q'')
	{
		if (timer.HasPassedMS((DWORD)1000))
		{
			std::cout<<timer.GetPassed(0)<<''\n'';
		}
	}
	return 0;
}

//HasPassedMS function (in DCTIMER.h)


bool CTIMER::HasPassedMS(DWORD time)
{
	__int64 Temp=0;
	if (QueryPerformanceCounter((LARGE_INTEGER*)&Temp)) //if successful

	{
		Temp/=(CTIMER::TicksPerS*1000);					//convert to MS

		if ((Temp-CTIMER::LASTCALL)>time)
		{
			CTIMER::LASTCALL=Temp;
			return TRUE;
		}
		return FALSE;
	}
	return FALSE;
}

Thanx for lookin at it -Dan Yes I realize im a n00b...
When General Patton died after World War 2 he went to the gates of Heaven to talk to St. Peter. The first thing he asked is if there were any Marines in heaven. St. Peter told him no, Marines are too rowdy for heaven. He then asked why Patton wanted to know. Patton told him he was sick of the Marines overshadowing the Army because they did more with less and were all hard-core sons of bitches. St. Peter reassured him there were no Marines so Patton went into Heaven. As he was checking out hi
Ademan555
Ademan555
...
When General Patton died after World War 2 he went to the gates of Heaven to talk to St. Peter. The first thing he asked is if there were any Marines in heaven. St. Peter told him no, Marines are too rowdy for heaven. He then asked why Patton wanted to know. Patton told him he was sick of the Marines overshadowing the Army because they did more with less and were all hard-core sons of bitches. St. Peter reassured him there were no Marines so Patton went into Heaven. As he was checking out hi
Ademan555
Ademan555
40+ views and no responses?? cmon, if i can do anything to make it more clear ask, i really need help on this

-Dan

Yes I realize im a n00b...
When General Patton died after World War 2 he went to the gates of Heaven to talk to St. Peter. The first thing he asked is if there were any Marines in heaven. St. Peter told him no, Marines are too rowdy for heaven. He then asked why Patton wanted to know. Patton told him he was sick of the Marines overshadowing the Army because they did more with less and were all hard-core sons of bitches. St. Peter reassured him there were no Marines so Patton went into Heaven. As he was checking out hi
Ademan555
Ademan555
heres the constructor


CTIMER::CTIMER()
{
CTIMER::TicksPerS=0;
QueryPerformanceCounter((LARGE_INTEGER*)&(CTIMER::LASTCALL));
}


the destructor shouldnt matter

Oh, and in case i didnt specify, LASTCALL is an _int64 taht stores the last time the function was called (in milliseconds)
When General Patton died after World War 2 he went to the gates of Heaven to talk to St. Peter. The first thing he asked is if there were any Marines in heaven. St. Peter told him no, Marines are too rowdy for heaven. He then asked why Patton wanted to know. Patton told him he was sick of the Marines overshadowing the Army because they did more with less and were all hard-core sons of bitches. St. Peter reassured him there were no Marines so Patton went into Heaven. As he was checking out hi
Rayno
Rayno
Right in this line:
Temp/=(CTIMER::TicksPerS*1000); //convert to MS

I think that should be:
Temp/=(CTIMER::TicksPerS/1000);

You divide the ticks per second by 1000 to get ticks per milisecond. Not multiply. There wouldn''t be more ticks in a milisecond than in a second.
Ademan555
Ademan555
argh your right, my logic was flip flopped cuz i was thinkin, well nvm, i dunno, but thanx

-Dan

Yes I realize im a n00b...
When General Patton died after World War 2 he went to the gates of Heaven to talk to St. Peter. The first thing he asked is if there were any Marines in heaven. St. Peter told him no, Marines are too rowdy for heaven. He then asked why Patton wanted to know. Patton told him he was sick of the Marines overshadowing the Army because they did more with less and were all hard-core sons of bitches. St. Peter reassured him there were no Marines so Patton went into Heaven. As he was checking out hi
Ademan555
Ademan555
Hmmm, HasPassedMS still refuses to EVER return true, no matter how long i wait, even if i input 0 for time to wait... any ideas? and thanx again for the otehr help :-D

-Dan

Yes I realize im a n00b...
When General Patton died after World War 2 he went to the gates of Heaven to talk to St. Peter. The first thing he asked is if there were any Marines in heaven. St. Peter told him no, Marines are too rowdy for heaven. He then asked why Patton wanted to know. Patton told him he was sick of the Marines overshadowing the Army because they did more with less and were all hard-core sons of bitches. St. Peter reassured him there were no Marines so Patton went into Heaven. As he was checking out hi
Rayno
Rayno
Well HasPassedMS will return FALSE either if QueryPerformanceCounter fails, or if it has not passed the given time. Now I don''t see where you set CTIMER::LASTCALL in your second example. I think the problem could be that it is not initialized. Or mabye you just didn''t post all of your code.
You should also call GetLastError to check if QueryPerformanceCounter failed.
Ademan555
Ademan555
alright thanx, lastcall is set in the constructor, but ill get LastError and see what that gives me
When General Patton died after World War 2 he went to the gates of Heaven to talk to St. Peter. The first thing he asked is if there were any Marines in heaven. St. Peter told him no, Marines are too rowdy for heaven. He then asked why Patton wanted to know. Patton told him he was sick of the Marines overshadowing the Army because they did more with less and were all hard-core sons of bitches. St. Peter reassured him there were no Marines so Patton went into Heaven. As he was checking out hi
Melekor
Melekor
Leaving out important code isn''t a good way to get helped.. Post the whole thing in one userfriendly block and I''ll gladly help you.
Ademan555
Ademan555
I''d gladly do that, but if youd notice i posted every function that gets called... but at your request...


#include <windows.h>
#include <math.h>
class CTIMER
{
public:
CTIMER();
~CTIMER();
void InitTimers();
bool HasPassedMS(__int64);
bool HasPassedMS(DWORD);
bool HasPassedSinceStartMS(__int64);
DWORD GetPassedMS();
DWORD GetPassed(int);
enum {_SECONDS, _DSECONDS, _CSECONDS, _MSECONDS};
protected:
__int64 LASTCALL; //LAST TIME FUNCTION WAS CALLED IN MILLISECONDS

static DWORD TicksPerS;
};

CTIMER::CTIMER()
{
CTIMER::TicksPerS=0;
QueryPerformanceCounter((LARGE_INTEGER*)&(CTIMER::LASTCALL));
if (CTIMER::TicksPerS)CTIMER::LASTCALL/=(CTIMER::TicksPerS/1000);
}

void CTIMER::InitTimers()
{
LARGE_INTEGER Temp;
Temp.QuadPart=0;
if (QueryPerformanceFrequency(&Temp))//Get Ticks Per Second

CTIMER::TicksPerS=Temp.LowPart; //Store in Class

CTIMER::LASTCALL/=(CTIMER::TicksPerS/1000);
}

CTIMER::~CTIMER()
{
CTIMER::LASTCALL=0;
}

bool CTIMER::HasPassedMS(__int64 time)
{
__int64 Temp=0;

if (QueryPerformanceCounter((LARGE_INTEGER*)&Temp)) //if successful

{
Temp/=(CTIMER::TicksPerS/1000); //convert to MS

if ((Temp-CTIMER::LASTCALL)>=time)
{
CTIMER::LASTCALL=Temp;
return TRUE;
}

return FALSE;
}
return FALSE;
}

bool CTIMER::HasPassedMS(DWORD time)
{
__int64 Temp=0;
if (QueryPerformanceCounter((LARGE_INTEGER*)&Temp)) //if successful

{
Temp/=(CTIMER::TicksPerS/1000); //convert to MS

if ((Temp-CTIMER::LASTCALL)>=time)
{
CTIMER::LASTCALL=Temp;
return TRUE;
}
return FALSE;
}
return FALSE;
}

bool CTIMER::HasPassedSinceStartMS(__int64 time)
{
__int64 Temp=0;

if (QueryPerformanceCounter((LARGE_INTEGER*)&Temp))
{
Temp/=(CTIMER::TicksPerS/1000); //convert to MS

if (Temp>time)
{
CTIMER::LASTCALL=Temp;
return TRUE;
}
}
return FALSE;
}

DWORD CTIMER::GetPassedMS()
{
__int64 Temp=0;
DWORD Passed;
if (QueryPerformanceCounter((LARGE_INTEGER*)&Temp)) //if Query Successful

{
Temp/=(CTIMER::TicksPerS*1000); //convert to MS

Passed=Temp-CTIMER::LASTCALL; //Get time passed

CTIMER::LASTCALL=Temp; //store Temp in LASTCALL

return Passed;
}
return FALSE;
}

DWORD CTIMER::GetPassed(int Sec)
{
__int64 Temp=0;
DWORD Passed;
if (QueryPerformanceCounter((LARGE_INTEGER*)&(Temp))) //if Query is successful

{
Temp/=CTIMER::TicksPerS; //convert to seconds

Passed=(DWORD)(Temp-(CTIMER::LASTCALL/1000)); //

CTIMER::LASTCALL=Temp*1000; //Ticks per MS LASTCALL

if (Sec) //if Sec is different than seconds

Passed*=pow(10, Sec); //convert to type

return Passed;
}
return FALSE;
}


Generally people DONT want to see every bit of code and hafta sift through it to find the problem, so i only posten the functions that were called... but if its what i hafta do to get helped... :-D

-Dan

Yes I realize im a n00b...
When General Patton died after World War 2 he went to the gates of Heaven to talk to St. Peter. The first thing he asked is if there were any Marines in heaven. St. Peter told him no, Marines are too rowdy for heaven. He then asked why Patton wanted to know. Patton told him he was sick of the Marines overshadowing the Army because they did more with less and were all hard-core sons of bitches. St. Peter reassured him there were no Marines so Patton went into Heaven. As he was checking out hi
yckx
yckx
Hmm. At least one of us doesn''t fully understand this....

CTIMER::CTIMER()
{
CTIMER::TicksPerS=0;
QueryPerformanceCounter((LARGE_INTEGER*)&(CTIMER::LASTCALL));
if (CTIMER::TicksPerS)CTIMER::LASTCALL/=(CTIMER::TicksPerS/1000);
}

You just set CTIMER::TicksPerS to 0, so the if statement never gets executed.

void CTIMER::InitTimers()
{
LARGE_INTEGER Temp;
Temp.QuadPart=0;
if (QueryPerformanceFrequency(&Temp))//Get Ticks Per Second
CTIMER::TicksPerS=Temp.LowPart; //Store in Class
CTIMER::LASTCALL/=(CTIMER::TicksPerS/1000);
}

If LASTCALL is supposed to store the time of the last call in milliseconds then dividing it by TicksPerS/1000 isn''t updating it properly. You need to find the time difference between this call and last call, multiply that by the TicksPerS/1000, and add that result to LASTCALL.

I don''t have time right now to really go over the rest of the code, but I saw a lot of /= going on. You should see if that''s really what you want to do, or if you are making similar mistakes to the one I pointed out above.

yckx
Ademan555
Ademan555
Ok the first one is a dumb mistake, i was trying to convert LASTCALL into a MS value in the constructor if TicksPerS was already set, but hten i set it to zero again :-/ but your second thing, lastcall is relative to the begining of the program, it allows you to find out how much time has passed between the last time the functions were called and this new function...
When General Patton died after World War 2 he went to the gates of Heaven to talk to St. Peter. The first thing he asked is if there were any Marines in heaven. St. Peter told him no, Marines are too rowdy for heaven. He then asked why Patton wanted to know. Patton told him he was sick of the Marines overshadowing the Army because they did more with less and were all hard-core sons of bitches. St. Peter reassured him there were no Marines so Patton went into Heaven. As he was checking out hi
yckx
yckx
quote:
Original post by Ademan555
Ok the first one is a dumb mistake, i was trying to convert LASTCALL into a MS value in the constructor if TicksPerS was already set, but hten i set it to zero again :-/ but your second thing, lastcall is relative to the begining of the program, it allows you to find out how much time has passed between the last time the functions were called and this new function...

Okay, I''ve had another look.

Well, first, LASTCALL won''t be set befure the constructor is called. It doesn''t exist before the constructor is called.

In InitTimers, you are not initializing LASTCALL to anything, and then you divide the uninitialized value by TicksPerS/1000. So any comparisons made to it by the other functions are meaningless. LASTCALL should be initialized in InitTmers with a call to GetPerformanceCounter. Then, you can divide it by TicksPerS/1000 if you want it in milliseconds.

yckx
Ademan555
Ademan555
alright that works, thanx
When General Patton died after World War 2 he went to the gates of Heaven to talk to St. Peter. The first thing he asked is if there were any Marines in heaven. St. Peter told him no, Marines are too rowdy for heaven. He then asked why Patton wanted to know. Patton told him he was sick of the Marines overshadowing the Army because they did more with less and were all hard-core sons of bitches. St. Peter reassured him there were no Marines so Patton went into Heaven. As he was checking out hi
yckx
yckx
quote:
Original post by Ademan555
alright that works, thanx


Yay.

yckx
Melekor
Melekor
Hey, on friday I said I would help as soon as you posted some code, but I was very ill on saturday so I will have to look over the code right now, Sunday.
Melekor
Melekor
Ok, after looking at it, I thought it would be more productive to show you a more simplified and compact version rather than try to point out all the problems.(There were lots, sorry)

Here is my simplified version, I hope that helps you get the idea


#include <windows.h>
#include <math.h>
#include <conio.h>
#include <iostream>

class CTIMER
{
public:
CTIMER();

static void InitTimers();
int GetMS();

private:
LARGE_INTEGER startTime;
static LARGE_INTEGER freq;
};

LARGE_INTEGER CTIMER::freq;

CTIMER::CTIMER()
{
QueryPerformanceCounter(&startTime); // what time was it when this class was constructed?
}

void CTIMER::InitTimers()
{
QueryPerformanceFrequency(&freq); // get the PerformanceCounter''s frequency
}

int CTIMER::GetMS()
{
LARGE_INTEGER temp;
QueryPerformanceCounter(&temp);

// end-start gives us the time elapsed by itself
// freq is designed to give us seconds, not miliseconds,
// so multiply by 1000 first to get miliseconds
// it''s better to multiply first rather than after, because that gives better precision when working with integers.
// eg. 1000 * 1000 / 40000 = 25 ms
// but 1000 / 40000 * 1000 = 0

return 1000 * (temp.QuadPart - startTime.QuadPart) / freq.QuadPart;
}

void main()
{
CTIMER::InitTimers(); // static member function syntax
CTIMER timer;

char bleh = 0;

// press any key to get the time in MS since the prog started,
// but press q to quit.

while (bleh != ''q'')
{
bleh = getch();
std::cout << timer.GetMS() << ''\n'';
}
}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.