bar_change: apply modifier on fill

This commit is contained in:
KikooDX 2021-05-24 12:11:04 +02:00
parent 1f0dc26baa
commit 8a30df2b51
5 changed files with 31 additions and 5 deletions

View File

@ -16,6 +16,7 @@ set(SOURCES
src/bar/init.c
src/bar/update.c
src/bar/draw.c
src/bar/change.c
)
set(ASSETS

View File

@ -13,8 +13,14 @@ struct Bar {
float fill;
};
#define BAR_TOTAL 4
enum BarID { BAR_CASH, BAR_HUMAN, BAR_SMILE, BAR_SUN };
struct Bar bar_init(enum BarID bar_id);
void bar_update(struct Bar *bar);
void bar_draw(struct Bar bar);
/* Increase/decrease bar fill level.
* Return 1 when bar is fully filled.
* Return -1 when bar is fully emptied.
* Return 0 otherwise. */
int bar_change(struct Bar *bar, float modifier);

16
src/bar/change.c Normal file
View File

@ -0,0 +1,16 @@
#include "bar.h"
int
bar_change(struct Bar *bar, float modifier)
{
bar->fill += modifier;
if (bar->fill >= 1.0) {
bar->fill = 1.0;
return 1;
}
if (bar->fill <= 0.0) {
bar->fill = 0.0;
return -1;
}
return 0;
}

View File

@ -8,8 +8,8 @@ bar_draw(struct Bar bar)
const int y = bar.y + bar.height - height;
/* borders */
drect_border(bar.x - 1, bar.y - 1, bar.x + BAR_WIDTH + 1, bar.y + bar.height + 1,
C_WHITE, 2, C_BLACK);
drect_border(bar.x - 1, bar.y - 1, bar.x + BAR_WIDTH + 1,
bar.y + bar.height + 1, C_WHITE, 2, C_BLACK);
/* fill */
drect(bar.x, y, bar.x + BAR_WIDTH, y + height, C_RGB(0, 32, 32));
}

View File

@ -2,7 +2,7 @@
#include <gint/display.h>
#include <gint/keyboard.h>
static struct Bar bars[4];
static struct Bar bars[BAR_TOTAL];
static void main_draw(void);
@ -12,9 +12,12 @@ main(void)
int i;
/* init */
for (i = 0; i < 4; i += 1)
for (i = 0; i < BAR_TOTAL; i += 1)
bars[i] = bar_init(i);
bar_change(&bars[BAR_CASH], 0.2);
bar_change(&bars[BAR_SMILE], 0.4);
main_draw();
getkey();
@ -33,7 +36,7 @@ main_draw(void)
dimage(0, 0, &bimg_background);
/* draw bars */
for (i = 0; i < 4; i += 1)
for (i = 0; i < BAR_TOTAL; i += 1)
bar_draw(bars[i]);
dupdate();