Anonymous
C ++ Direct3d 10, font-> DrawText () entfernt meine Welt
Post
by Anonymous » 23 Apr 2025, 11:15
Bei Verwendung von FONT-> DrawText () zum Zeichnen von Text auf dem Bildschirm verschwinden alle Scheitelpunkte.
Code: Select all
#pragma comment(lib, "D3D10.lib")
#pragma comment(lib, "d3dx10.lib")
#pragma comment (lib, "dinput8.lib")
#pragma comment (lib, "dxguid.lib")
#include
#include
#include
LPCTSTR WndClassName = L"Genesis";
HWND hwnd = NULL;
const int Width = 600;
const int Height = 600;
bool InitializeWindow(HINSTANCE hInstance, int ShowWnd, int width, int height, bool windowed);
HRESULT hr;
ID3D10Device* d3dDevice;
IDXGISwapChain* SwapChain;
ID3D10RenderTargetView* RenderTargetView;
ID3D10DepthStencilView* DepthStencilView;
ID3D10Effect* FX;
ID3D10InputLayout* VertexLayout;
ID3D10Buffer* VertexBuffer;
ID3D10EffectTechnique* Technique;
bool InitializeDirect3dApp(HINSTANCE hInstance);
bool InitScene();
void DrawScene();
bool ReleaseObjects();
int messageloop();
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg,WPARAM wParam, LPARAM lParam);
struct Vertex{ D3DXVECTOR3 pos; };
ID3DX10Font* Font;
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd){
if (!InitializeWindow(hInstance, nShowCmd, Width, Height, true))
{
MessageBox(0, L"Window Initialization - Failed", L"Error", MB_OK);
return 0;
}
if (!InitializeDirect3dApp(hInstance))
{
MessageBox(0, L"Direct3D Initialization - Failed", L"Error", MB_OK);
return 0;
}
if (!InitScene())
{
MessageBox(0, L"Scene Initialization - Failed", L"Error", MB_OK);
return 0;
}
messageloop();
if (!ReleaseObjects())
{
MessageBox(0, L"Object Releasing - Failed", L"Error", MB_OK);
return 0;
}
return 0;
}
bool InitializeWindow(HINSTANCE hInstance, int ShowWnd, int width, int height, bool windowed)
{
typedef struct _WNDCLASS {
UINT cbSize; UINT style; WNDPROC lpfnWndProc;
int cbClsExtra; int cbWndExtra; HANDLE hInstance;
HICON hIcon; HCURSOR hCursor; HBRUSH hbrBackground;
LPCTSTR lpszMenuName; LPCTSTR lpszClassName;
} WNDCLASS;
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = NULL;
wc.cbWndExtra = NULL;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 2);
wc.lpszMenuName = NULL;
wc.lpszClassName = WndClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassEx(&wc))
{
MessageBox(NULL, L"Error registering class",
L"Error", MB_OK | MB_ICONERROR);
return 1;
}
hwnd = CreateWindowEx(
NULL, WndClassName,
L"Window Title",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
width, height,
NULL,NULL,hInstance, NULL
);
if (!hwnd)
{
MessageBox(NULL, L"Error creating window",
L"Error", MB_OK | MB_ICONERROR);
return 1;
}
ShowWindow(hwnd, ShowWnd);
UpdateWindow(hwnd);
return true;
}
bool InitializeDirect3dApp(HINSTANCE hInstance)
{
UINT createDeviceFlags = 0;
D3D10_DRIVER_TYPE driverTypes[] = {D3D10_DRIVER_TYPE_HARDWARE, D3D10_DRIVER_TYPE_REFERENCE, };
UINT numDriverTypes = sizeof(driverTypes) / sizeof(driverTypes[0]);
DXGI_SWAP_CHAIN_DESC scd;
scd.BufferDesc.Width = Width;
scd.BufferDesc.Height = Height;
scd.BufferDesc.RefreshRate.Numerator = 60;
scd.BufferDesc.RefreshRate.Denominator = 1;
scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
scd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
scd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
//no multisampling
scd.SampleDesc.Count = 1;
scd.SampleDesc.Quality = 0;
scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
scd.BufferCount = 1;
scd.OutputWindow = hwnd;
scd.Windowed = true;
scd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
scd.Flags = 0;
D3D10CreateDeviceAndSwapChain(0, D3D10_DRIVER_TYPE_HARDWARE, 0, 0, D3D10_SDK_VERSION, &scd, &SwapChain, &d3dDevice);
ID3D10Texture2D* backBuffer;
SwapChain->GetBuffer(0, _uuidof(ID3D10Texture2D), reinterpret_cast(&backBuffer));
d3dDevice->CreateRenderTargetView(backBuffer, 0, &RenderTargetView);
backBuffer->Release();
d3dDevice->OMSetRenderTargets(1, &RenderTargetView, NULL);
// Setup the viewport
D3D10_VIEWPORT vp;
vp.Width = Width;
vp.Height = Height;
vp.MinDepth = 0.0f;
vp.MaxDepth = 1.0f;
vp.TopLeftX = 0;
vp.TopLeftY = 0;
d3dDevice->RSSetViewports(1, &vp);
return true;
}
bool InitScene()
{
Vertex vertices[] =
{
D3DXVECTOR3(0.0f, 0.5f, 0.5f),
D3DXVECTOR3(0.5f, -0.5f, 0.5f),
D3DXVECTOR3(-0.5f, -0.5f, 0.5f),
};
D3DX10_FONT_DESC fd;
fd.Height = 20;
fd.Width = 0;
fd.Weight = 0;
fd.MipLevels = 1;
fd.Italic = false;
fd.CharSet = OUT_DEFAULT_PRECIS;
fd.Quality = DEFAULT_QUALITY;
fd.PitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
wcscpy_s(fd.FaceName, L"Segoe UI");
D3DX10CreateFontIndirect(d3dDevice, &fd, &Font);
D3D10_BUFFER_DESC bd;
bd.Usage = D3D10_USAGE_DEFAULT;
bd.ByteWidth = sizeof(Vertex) * 3;
bd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
bd.CPUAccessFlags = 0;
bd.MiscFlags = 0;
D3D10_SUBRESOURCE_DATA InitData;
InitData.pSysMem = vertices;
d3dDevice->CreateBuffer(&bd, &InitData, &VertexBuffer);
UINT stride = sizeof(Vertex);
UINT offset = 0;
d3dDevice->IASetVertexBuffers(0, 1, &VertexBuffer, &stride, &offset);
D3D10_INPUT_ELEMENT_DESC layout[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 }, };
D3DX10CreateEffectFromFile(L"vertex.fx", NULL, NULL, "fx_4_0", D3D10_SHADER_ENABLE_STRICTNESS, 0,
d3dDevice, NULL, NULL, &FX, NULL, NULL);
Technique = FX->GetTechniqueByName("Tech");
D3D10_PASS_DESC PassDesc;
Technique->GetPassByIndex(0)->GetDesc(&PassDesc);
d3dDevice->CreateInputLayout(layout, 1, PassDesc.pIAInputSignature,
PassDesc.IAInputSignatureSize, &VertexLayout);
d3dDevice->IASetInputLayout(VertexLayout);
d3dDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
return true;
}
bool ReleaseObjects()
{
if (d3dDevice) d3dDevice->ClearState();
if (VertexBuffer) VertexBuffer->Release();
if (VertexLayout) VertexLayout->Release();
if (FX) FX->Release();
if (RenderTargetView) RenderTargetView->Release();
if (SwapChain) SwapChain->Release();
if (d3dDevice) d3dDevice->Release();
return true;
}
void DrawScene()
{
//Draw Scene Here
D3DXCOLOR bgColor(0.0f, 0.0f, 0.0f, 1.0f);
d3dDevice->ClearRenderTargetView(RenderTargetView, bgColor);
D3D10_TECHNIQUE_DESC techDesc;
Technique->GetDesc(&techDesc);
for (UINT p = 0; p < techDesc.Passes; ++p)
{
Technique->GetPassByIndex(p)->Apply(0);
d3dDevice->Draw(3, 0);
}
D3DXCOLOR fontColor(1.0f, 1.0f, 1.0f, 1.0f);
RECT rectangle = { 10, 10, 0, 0 };
Font->DrawText(0, L"Where's my triangle?", -1, &rectangle, DT_NOCLIP, fontColor);
SwapChain->Present(0, 0);
}
int messageloop() {
MSG msg;
ZeroMemory(&msg, sizeof(MSG));
while (true)
{
BOOL PeekMessageL( LPMSG lpMsg, HWND hWnd, UINT wMsgFilterMin, UINT wMsgFilterMax, UINT wRemoveMsg );
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else {
DrawScene();
}
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam,LPARAM lParam)
{
switch (msg)
{
case WM_KEYDOWN:
if (wParam == VK_ESCAPE) {
DestroyWindow(hwnd);
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd,msg, wParam,lParam);
}
< /code>
Ich habe versucht, den Code kurz zu halten, aber es sind so viele Zeilen erforderlich, daher hoffe ich, dass diese Frage nicht geschlossen wird, da der Code zu lang ist.// Vertex Shader
float4 VS( float4 Pos : POSITION ) : SV_POSITION
{
return Pos; //No calculations done here, just returns the position of the vertices
}
// Pixel Shader
float4 PS( float4 Pos : SV_POSITION ) : SV_Target
{
return float4( 0.0f, 1.0f, 0.0f, 1.0f );
}
technique10 Tech
{
pass P0
{
SetVertexShader( CompileShader( vs_4_0, VS() ) );
SetPixelShader( CompileShader( ps_4_0, PS() ) );
}
Dieser Code macht ein grünes Dreieck mit einem Text.>
1745399720
Anonymous
Bei Verwendung von FONT-> DrawText () zum Zeichnen von Text auf dem Bildschirm verschwinden alle Scheitelpunkte.[code]#pragma comment(lib, "D3D10.lib") #pragma comment(lib, "d3dx10.lib") #pragma comment (lib, "dinput8.lib") #pragma comment (lib, "dxguid.lib") #include #include #include LPCTSTR WndClassName = L"Genesis"; HWND hwnd = NULL; const int Width = 600; const int Height = 600; bool InitializeWindow(HINSTANCE hInstance, int ShowWnd, int width, int height, bool windowed); HRESULT hr; ID3D10Device* d3dDevice; IDXGISwapChain* SwapChain; ID3D10RenderTargetView* RenderTargetView; ID3D10DepthStencilView* DepthStencilView; ID3D10Effect* FX; ID3D10InputLayout* VertexLayout; ID3D10Buffer* VertexBuffer; ID3D10EffectTechnique* Technique; bool InitializeDirect3dApp(HINSTANCE hInstance); bool InitScene(); void DrawScene(); bool ReleaseObjects(); int messageloop(); LRESULT CALLBACK WndProc(HWND hWnd, UINT msg,WPARAM wParam, LPARAM lParam); struct Vertex{ D3DXVECTOR3 pos; }; ID3DX10Font* Font; int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd){ if (!InitializeWindow(hInstance, nShowCmd, Width, Height, true)) { MessageBox(0, L"Window Initialization - Failed", L"Error", MB_OK); return 0; } if (!InitializeDirect3dApp(hInstance)) { MessageBox(0, L"Direct3D Initialization - Failed", L"Error", MB_OK); return 0; } if (!InitScene()) { MessageBox(0, L"Scene Initialization - Failed", L"Error", MB_OK); return 0; } messageloop(); if (!ReleaseObjects()) { MessageBox(0, L"Object Releasing - Failed", L"Error", MB_OK); return 0; } return 0; } bool InitializeWindow(HINSTANCE hInstance, int ShowWnd, int width, int height, bool windowed) { typedef struct _WNDCLASS { UINT cbSize; UINT style; WNDPROC lpfnWndProc; int cbClsExtra; int cbWndExtra; HANDLE hInstance; HICON hIcon; HCURSOR hCursor; HBRUSH hbrBackground; LPCTSTR lpszMenuName; LPCTSTR lpszClassName; } WNDCLASS; WNDCLASSEX wc; wc.cbSize = sizeof(WNDCLASSEX); wc.style = CS_HREDRAW | CS_VREDRAW; wc.lpfnWndProc = WndProc; wc.cbClsExtra = NULL; wc.cbWndExtra = NULL; wc.hInstance = hInstance; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 2); wc.lpszMenuName = NULL; wc.lpszClassName = WndClassName; wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION); if (!RegisterClassEx(&wc)) { MessageBox(NULL, L"Error registering class", L"Error", MB_OK | MB_ICONERROR); return 1; } hwnd = CreateWindowEx( NULL, WndClassName, L"Window Title", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, width, height, NULL,NULL,hInstance, NULL ); if (!hwnd) { MessageBox(NULL, L"Error creating window", L"Error", MB_OK | MB_ICONERROR); return 1; } ShowWindow(hwnd, ShowWnd); UpdateWindow(hwnd); return true; } bool InitializeDirect3dApp(HINSTANCE hInstance) { UINT createDeviceFlags = 0; D3D10_DRIVER_TYPE driverTypes[] = {D3D10_DRIVER_TYPE_HARDWARE, D3D10_DRIVER_TYPE_REFERENCE, }; UINT numDriverTypes = sizeof(driverTypes) / sizeof(driverTypes[0]); DXGI_SWAP_CHAIN_DESC scd; scd.BufferDesc.Width = Width; scd.BufferDesc.Height = Height; scd.BufferDesc.RefreshRate.Numerator = 60; scd.BufferDesc.RefreshRate.Denominator = 1; scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; scd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED; scd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED; //no multisampling scd.SampleDesc.Count = 1; scd.SampleDesc.Quality = 0; scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; scd.BufferCount = 1; scd.OutputWindow = hwnd; scd.Windowed = true; scd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD; scd.Flags = 0; D3D10CreateDeviceAndSwapChain(0, D3D10_DRIVER_TYPE_HARDWARE, 0, 0, D3D10_SDK_VERSION, &scd, &SwapChain, &d3dDevice); ID3D10Texture2D* backBuffer; SwapChain->GetBuffer(0, _uuidof(ID3D10Texture2D), reinterpret_cast(&backBuffer)); d3dDevice->CreateRenderTargetView(backBuffer, 0, &RenderTargetView); backBuffer->Release(); d3dDevice->OMSetRenderTargets(1, &RenderTargetView, NULL); // Setup the viewport D3D10_VIEWPORT vp; vp.Width = Width; vp.Height = Height; vp.MinDepth = 0.0f; vp.MaxDepth = 1.0f; vp.TopLeftX = 0; vp.TopLeftY = 0; d3dDevice->RSSetViewports(1, &vp); return true; } bool InitScene() { Vertex vertices[] = { D3DXVECTOR3(0.0f, 0.5f, 0.5f), D3DXVECTOR3(0.5f, -0.5f, 0.5f), D3DXVECTOR3(-0.5f, -0.5f, 0.5f), }; D3DX10_FONT_DESC fd; fd.Height = 20; fd.Width = 0; fd.Weight = 0; fd.MipLevels = 1; fd.Italic = false; fd.CharSet = OUT_DEFAULT_PRECIS; fd.Quality = DEFAULT_QUALITY; fd.PitchAndFamily = DEFAULT_PITCH | FF_DONTCARE; wcscpy_s(fd.FaceName, L"Segoe UI"); D3DX10CreateFontIndirect(d3dDevice, &fd, &Font); D3D10_BUFFER_DESC bd; bd.Usage = D3D10_USAGE_DEFAULT; bd.ByteWidth = sizeof(Vertex) * 3; bd.BindFlags = D3D10_BIND_VERTEX_BUFFER; bd.CPUAccessFlags = 0; bd.MiscFlags = 0; D3D10_SUBRESOURCE_DATA InitData; InitData.pSysMem = vertices; d3dDevice->CreateBuffer(&bd, &InitData, &VertexBuffer); UINT stride = sizeof(Vertex); UINT offset = 0; d3dDevice->IASetVertexBuffers(0, 1, &VertexBuffer, &stride, &offset); D3D10_INPUT_ELEMENT_DESC layout[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0 }, }; D3DX10CreateEffectFromFile(L"vertex.fx", NULL, NULL, "fx_4_0", D3D10_SHADER_ENABLE_STRICTNESS, 0, d3dDevice, NULL, NULL, &FX, NULL, NULL); Technique = FX->GetTechniqueByName("Tech"); D3D10_PASS_DESC PassDesc; Technique->GetPassByIndex(0)->GetDesc(&PassDesc); d3dDevice->CreateInputLayout(layout, 1, PassDesc.pIAInputSignature, PassDesc.IAInputSignatureSize, &VertexLayout); d3dDevice->IASetInputLayout(VertexLayout); d3dDevice->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST); return true; } bool ReleaseObjects() { if (d3dDevice) d3dDevice->ClearState(); if (VertexBuffer) VertexBuffer->Release(); if (VertexLayout) VertexLayout->Release(); if (FX) FX->Release(); if (RenderTargetView) RenderTargetView->Release(); if (SwapChain) SwapChain->Release(); if (d3dDevice) d3dDevice->Release(); return true; } void DrawScene() { //Draw Scene Here D3DXCOLOR bgColor(0.0f, 0.0f, 0.0f, 1.0f); d3dDevice->ClearRenderTargetView(RenderTargetView, bgColor); D3D10_TECHNIQUE_DESC techDesc; Technique->GetDesc(&techDesc); for (UINT p = 0; p < techDesc.Passes; ++p) { Technique->GetPassByIndex(p)->Apply(0); d3dDevice->Draw(3, 0); } D3DXCOLOR fontColor(1.0f, 1.0f, 1.0f, 1.0f); RECT rectangle = { 10, 10, 0, 0 }; Font->DrawText(0, L"Where's my triangle?", -1, &rectangle, DT_NOCLIP, fontColor); SwapChain->Present(0, 0); } int messageloop() { MSG msg; ZeroMemory(&msg, sizeof(MSG)); while (true) { BOOL PeekMessageL( LPMSG lpMsg, HWND hWnd, UINT wMsgFilterMin, UINT wMsgFilterMax, UINT wRemoveMsg ); if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { if (msg.message == WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } else { DrawScene(); } } return msg.wParam; } LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam,LPARAM lParam) { switch (msg) { case WM_KEYDOWN: if (wParam == VK_ESCAPE) { DestroyWindow(hwnd); } return 0; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hwnd,msg, wParam,lParam); } < /code> Ich habe versucht, den Code kurz zu halten, aber es sind so viele Zeilen erforderlich, daher hoffe ich, dass diese Frage nicht geschlossen wird, da der Code zu lang ist.// Vertex Shader float4 VS( float4 Pos : POSITION ) : SV_POSITION { return Pos; //No calculations done here, just returns the position of the vertices } // Pixel Shader float4 PS( float4 Pos : SV_POSITION ) : SV_Target { return float4( 0.0f, 1.0f, 0.0f, 1.0f ); } technique10 Tech { pass P0 { SetVertexShader( CompileShader( vs_4_0, VS() ) ); SetPixelShader( CompileShader( ps_4_0, PS() ) ); } [/code] Dieser Code macht ein grünes Dreieck mit einem Text.>