I have a problem with line breaks in my bitmap font class :(
This time I debugged and I debugged and I debugged again. Bit really, it seems like this isn't my fault at all. Look at this:
public Size DrawText(string text, Surface dest, Point destPoint, int maxWidth, int maxHeight)
{
int curLine = 0;
int curLineWidth = 0;
int largestWidth = 0;
foreach(char c in text)
{
int num = getLetterNum(c);
int width = letterWidths[num];
if(c=='\n' || maxWidth>0 && curLineWidth+width>maxWidth)
{
if(curLineWidth>largestWidth)
largestWidth = curLineWidth;
curLine++;
curLineWidth = 0;
// PROBLEM THIS LINE
if(maxHeight>0 && (curLine+1)*blockSize>maxHeight)
break;
if(c=='\n')
continue;
}
if(dest!=null)
surface.Blit(new Rectangle(num%16*blockSize, num/16*blockSize, width, blockSize), dest,
new Point(destPoint.X+curLineWidth, destPoint.Y+curLine*blockSize));
curLineWidth += width;
}
return new Size(largestWidth, curLine*blockSize);
}
The error is at this part:
if(maxHeight>0 && (curLine+1)*blockSize>maxHeight)
break;
Clarification of variables:
- maxHeight = the maximum height of the text - when it's lower than 0, there is no limit.
- curLine = the current line, starting at 0
- blockSize = the size of a letter block (ie 16x16)
This statement is executed when a newline has occured, either by reaching the end of a line or when finding a newline character.
What I want this statement to do, is: "if there is a limit on the height of the text, check if the next line would break it - if so, break out of the foreach loop".
But it doesn't work, somehow, execution always continues over the if statement, as if it wasn't there. I double checked the condition, and it looks fine.
Then, I started the debugger, and let it ran to the place where the newline occures where it passes the height boundary. The breakpoint reached (the if stament), I checked all the variables included in the condition, and found out that it indeed was true. I also pasted the condition in that little window in VS.NET (forgot the name), and it returned true. But when I stepped the execution one step, it simply went to the next statement.
What did I do wrong?