Scenario: Using Dark GDK for game development. It's drawing capability is limited to
bitmaps and the screen (simplified statement). Dark GDK is a C++ interface to DirectX
functionality, though I see more procedural code than OOP in their approach. So I'm
working on an OOP wrapper for its 2D functionality to enhance my ability to inherit.
I've created an Image class which has as some of its private data the following:
DWORD width; // visible width of the image
DWORD height; // visible height of the image
DWORD depth; // color depth in bits
DWORD size; // size of image in bytes
UINT pitch8; // number of bytes across in the image, regardless of width
UINT pitch32; // number of DWORDs across in the image, regardless of width
UINT8 bpp;
DWORD *bytes; // pointer to image data for DWORD-sized data
IDirect3DDevice9 *device;
LPDIRECT3DTEXTURE9 data; // pointer to image object
D3DLOCKED_RECT imgData; // locking rectangle
ID3DXFont *font; // pointer to font data
UINT cursorX; // cursor x position for printing
UINT cursorY; // cursor y position for printing
UINT printW; // print width
UINT printH; // print height
void * imgPtr; // void pointer to the pixel data
LPSTR pData;
I've been able to develop some fucntions for drawing directly to the image, which is
then displayed in conjunction with a sprite as needed. What I've found is that,
whereas I can use the GDK to write text to the screen and a bitmap, I need to provide
that capability directly to an image. The text may not need to be displayed immediately
but whenever the image is displayed it needs to be there.
After "borrowing" some code I've come up with the following for establishing the font.
Some of it is hardcoded but that's only for developmental purposes. It still returns S_OK.
bool Image::SetFont(int h, UINT w, UINT weight, bool italics, LPCTSTR pFacename)
{
if (font != NULL) font->Release();
HRESULT result =
D3DXCreateFont (device, h, w, weight, 0, false, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, DEFAULT_QUALITY,
DEFAULT_PITCH, pFacename, &font);
return (result == S_OK);
}
Below is the code I use to "try" to draw the text string to the image. In my
test environment the image starts out blank white and the color I'm passing
for the text is black. The code I was borrowing had a clear statement that I
used initially but decided to remove since it wasn't necessary. The code also
had pSurfaceLevel->Release(); statement, which I left in, but I'm not
certain where the locking is being done, if it was in the code at all.
void Image::Print(LPSTR text, DWORD color)
{
RECT rct;
IDirect3DSurface9 *pSurfaceLevel; // pointer to surface
IDirect3DSurface9 *pSurfaceSave; // pointer to current render target
device->GetRenderTarget(0, &pSurfaceSave);
data->GetSurfaceLevel(0, &pSurfaceLevel);
device->SetRenderTarget(0, pSurfaceLevel);
rct.left = cursorX;
rct.right = cursorX + printW;
rct.top = cursorY;
rct.bottom = cursorY + printH;
D3DCOLOR prtColor = color;
if (font == NULL) return;
font->DrawText(NULL, text, -1, &rct, DT_CENTER | DT_VCENTER, prtColor);
device->SetRenderTarget(0, pSurfaceSave);
pSurfaceLevel->Release();
}
The text doesn't appear on the image.
Could someone advise me if this code has any merit or if I'm totally off base?
Any advice regarding the proper approach?
Thanks,
Lilith