Jump to content
  • Advertisement
Sign in to follow this  
Sijmen

[.net] Another weird one (C#)

This topic is 5388 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
try it with

return new size(-1,-1);


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)
return new Size(-1,-1); // check for this as an error or even throw an exception
//break; // may only be breaking out of the if statements you added

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



Share this post


Link to post
Share on other sites
I replaced the break statement with an exception throw, but it isn't thrown.

How can this be possible? The command window returns true on that statement!

Share this post


Link to post
Share on other sites
What version of the C# compiler are you using? Do you have the .NET 2.0 beta framework installed?

Share this post


Link to post
Share on other sites
I am now using Visual Studio .NET 2003, but yes, the .NET Framework 2.0 beta is installed. Could that be a problem?

Share this post


Link to post
Share on other sites
The other "wierd problem" thread involves another nonsensical problem, and I suspect the two problems may be related to the beta framework, because in my google searches I haven't found anything that is comparable.

Share this post


Link to post
Share on other sites
Having the 2.0 framework should not be the problem as the VS2003 enviroment compiles code using the 1.1 framework and not the 2.0

Share this post


Link to post
Share on other sites
I don't know why, but it works now as it should. I ran it at school once and now it runs at home, too. :S

Share this post


Link to post
Share on other sites
Sometimes all you need to do is restart the machine. If something is odd and can't be explained, always fall back on that olde ;-)

Happened to me the other day too actually - a database connection kept on throwing something about null parameter (when parameter was not null) - even stepped thru and proved to myself that it wasn't null before disappearing into OdbcConnection - restarted machine and it worked - and hasn't been a problem since.

Share this post


Link to post
Share on other sites
Still weird though, because I have restarted my machine several times.

By the way, is it possible to reference a project without including it, with VS.NET 2003?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!