我现在一直在学习DirectX和Windows编程。但是,我遇到了DirectX函数DrawText
的问题。不知怎的,它不会在窗口中显示文字,也无法弄清楚原因。这是我的代码:
的main.cpp
//Include's
#include"winwindow.hpp"
#include<time.h>
#include<iostream>
//main
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow){
c_winwindow window(hInstance,nCmdShow);
bool b_init=false;
b_init=window.init();
while(b_init){
window.clear(D3DCOLOR_XRGB(0xEF,0xEF,0xEF));
window.drawtext(0,0,L"Halo",D3DCOLOR_COLORVALUE(0xFF,0,0,0xFF));
window.display();
b_init=window.update();
}
return 0;
}
winwindow.hpp
//Include's
#include"windows.h"
#include"d3d9.h"
#include"d3dx9.h"
//c_winwindow
class c_winwindow{
private:
HWND hwnd;
MSG msg;
HINSTANCE hInstance;
int nCmdShow;
bool b_init;
static LRESULT CALLBACK Messagehandler(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);
LPDIRECT3D9 lpd3d;
LPDIRECT3DDEVICE9 lpd3ddevice;
ID3DXFont *p_d3dfont;
bool initwin();
bool initdx();
public:
c_winwindow(const HINSTANCE &hInstance,int nCmdShow);
~c_winwindow();
bool init();
bool update();
void clear(D3DCOLOR clr);
void display();
void drawtext(unsigned short int,unsigned short int,LPCWSTR,D3DCOLOR);
};
winwindow.cpp
//Include's
#include"winwindow.hpp"
#include<iostream>
//c_winwindow memberfunction's
//konstruktor
c_winwindow::c_winwindow(const HINSTANCE &temphInstance,int tempnCmdShow){
hInstance=temphInstance;
nCmdShow=tempnCmdShow;
hwnd=NULL;
lpd3d=NULL;
lpd3ddevice=NULL;
b_init=false;
}
//destruktor
c_winwindow::~c_winwindow(){
PostQuitMessage(0);
hwnd=NULL;
if(lpd3d!=NULL){
lpd3d->Release();
lpd3d=NULL;
}
if(lpd3ddevice!=NULL){
lpd3ddevice->Release();
lpd3ddevice=NULL;
}
if(p_d3dfont!=NULL){
p_d3dfont->Release();
p_d3dfont=NULL;
}
}
//initwin
bool c_winwindow::initwin(){
if(!b_init){
WNDCLASS wc;
ZeroMemory(&wc,sizeof(wc));
wc.lpfnWndProc=Messagehandler;
wc.hInstance=hInstance;
wc.lpszClassName=L"wndc";
RegisterClass(&wc);
hwnd=CreateWindow(L"wndc",L"TestWindow",WS_SYSMENU,10,10,1200,800,NULL,NULL,hInstance,NULL);
if(hwnd==NULL){
std::cout << "failed to create Window!" << std::endl;
return false;
}else{
b_init=true;
ShowWindow(hwnd,nCmdShow);
return true;
}
}else{
return true;
}
}
//initdx
bool c_winwindow::initdx(){
lpd3d=Direct3DCreate9(D3D_SDK_VERSION);
if(lpd3d==NULL){
std::cout << "couldn't create direct3d Object" << std::endl;
return false;
}
D3DPRESENT_PARAMETERS params;
ZeroMemory(¶ms,sizeof(params));
params.SwapEffect=D3DSWAPEFFECT_FLIP;
params.hDeviceWindow=hwnd;
params.Windowed=true;
params.BackBufferWidth=1200;
params.BackBufferHeight=800;
params.BackBufferFormat= D3DFMT_A8R8G8B8;
if(FAILED(lpd3d->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hwnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,¶ms,&lpd3ddevice))){
std::cout << "couldn't create direct3d device" << std::endl;
return false;
}
if(D3D_OK!=D3DXCreateFont(lpd3ddevice,22,0,FW_NORMAL,1,false,DEFAULT_CHARSET,OUT_DEFAULT_PRECIS,ANTIALIASED_QUALITY,DEFAULT_PITCH|FF_DONTCARE,L"Arial",&p_d3dfont)){
std::cout << "couldn't create font" << std::endl;
return false;
}
return true;
}
//init
bool c_winwindow::init(){
return initwin()&&initdx();
}
//update
bool c_winwindow::update(){
if(b_init){
if(GetMessage(&msg,NULL,0,0)){
TranslateMessage(&msg);
DispatchMessage(&msg);
return true;
}else{
if(lpd3d!=NULL){
lpd3d->Release();
lpd3d=NULL;
}
if(lpd3ddevice!=NULL){
lpd3ddevice->Release();
lpd3ddevice=NULL;
}
hwnd=NULL;
b_init=false;
return false;
}
}else{
return false;
}
}
//clear
void c_winwindow::clear(D3DCOLOR clr){
lpd3ddevice->Clear(0,0,D3DCLEAR_TARGET,clr,0,0);
lpd3ddevice->BeginScene();
}
//display
void c_winwindow::display(){
lpd3ddevice->EndScene();
lpd3ddevice->Present(0,0,0,0);
}
void c_winwindow::drawtext(unsigned short int us_x,unsigned short int us_y,LPCWSTR text,D3DCOLOR textcolor){
RECT r;
SetRect(&r,us_x,us_y,0,0);
p_d3dfont->DrawText(NULL,text,-1,&r,DT_LEFT,textcolor);
}
//WndProc
LRESULT CALLBACK c_winwindow::Messagehandler(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam){
switch(msg){
case(WM_DESTROY):
PostQuitMessage(0);
return 0;
break;
case(WM_PAINT):
return 0;
break;
}
return DefWindowProc(hwnd,msg,wParam,lParam);
}
窗口显示为灰色,其中没有文字。谁能解释为什么没有文字?