summaryrefslogtreecommitdiff
path: root/src/messages.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/messages.cc')
-rw-r--r--src/messages.cc53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/messages.cc b/src/messages.cc
new file mode 100644
index 00000000..071dc66e
--- /dev/null
+++ b/src/messages.cc
@@ -0,0 +1,53 @@
+#include "messages.hpp"
+
+#include "game.hpp"
+
+#include <fmt/format.h>
+#include <string>
+
+s16b Messages::size() const
+{
+ return buffer.size();
+}
+
+message const &Messages::at(int age) const
+{
+ assert(age >= 0);
+ assert(age < size());
+
+ // Age indexes backward through history and is zero-based, so...
+ std::size_t i = buffer.size() - 1 - age;
+
+ // Get the message
+ return buffer.at(i);
+}
+
+void Messages::add(cptr msg, byte color)
+{
+ assert(msg != nullptr);
+ add(std::string(msg), color);
+}
+
+void Messages::add(std::string const &msg, byte color)
+{
+ // If the message is the same as the last message,
+ // we just increment the counter instead of adding
+ // the message.
+ if ((!buffer.empty()) && (buffer.back().text == msg))
+ {
+ buffer.back().count += 1;
+ return;
+ }
+
+ // Push onto the end of the buffer.
+ message message;
+ message.color = color;
+ message.count = 1;
+ message.text = msg;
+ buffer.push_back(message);
+}
+
+void Messages::add(message const &m)
+{
+ buffer.push_back(m);
+}