I am trying to convert the example in the code bank for formatting text to stay in a box so far i have this. what happens is it cuts some letters off that it shouldn't. can anyone see what i am doing wrong
.h file
class cChatSystem
{
public:
void Chat(std::string sbuffer);
cChatSystem* GetPtr() {return this;}
private:
void DrawChat(); // Chat box will have a default position and size.
void FormatText(std::string text, int width);
std::string CleanText(std::string text);
std::string ls_line;
std::string ls_message;
int lc;
int newx,newy;
int limit;
int chars;
std::string restMsg;
std::string line;
int rest;
int fontheight;
int font_width;
};
cpp file
void cChatSystem::Chat(std::string sbuffer)
{
// create chat box, display text, check for more text, dsplay more icon.
DrawChat(); //Draw the box arounf the text.
fontheight = 0;
font_width = 0;
// Format text for writing.
FormatText(sbuffer, 450);
}
void cChatSystem::DrawChat()
{
int m_posX = 30;
int m_posY = 300;
int m_width = 580;
int m_height = 150;
d3d_boxAlpha ( m_posX, m_posY, m_width+m_posX, m_height+m_posY , 0, 0, 0, 175,dbGetDirect3DDevice() );// the box
d3d_line (m_posX, m_posY, m_posX, m_height+m_posY, 255, 255, 255, 255);// left side
d3d_line (m_posX+1, m_posY, m_posX+1, m_height+m_posY, 255, 255, 255, 255);// left side
d3d_line (m_posX+2, m_posY, m_posX+2, m_height+m_posY, 255, 255, 255, 255);// left side
d3d_line( m_posX, m_posY, m_width+m_posX, m_posY, 255, 255, 255, 255); // top
d3d_line( m_posX, m_posY+1, m_width+m_posX, m_posY+1, 255, 255, 255, 255); // top
d3d_line( m_posX, m_posY+2, m_width+m_posX, m_posY+2, 255, 255, 255, 255); // top
d3d_line( m_width+m_posX, m_posY, m_width+m_posX, m_height+m_posY, 255, 255, 255, 255); // right side
d3d_line( m_width+m_posX-1, m_posY, m_width+m_posX-1, m_height+m_posY, 255, 255, 255, 255); // right side
d3d_line( m_width+m_posX-2, m_posY, m_width+m_posX-2, m_height+m_posY, 255, 255, 255, 255); // right side
d3d_line( m_posX, m_height+m_posY, m_width+m_posX, m_height+m_posY, 255, 255, 255, 255); // bottom side
d3d_line( m_posX, m_height+m_posY-1, m_width+m_posX, m_height+m_posY-1, 255, 255, 255, 255); // bottom side
d3d_line( m_posX, m_height+m_posY-2, m_width+m_posX, m_height+m_posY-2, 255, 255, 255, 255); // bottom side
}
void cChatSystem::FormatText(std::string text, int width)
{
fontheight = dbTextHeight("A");
font_width = dbTextSize();
ls_message = text;
chars = ls_message.length();
int limit = (width / font_width);
newx = 60;
newy = 320;
while (chars > 0)
{
line = dbLeft((char*)ls_message.c_str(), limit);//extract the current line to write
if (rest > limit) //Clean line from spaces and leave last word complete
{
line = CleanText(line);
}
//`write new complete word and increase line
dbText( newx, newy, (char*)line.c_str());
newy = newy + fontheight;
//`move to the rest of the message and chop
chars = ls_message.length();
rest = chars - line.length(); //:`newlimit
restMsg = dbRight((char*)ls_message.c_str(), rest);
ls_message = restMsg;
}
}
std::string cChatSystem::CleanText(std::string text)
{
ls_line = text;
lc = ls_line.length();
if ( ls_line.substr(1,1) == " ")
{
ls_line = ls_line.substr(2,lc);// Remove space from start of line.
lc--;
}
while ( ls_line.substr(1,1) != " " )
{
lc--;
ls_line = ls_line.substr(1,lc);
}
return ls_line;
}