Jump to content
  • Advertisement
Sign in to follow this  
aaron_ds

GDI + Fonts + OpenGL

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

Hello. I'm trying to create a nice texture mapped quads font engine; a la Nehe Lesson 17. The tutorial is nice and all, but every font that the user wants to use must be turned into a bitmap before the program is ran. I'm creating a GUI with elements defined by XML; think XAML. One of the things I'd like to specify in the XML is fontname and size. eg:
<text position-x="0" position-y="0" wrap-at="46" font-name="Arial" font-size="12">
    Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
</text>
I would like to internalize the font bitmap creation and then create an opengl texture based on the bitmap. I can probably find a library out there that does this for me, but I'm doing this specifically to learn some GDI for fun. So far I have this in the font code:
void buildFont(const std::string & font_name, const int size) {
    HFONT	font;
    HFONT	oldfont;
    HBITMAP	hBitmap, hOldBitmap;
    BITMAP BMP;
    HDC hDCMem;
    int width=256, height=256;


    base = glGenLists(256);

    std::string char_set;
    for(int i=0; i<256; ++i){
        if(i>=32 && i<=128){
            char_set+=static_cast<char>(i);
        }
        else{
            char_set+=' ';
        }
    }
    hDCMem=CreateCompatibleDC(IRenderPolicy::device.hDC);
    hBitmap=CreateCompatibleBitmap(IRenderPolicy::device.hDC, width, height);


    font = CreateFont( -size, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_TT_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY,
				 FF_DONTCARE | DEFAULT_PITCH,     font_name.c_str());

    oldfont = (HFONT)SelectObject(IRenderPolicy::device.hDC, font);
    //draw font, then capture screen as texture.

    hOldBitmap = (HBITMAP)SelectObject (hDCMem, hBitmap);

    RECT rect={0,0, width, height};
    DrawText(hDCMem, char_set.c_str(), char_set.length(), &rect, DT_WORDBREAK );


    BITMAPINFO bmpInfo;// = (LPBITMAPINFO)(new char[sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)]);
    ZeroMemory(&bmpInfo,sizeof(BITMAPINFO));
    bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);

    SelectObject(hDCMem, hOldBitmap);
    GetDIBits(hDCMem, hBitmap, 0, height, NULL, &bmpInfo, DIB_RGB_COLORS);
    LPVOID lpvBits = new char[bmpInfo.bmiHeader.biSizeImage];
    GetDIBits(hDCMem, hBitmap, 0, height, lpvBits, &bmpInfo, DIB_RGB_COLORS);

    GetObject(hBitmap, sizeof(BMP), & BMP);
    texture_ptr=boost::shared_ptr<Texture>(new Texture(font_name, BMP));

    SelectObject(IRenderPolicy::device.hDC, oldfont);
    DeleteObject(font);

}


and to create the font texture from the bitmap:
bool loadTextureFromBitmapHandle(BITMAP& BMP){
    glGenTextures(1, this->texid);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
    glBindTexture(GL_TEXTURE_2D, * this->texid);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    //glTexImage2D( GL_TEXTURE_2D, 0, 3, BMP.bmWidth, BMP.bmHeight, 0,
    //GL_BGR_EXT, GL_UNSIGNED_BYTE, BMP.bmBits );
    gluBuild2DMipmaps( GL_TEXTURE_2D, 3, BMP.bmWidth, BMP.bmHeight, GL_BGR_EXT, GL_UNSIGNED_BYTE, BMP.bmBits );//Gahh! Crash. BMP.bmBits==0x0000
    this->width = BMP.bmWidth;
    this->height = BMP.bmHeight;
    this->bits = BMP.bmBitsPixel;
    return true;
}


I'm probably doing terrible things with the GDI functions. Some of the code is hacked together from font drawing tutorials and screen capture tutorials. I feel like such a noob with this GDI; I'm glad we have this forum. Thanks in advance!

Share this post


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

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!