Question:
Can someone explain GetTickCount() to me? In simple terms, and how I would use it for games. C++?
ciandoherty100
2007-12-15 05:13:04 UTC
I know of GetTickCount, and sort of know what it can be used for, but how? Im new to C++ :S
Three answers:
Byah!
2007-12-15 06:17:26 UTC
I only have limited experiance in C++, but I know quite a bit about timing mechanisms from some programming I have done for automotive performance testing. I read into this for only about 2 min, and this is a very simple thing to implement. In a nutshell, here is the theory behind it, and how to make it work for you.



The function GetTickCount() returns a value that represents the number of milliseconds (.001 sec) since your computer has been turned on. The use of this would be for a timing mechanism. Other languages have similar functions, but are harder to implement because you have to find or calulate the length of each tick. C++ is very easy because we know each tick is exactly 1 ms. You will need a static variable or a public variable--anything capable of holding the value of the "Start Time." Then when you are ready to stop the timer, you just compare the start time to the finish time.



So when you want the timer to start, call something like:



unsigned long StartTime = GetTickCount();



This will set the start time to the number of "ticks" (ms) that have elapsed since you booted the machine. Once you are ready to end the timer, you call another line similar to that to record and end time, then you can compare the values to determine the difference. This isn't a very efficient use of variables, but here is an example you should be able to follow.



unsigned long EndTime = GetTickCount();

unsigned long TimerValMS = EndTime - StartTime; // time in ms

float TimerValSec = TimerValMS / 1000; // convert to sec



If you're going to be using this in games, here is another example you may find useful. We will calculate the FPS of the display for the game. To do this, we will get the tick count each time we draw the screen, and calculate the difference to determine the FPS. The static variable will hold the ticks from the last pass through of drawing the screen so the first number it gives us is garbage. A simple IF statment can be used to get around that, and I will leave it up to you to figure out if it is necessary and how to do it. You will want to use this code at the end of the drawing function of your program loop (old school) or refresh function. The variable Interval will hold the value of ticks passed since the screen was last updated, and FPS will hold the value of calculated FPS.



static unsigned long LastTickCount; // define LastTickCount



unsigned long CurrentTickCount = GetTickCount(); // get current time

unsigned long Interval = CurrentTickCount - LastTickCount; // calculate ticks past since last iteration

float FPS = 1000 / Interval; // calculate FPS

LastTickCount = CurrentTickCount; // prepare LastTickCount for next iteration



The variable FPS holds the info you want, so you can stick it in the corner of the screen or create some mechanism to log it down somewhere for future reference. That should get you going.



Good Luck!
sarris
2016-12-17 10:48:41 UTC
Gettickcount Example
anonymous
2016-04-03 07:46:30 UTC
For the best answers, search on this site https://shorturl.im/ayeaE



What an amazing Word. Pleroma. There is an beautiful verse in Col 2:6 which speaks of ALL the FULLNESS of the Godhead, dwelling in the Lord Jesus Christ bodily. Fullness refers not to the riches of God but to the expression of the riches of God. What dwells in Christ is not only the riches of the Godhead but the expression of the riches of what God is. Can you imagine sister, that in this simple Man, over 2000 years ago, the very fullness of the Godhead dwelt in Him, and was PLEASED to dwell? Hallelujah!!! Today, we are HIS FULLNESS. Ephesians 1:23 Which is His Body, the fullness of the One who fills all in all. There is just so much here sister. I will get to it later. This is not a question that can be rushed. May we enjoy all His Riches and become His Fullness, His expression on the earth today.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...