Drawing text with DirectDraw
-
I'm developing an animated game. It's a Win32 app, written in C++. For various reasons, I'm porting it to use DirectDraw interfaces. I can't find a simple way to draw text. I saw examples of people blitting images of text, for example to draw "Hey" they draw image of 'H' then image of 'e'... I might draw with Windows GDI but that AFAIK this is drawing to 'windows'/'controls'/HWND , which will make the code some crazy mess. Where's the easy way ?
-
I'm developing an animated game. It's a Win32 app, written in C++. For various reasons, I'm porting it to use DirectDraw interfaces. I can't find a simple way to draw text. I saw examples of people blitting images of text, for example to draw "Hey" they draw image of 'H' then image of 'e'... I might draw with Windows GDI but that AFAIK this is drawing to 'windows'/'controls'/HWND , which will make the code some crazy mess. Where's the easy way ?
I believe you are either going to have to use letter-bitmaps as you mention above, or resort to using GUI to draw on the DirectDraw surface. I did this a long time ago, and I am not even sure if recent versions of DirectX still support the ability to have GDI/GDI+ draw on a DD surface... Creating a function that takes a source bitmap (of letters/numbers/symbols), a string, and a origin is not really that hard, and may work faster than you think. It might be faster than going back and forth between DX/GDI in the long run... Peace!
-=- James
Please rate this message - let me know if I helped or not! * * *
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
See DeleteFXPFiles -
I believe you are either going to have to use letter-bitmaps as you mention above, or resort to using GUI to draw on the DirectDraw surface. I did this a long time ago, and I am not even sure if recent versions of DirectX still support the ability to have GDI/GDI+ draw on a DD surface... Creating a function that takes a source bitmap (of letters/numbers/symbols), a string, and a origin is not really that hard, and may work faster than you think. It might be faster than going back and forth between DX/GDI in the long run... Peace!
-=- James
Please rate this message - let me know if I helped or not! * * *
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
See DeleteFXPFiles -
I managed to mix the
DirectDraw
andWindows GDI
like this:HDC hdc = NULL; HRESULT result = m\_lpDDSBack->GetDC(&hdc); //m\_lpDDSBack is my backbuffer surface wchar\_t text\[256\] = "thank god"; int x = 100; int y = 200; size\_t length = wcslen(text); CPTDDUtils::MyDrawText(hdc, x, y, text, length); m\_lpDDSBack->ReleaseDC(hdc);
void CPTDDUtils::MyDrawText(HDC hdc, int x, int y, LPCTSTR lpString, int stringSize)
{
/*HFONT*/ HGDIOBJ hFont, hOldFont;// Retrieve a handle to the variable stock font. hFont = GetStockObject(ANSI\_VAR\_FONT); // Select the variable stock font into the specified device context. if (hOldFont = SelectObject(hdc, hFont)) { TextOut(hdc, x, y,lpString , stringSize); // Restore the original font. SelectObject(hdc, hOldFont); }
}
I do this after I drew the bitmaps, and before I 'flip' . So this draws the text but in white rectangle. I don't want the white rectangle, I want this rectangle to be transparent. Also, I rather use some normal font. Also, if this code is 'messed up', please let me know. :)
-
I managed to mix the
DirectDraw
andWindows GDI
like this:HDC hdc = NULL; HRESULT result = m\_lpDDSBack->GetDC(&hdc); //m\_lpDDSBack is my backbuffer surface wchar\_t text\[256\] = "thank god"; int x = 100; int y = 200; size\_t length = wcslen(text); CPTDDUtils::MyDrawText(hdc, x, y, text, length); m\_lpDDSBack->ReleaseDC(hdc);
void CPTDDUtils::MyDrawText(HDC hdc, int x, int y, LPCTSTR lpString, int stringSize)
{
/*HFONT*/ HGDIOBJ hFont, hOldFont;// Retrieve a handle to the variable stock font. hFont = GetStockObject(ANSI\_VAR\_FONT); // Select the variable stock font into the specified device context. if (hOldFont = SelectObject(hdc, hFont)) { TextOut(hdc, x, y,lpString , stringSize); // Restore the original font. SelectObject(hdc, hOldFont); }
}
I do this after I drew the bitmaps, and before I 'flip' . So this draws the text but in white rectangle. I don't want the white rectangle, I want this rectangle to be transparent. Also, I rather use some normal font. Also, if this code is 'messed up', please let me know. :)
Look up the
SetBkMode(...)
function for setting up Transparency. Also look up theExtTextOut(...)
function, as I bet you will be moving to it soon! :) You can specify your own font to use by using theCreateFont(...)
andCreateFontIndirect(...)
functions. Better to create the font once and reuse it. If you want something like a multicolor font, you will need to draw the bitmaps for it yourself. Peace!-=- James
Please rate this message - let me know if I helped or not! * * *
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
See DeleteFXPFiles -
Look up the
SetBkMode(...)
function for setting up Transparency. Also look up theExtTextOut(...)
function, as I bet you will be moving to it soon! :) You can specify your own font to use by using theCreateFont(...)
andCreateFontIndirect(...)
functions. Better to create the font once and reuse it. If you want something like a multicolor font, you will need to draw the bitmaps for it yourself. Peace!-=- James
Please rate this message - let me know if I helped or not! * * *
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
See DeleteFXPFiles -
I managed to mix the
DirectDraw
andWindows GDI
like this:HDC hdc = NULL; HRESULT result = m\_lpDDSBack->GetDC(&hdc); //m\_lpDDSBack is my backbuffer surface wchar\_t text\[256\] = "thank god"; int x = 100; int y = 200; size\_t length = wcslen(text); CPTDDUtils::MyDrawText(hdc, x, y, text, length); m\_lpDDSBack->ReleaseDC(hdc);
void CPTDDUtils::MyDrawText(HDC hdc, int x, int y, LPCTSTR lpString, int stringSize)
{
/*HFONT*/ HGDIOBJ hFont, hOldFont;// Retrieve a handle to the variable stock font. hFont = GetStockObject(ANSI\_VAR\_FONT); // Select the variable stock font into the specified device context. if (hOldFont = SelectObject(hdc, hFont)) { TextOut(hdc, x, y,lpString , stringSize); // Restore the original font. SelectObject(hdc, hOldFont); }
}
I do this after I drew the bitmaps, and before I 'flip' . So this draws the text but in white rectangle. I don't want the white rectangle, I want this rectangle to be transparent. Also, I rather use some normal font. Also, if this code is 'messed up', please let me know. :)
Hanan888 wrote:
wchar_t wchar_t text[256] = "thank god";
Why the 256? Use this instead:
wchar_t text[] = "thank god";
Why waste 492 bytes?
Hanan888 wrote:
size_t length = wcslen(text);
If the above change is made this runtime call call be changed to a compile time calculation as follows:
size_t length = sizeof(text)/sizeof(text[0]) - 1;
Steve
-
Hanan888 wrote:
wchar_t wchar_t text[256] = "thank god";
Why the 256? Use this instead:
wchar_t text[] = "thank god";
Why waste 492 bytes?
Hanan888 wrote:
size_t length = wcslen(text);
If the above change is made this runtime call call be changed to a compile time calculation as follows:
size_t length = sizeof(text)/sizeof(text[0]) - 1;
Steve