Yui的狗窝
Yui的狗窝
DirectX9的字体使用

一则使用ID3DXFont和D3D网格来进行字体绘制的例子。

//utility.h
#pragma once
#include<d3d9.h>
#include<d3dx9.h>
const D3DXCOLOR         WHITE = D3DCOLOR_XRGB(255, 255, 255);
const D3DXCOLOR         BLACK = D3DCOLOR_XRGB(0, 0, 0);
const D3DXCOLOR         YELLOW = D3DCOLOR_XRGB(255, 255, 0);
const D3DXCOLOR         BLUE = D3DCOLOR_XRGB(0, 0, 255);
IDirect3D9              *pd3d;
IDirect3DDevice9        *pd3dDevice;
D3DCAPS9                caps;
int                     iUseProcess;
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    case WM_KEYDOWN:
        if (wParam == VK_ESCAPE)
            DestroyWindow(hWnd);
        break;
    }
    return DefWindowProc(hWnd, message, wParam, lParam);
}
bool InitD3d(HINSTANCE hInstance, int cmdShow)
{
    WNDCLASS wc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hInstance = hInstance;
    wc.lpfnWndProc = WndProc;
    wc.lpszClassName = L"fontx";
    wc.lpszMenuName = NULL;
    wc.style = CS_HREDRAW | CS_VREDRAW;
    RegisterClass(&wc);
    HWND hWnd = CreateWindow(L"fontx", L"font", NULL, 200, 200, 800, 600, NULL, NULL, hInstance, 0);
    ShowWindow(hWnd, cmdShow);
    UpdateWindow(hWnd);
    pd3d = Direct3DCreate9(D3D_SDK_VERSION);
    pd3d->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &caps);
    if (caps.Caps&D3DDEVCAPS_HWTRANSFORMANDLIGHT)
        iUseProcess = D3DCREATE_HARDWARE_VERTEXPROCESSING;
    else
        iUseProcess = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
    D3DPRESENT_PARAMETERS d3dpp;
    d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
    d3dpp.BackBufferCount = 1;
    d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
    d3dpp.BackBufferHeight = 600;
    d3dpp.BackBufferWidth = 800;
    d3dpp.EnableAutoDepthStencil = true;
    d3dpp.Flags = 0;
    d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
    d3dpp.hDeviceWindow = hWnd;
    d3dpp.MultiSampleQuality = 0;
    d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
    d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.Windowed = true;
    if (pd3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, iUseProcess, &d3dpp, &pd3dDevice))
        return true;
    return false;
}
D3DMATERIAL9 InitMtrl(D3DXCOLOR ambient, D3DXCOLOR specular, D3DXCOLOR diffuse, D3DXCOLOR emissive, float power)
{
    D3DMATERIAL9 tem;
    tem.Ambient = ambient;
    tem.Specular = specular;
    tem.Diffuse = diffuse;
    tem.Emissive = emissive;
    tem.Power = power;
    return tem;
}
D3DLIGHT9 InitDirLight(D3DXCOLOR ambient, D3DXCOLOR specular, D3DXCOLOR diffuse, D3DXVECTOR3 dir)
{
    D3DLIGHT9 tem;
    ::ZeroMemory(&tem, sizeof(tem));
    tem.Type = D3DLIGHT_DIRECTIONAL;
    tem.Ambient = ambient;
    tem.Specular = specular;
    tem.Diffuse = diffuse;
    tem.Direction = dir;
    return tem;
}
</code>
<code lang="cpp">
//render.h
#pragma once
#include"utility.h"
#include<string>
ID3DXMesh       *text;
ID3DXFont       *d3dxFont;
float           fps = 0;
float           elapseTime;
int             count = 0;
char            fpsStr[64];
WCHAR           fpsWStr[64];
void Setup(float width, float height);
void Render(float timeDelta);
void Setup(float width, float height)
{
    HDC hdc = CreateCompatibleDC(0);
    HFONT hFont;
    HFONT hFontOld;
    LOGFONT lf;//填充LOGFONT字段,也可定义为D3DSXFONT_DESC类型
    ::ZeroMemory(&lf, sizeof(lf));
    lf.lfHeight = 25;
    lf.lfWidth = 12;
    lf.lfEscapement = 0;
    lf.lfOrientation = 0;
    lf.lfWeight = 500;//文字粗细
    lf.lfItalic = false;
    lf.lfUnderline = false;
    lf.lfStrikeOut = false;
    lf.lfCharSet = DEFAULT_CHARSET;
    lf.lfOutPrecision = 0;
    lf.lfClipPrecision = 0;
    lf.lfQuality = 0;
    lf.lfPitchAndFamily = 0;
    swprintf(lf.lfFaceName, L"%S", "Times new Roman");//字体设置
//  strcpy(lf.lfFaceName, "Times new Roman");
    hFont = CreateFontIndirect(&lf);
    hFontOld = (HFONT)SelectObject(hdc, hFont);
    D3DXCreateText(pd3dDevice, hdc, L"Direct3D", 0.001f, 0.4f, &text, 0, 0);//获取指向ID3DXFont的指针
    D3DXCreateFontIndirect(pd3dDevice, (D3DXFONT_DESCW*)&lf, &d3dxFont);//获取指向ID3DXFont的指针
    SelectObject(hdc, hFontOld);
    DeleteObject(hFont);
    DeleteDC(hdc);
    D3DLIGHT9 dir = InitDirLight(WHITE*0.6f, WHITE*0.4f, WHITE, D3DXVECTOR3(0.0f, -0.5f, 1.0f));
    pd3dDevice->SetLight(0, &dir);
    pd3dDevice->LightEnable(0, true);
    pd3dDevice->SetRenderState(D3DRS_NORMALIZENORMALS, true);
    pd3dDevice->SetRenderState(D3DRS_SPECULARENABLE, true);
    D3DXMATRIX proj;
    D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI / 0.4f, width / height, 1.0f, 1000.f);
    pd3dDevice->SetTransform(D3DTS_PROJECTION, &proj);
    D3DXVECTOR3 pos(0.0f, 1.5f, -3.3f);
    D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
    D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
    D3DXMATRIX V;
    D3DXMatrixLookAtLH(&V,&pos,&target,&up);
    pd3dDevice->SetTransform(D3DTS_VIEW, &V);
}
void Render(float timeDelta)
{
    if (pd3dDevice)
    {
        elapseTime += timeDelta;
        count++;
        if (elapseTime >= 1.f)
        {
            fps = (float)count / elapseTime;
            count = 0;
            elapseTime = 0;
            sprintf(fpsStr, "%f", fps);
        }
        RECT rect{ 0,0,800,600 };
        static float y = 0.f;
        D3DXMATRIX yRot, ry;
        D3DXMatrixRotationY(&yRot, y);
        if (y >= 6.28f)
            y = 0;
        y += 0.01f;
        D3DXMatrixTranslation(&ry, -1.6f, 0.0f, 0.0f);
        ry = ry * yRot;
        pd3dDevice->SetTransform(D3DTS_WORLD, &ry);
        pd3dDevice->BeginScene();
        pd3dDevice->Clear(0, 0, D3DCLEAR_ZBUFFER | D3DCLEAR_TARGET, 0xff000000, 1.f, 0);
        swprintf(fpsWStr, L"%S", fpsStr);
        LPCWSTR tem = fpsWStr;
        d3dxFont->DrawTextW(0, tem, -1, &rect, DT_TOP | DT_LEFT, 0xffffffffff);
        D3DMATERIAL9 mtrl = InitMtrl(WHITE, WHITE*0.4f, WHITE*0.6f, BLACK, 5.f);
        pd3dDevice->SetMaterial(&mtrl);
        text->DrawSubset(0);
        pd3dDevice->EndScene();
        pd3dDevice->Present(0, 0, 0, 0);
    }
}
//render.cpp
#include"render.h"
#include<time.h>
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE prevInstance, LPWSTR cmdLine, int cmdShow)
{
    InitD3d(hInstance, cmdShow);
    Setup(800, 600);
    MSG msg{ 0 };
    DWORD cur, prev;
    prev = timeGetTime();
    while (msg.message != WM_QUIT)
    {
        cur = (float)timeGetTime();
        Render((cur - prev) * 0.001f);
        prev = cur;
        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }
    return 0;
}
}
没有标签
首页      读书笔记      directx龙书的学习笔记      DirectX9的字体使用
https://secure.gravatar.com/avatar/d0fe7f1b17d5e9122db921e3a8cc327f?s=256&d=mm&r=g

Suzumiya, Yui

文章作者

发表评论

textsms
account_circle
email

Yui的狗窝

DirectX9的字体使用
一则使用ID3DXFont和D3D网格来进行字体绘制的例子。 //utility.h #pragma once #include<d3d9.h> #include<d3dx9.h> const D3DXCOLOR WHITE = D3DCOLOR_XRGB(255, 25…
扫描二维码继续阅读
2018-05-02


没有激活的小工具