• ### Announcements

#### Archived

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

# Timeing it all up????

## Recommended Posts

Viri-    122
My question has to do with creating a reliable timer that fires every few secconds. I am aware of the fact that windows has a WM_TIMER message handling and setup for timers using the WinAPI but the way my engine works I have all my windows handling and message handling stuff in one .h and .cpp file called window.h/.cpp. So my problem is that I want to start a timer in my main.cpp file and be able to set variables from my message handler. Could I create a class to work with with the two files or how can I do this antoher way? I know of setting the GetTickCount and that works great for just a few effects but the other timer works out better for me right now.. how can I work around this.. and does this even make sense? It''s quite early right now but hey thats life of coding.. Thanks for all and any help, Also does anyone know of any examples of timers etc that I could take a look at? Thanks, David

##### Share on other sites
MarkO    122
In answer to first question...Yes you can pass info between files such as the timer stuff...
One way is to use global variables and the extern command
for example: in your windows.cpp file you might make a variable name LONGLONG Timer; in your main.cpp you can access that variable with the declaration extern LONGLONG Timer;
Then you simply use that variable in that file as though you created it in that file...
Another way is to make a function in your windows.cpp file which does your timer stuff....define that function in windows.h then include windows.h in your main.cpp file...passing in pointers or simply returning a value depending on how much info you need...this should be the prefferred method i think as global variables are to be avoided when possible...

Question 2 alternate way of doing timers...i am not sure what the internals of the Gettickcount function is but the timers i use are these...which are probably the same as the GetTickCount()
  LONGLONG Cur_Time; // current timeLONGLONG Time_Count_1sec; LONGLONG Perf_cnt; // performance timer frequencyBOOL Perf_Flag=FALSE; // flag determining which timer to useLONGLONG Next_Time=0; // time to render next frameLONGLONG Next_Time_1sec; // time for next 1 sec intervalLONGLONG Last_Time=0; // time of previous framedouble Time_Elapsed; // time since previous framedouble Time_Scale; // scaling factor for timeif (QueryPerformanceFrequency((LARGE_INTEGER *) &Perf_cnt)) { // yes, set time_count and timer choice flag Perf_Flag=TRUE; Time_Count_1sec=Perf_cnt/10; QueryPerformanceCounter((LARGE_INTEGER*) &Next_Time); ; //function that grabs the time and stores it in Next_Time Time_Scale=1.0/Perf_cnt; //used in equation to measure elapsed time } else { // no performance counter, read in using timeGetTime /************TIME COUNT VARIABLES*******/ Time_Count_1sec=100; //sets the time in ms to activate loop...1000ms is 1 second Next_Time=timeGetTime(); // no performance counter, read in using timeGetTime // Last_Time=timeGetTime(); Time_Scale=0.001f; } // save time of last frame Last_Time=Next_Time;while (notDone) //game loop { // use the appropriate method to get time // and calculate elapsed time since last frame if (Perf_Flag) QueryPerformanceCounter((LARGE_INTEGER *) &Cur_Time); else Cur_Time=timeGetTime(); //amount of time passed since last render frame Time_Elapsed=(Cur_Time-Last_Time)*Time_Scale; // save frame time Last_Time=Cur_Time; if (Cur_Time>Next_Time_1sec) { // letter+=AddLetter(); // set time for next frame Next_Time_1sec = Cur_Time + Time_Count_1sec; //ADD in any code you want done once per second here //End of extra code done once per second }

All this code is in my winmain
i basically just move the numbers around to get the various timers i need
I almost forgot to mention i got most of this code from sams teach yourself dx7 in 24hour book as well as off other helpful postings from people on this group with a few minor modifications
hope this helps

Edited by - MarkO on August 8, 2001 8:02:23 AM