I want to follow a thread progress. I have already implemented progress bar graphically, but I am wondering how to efficiently measure progress of the thread in a real time.
Progress bar
template<typename T>
inline T Saturate(T value, T min = static_cast<T>(0.0f), T max = static_cast<T>(1.0f))
{
return value < static_cast<T>(min) ? static_cast<T>(min) : value > static_cast<T>(max) ? static_cast<T>(max) : value;
}
void ProgressBar(float progress, const Vector2& size)
{
Panel* window = getPanel();
Vector2 position = //some position
progress = Saturate(progress);
window->renderer->FillRect({ position, size }, 0xff00a5ff);
window->renderer->FillRect(Rect(position.x, position.y, Lerp(0.0f, size.w, progress), size.h), 0xff0000ff);
//progress will be shown as a %
std::string progressText;
//ToString(value, how many decimal places)
progressText = ToString(progress * 100.0f, 2) + "%";
const float textWidth = font->getWidth(progressText) * context.fontScale,
textX = Clamp(Lerp(position.x, position.x + size.w, progress), position.x, position.x + size.w - textWidth);
window->renderer->DrawString(progressText, Vector2(textX, position.y + font->getAscender(progressText) * context.fontScale * 0.5f), 0xffffffff, context.fontScale, *font.get());
}
and somewhere in a game loop, example usage
static float prog = 0.0f;
float progSpeed = 0.01f;
static float progDir = 1.0f;
prog += progSpeed * (1.0f / 60.0f) * progDir;
ProgressBar(prog, { 100.0f, 30.0f });
I know how to measure execution time:
uint t1 = getTime();
//... do sth
uint t2 = getTime();
uint executionTime = t2 - t1;
but of course progress bar will be updated after execution, so it won't be shown in a real time.
Should I use a new thread? Are there any other methods to do this?