Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » I need a high resolution timer with QPC

Credits go to CGamesPlay, GullRaDriel, orz, and Richard Phipps for helping out!
This thread is locked; no one can reply to it. rss feed Print
 1   2 
I need a high resolution timer with QPC
Paul whoknows
Member #5,081
September 2004
avatar

How should I use Orz's routines?
I need to see a main loop implemented with those routines to understand how they work.

____

"The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner.

orz
Member #565
August 2000

Quote:

Here are Orz's routines for timing:
...
All these examples comes from pmask library [sourceforge.net], by Orz.

I think looking at it will definitely solve your problem Paul.

Yeah, that's my timing measurement code. A few caveats:
1. the gettimeofday() implemenation there is bugged... just some easily fixed typos, fixed in my lastest version of that file (so far existing on my computer only...)
2. while that's in the PMASK download for use by the examples, I consider it to not be a part of the examples, not the library itself.

Quote:

thread's title said:
I need a high resolution timer with QPC

That was the title, but your original post said

Quote:

I found a lot of timer routines using QPC but there is no explanation about how to use these routines in the main loop

So I posted code that demonstrated how to use a timer function in a games main loop, independant of whether that timer function used QPC. Though, for usage with QPC, its best with the ints changed to doubles. i.e.

1 double next_tick_ms = get_time_ms();
2 double ticks_since_last_frame = 0;
3 double ms_per_tick = 1000.0 / 60;//60 hertz
4 double last_frame_ms = 0;
5 while (!quitting) {
6 bool do_frame = false;
7 double current_ms = get_time_ms();
8 if (current_ms >= next_tick_ms) {
9 calculate_physics();
10 next_tick_ms += ms_per_tick;
11 //we're too far behind
12 if (next_tick_ms < current_ms - 20) next_tick_ms = current_ms - 20;
13 ticks_since_last_frame++;
14 if (ticks_since_last_frame > 5) do_frame = true;
15 }
16 else {
17 if (ticks_since_last_frame || current_ms > last_frame_ms + 100)
18 do_frame = true;
19 }
20 if (do_frame) {
21 render_frame();
22 ticks_since_last_frame = 0;
23 last_frame_ms = current_ms;
24 }
25 }

edit: fixed a typo in the code (mispelled since as sicne in one case), added a value of ms_per_tick

The only thing necessary (with or without changing the ints to doubles) to make that into a functional QPC-timed game loop is a QPC based timer such as:

1double ms_per_QPC_tick=0;
2void init_get_time_ms() {
3 LARGE_INTEGER fred;
4 if (QueryPerformanceFrequency(&fred)) {
5 ms_per_QPC_tick = 1000.0 / fred.QuadPart;
6 }
7 return -1;//do whatever you do on an error
8}
9volatile double get_time_ms() {
10 LARGE_INTEGER bob;
11 if (QueryPerformanceCounter(&bob)) {
12 return bob.QuadPart * ms_per_QPC_tick;
13 }
14 return -1;//do whatever you do on an error
15}

edit:

Quote:

How should I use Orz's routines?
I need to see a main loop implemented with those routines to understand how they work.

That is the main loop there in my first post, and again in this post. It's untested, but is a demonstration of the concepts. If you want one with delta-timing instead, just ask.

Quote:

From Pmask libray you have test.c and test2.c which use Orz's timing routines so all your need are filled .

Hm... not necessarily a good idea... (edit: I feel kinda stupid saying that something of mine isn't a good example for some purpose, but really the PMASK tests weren't written with this is mind... perhaps I should clean up test2.c sometime...)
test.c does not use any timing routines from my get_time.c or anything else except Allegro. It does not use high resolution time. So, it is not really a good example. edit: its timing method is also totally inappropriate for a game
test.2 does use my timing routines. However, it's not the best example as the main loop is kinda quick-and-dirty, cluttered up with text output and keyboard input stuff, and whatnot. Still, it is an example of using my time functions to time something in a relatively smart manner compared to many allegro example programs. It's also undocumented, and uses different priorities than many games might prefer. (for instance it will never use more than 50% of CPU time on graphics)

Paul whoknows
Member #5,081
September 2004
avatar

Thank you very much! I'll try to implement your routines now!

____

"The unlimited potential has been replaced by the concrete reality of what I programmed today." - Jordan Mechner.

 1   2 


Go to: