Howdy,
I just need to know when to increment my framecounter in code like this:
| 1 | volatile int speed_counter = 0; |
| 2 | |
| 3 | volatile int framecounter = 0; |
| 4 | volatile int framerate = 0; |
| 5 | |
| 6 | void IncSpeedCounter() |
| 7 | { |
| 8 | speed_counter++; |
| 9 | } |
| 10 | |
| 11 | END_OF_FUNCTION(IncSpeedCounter) |
| 12 | |
| 13 | void GetFR() |
| 14 | { |
| 15 | framerate = framecounter; |
| 16 | framecounter = 0; |
| 17 | } |
| 18 | |
| 19 | END_OF_FUNCTION(GetFR) |
| 20 | |
| 21 | void PlayGame() |
| 22 | { |
| 23 | LOCK_VARIABLE(speed_counter); |
| 24 | LOCK_FUNCTION(IncSpeedCounter); |
| 25 | |
| 26 | install_int_ex(IncSpeedCounter, BPS_TO_TIMER(60)); |
| 27 | install_int_ex(GetFR, BPS_TO_TIMER(1)); |
| 28 | |
| 29 | while (!game_over) |
| 30 | { |
| 31 | while (speed_counter > 0) |
| 32 | { |
| 33 | //Should I increment it in here? (framecounter++) |
| 34 | update_game_logic(); |
| 35 | speed_counter--; |
| 36 | } |
| 37 | |
| 38 | //Or should I increment it here? (framecounter++) |
| 39 | update_display(); |
| 40 | } |
| 41 | } |
If you want to count the number of redraws, then the latter. If you want the number of (logical) updates, the former. I assume you want the first, so you need the latter. (Wow, now that was perfectly confusing, wasn't it?
)
Put it right above that update_display();.
If I increment at the latter, than anything I pass to BPS_TO_TIMER() doesn't affect the FPS, and when there is actually only 1 Frame Per Second, it says that there is like 100 Frames Per Second. How do fix this and have control of my FPS?
Something like this will prevent extraneous redraws:
| 1 | bool need_redraw = false; |
| 2 | while(!game_over) |
| 3 | { |
| 4 | while(speed_counter > 0) |
| 5 | { |
| 6 | update_game_logic(); |
| 7 | speed_counter--; |
| 8 | need_redraw = true; |
| 9 | } |
| 10 | |
| 11 | if(need_redraw) |
| 12 | { |
| 13 | framecounter++; |
| 14 | update_display(); |
| 15 | need_redraw = false; |
| 16 | } |
| 17 | } |
Problem fixed.
Thanks!
The Unknown: would you want that to be in a while() loop, or a simple if() statement?
| 1 | volatile int speed_counter = 0; |
| 2 | |
| 3 | volatile int framecounter = 0; |
| 4 | volatile int framerate = 0; |
| 5 | |
| 6 | |
| 7 | void incrementSpeedCounter() |
| 8 | { |
| 9 | speed_counter++; |
| 10 | } |
| 11 | END_OF_FUNCTION(incrementSpeedCounter) |
| 12 | |
| 13 | |
| 14 | void getFrameRate() |
| 15 | { |
| 16 | framerate = framecounter; |
| 17 | framecounter = 0; |
| 18 | } |
| 19 | END_OF_FUNCTION(getFrameRate) |
| 20 | |
| 21 | void PlayGame() |
| 22 | { |
| 23 | LOCK_VARIABLE(speed_counter); |
| 24 | LOCK_FUNCTION(incrementSpeedCounter); |
| 25 | |
| 26 | install_int_ex(incrementSpeedCounter, BPS_TO_TIMER(60)); |
| 27 | install_int_ex(getFrameRate, BPS_TO_TIMER(1)); |
| 28 | |
| 29 | while (!game_over) |
| 30 | { |
| 31 | while (speed_counter > 0) |
| 32 | { |
| 33 | update_game_logic(); |
| 34 | speed_counter--; |
| 35 | need_redraw = true; |
| 36 | } |
| 37 | |
| 38 | // Should this be a "while" or an "if"? |
| 39 | if (speed_counter == 0) |
| 40 | rest(1); |
| 41 | |
| 42 | if (need_redraw) |
| 43 | { |
| 44 | framecounter++; |
| 45 | update_display(); |
| 46 | need_redraw = false; |
| 47 | } |
| 48 | } |
| 49 | } |
TT,
Your code will call the rest(1) nearly every time it loops past it. Would this cause more logical updates to occur for every screen update than if it were placed before the while loop(as shown below), because the while block is exited when speed_counter is zero?
| 1 | while (!game_over) |
| 2 | { |
| 3 | // Should this be a "while" or an "if"? |
| 4 | if (speed_counter == 0) |
| 5 | rest(1); |
| 6 | |
| 7 | while (speed_counter > 0) |
| 8 | { |
| 9 | update_game_logic(); |
| 10 | speed_counter--; |
| 11 | need_redraw = true; |
| 12 | } |
| 13 | |
| 14 | if (need_redraw) |
| 15 | { |
| 16 | framecounter++; |
| 17 | update_display(); |
| 18 | need_redraw = false; |
| 19 | } |
| 20 | } |
I'd say it wouldn't make much of a difference whether a "while" or an "if" was used if the code given was all the processing that is done (but I'd be interested if someone can show a scenario where it would have an impact). But personally I go for an "if". Do the rest(!) command once and then check everything else before testing again whether the rest(!) should be called.
an if statement would run quicker than a while loop, since the amount of times a while loop needs to run is unknown... but, the difference in negligable 
Infact, to save a whole new if/while statement, just add it an an else to if(need_redraw)
It would still make no difference though.
The amount of times the while statement needs to be run is known to be one. Since rest will sleep for 10 milliseconds, and his timer fires more frequently than that, the condition will always be true after a rest.
[append]
So yes, the faster is if, but the difference is microscopic.