Skip to content

Instantly share code, notes, and snippets.

@aleksas
Created June 17, 2020 13:12
Show Gist options
  • Save aleksas/863f638f39d5d02c5cdbcc9feabbbb4b to your computer and use it in GitHub Desktop.
Save aleksas/863f638f39d5d02c5cdbcc9feabbbb4b to your computer and use it in GitHub Desktop.
/*
Render a Horizontal Percentage Bar
Drains left to right normally, if width is negative it will drain right to left.
Percent is clamped 0.0f - 1.0f
*/
void RenderHPBar(int x, int y, int w, int h, float Percent, SDL_Color FGColor, SDL_Color BGColor) {
Percent = Percent > 1.f ? 1.f : Percent < 0.f ? 0.f : Percent;
SDL_Color old;
SDL_GetRenderDrawColor(Renderer, &old.r, &old.g, &old.g, &old.a);
SDL_Rect bgrect = { x, y, w, h };
SDL_SetRenderDrawColor(Renderer, BGColor.r, BGColor.g, BGColor.b, BGColor.a);
SDL_RenderFillRect(Renderer, &bgrect);
SDL_SetRenderDrawColor(Renderer, FGColor.r, FGColor.g, FGColor.b, FGColor.a);
int pw = (int)((float)w * Percent);
int px = x + (w - pw);
SDL_Rect fgrect = { px, y, pw, h };
SDL_RenderFillRect(Renderer, &fgrect);
SDL_SetRenderDrawColor(Renderer, old.r, old.g, old.b, old.a);
}
/*
Render a Vertical Percentage Bar
Drains top to bottom normally, if height is negative it will drain bottom to top
Percent is clamped 0.0f - 1.0f
*/
void RenderVPBar(int x, int y, int w, int h, float Percent, SDL_Color FGColor, SDL_Color BGColor) {
Percent = Percent > 1.f ? 1.f : Percent < 0.f ? 0.f : Percent;
SDL_Color old;
SDL_GetRenderDrawColor(Renderer, &old.r, &old.g, &old.g, &old.a);
SDL_Rect bgrect = { x, y, w, h };
SDL_SetRenderDrawColor(Renderer, BGColor.r, BGColor.g, BGColor.b, BGColor.a);
SDL_RenderFillRect(Renderer, &bgrect);
SDL_SetRenderDrawColor(Renderer, FGColor.r, FGColor.g, FGColor.b, FGColor.a);
int ph = (int)((float)h * Percent);
int py = y + (h - ph);
SDL_Rect fgrect = { x, py, w, ph };
SDL_RenderFillRect(Renderer, &fgrect);
SDL_SetRenderDrawColor(Renderer, old.r, old.g, old.b, old.a);
}
/*
color - Returns an SDL_Color with the appropriate values
*/
SDL_Color color(Uint8 r, Uint8 g, Uint8 b, Uint8 a) {
SDL_Color col = {r,g,b,a};
return col;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment