我正在寻找一种将文本绘制到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)的辅助方法,该方法应在将当前时间保存到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仅包含游戏的捕获,但其中没有其他文本。

我哪里做错了?

最佳答案

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

目前,您的绘制全部针对该默认1x1位图进行,然后在删除DC时就被丢弃。

为什么要在drawInformationToSurface函数中完全创建新的DC?在我看来,您应该直接参考传入的surfaceDC

关于c++ - 将文本绘制到IDirect3DSurface9中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31492523/

10-13 06:30