comparison libmlk-rpg/rpg/battle-message.c @ 293:5abf2ebb1621

rpg: add battle-message
author David Demelier <markand@malikania.fr>
date Fri, 05 Mar 2021 10:08:29 +0100
parents
children 196264679079
comparison
equal deleted inserted replaced
292:08ab73b32832 293:5abf2ebb1621
1 /*
2 * battle-message.c -- automatic top center message in battle
3 *
4 * Copyright (c) 2020 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
21 #include <core/window.h>
22
23 #include <ui/align.h>
24 #include <ui/frame.h>
25 #include <ui/label.h>
26
27 #include "battle-message.h"
28
29 #define DELAY 1500
30
31 bool
32 battle_message_update(struct battle_message *msg, unsigned int ticks)
33 {
34 assert(msg);
35
36 msg->elapsed += ticks;
37
38 return msg->elapsed >= DELAY;
39 }
40
41 void
42 battle_message_draw(const struct battle_message *msg)
43 {
44 assert(msg);
45
46 struct frame f = {0};
47 struct label l = {0};
48 unsigned int lw = 0, lh = 0;
49
50 /* Prepare message frame. */
51 f.w = window.w / 3;
52 f.h = window.h / 15;
53 f.theme = msg->theme;
54
55 /* Center on top. */
56 align(ALIGN_TOP, &f.x, &f.y, f.w, f.h, 0, 20, window.w, window.h);
57
58 /* Prepare message label box. */
59 l.text = msg->text;
60 l.flags = LABEL_FLAGS_SHADOW;
61 l.theme = msg->theme;
62 label_query(&l, &lw, &lh);
63
64 /* Align the text in the box. */
65 align(ALIGN_CENTER, &l.x, &l.y, lw, lh, f.x, f.y, f.w, f.h);
66
67 frame_draw(&f);
68 label_draw(&l);
69 }