Sign in to follow this  

Text Engine - Handling newlines, multiple classes on one DC

This topic is 4273 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

Howdy all, Decided to post in for beginners, but I'm using OpenGL (that means wglUseFontBitmats and display lists, like nehe lesson 13). I've gone through an entire day trying to figure this out by programming and designing, but nothing I've done so far has worked. Essentially, in my print function for my text class, if there is a newline, I need to change the raster position so that the X is the same, but the Y is Y+(height of font)+(slight offset) to simulate an actual newline. My text functions work fine so far, I'm just trying to get the newline to work. Any ideas? Also, a question. I've encapsulated all my text functions in a 2D Text class (all my functions are for 2D only so far). When I create multiple instances of the 2D Text class on the same window (thus, using the same DC), I get essentially undefined behavior (as far as I can tell); occasionally one class prints, sometimes the other one prints, sometimes nothing prints, but never both. Their positions are both on the screen and static, so I know that it's not me printing off the screen. Sorry for the long post...I'll post code if anyone wants it, although it is rather lengthy. Thanks so much! Aviosity

Share this post


Link to post
Share on other sites
Quote:
Original post by aviosity
My text functions work fine so far, I'm just trying to get the newline to work. Any ideas?


What exactly are you having trouble with?

In Windows, (height of font)+(slight offset) translates to:

TEXTMETRIC metrics
GetTextMetrics(dc, &metrics);

(metrics.tmHeight)+(metrics.tmExternalLeading)

Share this post


Link to post
Share on other sites
Hey Mike,

I think you actually may have just given me some insight into my second question. It appears that GetTextMetrics(hDC,&metrics) automatically knows the font you're referring to when you call it, thus implying that there can only be one font per DC. Am I correct, or completely off base?

Sorry if my post seemed vague. I was looking for techniques to extract the newline; in Nehe Lesson 13, he uses a function with a variable argument list, and processes them accordingly. He uses vsprintf (which I've translated to vsprintf_s since vsprintf is now deprecated) in between va_start and va_end to parse the format string for variables and replace them with the actual numbers. However, it would be great if while it's processing the string and runs across a newline, that it automatically increments the Y position at that location in the string only.

Example:

TEXTMETRIC metrics
GetTextMetrics(dc, &metrics);
glPrint("FPS: %d\nScore: %d\n",g_FPS,g_score);

When running through this string, assuming the Y raster position starts at 10 (this is in 2D screen mode, so 10 literally means 10 pixels from the top edge):

"FPS: %d" Y Raster Pos - 10
"Score: %d" Y Raster Pos - 10+(metrics.tmHeight)+(metrics.tmExternalLeading)

Thanks a lot!
Aviosity

Share this post


Link to post
Share on other sites
Yes, you can only have one font in a DC at a time. When you want to use a different font you need to select a different one in. Usually what I do is to create all the fonts I need up front so I have the HFONT's lieing around when I need them.

The easiest thing to do would be for your print routine to break the string up into lines and call a subfunction to do the job of actually printing a line. Some pseudo-code might be:

void glPrint(int x, int y, const char * format, ...)
{
// do all the vararg stuff and get the final result into a single buffer

glPrintBuffer(x, y, buffer)
}

void glPrintBuffer(int x, int y, const char * buffer)
{
// linebreak the buffer (lots of corner cases I don't address here)
while (buffer[0] != '\0')
{
const char * end = FindEndOfLine(buffer);
glPrintLine(x, y, buffer, end-buffer);
y += line_height;
buffer = end; // reset to next line
if ('\n' == end[0]) buffer = end + 1
}
}

void glPrintLine(int x, int y, const char * buffer, size_t len)
{
// OpenGL stuff
}

Share this post


Link to post
Share on other sites
Hey Mike,

That's exactly what I started to do, seems to be working OK other than some bugs that I've thrown in. Thanks for the tip on the fonts and DCs, that'll save me a lot of time.

Thanks so much,
Aviosity

Share this post


Link to post
Share on other sites

This topic is 4273 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this