Jump to content
  • Advertisement
Sign in to follow this  
AndyEsser

Problem with GDI+ Text Rendering

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

Hi All,

I have a routine that I use to render GDI+ text and then upload that to an OpenGL texture.

The problem I have is that each call of the function doesn't seem to clear up after itself. The GDI Handles in use by the engine increments constantly when the functions called.


bool Texture::RenderGDIText(LPCWSTR sTextString, GDITextSettings ts)
{

HBITMAP hBitmap;
HGDIOBJ hOldBitmap;
PointF Pos;
FontFamily fontFamily(ts.fontFamily);
Font font(&fontFamily, ts.fontSize, ts.fontStyle, UnitPoint);
SolidBrush brush(ts.fontColour);

Status status;
RectF Size;
RectF BoundingBox;
std::vector<byte> vBitmap;

Graphics hGraphic(hDC);

textureData.clear();
textureData.resize(Width * Height * 4);

hBitmap = CreateBitmap(Width, Height, 4, 8, &textureData[0]);
hOldBitmap = SelectObject(hDC, hBitmap);

SetBkMode(hDC, TRANSPARENT);

status = hGraphic.SetTextRenderingHint(TextRenderingHintAntiAlias);
status = hGraphic.SetSmoothingMode(SmoothingModeAntiAlias);

status = hGraphic.Clear(Color(0, 255, 255, 255));

status = hGraphic.MeasureString(sTextString, -1, &font, Pos, &Size);

switch(ts.fontAlign)
{
case 0:
status = hGraphic.TranslateTransform((Width / 2) - (Size.Width / 2), (Height / 2) - (Size.Height / 2), MatrixOrderAppend);
break;

case 1:
status = hGraphic.TranslateTransform(0, 0, MatrixOrderAppend);
break;

case 2:
status = hGraphic.TranslateTransform((Width / 2) - (Size.Width / 2), 0, MatrixOrderAppend);
break;

case 3:
status = hGraphic.TranslateTransform(Width - Size.Width, 0, MatrixOrderAppend);
break;

case 4:
status = hGraphic.TranslateTransform(0, (Height / 2) - (Size.Height / 2), MatrixOrderAppend);
break;

case 5:
status = hGraphic.TranslateTransform((Width / 2) - (Size.Width / 2), (Height / 2) - (Size.Height / 2), MatrixOrderAppend);
break;

case 6:
status = hGraphic.TranslateTransform(Width - Size.Width, (Height / 2) - (Size.Height / 2), MatrixOrderAppend);
break;

case 7:
status = hGraphic.TranslateTransform(0, (Height - Size.Height), MatrixOrderAppend);
break;

case 8:
status = hGraphic.TranslateTransform((Width / 2) - (Size.Width / 2), (Height - Size.Height), MatrixOrderAppend);
break;

case 9:
status = hGraphic.TranslateTransform(Width - Size.Width, (Height - Size.Height), MatrixOrderAppend);
break;

}

status = hGraphic.TranslateTransform(0, 0, MatrixOrderAppend);
status = hGraphic.ScaleTransform(1, 1, MatrixOrderAppend);

status = hGraphic.DrawString(sTextString, -1, &font, Pos, &brush);

long ret = GetBitmapBits(hBitmap, Width * Height * 4, (LPVOID) &textureData[0]);

SelectObject(hDC, hOldBitmap);

FlipX = false;
FlipY = true;

NeedsUpdate = true;

hGraphic.ReleaseHDC(hDC);

DeleteObject(&hGraphic);
DeleteObject(&hOldBitmap);
DeleteObject(&hBitmap);
DeleteObject(&fontFamily);
DeleteObject(&brush);
DeleteObject(&font);

return true;

}



Can anyone see anything I'm doing obviously wrong?

Share this post


Link to post
Share on other sites
Advertisement
I believe DeleteObject() takes HGDIOBJ as an argument, not a reference or pointer.

E.g.,

DeleteObject(hBitmap) vs. DeleteObject(&hBitmap)

Share this post


Link to post
Share on other sites
That was exactly it. I've updated the code to this:


DeleteObject(&hGraphic);
DeleteObject(hOldBitmap);
DeleteObject(hBitmap);
DeleteObject(&fontFamily);
DeleteObject(&brush);
DeleteObject(&font);



and it no longer leaks.

Thanks!

Share this post


Link to post
Share on other sites
No problem. It's the obvious ones that hurt the most. [wink]

BTW, if you select the old bitmap back into the dc before you release it, I don't think you have to delete it yourself. The dc will take care of it. (if I remember correctly)

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.

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!