Created
April 6, 2013 00:00
-
-
Save FlyingJester/5323579 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void TS_BMPFont::addline(const char **textlines, int *readylines, char *linebuffer){ | |
(*readylines)++; | |
textlines = (const char **)realloc(textlines, (*readylines)*sizeof(const char *)); | |
textlines[(*readylines)-1] = strdup(linebuffer); | |
linebuffer = (char *)realloc(linebuffer, 1); | |
*linebuffer = '\0'; | |
} | |
const char **TS_BMPFont::wordWrapString(const char *t, int w, int* num) { | |
*num = 0; | |
intptr_t startOfWord = (intptr_t)t; //NUMBER OF CHARACTERS | |
const char **textlines = (const char **)calloc(0, sizeof(const char *)); | |
int readylines = 0; | |
int lineWidth = 0; //PIXEL WIDTH! | |
int tlength = strlen(t); | |
char *linebuffer = (char *)calloc(1, sizeof(char)); | |
*linebuffer = '\0'; | |
for(int i = 0; i<tlength; i++){ | |
if((t[i]==' ')||(t[i]=='\n')){ | |
bool newline = false; | |
int wordlen = (((intptr_t)t)+i)-startOfWord; //NUMBER OF CHARACTERS | |
const char *tword = strncpy((char *)calloc(wordlen, sizeof(char)), (const char *)startOfWord, wordlen); | |
if(lineWidth+this->getStringWidth(tword)>w){ | |
this->addline(textlines, &readylines, linebuffer); | |
lineWidth=this->getStringWidth(tword); | |
newline = true; | |
} | |
else{ | |
lineWidth+=this->getStringWidth(tword); | |
} | |
linebuffer = strcat(linebuffer, tword); | |
linebuffer = (char *)realloc(linebuffer, (strlen(linebuffer)+wordlen)*sizeof(char)); | |
if((t[i]==' ')&&(!newline)){ | |
if(lineWidth+this->getStringWidth(" ")<=w){ | |
linebuffer = (char *)realloc(linebuffer, (lineWidth+2)*sizeof(char)); | |
} | |
} | |
else if((t[i]=='\n')&&(!newline)){ | |
this->addline(textlines, &readylines, linebuffer); | |
lineWidth = 0; | |
} | |
free((void*)tword); | |
startOfWord = ((intptr_t)t)+(i); | |
} | |
} | |
(readylines)++; | |
textlines = (const char **)realloc(textlines, (readylines)*sizeof(const char *)); | |
textlines[(readylines)-1] = strdup(linebuffer); | |
free(linebuffer); | |
(*num) = readylines; | |
return textlines; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment