See the first image below for the problem. You can see that the first line of text correctly aligns, but the second does not.
A few months ago I wrote some code that centers the text in a string in order to process it nicely onto a console for a gameserver modification. This code works really nicely, but has two major problems:
The gameserver crashes if we go over the 112 character limit of the actual console (this problem doesn't occur in a normal console message)
Use of colours in the text causes the string to shift to the right by two characters for each colour code
I am looking to solve the second problem (although solving the first would be a massive benefit), but do not have a clue how to do this. I cannot simply take the colour coding out since this is needed to colour the text.
This code might be simple if I didn't have 9 colour types to choose from:
*$1= White
*$2= Dark Blue
*$3= Green
*$4= Red
*$5= Yellow
*$6= Light Blue
*$7= Purple
*$8= Orange
*$9= Grey
I don't want to simply look for the '$' either, since this is used as a normal character in some cases.
Is ignoring the $1-$9 when inserting the spaces possible? If so, how would I approach this?
The Code
int CScriptBind_GameRules::CentreTextForConsole(IFunctionHandler *pH, const char *msg)
{
if (msg)
{
const int linelength=200;
char newmsg[linelength+1];
for(int i=0;i<linelength;i++)
newmsg[i]=0;
for(int i=0;i<linelength;i++)
newmsg[i]=' ';
newmsg[linelength]=0;
int msglen=strlen(msg);
int startpos=4+linelength/2-msglen/2;
for(int i=msglen-1;i>=0;i--)
newmsg[startpos+i]=msg[i];
msg=0; //Null the message.
return pH->EndFunction(newmsg);
}
else
{
return pH->EndFunction();
}
Example
I can't get to my development PC right now as I'm as work- I'll post a screenshot in a few hours when I get back if this issue hasn't been resolved.
Caller from Lua:
Core.CenteredConsole:All(g_gameRules.game:CentreTextForConsole("$1Crysis$4Wars Infinity iX 5.0"));
This shows what it should look like. The message only works properly if it is less than 80 characters long.