c++ - Sleep(1) and SDL_Delay(1) takes 15 ms -
I am writing a C ++ / SDL / OpenGL application, and I have the most weird bug. This game worked fine with ordinary variables timestamps. But then FPS started behaving strangely. I came to know that both sleep (1) and SDL Della (15) take 15 ms to complete.
Any input in 0-15 takes 15ms to complete the input, locks FPS at about 64. To 16, it takes 30 ms oo
I do not know why this is happening This is the most awkward bug I've ever encountered.
My Loop looks like this:
while (1) {GLuint t = SDL_GetTicks (); Sleep (1); // or SDL Della (1) COT & lt; & Lt; SDL_GetTicks () - T & LT; & Lt; Endl; // Output 15} It takes very little 1ms as is believed, but in most of the time it takes 15ms.
My OS is Windows 8.1. CPU is an Intel i7 i am using SDL2
error of ticker is 64 Hz or 15.625 ms / Tick you will have to change it with 1000hz == 1ms over time. BeanPeriod (1) MSDN article:
If the goal here is to get a certain frequency sequence, you will need a higher resolution Timers should be used, but unfortunately they can only be voted, so the combination of polling and oyster Gold is necessary to reduce U overhead. Example code, which assumes that a sleep (1) can take up to 2 MS (which is with Windows XP, but not with later versions of Windows).
/ * To run on a fixed frequency thread / / # FREQ 400 / * frequency * / type long typed UI 64; / * Unsigned 64 bit int * / LARGE_INTEGER liPerfTemp; / * Is used for query * / UI64 uFreq = FREQ; / * Process Frequency * / UI4UOrig; / * Original tick * / UI64 uWait; / * Tick Rate / Freak * / UI 64 Eurem = 0; / * Tick rate% freq * / UI64 uPrev; / * Previous tick based on the original tick * / UI64 uDelta; / * Current tick - previous * / UI64 u2ms; / * 2ms ticks * / #if 0 / * optional error check * / static DWORD dwLateStep = 0; #endif / * Wait for some event to start this thread code * / timepieriod (1); / * Set duration 1ms * / sleep (128); / * To fix it * * / u2ms = (UI64) (liPerfFreq.QuadPart) +49 9) / ((UI64) 500); QueryPerformanceCounter ((PLARGE_INTEGER) & liPerfTemp); UOrig = uPrev = liPerfTemp.QuadPart; While (1) {/ * based on UHT and URM urm * / uWait = (UI64) (liPerfFreq.QuadPart) + Eurem) / uFreq; URem = ((UI64) (liPerfFreq.QuadPart + Eurem)% uFreq; / * Wait for UWait ticks * / while (1) {QueryPerformanceCounter ((PLARGE_INTEGER) & liPerfTemp); UDelta = (UI64) (liPerfTemp.QuadPart - uPrev); Break (uDelta> = uWait); If ((uWait - uDelta)> u2ms sleep (1); } #if / / Optional error check * / if (uDelta> = (uWait * 2) dwLateStep + = 1; #endif uPrev + = uWait; / * Fixed frequency code goes here * / / * when done with a break of some time / time / time expired (1); / * Restore period * /
Comments
Post a Comment