annotate src/core/message.c @ 59:52792b863ff7

misc: separate core from game
author David Demelier <markand@malikania.fr>
date Tue, 21 Jan 2020 12:42:33 +0100
parents src/message.c@b815621df3e3
children 7266c750b649
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
24
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
1 /*
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
2 * message.c -- message dialog
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
3 *
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
4 * Copyright (c) 2020 David Demelier <markand@malikania.fr>
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
5 *
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
6 * Permission to use, copy, modify, and/or distribute this software for any
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
7 * purpose with or without fee is hereby granted, provided that the above
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
8 * copyright notice and this permission notice appear in all copies.
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
9 *
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
17 */
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
18
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
19 #include <assert.h>
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
20
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
21 #include "font.h"
28
783841af4033 core: implement painter API, closes #2451 @1h
David Demelier <markand@malikania.fr>
parents: 24
diff changeset
22 #include "message.h"
783841af4033 core: implement painter API, closes #2451 @1h
David Demelier <markand@malikania.fr>
parents: 24
diff changeset
23 #include "painter.h"
783841af4033 core: implement painter API, closes #2451 @1h
David Demelier <markand@malikania.fr>
parents: 24
diff changeset
24 #include "sprite.h"
24
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
25 #include "texture.h"
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
26
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
27 #define MESSAGE_SPEED 1000 /* Time delay for animations */
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
28 #define MESSAGE_TIMEOUT 5000 /* Time for auto-closing */
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
29
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
30 void
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
31 message_start(struct message *msg)
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
32 {
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
33 assert(msg);
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
34
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
35 msg->elapsed = 0;
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
36 msg->state = MESSAGE_OPENING;
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
37 }
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
38
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
39 void
46
b815621df3e3 core: remove all fixed width integers, closes #2460
David Demelier <markand@malikania.fr>
parents: 28
diff changeset
40 message_update(struct message *msg, unsigned int ticks)
24
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
41 {
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
42 assert(msg);
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
43
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
44 msg->elapsed += ticks;
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
45
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
46 switch (msg->state) {
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
47 case MESSAGE_OPENING:
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
48 if (msg->elapsed >= MESSAGE_SPEED)
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
49 msg->state = MESSAGE_SHOWING;
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
50 break;
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
51 case MESSAGE_SHOWING:
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
52 /* Do automatically switch state if requested by the user. */
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
53 if (msg->flags & MESSAGE_AUTOMATIC && msg->elapsed >= MESSAGE_TIMEOUT)
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
54 msg->state = MESSAGE_HIDING;
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
55
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
56 break;
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
57 default:
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
58 break;
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
59 }
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
60 }
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
61
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
62 void
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
63 message_draw(struct message *msg)
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
64 {
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
65 assert(msg);
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
66
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
67 /* TODO: more constant variables. */
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
68 struct texture *lines[3];
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
69 int x = 160;
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
70 int y = 80;
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
71
28
783841af4033 core: implement painter API, closes #2451 @1h
David Demelier <markand@malikania.fr>
parents: 24
diff changeset
72 painter_set_color(0xff0000ff);
783841af4033 core: implement painter API, closes #2451 @1h
David Demelier <markand@malikania.fr>
parents: 24
diff changeset
73 painter_draw_rectangle(true, x, y, 960, 160);
24
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
74
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
75 for (int i = 0; msg->text[i]; ++i) {
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
76 lines[i] = font_render(msg->font, msg->text[i], 0xffffffff);
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
77
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
78 if (!lines[i])
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
79 continue;
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
80
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
81 texture_draw(lines[i], x, y);
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
82 texture_close(lines[i]);
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
83 y += 53;
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
84 }
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
85 }
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
86
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
87 void
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
88 message_hide(struct message *msg)
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
89 {
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
90 assert(msg);
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
91
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
92 msg->elapsed = 0;
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
93 }
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
94
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
95 bool
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
96 message_is_complete(struct message *msg)
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
97 {
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
98 assert(msg);
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
99
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
100 return msg->state == MESSAGE_HIDING && msg->elapsed >= MESSAGE_SPEED;
4a06503641eb core: start basic implementation of dialog, continue #2449 @2h
David Demelier <markand@malikania.fr>
parents:
diff changeset
101 }