Skip to main content
GameDev.net gamedev.net
🔒 Locked

timeBeginPeriod not working

Started by Prads Jun 26, 2011 at 1:54 PM 3 replies 3.7k views
Original Post
Prads
Prads
Ok, I am trying to use timeBeginPeriod with timeGetTime but it's not working. Here's the code:



#include <windows.h>
#include <iostream>
using namespace std;

#pragma comment(lib, "Winmm.lib")

int main() {
DWORD time;
if (timeBeginPeriod(5) == TIMERR_NOCANDO) {
cout << "Not can't do...\r\n";
cin.get();
return 0;
}
for (int i = 0; i < 1000; i++) {
time = timeGetTime();
cout << time << "\r\n";
}
timeEndPeriod(5);
cin.get();
return 0;
}



As you can see I am trying to set precision to 5 ms but timeGetTime keeps on returning with precision of 1 ms. So, what am I doing wrong?
My first 3D game: Click Here
SiCrane
SiCrane
timeBeginPeriod() affects the system global time period. Windows will use the highest resolution (lowest value) requested by any process on the system. This basically means that you can reliably increase resolution, but not reliably decrease it.
Prads
Prads
Oh ok. So, can I expect it to return 1 ms in every windows system or is it safe to set it to 1 ms before starting my program? Thanks!

EDIT: Also, different system may have different highest resolution right? So, how high value can the highest resolution in systems be?
My first 3D game: Click Here
Antheus
Antheus

Oh ok. So, can I expect it to return 1 ms in every windows system or is it safe to set it to 1 ms before starting my program?


Timing on PC isn't reliable due to hardware.

Any code that relies on timing should tolerate lowest timing resolution of up to ~50ms or so. It should also expect the timing to vary during the run-time of the application (battery saving mode, temperature load balancing and similar).

EDIT: Also, different system may have different highest resolution right? So, how high value can the highest resolution in systems be?[/quote]

One clock cycle or about 1/3,000,000,000 seconds - this is the value returned by rdtsc. But as said, no code should require anything lower than 50ms or so, unless using dedicated timing hardware.
Prads
Prads
[font=arial, verdana, tahoma, sans-serif][size=2]Hmmm... Actually I am going to use this as a timer for my game. I want to give player two options, to force frame rate to 60 FPS (58 - 59 FPS to be exact) or to use default frame rate of the monitor. For forcing frames to 60 FPS, code should work (haven't written it yet) by getting time using getTimeGet and after there is a difference of 17ms between previous frame render time, new frame will be rendered. So, do you think it's good idea to use timeGetTime here. Thanks![/font]
My first 3D game: Click Here

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.