将文本绘制到IDirect3DSurface9中

时间:2015-07-18 15:08:34

标签: c++ winapi directx direct3d directx-9

我正在寻找一种方法将Text绘制到IDirect3DSurface9实现类。我的目标是在屏幕截图中写入一些文字,就像截图时一样。

用于制作我的游戏截图的原始(工作)代码:

void CreateScreenShot(IDirect3DDevice9* device, int screenX, int screenY)
{
IDirect3DSurface9* frontbuf; //this is our pointer to the memory location containing our copy of the front buffer

// Creation of the surface where the screen shot will be copied to
device->CreateOffscreenPlainSurface(screenX, screenY, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &frontbuf, NULL);

// Copying of the Back Buffer to our surface
HRESULT hr = device->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &frontbuf);
if (hr != D3D_OK)
{
   frontbuf->Release();
   return;
}

// Aquiring of the Device Context of the surface to be able to draw into it
HDC surfaceDC;
if (frontbuf->GetDC(&surfaceDC) == D3D_OK)
{
   drawInformationToSurface(surfaceDC, screenX);
   frontbuf->ReleaseDC(surfaceDC);
}

// Saving the surface to a file. Creating the screenshot file
D3DXSaveSurfaceToFile("ScreenShot.bmp", D3DXIFF_BMP, frontbuf, NULL, NULL);
}

现在您可以看到我创建了一个名为drawInformationToSurface(HDC surfaceDC, int screenX)的辅助方法,该方法应该将当前时间写入Surface,然后才能保存到HDD中。

void drawInformationToSurface(HDC surfaceDC, int screenX)
{
// Creation of a new DC for drawing operations
HDC memDC = CreateCompatibleDC(surfaceDC);

// Aquiring of the current time String with my global Helper Method
const char* currentTimeStr = GetCurrentTimeStr();

// Preparing of the HDC
SetBkColor(memDC, 0xff000000);
SetBkMode(memDC, TRANSPARENT);
SetTextAlign(memDC, TA_TOP | TA_LEFT);
SetTextColor(memDC, GUI_FONT_COLOR_Y);

// Draw a the time to the surface
ExtTextOut(memDC, 0, 0, ETO_CLIPPED, NULL, currentTimeStr, strlen(currentTimeStr), NULL);

// Free resources for the mem DC
DeleteDC(memDC);
}

可悲的是,拍摄的ScreenShot.bmp只包含游戏的捕捉,但没有附加文字。

我哪里出错了?

1 个答案:

答案 0 :(得分:2)

CreateCompatibleDC为您提供了一个与现有DC兼容的新DC,但它实际上并不是同一个DC。当创建一个新的DC时,它会选择一个默认的1x1位图 - 您需要先选择自己的位图,然后才能渲染到内存位图(然后再恢复旧的位图)。

目前您的绘图全部发生在此默认的1x1位图上,然后在删除DC时被丢弃。

为什么要在drawInformationToSurface功能中创建新DC?对我来说,你应该直接看到传递给surfaceDC