unsigned short Font::TextHeight( const char *str, unsigned short linew, unsigned short spacing ) const { unsigned short lines = 0; int pos = 0, endpos = strlen(str); while ( pos < endpos ) { pos += FitText( &str[pos], linew, true ); ++lines; } return lines * (Height() + spacing); }
/** Add a string to the console. */ bool addConsoleMessage(const char *Text, CONSOLE_TEXT_JUSTIFICATION jusType, SDWORD player, bool team) { CONSOLE_MESSAGE consoleStorage; if (!allowNewMessages) { return false ; // Don't allow it to be added if we've disabled adding of new messages } std::istringstream stream(Text); std::string lines; char messageText[MAX_CONSOLE_STRING_LENGTH]; while (std::getline(stream, lines)) { // We got one "line" from the total string, now we must check // to see if it fits, if not, we truncate it. (Full text is in the logs) // NOTE: may want to break up line into multi-line so it matches the log std::string FitText(lines); while (!FitText.empty()) { int pixelWidth = iV_GetTextWidth(FitText.c_str()); if (pixelWidth <= mainConsole.width) { break; } FitText.resize(FitText.length() - 1); // Erase last char. } sstrcpy(messageText, FitText.c_str()); debug(LOG_CONSOLE, "(to player %d): %s", (int)player, messageText); consoleStorage.player = player; // set justified text flags switch (jusType) { case LEFT_JUSTIFY: consoleStorage.JustifyType = FTEXT_LEFTJUSTIFY; // Align to left edge of screen break; case RIGHT_JUSTIFY: consoleStorage.JustifyType = FTEXT_RIGHTJUSTIFY; // Align to right edge of screen break; case CENTRE_JUSTIFY: consoleStorage.JustifyType = FTEXT_CENTRE; // Align to centre of the screen break; default: debug(LOG_FATAL, "Unknown type of text justification for console print, aborting."); abort(); break; } consoleStorage.text = messageText; consoleStorage.timeAdded = realTime; // Store the time when it was added if (player == INFO_MESSAGE) { InfoMessages.push_back(consoleStorage); } else { ActiveMessages.push_back(consoleStorage); // everything gets logged here for a specific period of time if (team) { TeamMessages.push_back(consoleStorage); // persistent team specific logs } HistoryMessages.push_back(consoleStorage); // persistent messages (all types) } } return true; }