MCPcopy Create free account
hub / github.com/OpenLoco/OpenLoco / wrapString

Function wrapString

src/OpenLoco/src/Graphics/TextRenderer.cpp:1292–1410  ·  view source on GitHub ↗

0x00495301 Note: Returned break count is -1. TODO: Refactor out this -1. @return maxWidth @ (numLinesToDisplayAllChars-1) @

Source from the content-addressed store, hash-verified

1290 // Note: Returned break count is -1. TODO: Refactor out this -1.
1291 // @return maxWidth @<cx> (numLinesToDisplayAllChars-1) @<di>
1292 static std::pair<uint16_t, uint16_t> wrapString(Font font, char* buffer, uint16_t stringWidth)
1293 {
1294 // std::vector<const char*> wrap; TODO: refactor to return pointers to line starts
1295 uint16_t wrapCount = 0;
1296 uint16_t maxWidth = 0;
1297
1298 for (auto* ptr = buffer; *ptr != '\0';)
1299 {
1300 auto* startLine = ptr;
1301 uint16_t lineWidth = 0;
1302 auto lastWordLineWith = lineWidth;
1303 auto* wordStart = ptr;
1304 for (; *ptr != '\0' && lineWidth < stringWidth; ++ptr)
1305 {
1306 const auto chr = static_cast<uint8_t>(*ptr);
1307 if (chr >= ControlCodes::noArgBegin && chr < ControlCodes::noArgEnd)
1308 {
1309 bool forceEndl = false;
1310 switch (chr)
1311 {
1312 case ControlCodes::newline:
1313 {
1314 *ptr = '\0';
1315 forceEndl = true;
1316 ++ptr; // Skip over '\0' when forcing a new line
1317 wrapCount++;
1318 // wrap.push_back(startLine); TODO: refactor to return pointers to line starts
1319 maxWidth = std::max(maxWidth, lineWidth);
1320 break;
1321 }
1322 case ControlCodes::Font::small:
1323 font = Font::small;
1324 break;
1325 case ControlCodes::Font::large:
1326 font = Font::large;
1327 break;
1328 case ControlCodes::Font::bold:
1329 font = Font::medium_bold;
1330 break;
1331 case ControlCodes::Font::regular:
1332 font = Font::medium_normal;
1333 break;
1334 }
1335 if (forceEndl)
1336 {
1337 break;
1338 }
1339 }
1340 else if (chr >= ControlCodes::oneArgBegin && chr < ControlCodes::oneArgEnd)
1341 {
1342 switch (chr)
1343 {
1344 case ControlCodes::moveX:
1345 lineWidth = static_cast<uint8_t>(ptr[1]);
1346 break;
1347 }
1348 ptr += 1;
1349 }

Callers 5

createErrorWindowFunction · 0.85
commonFunction · 0.85
drawStringLeftWrappedFunction · 0.85
drawStringCentredWrappedFunction · 0.85
wrapStringMethod · 0.85

Calls 4

getG1ElementFunction · 0.85
getCharacterWidthFunction · 0.85
locoStrlenFunction · 0.85
getIndexMethod · 0.45

Tested by

no test coverage detected