I made a few changes to my code, again, and the timer now works as expected, but why are my debug values showing such bizarre results? I took a screenshot to explain what I'm seeing. Why is the "time_value" variable showing 14.7 quinquaseptuagintillion (or 1.47E229), the start value as zero and, after some time, the current value going negative? If it's a 64-bit variable, variable overflow is not the case. This is my updated code:
Code:
void wait_time(float seconds)
{
LARGE_INTEGER timer_start; // the performance functions use a large integer - this is the starting point
LARGE_INTEGER timer_current; // this is the current time point - current minus start gives the amount of time that passed
LARGE_INTEGER timer_frequency; // this is the frequency - difference divided by frequency gives time in seconds
double time_value; // the time, in seconds
QueryPerformanceFrequency(&timer_frequency); // obtain the frequency first
QueryPerformanceCounter(&timer_start); // then the start time
QueryPerformanceCounter(&timer_current); // then the start time
time_value = (double)((timer_current.QuadPart-timer_start.QuadPart)/timer_frequency.QuadPart); // find the time value, in seconds - typecast to allow floating point precision rather than problematic integerical division
while (time_value < seconds) // a 1-second duration
{
// MessageBox(hwnd, "This loop is running.", "Loop test", MB_OK);
Sleep(100); // to reduce CPU consumption
QueryPerformanceCounter(&timer_current); // query the timer_count value
time_value = (double)((timer_current.QuadPart-timer_start.QuadPart)/timer_frequency.QuadPart); // repeatedly reobtain the time passage
}
sprintf(debug_msg, "Debug values obtained:\n\ntimer_frequency - %d\ntimer_start - %d\ntimer_current - %d\ntime_value - %5.8f",
timer_frequency.QuadPart, timer_start.QuadPart, timer_current.QuadPart, time_value);
MessageBox(hwnd, debug_msg, "Debug results", MB_OK);
}
The timer runs just fine and I call it by using "wait_time(2.0);" somewhere in the program to wait two seconds or "wait_time(0.25);" to wait a quarter second (15 monitor refreshes).
As to the case with the sleep value being 100 in my earlier case (it works in my code above), the program would just keep running nonstop although it wouldn't get out of the loop. There was no CPU usage as a result of this. If I changed it back to the 990 it was before, the loop would terminate. Is there a better way to monitor variables than through message boxes or using text to display values? In my old tool, I'd just define a panel (a struct) with a digits element in it to get values to display in realtime. The message box only shows them at the very instance it appears. For example:
Code:
panel debug_panel
{
pos_x = 4; // position it somewhere near the top left part of the window
pos_y = 3;
digits = 0, 0, 12.3, some_font, 1, var_to_monitor;
digits = 0, 0, 12.3, some_font, 1, another_var_to_monitor;
}
The 12.3 is otherwise %7.3f in a sprintf() instruction. The zeros at the beginning are the positions of the digits relative to the top left corner of the panel. The 1 is the multiplier.