绘制文本到IDirect3DSurface9

Draw a Text into a IDirect3DSurface9

本文关键字:IDirect3DSurface9 文本 绘制      更新时间:2023-10-16

我正在寻找一种方法来绘制文本到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。

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);
}

遗憾的是,截图。bmp只包含游戏截图,但没有附加文本。

我哪里做错了?

CreateCompatibleDC为您提供了一个与现有DC 兼容的新DC ,但它实际上不是同一个DC。当一个新的DC被创建时,它有一个默认的1x1位图被选中-你需要在它的位置选择你自己的位图,然后你可以渲染到你的内存位图(然后恢复旧的位图之后)。

此时,您的绘图都发生在这个默认的1x1位图上,然后在您删除DC时简单地扔掉。

为什么要在drawInformationToSurface函数中创建一个新的DC ?对我来说,它看起来像你应该直接绘制到surfaceDC的传递。

相关文章:
  • 没有找到相关文章