| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
#include "animated_counter.h" |
| 27 |
|
| 28 |
#include <math.h> |
| 29 |
#include <cdogs/font.h> |
| 30 |
|
| 31 |
#define INC_RATIO 0.05f |
| 32 |
|
| 33 |
|
| 34 |
AnimatedCounter AnimatedCounterNew(const char *prefix, const int max) |
| 35 |
{ |
| 36 |
AnimatedCounter a = { NULL, max, 0, INC_RATIO }; |
| 37 |
CSTRDUP(a.prefix, prefix); |
| 38 |
return a; |
| 39 |
} |
| 40 |
void AnimatedCounterTerminate(AnimatedCounter *a) |
| 41 |
{ |
| 42 |
CFREE(a->prefix); |
| 43 |
} |
| 44 |
|
| 45 |
bool AnimatedCounterUpdate(AnimatedCounter *a, const int ticks) |
| 46 |
{ |
| 47 |
if (a->current == a->max) |
| 48 |
{ |
| 49 |
return true; |
| 50 |
} |
| 51 |
float inc = (a->max - a->current) * a->incRatio; |
| 52 |
for (int i = 0; i < ticks; i++) |
| 53 |
{ |
| 54 |
const int diff = a->max - a->current; |
| 55 |
while (diff > 0 ? inc > diff : inc < diff) |
| 56 |
{ |
| 57 |
inc *= INC_RATIO; |
| 58 |
} |
| 59 |
a->current += (int)(diff > 0 ? ceil(inc) : floor(inc)); |
| 60 |
} |
| 61 |
return false; |
| 62 |
} |
| 63 |
void AnimatedCounterReset(AnimatedCounter *a, const int value) |
| 64 |
{ |
| 65 |
a->max = value; |
| 66 |
} |
| 67 |
struct vec2i AnimatedCounterDraw(const AnimatedCounter *a, const struct vec2i pos) |
| 68 |
{ |
| 69 |
const struct vec2i pos2 = FontStr(a->prefix, pos); |
| 70 |
char buf[256]; |
| 71 |
sprintf(buf, "%d", a->current); |
| 72 |
return FontStr(buf, pos2); |
| 73 |
} |
| 74 |
struct vec2i AnimatedCounterTimeDraw(const AnimatedCounter *a, const struct vec2i pos) |
| 75 |
{ |
| 76 |
const struct vec2i pos2 = FontStr(a->prefix, pos); |
| 77 |
char buf[256]; |
| 78 |
sprintf(buf, "%d:%02d", a->current / 60, a->current % 60); |
| 79 |
return FontStr(buf, pos2); |
| 80 |
} |