Skip to content

Instantly share code, notes, and snippets.

@TIny-Hacker
Last active November 10, 2023 23:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TIny-Hacker/8d83b43cbc90e201dbf0bebeafeb07f0 to your computer and use it in GitHub Desktop.
Save TIny-Hacker/8d83b43cbc90e201dbf0bebeafeb07f0 to your computer and use it in GitHub Desktop.
Simple text wrapping and breaking at the end of words for the TI-84 Plus CE
// Find where to break lines
unsigned int spaceSearch(const char *string, const unsigned int start) {
for (int k = start; k >= 0; k--) {
if (string[k] == ' ') {
return k + 1;
}
}
return start;
}
void printStringWrap(const char *string, unsigned int x, uint8_t y, unsigned int charsPerLine, uint8_t maxLines) {
// Offset in the current string to begin displaying the current line's text at
unsigned int offset = 0;
// Current number of lines that the text has been drawn for (To ensure it doesn't pass maxLines)
uint8_t line = 1;
// Offset in the string where to end displaying the current line's text
unsigned int end = 0;
while (line <= maxLines) {
gfx_SetTextXY(x, y);
// Break lines at the end of words, if possible
if (strlen(string + offset) <= charsPerLine || line == maxLines) {
end = charsPerLine;
} else {
end = util_SpaceSearch(string + offset, charsPerLine);
}
for (unsigned int byte = 0; byte < end; byte++) {
if ((string + offset)[byte] == '\0') {
return;
}
if (line == maxLines && byte == end - 1 && (string + offset)[byte + 1] != '\0') {
// String extends past maximum space allowed, so cut it off with ...
gfx_PrintString("...");
} else {
gfx_PrintChar((string + offset)[byte]);
}
}
offset += end;
if (*(string + offset) == '\0') {
return y;
}
y += 12;
line++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment