Substringy stuff
Posted: Sun Oct 09, 2011 7:54 pm
In my mod (EntityPlus) I'm printing objective messages to the scoreboard. Since the scoreboard only has place for 60 characters next to each other, I've made it line-break the objective text and print the remainder on the next line, with a maximum of 4 lines. I did this as follows:
(note that there are two calls to CG_DrawSmallStringColor because one of them draws the text in black at a small offset to get a dropshadow effect)
I've tried applying this same logic to a piece of code in q3_ui:
While the objectives code works fine, the q3_ui code does not. The input for that function is
"This is just a piece of text that was entered to test how this functionality works."
but on-screen I get to see this:
ks.
tered to test how this functionality wor
ks.
I'm not so smart when it comes to pointers and char arrays and stuffs like that and I do realize that there's a difference between desc (which is declared as char array with a specific length) and s (which is declared as a pointer to a char) but I'm not sure how to do this otherwise without the whole thing crashing. Anyone know what I'm doing wrong here?
If I echo the same thing to console though:
The console shows me exactly what I want to see.
Code: Select all
const char *s;
[...]
objlen = strlen(s);
for (i = 0; i < 4; i++) {
if ( objlen < (i * 60) + 1)
break;
CG_DrawSmallStringColor( 82, 266 + (SMALLCHAR_HEIGHT * i), va("%.60s", &s[i * 60]), color_black);
CG_DrawSmallStringColor( 80, 264 + (SMALLCHAR_HEIGHT * i), va("%.60s", &s[i * 60]), color);
}
I've tried applying this same logic to a piece of code in q3_ui:
Code: Select all
const char desc[MAX_DESCRIPTIONLENGTH];
Q_strncpyz(desc, epMenuInfo.mapdescriptions[epMenuInfo.currentmap], sizeof(desc));
objlen = strlen(desc);
for (i = 0; i < MAX_DESCRIPTIONLINES; i++) {
if ( objlen < (i * MAX_DESCRIPTIONLINELENGTH) + 1)
break;
epMenuInfo.mapDescriptionLines[i].string = va("%.40s", &desc[i * MAX_DESCRIPTIONLINELENGTH]);
}
"This is just a piece of text that was entered to test how this functionality works."
but on-screen I get to see this:
ks.
tered to test how this functionality wor
ks.
I'm not so smart when it comes to pointers and char arrays and stuffs like that and I do realize that there's a difference between desc (which is declared as char array with a specific length) and s (which is declared as a pointer to a char) but I'm not sure how to do this otherwise without the whole thing crashing. Anyone know what I'm doing wrong here?
If I echo the same thing to console though:
Code: Select all
Com_Printf("%s\n", va("%.40s", &desc[i * MAX_DESCRIPTIONLINELENGTH]));