Mercurial > molko
comparison libmlk-rpg/mlk/rpg/battle-indicator.c @ 434:4e78f045e8c0
rpg: cleanup hierarchy
author | David Demelier <markand@malikania.fr> |
---|---|
date | Sat, 15 Oct 2022 21:24:17 +0200 |
parents | src/libmlk-rpg/rpg/battle-indicator.c@862b15c3a3ae |
children | 773a082f0b91 |
comparison
equal
deleted
inserted
replaced
433:862b15c3a3ae | 434:4e78f045e8c0 |
---|---|
1 /* | |
2 * battle-indicator.c -- drawable for rendering a hp/mp count usage | |
3 * | |
4 * Copyright (c) 2020-2022 David Demelier <markand@malikania.fr> | |
5 * | |
6 * Permission to use, copy, modify, and/or distribute this software for any | |
7 * purpose with or without fee is hereby granted, provided that the above | |
8 * copyright notice and this permission notice appear in all copies. | |
9 * | |
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | |
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | |
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | |
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | |
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
17 */ | |
18 | |
19 #include <assert.h> | |
20 #include <math.h> | |
21 #include <stdio.h> | |
22 #include <string.h> | |
23 | |
24 #include <mlk/core/color.h> | |
25 #include <mlk/core/font.h> | |
26 #include <mlk/core/panic.h> | |
27 | |
28 #include <mlk/ui/theme.h> | |
29 | |
30 #include "battle-indicator.h" | |
31 | |
32 #define THEME(bti) ((bti)->theme ? (bti)->theme : theme_default()) | |
33 #define STEP (2) | |
34 #define DELAY (5) | |
35 | |
36 static inline unsigned int | |
37 inc(int cmp, int tgt) | |
38 { | |
39 return tgt > cmp ? fmin(cmp + STEP, tgt) : fmax(cmp - STEP, tgt); | |
40 } | |
41 | |
42 static inline int | |
43 colored(const struct battle_indicator *bti) | |
44 { | |
45 /* Only check r, g, b and ignore alpha. */ | |
46 return COLOR_R(bti->cur) == COLOR_R(bti->color) && | |
47 COLOR_G(bti->cur) == COLOR_G(bti->color) && | |
48 COLOR_B(bti->cur) == COLOR_B(bti->color); | |
49 } | |
50 | |
51 void | |
52 battle_indicator_start(struct battle_indicator *bti) | |
53 { | |
54 assert(bti); | |
55 | |
56 char buf[128]; | |
57 const struct theme *theme = THEME(bti); | |
58 | |
59 snprintf(buf, sizeof (buf), "%u", bti->amount); | |
60 | |
61 bti->cur = 0xffffffff; | |
62 bti->elapsed = 0; | |
63 bti->alpha = 250; | |
64 | |
65 if (font_render(theme->fonts[THEME_FONT_INTERFACE], &bti->tex[0], buf, bti->cur) < 0|| | |
66 font_render(theme->fonts[THEME_FONT_INTERFACE], &bti->tex[1], buf, 0x000000ff) < 0) | |
67 panic(); | |
68 } | |
69 | |
70 int | |
71 battle_indicator_completed(const struct battle_indicator *bti) | |
72 { | |
73 assert(battle_indicator_ok(bti)); | |
74 | |
75 return colored(bti) && bti->alpha == 0; | |
76 } | |
77 | |
78 int | |
79 battle_indicator_ok(const struct battle_indicator *bti) | |
80 { | |
81 return bti && texture_ok(&bti->tex[0]) && texture_ok(&bti->tex[1]); | |
82 } | |
83 | |
84 int | |
85 battle_indicator_update(struct battle_indicator *bti, unsigned int ticks) | |
86 { | |
87 assert(battle_indicator_ok(bti)); | |
88 | |
89 bti->elapsed += ticks; | |
90 | |
91 if (bti->elapsed > DELAY) { | |
92 bti->elapsed = 0; | |
93 | |
94 if (!colored(bti)) { | |
95 /* Update colors first. */ | |
96 bti->cur = COLOR_HEX( | |
97 inc(COLOR_R(bti->cur), COLOR_R(bti->color)), | |
98 inc(COLOR_G(bti->cur), COLOR_G(bti->color)), | |
99 inc(COLOR_B(bti->cur), COLOR_B(bti->color)), | |
100 255 | |
101 ); | |
102 | |
103 texture_set_color_mod(&bti->tex[0], bti->cur); | |
104 } else { | |
105 /* Update alpha next. */ | |
106 bti->alpha -= 10; | |
107 | |
108 texture_set_alpha_mod(&bti->tex[0], bti->alpha); | |
109 texture_set_alpha_mod(&bti->tex[1], bti->alpha); | |
110 } | |
111 } | |
112 | |
113 return battle_indicator_completed(bti); | |
114 } | |
115 | |
116 void | |
117 battle_indicator_draw(const struct battle_indicator *bti, int x, int y) | |
118 { | |
119 assert(battle_indicator_ok(bti)); | |
120 | |
121 texture_draw(&bti->tex[1], x + 1, y + 1); | |
122 texture_draw(&bti->tex[0], x, y); | |
123 } | |
124 | |
125 void | |
126 battle_indicator_finish(struct battle_indicator *bti) | |
127 { | |
128 assert(bti); | |
129 | |
130 texture_finish(&bti->tex[0]); | |
131 texture_finish(&bti->tex[1]); | |
132 | |
133 memset(bti, 0, sizeof (*bti)); | |
134 } |