2

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"));

desc

This shows what it should look like. The message only works properly if it is less than 80 characters long.

enter image description here

RBerteig
  • 41,948
  • 7
  • 88
  • 128
AStopher
  • 4,207
  • 11
  • 50
  • 75

1 Answers1

1

Your program crashes because of the defined length of your arrays. Resize newmsg appropriately

Also check that startpos + i >= 0 && < length of array ...


As for your second question:

As you said:

Use of colours in the text causes the string to shift to the right by two characters for each colour code

Seems that colour codes are being removed before printed and your problems arises because you are taking into account a length of a message which includes color codes...

If so, how would I approach this?`

Maybe adding 2 less spaces for each color code found on the msg for example.

RBerteig
  • 41,948
  • 7
  • 88
  • 128
rmp
  • 1,053
  • 7
  • 15
  • Changed in the example. – AStopher Nov 20 '13 at 13:55
  • There's already a function in the Lua that stops this from happening. – AStopher Nov 20 '13 at 13:59
  • wanna bet? the problem is not in lua, is on c++ side... use a debugger to see where and why it is crashing – rmp Nov 20 '13 at 14:00
  • @AlexanderStopher You have to check for string termination and count how many possible characters can be in the new string rather than having a 200 for your line length. – Bob. Nov 20 '13 at 14:02
  • @rmp Yes... that's why I originally tagged the question C++ and why I'm showing the C++ code. – AStopher Nov 20 '13 at 14:03
  • @Bob. There's no need to do this, Lua limits the console message to a maximum of 112 characters before it actually sends to C++. – AStopher Nov 20 '13 at 14:08
  • @rmi Also cannot check this as the answer since the original question hasn't been answered, I only mentioned the problem with the server crashing since it's related, but didn't require answering. :) – AStopher Nov 20 '13 at 14:10
  • @rmp The EndFunction just ends execution of the function and parses the produced value back to Lua. It's a way of protecting against functions that have memory leaks. – AStopher Nov 20 '13 at 14:19
  • @rmp Someone else solved this for me, put I'm marking your post as the solution as it fixed the second error for me. Thanks all :) – AStopher Nov 20 '13 at 19:49