使用SpriteFont类绘制文本时出现问题 [英] Problems when drawing text using the SpriteFont class

查看:145
本文介绍了使用SpriteFont类绘制文本时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SpriteFont/SpriteBatch类将文本呈现到我的游戏上,因为坦率地说,我已经厌倦了使用Direct2D和DirectWrite ...但是每次我使用SpriteFont绘制文本时,我都会在屏幕上书写文本,但是它是写在黑色背景上的...黑色背景遮住了我游戏的整个场景..有什么方法可以删除黑色背景而只保留文本吗?

I'm using the SpriteFont/SpriteBatch classes to render text onto my game because quite frankly, i am tired of using Direct2D and DirectWrite... But everytime I draw text using SpriteFont, I get the text written on the screen, but it is written on a black background... The black background blocks the entire scene of my game.. is there any way to remove the black background and only keep the text?

下面是我对SpriteFont的实现.

Down below is my implementation of SpriteFont..

void RenderText(int FPS)
{

std::unique_ptr<DirectX::SpriteFont> Sprite_Font(new DirectX::SpriteFont(device, L"myfile.spritefont"));

std::unique_ptr<DirectX::SpriteBatch> Sprite_Batch(new DirectX::SpriteBatch(DevContext));

Sprite_Batch->Begin();

Sprite_Font->DrawString(Sprite_Batch.get(), L"FPS: ", DirectX::XMFLOAT2(200,200));

Sprite_Batch->End();


}

在我看来,由于在函数ClearRenderTargetView()中指定了值,因此绘制了黑色背景.

It seems to me that the black background is drawn because of the values that I specified in the function ClearRenderTargetView().

float BackgroundColor[4] = { 0.0f, 0.0f, 0.0f, 0.0f };

DevContext->ClearRenderTargetView(RenderTarget, BackgroundColor); //This is where the black background gets drawn over my entire scene

每次我将BackgroundColor[4]更改为不同的值时,背景色也会相应变化.如何从游戏中删除黑色背景,仅包含文字?

Everytime i change BackgroundColor[4] to different values, the background color changes as well, respectably. How can I remove the black background from my game and only include the text?

这是我的全部代码.

#include <Windows.h>
#include <SpriteFont.h>
#include <SpriteBatch.h>
#include <d3dcompiler.h>
#include <SimpleMath.h>

#pragma comment (lib, "dinput8.lib")
#pragma comment (lib, "D3D11.lib")
#pragma comment (lib, "d3dcompiler.lib")

LRESULT CALLBACK WindowProcedure(HWND, unsigned int, WPARAM, LPARAM);
void Create_Window(HINSTANCE&);
void Initialize_Direct3D11(HINSTANCE);
void Initialize_Rendering_Pipeline();
void Initialize_Sprites();
void Render_Frame();
void Render_Text();
void Create_Vertex_Buffer_for_triangle();

HWND MainWindow;
IDXGISwapChain * SwapChain;
ID3D11Device * device;
ID3D11DeviceContext * DevContext;
ID3D11RenderTargetView * RenderTarget;
ID3D11Buffer * VertexBuffer;
ID3D10Blob * VertexShader;
ID3D10Blob * PixelShader;
ID3D11VertexShader * VS;
ID3D11PixelShader * PS;
ID3D11InputLayout * inputLayout;
std::unique_ptr<DirectX::SpriteFont> Sprite_Font;
std::unique_ptr<DirectX::SpriteBatch> Sprite_Batch;
DirectX::SimpleMath::Vector2 m_fontPos;
const wchar_t* output = L"Hello World";


struct Vertex_Buffer
{
 float Positions[3];


Vertex_Buffer(float x, float y, float z)
{
    Positions[0] = x;
    Positions[1] = y;
    Positions[2] = z;
};

};


int WINAPI WinMain(HINSTANCE CurrentInstance, HINSTANCE PrevInstance, LPSTR ignore, int WindowShow)
{
MSG message;
HRESULT status;

Create_Window(CurrentInstance);
Initialize_Direct3D11(CurrentInstance);
Initialize_Sprites();
Initialize_Rendering_Pipeline();
Create_Vertex_Buffer_for_triangle();


while (true)
{
    if (PeekMessage(&message, MainWindow, 0, 0, PM_REMOVE))
    {
        TranslateMessage(&message);
        DispatchMessage(&message);

    }

    else
    {   
        Render_Frame();
        Render_Text();
        SwapChain->Present(0, 0);
    }

}

}

void Initialize_Sprites()
{
Sprite_Font.reset(new DirectX::SpriteFont(device, L"myfile.spritefont"));
Sprite_Batch.reset(new DirectX::SpriteBatch(DevContext));

m_fontPos.x = 200;
m_fontPos.y = 200;

}

void Create_Window(HINSTANCE &CurrentInstance)
{
WNDCLASSEX windowclass;                                 


ZeroMemory(&windowclass, sizeof(WNDCLASSEX));
windowclass.cbSize = sizeof(WNDCLASSEX);                
windowclass.lpszClassName = L"Window Class";        
windowclass.hInstance = CurrentInstance;                
windowclass.lpfnWndProc = WindowProcedure;              
windowclass.hIcon = LoadIcon(NULL, IDI_WINLOGO);        
windowclass.hCursor = LoadCursor(NULL, IDC_ARROW);      


RegisterClassEx(&windowclass);


MainWindow = CreateWindowEx(
    0,                                                  
    L"Window Class",                                    
    L"The Empire of Anatoria",                          
    WS_OVERLAPPEDWINDOW,                                
    CW_USEDEFAULT,                                      
    CW_USEDEFAULT,                                      
    800,                                                
    600,                                                
    NULL,                                               
    NULL,                                               
    CurrentInstance,                                    
    NULL                                                
    );


ShowWindow(MainWindow, SW_SHOW);
}

void Render_Text()
{
DirectX::SimpleMath::Vector2 origin = Sprite_Font->MeasureString(output);

Sprite_Batch->Begin();
Sprite_Font->DrawString(Sprite_Batch.get(), output,
    m_fontPos, DirectX::Colors::White, 0.f, origin);
Sprite_Batch->End();

}

void Initialize_Direct3D11(HINSTANCE instance) 
{
DXGI_MODE_DESC BackBufferDesc;
DXGI_SWAP_CHAIN_DESC SwapChainDesc;



ZeroMemory(&BackBufferDesc, sizeof(DXGI_MODE_DESC));
BackBufferDesc.Width = 400;                         
BackBufferDesc.Height = 400;                         
BackBufferDesc.RefreshRate.Numerator = 60;          
BackBufferDesc.RefreshRate.Denominator = 1;         
BackBufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; 


ZeroMemory(&SwapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC));
SwapChainDesc.BufferDesc = BackBufferDesc;      
SwapChainDesc.BufferCount = 1;                          
SwapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
SwapChainDesc.SampleDesc.Count = 1;                     
SwapChainDesc.SampleDesc.Quality = 0;                   
SwapChainDesc.OutputWindow = MainWindow;            
SwapChainDesc.Windowed = TRUE;                          
SwapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;    



D3D11CreateDeviceAndSwapChain(NULL,                      
    D3D_DRIVER_TYPE_HARDWARE,   
    NULL,                        
    NULL,                       
    NULL,                   
    NULL,                        
    D3D11_SDK_VERSION,       
    &SwapChainDesc,          
    &SwapChain,             
    &device,                    
    NULL,                       
    &DevContext             
    );



ID3D11Texture2D * BackBuffer;
SwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (void**)&BackBuffer);
device->CreateRenderTargetView(BackBuffer, NULL, &RenderTarget);
DevContext->OMSetRenderTargets(
            1,                            
            &RenderTarget,                  
            NULL                            
            );
BackBuffer->Release();

DevContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);

}

void Initialize_Rendering_Pipeline()
{
D3DCompileFromFile(L"VertexShader.hlsl", 0, 0, "main", "vs_5_0", 0, 0, &VertexShader, 0);
D3DCompileFromFile(L"VertexShader.hlsl", 0, 0, "Pixel_Shader", "ps_5_0", 0, 0, &PixelShader, 0);

device->CreateVertexShader(VertexShader->GetBufferPointer(), VertexShader->GetBufferSize(), NULL, &VS);
device->CreatePixelShader(PixelShader->GetBufferPointer(), PixelShader->GetBufferSize(), NULL, &PS);

DevContext->VSSetShader(VS, 0, 0);
DevContext->PSSetShader(PS, 0, 0);


D3D11_VIEWPORT Raster;

ZeroMemory(&Raster, sizeof(D3D11_VIEWPORT));
Raster.MinDepth = 0.0f;
Raster.MaxDepth = 1.0f;
Raster.Width = 400;
Raster.Height = 400;
DevContext->RSSetViewports(1, &Raster);



D3D11_INPUT_ELEMENT_DESC InputLayout[1];

ZeroMemory(&InputLayout[0], sizeof(D3D11_INPUT_ELEMENT_DESC));
InputLayout[0].SemanticName = "POSITION";
InputLayout[0].Format = DXGI_FORMAT_R32G32B32_FLOAT;
InputLayout[0].InputSlot = 0;
InputLayout[0].AlignedByteOffset = 0;
InputLayout[0].InputSlotClass = D3D11_INPUT_PER_VERTEX_DATA;

device->CreateInputLayout(
    InputLayout,
    1,
    VertexShader->GetBufferPointer(),
    VertexShader->GetBufferSize(),
    &inputLayout
    );
DevContext->IASetInputLayout(inputLayout);
}

void Render_Frame()
{
float BackgroundColor[4] = {0.0f, 0.0f, 0.0f, 1.0f};

DevContext->ClearRenderTargetView(RenderTarget, BackgroundColor);

DevContext->Draw(3, 0);

}

void Create_Vertex_Buffer_for_triangle()
{
D3D11_BUFFER_DESC VertexBufferDesc;
D3D11_SUBRESOURCE_DATA VertexData;
UINT stride = sizeof(Vertex_Buffer);
UINT offset = 0;

ZeroMemory(&VertexBufferDesc, sizeof(D3D11_BUFFER_DESC));
VertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
VertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
VertexBufferDesc.CPUAccessFlags = 0;
VertexBufferDesc.ByteWidth = sizeof(Vertex_Buffer) * 3;


Vertex_Buffer VerticesData[] =
{
    Vertex_Buffer(0.0f, 0.5f, 0.5f),
    Vertex_Buffer(0.5f, -0.5f, 0.5f),
    Vertex_Buffer(-0.5f, -0.5f, 0.5f)
};


ZeroMemory(&VertexData, sizeof(D3D11_SUBRESOURCE_DATA));
VertexData.pSysMem = VerticesData;

device->CreateBuffer(
    &VertexBufferDesc,
    &VertexData,
    &VertexBuffer);

DevContext->IASetVertexBuffers(
    0,
    1,
    &VertexBuffer,
    &stride,
    &offset
    );

}

LRESULT CALLBACK WindowProcedure(HWND handle, unsigned int message, WPARAM ignore1, LPARAM ignore2)
{
switch (message)
{

case WM_CREATE:
    return 0;


case WM_CLOSE:
    DestroyWindow(handle);
    return 0;


default:
    return DefWindowProc(handle, message, ignore1, ignore2);
}


}

这是VertexShader.hlsl文件

Here is the VertexShader.hlsl file

float4 main( float4 pos : POSITION ) : SV_POSITION
{
return pos;
}

float4 Pixel_Shader() : SV_TARGET
{
return float4(1.0f, 0.0f, 0.0f, 1.0f);
}

推荐答案

您和我在SpriteFont上也遇到了同样的问题.在调用SpriteFont之后,我们忘记了重置VertexBuffer s和所有其他渲染状态.请参阅我的帖子和 Chuck Walbourn 中的解决方案: DirectX :: SpriteFont/SpriteBatch阻止绘制3D场景.

You and me have the same problem with SpriteFont. We forgot to reset the VertexBuffers and all other rendering states after the call to SpriteFont. See my post and the solution from Chuck Walbourn: DirectX::SpriteFont/SpriteBatch prevents 3D scene from drawing.

引用Chuck:

您可以在InitScene中为场景设置渲染状态,但是绘制其他任何东西都会改变状态,这正是SpriteBatch所做的.我记录了DirectX Toolkit中每个对象在Wiki上的呈现状态.如果您进行的绘制不止一个绘制,则需要设置绘制需要每一帧的所有状态,而不是假设它是永久设置的.

You set the render state up for your scene in InitScene, but drawing anything else changes the state which is exactly what SpriteBatch does. I document which render states each object in DirectX Toolkit manipulates on the wiki. You need to set all the state your Draw requires each frame, not assume it is set forever, if you do more than a single draw.

有关提供更多信息的其他链接,请参见我的问题.

See my question for additional links providing more information.

这篇关于使用SpriteFont类绘制文本时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆