-
Notifications
You must be signed in to change notification settings - Fork 0
/
DebugLog.cpp
91 lines (75 loc) · 1.81 KB
/
DebugLog.cpp
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
90
91
#include "DebugLog.h"
DebugLog::DebugLog() {
m_loaded = false;
m_visible = true;
m_maxLogMessages = 20;
}
DebugLog::~DebugLog() {
this->ClearLogs();
}
void DebugLog::OnLoad(std::shared_ptr<CVarManagerWrapper> cvarWrapper) {
if (m_loaded) {
return;
}
m_visible = true;
m_loaded = true;
m_cvarWrapper = cvarWrapper;
Log("[DebugLog] loaded module");
}
void DebugLog::OnUnload() {
m_loaded = false;
RawLog("[DebugLog] unloaded module");
}
void DebugLog::ShowLogs(bool visible) {
m_visible = visible;
}
void DebugLog::RawLog(std::string message) {
if (m_cvarWrapper) {
m_cvarWrapper->log(message);
}
}
void DebugLog::Log(std::string message) {
LogMessage* log = new LogMessage();
log->m_text = message;
m_logs.push_back(log);
RawLog(message);
if (m_logs.size() > m_maxLogMessages) {
LogMessage* purge = m_logs.front();
delete purge;
purge = NULL;
m_logs.pop_front();
}
}
void DebugLog::ClearLogs() {
while (m_logs.size()) {
LogMessage* purge = m_logs.front();
delete purge;
purge = NULL;
m_logs.pop_front();
}
}
void DebugLog::Render(CanvasWrapper& canvas, int xPos, int yPos) {
if (!m_loaded) {
RawLog("[DebugLog::Render] not loaded");
return;
}
if (!m_visible) {
RawLog("[DebugLog::Render] not visible");
return;
}
// Render logs:
auto screenSize = canvas.GetSize();
list<LogMessage*>::iterator it;
unsigned count = 0;
for (it = m_logs.begin(); it != m_logs.end(); ++it, ++count) {
LogMessage* log = *it;
log->Render(canvas, xPos, yPos - (count * 35), 2, 2, 255);
}
}
// Log messages are told where to draw
void LogMessage::Render(CanvasWrapper& canvas, int xPos, int yPos, int xScale, int yScale, int alpha) {
Vector2 drawLoc = { xPos, yPos };
canvas.SetPosition(drawLoc);
canvas.SetColor(m_color.R, m_color.G, m_color.B, alpha);
canvas.DrawString(m_text, xScale, yScale);
}