comparison src/message.c @ 24:4a06503641eb

core: start basic implementation of dialog, continue #2449 @2h
author David Demelier <markand@malikania.fr>
date Fri, 10 Jan 2020 13:30:01 +0100
parents
children 783841af4033
comparison
equal deleted inserted replaced
23:bc9637a2601b 24:4a06503641eb
1 /*
2 * message.c -- message dialog
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 "font.h"
22 #include "texture.h"
23 #include "message.h"
24 #include "sprite.h"
25 #include "window.h"
26
27 #define MESSAGE_SPEED 1000 /* Time delay for animations */
28 #define MESSAGE_TIMEOUT 5000 /* Time for auto-closing */
29
30 void
31 message_start(struct message *msg)
32 {
33 assert(msg);
34
35 msg->elapsed = 0;
36 msg->state = MESSAGE_OPENING;
37 }
38
39 void
40 message_update(struct message *msg, unsigned ticks)
41 {
42 assert(msg);
43
44 msg->elapsed += ticks;
45
46 switch (msg->state) {
47 case MESSAGE_OPENING:
48 if (msg->elapsed >= MESSAGE_SPEED)
49 msg->state = MESSAGE_SHOWING;
50 break;
51 case MESSAGE_SHOWING:
52 /* Do automatically switch state if requested by the user. */
53 if (msg->flags & MESSAGE_AUTOMATIC && msg->elapsed >= MESSAGE_TIMEOUT)
54 msg->state = MESSAGE_HIDING;
55
56 break;
57 default:
58 break;
59 }
60 }
61
62 void
63 message_draw(struct message *msg)
64 {
65 assert(msg);
66
67 /* TODO: more constant variables. */
68 struct texture *lines[3];
69 int x = 160;
70 int y = 80;
71
72 window_set_color(0xff0000ff);
73 window_draw_rectangle(true, x, y, 960, 160);
74
75 for (int i = 0; msg->text[i]; ++i) {
76 lines[i] = font_render(msg->font, msg->text[i], 0xffffffff);
77
78 if (!lines[i])
79 continue;
80
81 texture_draw(lines[i], x, y);
82 texture_close(lines[i]);
83 y += 53;
84 }
85 }
86
87 void
88 message_hide(struct message *msg)
89 {
90 assert(msg);
91
92 msg->elapsed = 0;
93 }
94
95 bool
96 message_is_complete(struct message *msg)
97 {
98 assert(msg);
99
100 return msg->state == MESSAGE_HIDING && msg->elapsed >= MESSAGE_SPEED;
101 }