Archived

This topic is now archived and is closed to further replies.

How do you make a basic timer?

This topic is 5046 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

this is what I found
#define TIMER_ID_1SEC 1
#define TIMER_ID_3SEC 2

SetTimer(hwnd, Timer_ID_1SEC, 1000,NULL);
SetTimer(hwnd, TImer_ID_3SEC, 3000,NULL);

case WM_TIMER:
{
switch(wparam)
{
case TIMER_ID_1Sec:
{
}
case Timer_ID_3Sec:
{
}break;
default:break;
}
return(0);
}break;


case WM_DESTROY:
[
KillTimer(hwnd, TIMER_ID_1SEC);
KillTimer(hwnd, TIMER_ID_3SEC);
PostQuiteMessage(0);
}break;
but this dont look like It will do what I want then there is this one
DWORD GetTickCount(void);

DWORD start_time = GetTickCount();
while ((GetTickCount() - start_time) < 33);
this the onw I ma using but it is hard to change it very easly and it is not that precise. Got any ideas?

Share this post


Link to post
Share on other sites
I also found this one


#include <stdio.h>
#include <time.h>

int main ( void )
{
clock_t start, end;

/* Start the timer */
start = clock();

printf ( "Please wait a few moments and hit return\n" );
getchar();

/* End the timer */
end = clock();

/* Print out the difference */
printf ( "The interval was: %f seconds\n",
(double)( end - start ) / (double)CLOCKS_PER_SEC );

return 0;
}


I am still trying to figure out how to use it for what I need.

sudo code is

images moves down 1 unit waits for timer to hit one second and then moves down another unit. after 10 lines are cleared then
it repeats but moves after timer hits 500 milliseconds
and repeats
ect

Share this post


Link to post
Share on other sites
I dont have my timer code with me but I can try to remember it

class CTimer
{

protected:

INT64 m_Frequency;

INT64 m_StartTime;
INT64 m_EndTime;

float m_Elapsed;

public:

// Call once at init
void Init()
{
QueryPerformanceFrequency(&m_Frequency);
QueryPerformanceCounter(&m_StartTime);
}

// Call once per frame
void Update()
{
QueryPerformanceCounter(&m_EndTime);

m_Elapsed = (m_EndTime - m_StartTime) / m_Frequency;

QueryPerformanceCounter(&m_StartTime);
}

// Time elapsed between frames
float GetElapsed() { return m_Elapsed; }

};

Share this post


Link to post
Share on other sites
Sorry I don’t think I am asking the right question.

Because I just tried a timer and I can get it to print stuff every second but I am missing on how to print out things when it is called only when the time hits a second.
It would be like a wait command to wait for timer to equal 1 second

Does that make sense?

Share this post


Link to post
Share on other sites
I think you will want to learn how to handle WM_TIMER messages

Using Timers (Windows User Interface)
http://msdn.microsoft.com/library/en-us/winui/WinUI/WindowsUserInterface/Windowing/Timers/UsingTimers.asp

Share this post


Link to post
Share on other sites
case WM_DESTROY:
[
KillTimer(hwnd, TIMER_ID_1SEC);
KillTimer(hwnd, TIMER_ID_3SEC);
PostQuiteMessage(0);
}break:


should be:

case WM_DESTROY:
{
KillTimer(hwnd, TIMER_ID_1SEC);
KillTimer(hwnd, TIMER_ID_3SEC);
PostQuiteMessage(0);
break:
}



Share this post


Link to post
Share on other sites
this is what I found out through my research
////////////////////////////////////////
WM_TIMER
GetTickCount()
Clock()
Time()
timeGetTime()
SetTimer();
sysdelay();
QueryPerformanceCounter();

This is all of the functions I could find

Is there any others;

And which one would I use for my function?


Share this post


Link to post
Share on other sites
I gave you this link before

http://msdn.microsoft.com/library/en-us/winui/WinUI/WindowsUserInterface/Windowing/Timers/UsingTimers.asp

it tells u exactly how to use WM_TIMER
I guess your too lazy to look at it
Im bored so I will try to explain how to use WM_TIMER

// This creates a timer that goes off every five seconds
SetTimer(hwnd, IDT_TIMER1, 5000, (TIMERPROC) NULL);

// Here is how to handle the WM_TIMER message

case WM_TIMER:
{
switch( wParam )
{
case IDT_TIMER1:
{
// Print Something To The Screen Every 5 seconds
// FunctionToPrintToTheScreen("Hello");
return 0;
}
}
return 0;
}


// And finally you can destroy the timer
KillTimer(hwnd, IDT_TIMER1);

Share this post


Link to post
Share on other sites
I already read and printed it off and every thing but I dont see how that will fit into my code you see. and if I cant see how it fits then how can I use it.

this is were it will go


void GameLoop()
{


if(GamePaused)
{

}
else

if( timeGetTime() > start_time + faster )
{
PlaySound("MenuKnopka.wav ", hInstMain, SND_ASYNC | SND_FILENAME );
Move(0,1);
start_time=GetTickCount();

}

else
if (Level = 10)
{
NewLevel();
}

}

int NewLevel()
{
int faster;
//int Level;

if( StartTime + faster < timeGetTime())
{
PlaySound("MenuKnopka.wav ", hInstMain, SND_ASYNC | SND_FILENAME );
--faster;
// Level = 0;

Move(0,1);
start_time=GetTickCount();

}
}


I need the timer to work like this

when timer hits 1000 then move image do that tell level = 10
then when timer hits 500 then move image do that tell level = 10

so how would WM_TIMER work very well here I cant have it in my winproc becaus buttons are being hit and when buttons are hit it stops the timer understand


[edited by - kingpinzs on February 22, 2004 11:28:46 PM]

Share this post


Link to post
Share on other sites
ok i thought u wanted something to print to the screen every ''x'' seconds.

using the timer class I gave before
this next bit of code may not be the full thing of what you wanted but it may help u to figure this out


CTimer Timer;
float TimeTrack;

// Init
Timer.Init();

// Loop
Timer.Update();
TimeTrack += m_Timer.GetElapsed();

// If one second has elapsed
if( TimeTrack >= 1.0f )
{
TimeTrack = 0.0f;
// Move the thing down
Move(0, 1);
}


let me know if this is going in the right direction

Share this post


Link to post
Share on other sites
why does this not work?


/////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////


void GameLoop()
{
if(GamePaused)
{

}
else
Start_Clock();

if (Level >= 1)
{
if(( Get_Clock() - start_time) > faster)
{
PlaySound("MenuKnopka.wav ", hInstMain, SND_ASYNC | SND_FILENAME );
Move(0,1);
start_time=Get_Clock();

}

}
else
if (Level >= 1)
{
NewLevel();
//start_time=Get_Clock();

}

}
/////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////


int NewLevel()
{

int Level;

if (Level >= 1);
++faster;
if ( (Get_Clock() - start_time) > faster * 1000)
{
PlaySound("MenuKnopka.wav ", hInstMain, SND_ASYNC | SND_FILENAME );
Move(0,1);
start_time=Get_Clock();
Level = 1;
}





}


[edited by - kingpinzs on February 23, 2004 12:56:31 AM]

Share this post


Link to post
Share on other sites
one part i noticed is in your NewLevel() function

int Level; // non initialized integer var
if (Level >= 1); // this will not be accurate as Level has not been assigned a value
++faster; // this is done each time NewLevel() is called instead of when level is greater than 1 due to a semi-colon at the end of your if statement

did my previous post help u at all ?

Share this post


Link to post
Share on other sites
eFoDay thanks for your help. I just got it to work.


/////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////


void GameLoop()
{
if(GamePaused)
{

}
else


if (Level < 1)
{
if(( Get_Clock() - start_time) > faster)
{
start_clock_count = 0;
start_time=Get_Clock();
PlaySound("MenuKnopka.wav ", hInstMain, SND_ASYNC | SND_FILENAME );
Move(0,1);

}

}
else
if (Level >= 1)
{
NewLevel();
//start_time=Get_Clock();

}

}
/////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////



this is what I was trying to do all the time now I just need to put it into a loop and increase the speed ever so often

Share this post


Link to post
Share on other sites