【问题标题】:Draw a Text into a IDirect3DSurface9将文本绘制到 IDirect3DSurface9
【发布时间】:2015-07-18 15:08:33
【问题描述】:

我正在寻找一种将文本绘制到 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 之前将其写入 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);
}

遗憾的是,截取的 ScreenShot.bmp 仅包含游戏的捕获,但其中没有其他文本。

我哪里做错了?

【问题讨论】:

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


    【解决方案1】:

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

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

    为什么要在 drawInformationToSurface 函数中创建一个新的 DC?在我看来,您应该直接使用 surfaceDC 传递的内容。

    【讨论】:

    • 哦,原来如此。我复制了一些我在网上找到的代码,用于从表面创建 bmp,但如果我想直接在表面上绘制,我将不得不使用原始 DC!非常感谢
    猜你喜欢
    • 1970-01-01
    • 2018-08-09
    • 2017-11-20
    • 2017-11-29
    • 1970-01-01
    • 2013-09-21
    • 1970-01-01
    • 1970-01-01
    • 2016-05-15
    相关资源
    最近更新 更多