sprintf and new line

Using VS.Net 2003, VC++, and console app…

I use the following function to print text to the window.

char myString[255];

void drawString(char *string)
{
  int len, i;
  len = (int) strlen(string);
  for (i = 0; i < len; i++)
  {
    glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, string[i]);
  }
}

And call it this way…

	glPushMatrix();
		glRasterPos3f(-1.5, 1.0, -2);
		sprintf(myString, "LineOne:");
		drawString(myString);
	glPopMatrix();
	glPushMatrix();
		glRasterPos3f(-1.5, 0.95, -2);
		sprintf(myString, "LineTwo:  %d ",(int)MY_ANGLE);
		drawString(myString);
	glPopMatrix();

I have a few more lines to print, but how can I put all the lines I need in only one sprintf statement so it’s in only one pushMatrix?

Thanks,

Zath

You should parse the string for the ’
’ character and put each line separatelly with different Y translation.

Zath,
You need to split(tokenize) the input string into multiple substrings as glAren mentioned. Then, draw each line and move the raster position down for drawing the next line. It is an off topic, but here is a C++ snippet to tokenize a string:

void tokenizeString(const string& str, const string& delimiter, vector<string>& lines)
{
    string subStr;
    string::size_type from;
    string::size_type to;

    // reset container
    lines.clear();

    from = 0; // start from the first char

    // find the delimiter then extract sub-string upto delimiter
    while((to = str.find(delimiter, from+1)) != string::npos)
    {
        subStr = str.substr(from, to-from); // "to" points at the delimiter, ignore delimiter
        lines.push_back(subStr);

        from = to + 1;                      // next
    }

    // don't forget the last token
    subStr = str.substr(from, to-from);
    lines.push_back(subStr);
}

And your drawing routine probably looks like this:

...
vector<string> lines;
tokenizeString(srcString, "
", lines); // split input string to multiple lines

// draw each line with drawString()
float lineSpace = 0.1f; // adjust it yourself
int lineCount = (int)lines.size();
for(int i = 0; i < lineCount; ++i)
{
    glRasterPos3f(posX, posY, posZ);
    drawString(lines.at(i).c_str());
    posY -= lineSpace;    // next line
}
...

Hope it helps…