Created
June 17, 2020 13:12
-
-
Save aleksas/863f638f39d5d02c5cdbcc9feabbbb4b to your computer and use it in GitHub Desktop.
SDL progress bar from https://forums.libsdl.org/viewtopic.php?p=46932
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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