-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.d
89 lines (56 loc) · 1.66 KB
/
message.d
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import allegro5.allegro_font;
import main : messageFont;
import globals;
struct MessageLine{
MessageColor color;
string message;
}
MessageLine[MessageBufferSize] messageLines;
void addMessage(MessageColor color, string mess)
{
// method for adding a new message:
// split the output string to words
// try to add word, check if pixel width exceeds limit
// if width exceeds limit
// add message with string so far
// scroll messages up
// add last word to start of new line
// continue
import std.array : split;
import std.string : toStringz;
import globals : MsgBoxWidth;
string[] words;
words = split(mess);
string msgLine;
int currWidth;
foreach(word; words)
{
word ~= " ";
int wordWidth = al_get_text_width(messageFont, word.toStringz);
if(currWidth + wordWidth >= MsgBoxWidth)
{
//add the line so far
scrollMessages();
messageLines[MessageBufferSize - 1] = MessageLine(color, msgLine);
//reset width and line text
currWidth = 0;
msgLine = word;
} else
{
//add word to line to output and update width
msgLine ~= word;
currWidth += wordWidth;
}
}
// add new message to end of list
scrollMessages();
messageLines[MessageBufferSize - 1] = MessageLine(color, msgLine);
}
void scrollMessages()
{
// scroll existing messages up
foreach(i; 1..MessageBufferSize)
{
messageLines[i-1] = messageLines[i];
}
}