|
本人刚刚开始学习DirectX,运行下面自己写的代码后弹出对话框:Can Not Create the DirectX3D Device!
但是我运行其他实例却能创建。不知道为何,求助。
以下代码:
//以下是 Game.h 文件。
#include <d3d9.h>
#include <windows.h>
class Game
{
public:
Game();
~Game();
bool InitInstance(HWND hWnd);
bool Update();
bool ShutDown();
private:
LPDIRECT3D9 m_pD3D;
LPDIRECT3DDEVICE9 m_pD3DDevice;
};
Game::Game()
{
m_pD3D = NULL;
m_pD3DDevice = NULL;
}
Game::~Game()
{
if(m_pD3D) m_pD3D->Release();
if(m_pD3DDevice) m_pD3DDevice->Release();
}
bool Game::InitInstance(HWND hWnd)
{
D3DPRESENT_PARAMETERS PresentParams;
m_pD3D = Direct3DCreate9(D3D_SDK_VERSION);
if (!m_pD3D)
{
MessageBox (NULL, TEXT("Can Not Create m_pD3D"),TEXT("Error"), MB_ICONERROR);
return 0;
}
ZeroMemory( & resentParams, sizeof(PresentParams) );
PresentParams.BackBufferHeight = 480;
PresentParams.BackBufferWidth = 640;
PresentParams.SwapEffect = D3DSWAPEFFECT_DISCARD;
PresentParams.Windowed = true;
PresentParams.BackBufferCount = 1;
PresentParams.hDeviceWindow = hWnd;
if(!(m_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &PresentParams, &m_pD3DDevice)))
{
MessageBox (NULL, TEXT("Can Not Create the DirectX3D Device!"),TEXT("Error"), MB_ICONERROR);
return 0;
}
return 0;
}
//以下是 Main.cpp 文件
#include "Game.h"
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
Game m_game;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int iCmdShow)
{
USHORT Width = 640;
USHORT Height = 480;
static TCHAR szAppName[] = TEXT("DEMO");
HWND hWnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hInstance = hInstance;
wndclass.lpszClassName = szAppName;
wndclass.lpszMenuName = NULL;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT("Can Not Register Windows Class."),szAppName, MB_ICONERROR);
return 0;
}
hWnd= CreateWindow( szAppName,
TEXT ("DEMO"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
Width,
Height,
NULL,
NULL,
hInstance,
NULL );
m_game.InitInstance(hWnd);
ShowWindow(hWnd, iCmdShow);
while (1)
{
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
{
if (msg.message==WM_QUIT) break;
TranslateMessage( &msg );
DispatchMessage ( &msg );
}
else
{
//
}
}
return 0;
}
LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam,LPARAM lParam)
{
switch(message)
{
case WM_CREATE:
break;
case WM_CLOSE:
PostQuitMessage(0);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_SIZE:
break;
default:
return DefWindowProc( hWnd, message, wParam, lParam);
}
return 0;
}
|
|