File: InfoConsole.cpp

package info (click to toggle)
spring 106.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 55,316 kB
  • sloc: cpp: 543,954; ansic: 44,800; python: 12,575; java: 12,201; awk: 5,889; sh: 1,796; asm: 1,546; xml: 655; perl: 405; php: 211; objc: 194; makefile: 76; sed: 2
file content (249 lines) | stat: -rw-r--r-- 6,286 bytes parent folder | download | duplicates (3)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#include "InfoConsole.h"
#include "GuiHandler.h"
#include "Rendering/Fonts/glFont.h"
#include "System/EventHandler.h"
#include "System/SafeUtil.h"
#include "System/Config/ConfigHandler.h"
#include "System/Log/LogSinkHandler.h"

#define border 7

CONFIG(int, InfoMessageTime).defaultValue(10).description("Time until old messages disappear from the ingame console.");
CONFIG(std::string, InfoConsoleGeometry).defaultValue("0.26 0.96 0.41 0.205");

CInfoConsole* infoConsole = nullptr;

static uint8_t infoConsoleMem[sizeof(CInfoConsole)];




void CInfoConsole::InitStatic() {
	assert(infoConsole == nullptr);
	infoConsole = new (infoConsoleMem) CInfoConsole();
}

void CInfoConsole::KillStatic() {
	assert(infoConsole != nullptr);
	spring::SafeDestruct(infoConsole);
	std::memset(infoConsoleMem, 0, sizeof(infoConsoleMem));
}


void CInfoConsole::Init()
{
	maxLines = 1;
	newLines = 0;

	msgPosIndx = 0;
	numPosMsgs = 0;

	rawId = 0;
	lifetime = configHandler->GetInt("InfoMessageTime");

	xpos = 0.0f;
	ypos = 0.0f;
	width = 0.0f;
	height = 0.0f;

	fontScale = 1.0f;
	fontSize = fontScale * smallFont->GetSize();;

	const std::string geo = configHandler->GetString("InfoConsoleGeometry");

	if (sscanf(geo.c_str(), "%f %f %f %f", &xpos, &ypos, &width, &height) != 4) {
		xpos = 0.26f;
		ypos = 0.96f;
		width = 0.41f;
		height = 0.205f;
	}

	enabled = (width != 0.0f && height != 0.0f);
	inited = true;


	logSinkHandler.AddSink(this);
	eventHandler.AddClient(this);

	rawLines.clear();
	infoLines.clear();

	lastMsgPositions.fill(ZeroVector);
}

void CInfoConsole::Kill()
{
	logSinkHandler.RemoveSink(this);
	eventHandler.RemoveClient(this);

	inited = false;
}


void CInfoConsole::Draw()
{
	if (!enabled)
		return;
	if (smallFont == nullptr)
		return;

	// infoConsole exists before guiHandler does, but is never drawn during that period
	assert(guihandler != nullptr);

	{
		// make copies s.t. mutex is not locked during glPrint calls
		std::lock_guard<decltype(infoConsoleMutex)> scoped_lock(infoConsoleMutex);

		if (infoLines.empty())
			return;

		tmpInfoLines.clear();
		tmpInfoLines.insert(tmpInfoLines.cend(), infoLines.cbegin(), infoLines.cbegin() + std::min(infoLines.size(), maxLines)); 
	}

	// always draw outlined text here, saves an ugly black background
	// (the default console is practically no longer used in any case)
	const uint32_t fontOptions = FONT_NORM | FONT_OUTLINE | FONT_BUFFERED;
	const float fontHeight = fontSize * smallFont->GetLineHeight() * globalRendering->pixelY;

	float curX = xpos + border * globalRendering->pixelX;
	float curY = ypos - border * globalRendering->pixelY;

	smallFont->SetTextColor(1.0f, 1.0f, 1.0f, 1.0f);
	smallFont->SetOutlineColor(0.0f, 0.0f, 0.0f, 1.0f);

	for (size_t i = 0, n = tmpInfoLines.size(); i < n; ++i) {
		smallFont->glPrint(curX, curY -= fontHeight, fontSize, fontOptions, tmpInfoLines[i].text);
	}

	smallFont->DrawBufferedGL4();
	smallFont->SetColors(); // default
}


void CInfoConsole::Update()
{
	std::lock_guard<decltype(infoConsoleMutex)> scoped_lock(infoConsoleMutex);

	if (infoLines.empty())
		return;

	// pop old messages after timeout
	if (infoLines[0].timeout <= spring_gettime())
		infoLines.pop_front();

	if (smallFont == nullptr)
		return;

	// if we have more lines then we can show, remove the
	// oldest and make sure the others are shown long enough
	const float  maxHeight = (height * globalRendering->viewSizeY) - (border * 2);
	const float fontHeight = smallFont->GetLineHeight();

	// height=0 will likely be the case on HEADLESS only
	maxLines = (fontHeight > 0.0f)? math::floor(maxHeight / (fontSize * fontHeight)): 1;

	for (size_t i = infoLines.size(); i > maxLines; i--) {
		infoLines.pop_front();
	}
}

void CInfoConsole::PushNewLinesToEventHandler()
{
	{
		std::lock_guard<decltype(infoConsoleMutex)> scoped_lock(infoConsoleMutex);

		if (newLines == 0)
			return;

		const size_t lineCount = rawLines.size();
		const size_t startLine = lineCount - std::min(lineCount, newLines);

		tmpLines.clear();
		tmpLines.reserve(lineCount - startLine);

		for (size_t i = startLine; i < lineCount; i++) {
			tmpLines.push_back(rawLines[i]);
		}

		newLines = 0;
	}

	for (const RawLine& rawLine: tmpLines) {
		eventHandler.AddConsoleLine(rawLine.text, rawLine.section, rawLine.level);
	}
}


size_t CInfoConsole::GetRawLines(std::vector<RawLine>& lines)
{
	std::lock_guard<decltype(infoConsoleMutex)> scoped_lock(infoConsoleMutex);

	const size_t numNewLines = newLines;

	lines.clear();
	lines.assign(rawLines.begin(), rawLines.end());

	return (newLines = 0, numNewLines);
}


void CInfoConsole::RecordLogMessage(int level, const std::string& section, const std::string& message)
{
	std::lock_guard<decltype(infoConsoleMutex)> scoped_lock(infoConsoleMutex);

	if (section == prvSection && message == prvMessage)
		return;

	newLines += (newLines < maxRawLines);

	if (rawLines.size() > maxRawLines)
		rawLines.pop_front();

	rawLines.emplace_back(prvMessage = message, prvSection = section, level, rawId++);

	if (smallFont == nullptr)
		return;

	// NOTE
	//   do not remove elements from infoLines here, ::Draw iterates over it
	//   and can call LOG() which will end up back in ::RecordLogMessage
	const std::string& wrappedText = smallFont->Wrap(message, fontSize, (width * globalRendering->viewSizeX) - (2 * border));
	const std::u8string& unicodeText = toustring(wrappedText);

	for (auto& splitLine: smallFont->SplitIntoLines(unicodeText)) {
		// add the line to the console
		infoLines.emplace_back();

		InfoLine& l = infoLines.back();
		l.text    = std::move(splitLine);
		l.timeout = spring_gettime() + spring_secs(lifetime);
	}
}


void CInfoConsole::LastMessagePosition(const float3& pos)
{
	// reset index to head when a new msg comes in
	msgPosIndx  = numPosMsgs % lastMsgPositions.size();
	numPosMsgs += 1;

	lastMsgPositions[msgPosIndx] = pos;
}

const float3& CInfoConsole::GetMsgPos(const float3& defaultPos)
{
	if (numPosMsgs == 0)
		return defaultPos;

	const float3& p = lastMsgPositions[msgPosIndx];

	// cycle to previous position
	msgPosIndx += (std::min(numPosMsgs, lastMsgPositions.size()) - 1);
	msgPosIndx %= std::min(numPosMsgs, lastMsgPositions.size());

	return p;
}