• Create Account

Banner advertising on our site currently available from just \$5!

### #ActualDareDeveloper

Posted 06 July 2013 - 05:35 AM

Have you logged how many times the else-block is executed?

...
if(sleep_length_ms > 0) {
Sleep(sleep_length_ms);
next_mark += duration;
} else {
// if running behind
QueryPerformanceCounter(&counter);
}
...


I'm testing this class using a simple Allegro program where a circle bounces off the edges. I tried using the timer class provided by Allegro and the issue resolves so I'm quite sure that the problem lies within my timer class. I can't seem to figure out what I'm doing wrong and would be very grateful if someone can have a read through the code. Thank you!

Not sure ... it looks suspicious somehow. Is it important to read the time again there? What about a code like that:

    bool wait() {
bool slept = false;

QueryPerformanceCounter(&counter);
sleep_length_ms = (int) (ratio * (next_mark - counter.QuadPart));

if(sleep_length_ms > 0) {
Sleep(sleep_length_ms);
updated = true;
}
next_mark += duration;

return slept;
}

Timer timer(60);

bool slept = false;
while(run_game)
{
update_game();
render_game();

do {
slept = timer.wait();
} while (!slept)
}

Probably it is something else entirely ... but it looks more regular (wait always uses duration steps ... even if it might skip one frame).

Edit: ok that is ugly and causes a sleep when already running behind ... I think I'd try to turn wait() into two functions and pass the current time to them:

wait(currentTime) and updateNextMark(currentTime) that doesn't read the time again and increments next_mark until it is bigger than the current time.

Currently wait() is a little "side-effecty".

### #2DareDeveloper

Posted 06 July 2013 - 05:35 AM

Have you logged how many times the else-block is executed?

...
if(sleep_length_ms > 0) {
Sleep(sleep_length_ms);
next_mark += duration;
} else {
// if running behind
QueryPerformanceCounter(&counter);
}
...


I'm testing this class using a simple Allegro program where a circle bounces off the edges. I tried using the timer class provided by Allegro and the issue resolves so I'm quite sure that the problem lies within my timer class. I can't seem to figure out what I'm doing wrong and would be very grateful if someone can have a read through the code. Thank you!

Not sure ... it looks suspicious somehow. Is it important to read the time again there? What about a code like that:

    bool wait() {
bool slept = false;

QueryPerformanceCounter(&counter);
sleep_length_ms = (int) (ratio * (next_mark - counter.QuadPart));

if(sleep_length_ms > 0) {
Sleep(sleep_length_ms);
updated = true;
}
next_mark += duration;

return slept;
}

Timer timer(60);

bool slept = false;
while(run_game)
{
update_game();
render_game();

do {
slept = timer.wait();
} while (!slept)
}

Probably it is something else entirely ... but it looks more regular (wait always uses duration steps ... even if it might skip one frame).

Edit: ok that is ugly and causes a sleep when already running behind ... I think I'd try to turn wait() into two functions and pass the current time to them:

wait(currentTime) and updateNextMark(currentTime) that doesn't read the time again and increments next_mark until it is bigger than the current time.

### #1DareDeveloper

Posted 06 July 2013 - 05:07 AM

Have you logged how many times the else-block is executed?

...
if(sleep_length_ms > 0) {
Sleep(sleep_length_ms);
next_mark += duration;
} else {
// if running behind
QueryPerformanceCounter(&counter);
}
...


I'm testing this class using a simple Allegro program where a circle bounces off the edges. I tried using the timer class provided by Allegro and the issue resolves so I'm quite sure that the problem lies within my timer class. I can't seem to figure out what I'm doing wrong and would be very grateful if someone can have a read through the code. Thank you!

Not sure ... it looks suspicious somehow. Is it important to read the time again there? What about a code like that:

    bool wait() {
bool slept = false;

QueryPerformanceCounter(&counter);
sleep_length_ms = (int) (ratio * (next_mark - counter.QuadPart));

if(sleep_length_ms > 0) {
Sleep(sleep_length_ms);
updated = true;
}
next_mark += duration;

return slept;
}

Timer timer(60);

bool slept = false;
while(run_game)
{
update_game();
render_game();

do {
slept = timer.wait();
} while (!slept)
}

Probably it is something else entirely ... but it looks more regular (wait always uses duration steps ... even if it might skip one frame).

PARTNERS