TextOut()

I need to render GDI text using TextOut() to a OpenGL double-buffer window.
How can I do this?

I need to use TextOut() because not only does it give me anti-aliased fonts,
I also need to use the exact screen coordinate positioning it provides.

If memory serves well, you wouldn’t need much more than an HDC. Of course this means that you
a) build your application using the win32 framework
b) you somehow get the HDC of the current window (I don’t know how, but I think it’s possible).

I think though that when I used it I had flickering problems. I’d suggest, do a search on google for OpenGL Game Programming, go to errata and download the source code for the chapter regarding text (look at the book contents to see which one is it). They have a pretty good coverage of doing fonts. As for the screen coordinates, it might be a bit of work, but it should be trivial.

The problem is OpenGL doesn’t allow GDI to directly render to the backbuffer.

So, how can I get fonts created by TextOut on the backbuffer?

Q131024 - Drawing Three-Dimensional Text in OpenGL Appliations
http://support.microsoft.com/kb/q131024/

GDI operations, such as TextOut, can be performed on an OpenGL window only if the window
is single-buffered. The Windows NT implementation of OpenGL does not support GDI graphics
in a double-buffered window…

I was hoping I could use glDrawPixels() to do it, but that function only draws
directly to the frame buffer. Using it on a double-buffer window doesn’t work.
I can’t draw directly to the frame buffer, because as you know it will flicker.

Can’t you render the text to a buffer in memory?
That way you could store it as a texture, render it as a quad, and only re-upload it when it has changed.

Since there are other elements on the backbuffer that need to be layered, how exactly would
I preserve the transparency of the text, when copying it from the offscreen surface to the backbuffer?
The text can be any color, including black.

Ugh “surface!”

But I know what you mean.

Here’s an easy way: Create a next_power_of_2(number of chars you need)*char_width width, char_height image and type out the chars you need in order in a fixed width font (width of char_width)

Type them out in the order they are presented here: http://www.jimprice.com/jim-asc.htm

Just start with the one you want; I usually use all the punctuation, letters and digits and forget about the extended chars (not on that page) and the control chars (no use for them anyway, in this case)

Then upload your texture; at render time for a string, the easiest way to do it would be:

void draw_a_string(const char *dstring)
{
     glFloat left, right, top, bottom;
     float length=0.0f;

     glBindTexture(GL_TEXTURE_2D, font_texture_name);

     glBegin(GL_QUADS);

     for(1; *dstring; ++dstring)
     {
          left=(*dstring%16*16)/(float)texture_width;
          right=(*dstring%16*16+15)/(float)texture_width;
          top=(*dstring/16*16)/(float)texture_width;
          bottom=(*dstring/16*16+15)/(float)texture_width;

          glTexCoord2f(left, top);  glVertex2f(length, 0.0f);
          glTexCoord2f(left, bottom);  glVertex2f(length, height);
          glTexCoord2f(right, bottom);  glVertex2f(length+1, height);
          glTexCoord2f(right, top);  glVertex2f(length+1, 0.0f);

          ++length;
     }

     glEnd();
}

(Your function will then have to use the ASCII character set so if that’s a problem then… uh, I dunno)

Might be errors in there, I’m typing this quickly at work where I never get to use OpenGL except when I’m driving home .

Thanks, but I don’t believe that addresses my problem. I’m not creating text offline.

I’m creating text while the program is running, and I have no idea what kind
of text it will be until it is created, like a text editor.

Then I’m not sure that TextOut will work for you at all.

Unless TextOut can also write an alpha channel, you’ll lose all transparency information.

Maybe you could try FTGL?
(try http://homepages.paradise.net.nz/henryj/code/index.html )

I require TextOut() for its screen coordinate text placement.

I haven’t been able to re-produce exactly how TextOut() places text using outlined fonts
or a texture with text on it. Any other method of text creation, using FTGL or other
library, must duplicate TextOut() exactly, including where it places text.

The function doesn’t care if your string is static or cosntantly changing. If you just set up a texture to store a “font” for lack of a better term, it’s extremely simple to write a function that will render an arbitrary string.