C++ 如何使用stb_truetype在directx9中呈现文本?

C++ 如何使用stb_truetype在directx9中呈现文本?,c++,c,directx,directx-9,C++,C,Directx,Directx 9,如何在C/C++中使用D3D9使用stb_truetype库呈现文本?我在各种论坛/网站和图书馆文档中查找d3d9中的一些示例,但没有找到任何示例。一种方法是创建d3d9纹理,然后将渲染文本位图加载到其中。然后,可以像使用任何其他纹理一样使用生成的纹理 #define STB_TRUETYPE_IMPLEMENTATION #include "stb_truetype.h" IDirect3DTexture9 *LoadTextureFromText(const char *text) {

如何在C/C++中使用D3D9使用stb_truetype库呈现文本?我在各种论坛/网站和图书馆文档中查找d3d9中的一些示例,但没有找到任何示例。

一种方法是创建d3d9纹理,然后将渲染文本位图加载到其中。然后,可以像使用任何其他纹理一样使用生成的纹理

#define STB_TRUETYPE_IMPLEMENTATION 
#include "stb_truetype.h"
IDirect3DTexture9 *LoadTextureFromText(const char *text)
{
    IDirect3DTexture9 *d3dTexture = 0;

    /* load font file */
    long size;
    unsigned char* fontBuffer;

    FILE* fontFile = fopen("C:\\path\\to\\font.ttf", "rb");
    fseek(fontFile, 0, SEEK_END);
    size = ftell(fontFile); /* how long is the file ? */
    fseek(fontFile, 0, SEEK_SET); /* reset */

    fontBuffer = (unsigned char*)malloc(size);

    fread(fontBuffer, size, 1, fontFile);
    fclose(fontFile);

    /* prepare font */
    stbtt_fontinfo info;
    if (!stbtt_InitFont(&info, fontBuffer, 0))
    {
        printf("failed\n");
    }

    int b_w = 512; /* bitmap width */
    int b_h = 128; /* bitmap height */
    int l_h = 64; /* line height */

    /* create a bitmap for the phrase */
    unsigned char* bitmap = (unsigned char*)calloc(b_w * b_h, sizeof(unsigned char));

    /* calculate font scaling */
    float scale = stbtt_ScaleForPixelHeight(&info, l_h);

    int x = 0;

    int ascent, descent, lineGap;
    stbtt_GetFontVMetrics(&info, &ascent, &descent, &lineGap);

    ascent *= scale;
    descent *= scale;

    int i;
    for (i = 0; i < strlen(text); ++i)
    {
        /* get bounding box for character (may be offset to account for chars that dip above or below the line */
        int c_x1, c_y1, c_x2, c_y2;
        stbtt_GetCodepointBitmapBox(&info, text[i], scale, scale, &c_x1, &c_y1, &c_x2, &c_y2);

        /* compute y (different characters have different heights */
        int y = ascent + c_y1;

        /* render character (stride and offset is important here) */
        int byteOffset = x + (y  * b_w);
        stbtt_MakeCodepointBitmap(&info, bitmap + byteOffset, c_x2 - c_x1, c_y2 - c_y1, b_w, scale, scale, text[i]);

        /* how wide is this character */
        int ax;
        stbtt_GetCodepointHMetrics(&info, text[i], &ax, 0);
        x += ax * scale;

        /* add kerning */
        int kern;
        kern = stbtt_GetCodepointKernAdvance(&info, text[i], text[i + 1]);
        x += kern * scale;
    }

    //Create a D3D9 texture and load the generated image. The text bitmap is 1 channel.
    D3DXCreateTexture(direct3DDevice, b_w, b_h, 1, D3DUSAGE_DYNAMIC, D3DFMT_L8, D3DPOOL_DEFAULT, (LPDIRECT3DTEXTURE9*)&d3dTexture);
    D3DLOCKED_RECT rect;
    d3dTexture->LockRect(0, &rect, NULL, 0);
    memcpy(rect.pBits, bitmap, b_w * b_h);
    d3dTexture->UnlockRect(0);

    free(fontBuffer);
    free(bitmap);

    return d3dTexture;
}
#定义STB\u TRUETYPE\u实现
#包括“stb_truetype.h”
IDirect3DTexture9*LoadTextureFromText(常量字符*text)
{
IDirect3DTexture9*d3dTexture=0;
/*加载字体文件*/
长尺寸;
无符号字符*fontBuffer;
FILE*fontFile=fopen(“C:\\path\\to\\font.ttf”,“rb”);
fseek(fontFile,0,SEEK_END);
size=ftell(fontFile);/*文件有多长*/
fseek(fontFile,0,SEEK_SET);/*重置*/
fontBuffer=(无符号字符*)malloc(大小);
fread(fontBuffer,size,1,fontFile);
fclose(fontFile);
/*准备字体*/
stbtt_fontinfo信息;
如果(!stbtt_InitFont(&info,fontBuffer,0))
{
printf(“失败的\n”);
}
int b_w=512;/*位图宽度*/
int b_h=128;/*位图高度*/
int l_h=64;/*线高*/
/*为短语创建位图*/
无符号字符*位图=(无符号字符*)calloc(b_w*b_h,sizeof(无符号字符));
/*计算字体比例*/
浮动比例=stbtt\U比例固定高度(&info,l\U h);
int x=0;
内部上升、下降、线间隙;
stbtt_GetFontVMetrics(信息、上升、下降和线间距);
上升*=刻度;
下降*=刻度;
int i;
对于(i=0;iLockRect(0,&rect,NULL,0);
memcpy(rect.pBits、位图、b_w*b_h);
d3dTexture->UnlockRect(0);
免费(fontBuffer);
免费(位图);
返回d3dTexture;
}

您的示例对我帮助很大,但我遇到了一个问题,我正在使用ID3DXSprite绘制纹理,文本正在绘制,但有一个黑框,为什么会发生这种情况?我做错了吗?代码(很抱歉发布链接,代码太大,无法在注释中发布):
paste.ofcode.org/bZ7dwEwqHGdj8KG2xLMSGK
你没有提供完整的代码,我不知道谁调用print_text或者精灵是如何创建的。我不是directx专家,但是如果你提供了所有的代码,我可以看看。D3DX9、D3DX10和D3DX11都不推荐使用。你应该改用directx 11,在这种情况下你可以使用
请注意,如果非常高质量和可伸缩的文本对你很重要,那么你应该考虑使用Direct3D 11。对“我如何用Direct3D 9做X”的所有问题的一般答案都是通用的。问题是:不要使用Direct3D 9,而是使用Direct3D 11。它是传统的,基本上没有理由使用它。请参阅和