I don't understand the purpose of this

I was studying someones code and something didn't make sense to me. I don't understand the point of having clock() - GameAvailTMR, I thought about it for awhile and couldn't figure it out. I removed it from the code and didn't see any noticeable changes, so I'm thinking it must be for a reason I can't see or else there's no point in putting it there. Also if GameAvailTMR is equal to clock() then wouldn't clock() - GameAvailTMR = 0? I know I'm wrong because the code runs the if statement, but it really doesn't make sense to me. If more code is needed to tell the answer let me know and I'll post the rest.
 if(clock() - GameAvailTMR > 100)
        GameAvailTMR = clock();
//more code 
Last edited on
there is one option which completes this action
Last edited on
Also if GameAvailTMR is equal to clock() then wouldn't clock() - GameAvailTMR = 0?
No, see this:


clock() permanently advances.

Line 3 checks how much time is elapse since the last remembered time on line 5.
Line 5 resets GameAvailTMR to the current time point
You have an outer while loop. How often does it repeat?

You do something within the loop, but only when time (clock) has advanced at least "100" from the previous time you did that something. Logically, you do not want to do the something too often.

Lets take a display as an example. Lets say that it can refresh 50Hz (50 times per second; 50 frames per second). Lets say it takes 2ms to draw a frame, so a loop could draw 500 times per second. It makes no sense to draw a frame more than 50 times per second, so only once every 20ms. If we update timestamp (and draw) only when 20ms has passed from previous update, we do not draw too often.
For some reason I seemed to think GameAvailTMR = clock() meant GameAvailTMR would run continuously like clock(). Maybe because GameAvailTMR is short for GameavailTimer, so I started thinking of it as a timer. Anyway thanks for the help, I really appreciate it.
Topic archived. No new replies allowed.