gpt4 book ai didi

c++ - 发现缺陷 c++ 代码

转载 作者:行者123 更新时间:2023-11-30 04:29:35 24 4
gpt4 key购买 nike

有人能找出这个代码部分的缺陷吗?我花了几个小时试图弄清楚它有什么问题。我基本上在基本的 directx 应用程序上恢复了巨大的滞后,有人能向我解释什么是错误的以及为什么会导致它吗.

#include <Windows.h>
#include <d3d9.h>

// Function Prototypes
LRESULT CALLBACK MsgProc(HWND Wnd,UINT message,WPARAM wParam,LPARAM lParam);
INT CreateAndRegisterWindow(HINSTANCE hInst);
INT initilizeD3D(HWND hWnd);
VOID Render();
//Globals
HWND GlobalWindowHandle;
LPDIRECT3D9 lpD3D9;
LPDIRECT3DDEVICE9 lpD3DDevice9;
const wchar_t ClassName[] = L"Tutorial";

//Entry Point
INT WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrevInst,LPSTR lpCmdLine,INT CmdShow)
{
// Register the window class
WNDCLASSEX wc =
{
sizeof( WNDCLASSEX ), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle( NULL ), NULL, NULL, NULL, NULL,
L"D3D Tutorial", NULL
};
RegisterClassEx( &wc );

// Create the application's window
GlobalWindowHandle = CreateWindow( L"D3D Tutorial", L"D3D Tutorial 01: CreateDevice",
WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,
NULL, NULL, wc.hInstance, NULL );


initilizeD3D(GlobalWindowHandle);
ShowWindow(GlobalWindowHandle,SW_SHOW);
UpdateWindow(GlobalWindowHandle);
MSG msg;
while(GetMessage(&msg,NULL,NULL,NULL))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}



}
INT CreateAndRegisterWindow(HINSTANCE hInst)
{
WNDCLASSEX wcex;
ZeroMemory(&wcex, sizeof(wcex));
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.hbrBackground = (HBRUSH)COLOR_WINDOW;
wcex.hCursor = LoadCursor(0,IDC_ARROW);
wcex.hInstance = hInst;
wcex.lpfnWndProc = (WNDPROC)MsgProc;
wcex.lpszClassName = ClassName;
if(FAILED(RegisterClassEx(&wcex)))
{
MessageBoxA(GetForegroundWindow(),"Failed to register class.","Error",MB_ICONERROR);
return EXIT_FAILURE;
}
GlobalWindowHandle = CreateWindow(ClassName,L"Tutorial",WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,300,350,NULL,NULL,0,NULL);
if(!GlobalWindowHandle)
MessageBoxA(GetForegroundWindow(),"Failed to create window.","Error",MB_ICONERROR);
return EXIT_SUCCESS;
}

LRESULT CALLBACK MsgProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
switch(msg)
{
case WM_PAINT:
UpdateWindow(hWnd);
Render();
return 0;
case WM_DESTROY:
PostQuitMessage(EXIT_SUCCESS);
return 0;

}
return DefWindowProc(hWnd,msg,wParam,lParam);
}

INT initilizeD3D(HWND hWnd)
{
if(NULL==(lpD3D9 = Direct3DCreate9(D3D_SDK_VERSION)))
MessageBoxA(GetForegroundWindow(),"Failed to create direct3d device","Error",MB_ICONERROR);
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
if(FAILED(lpD3D9->CreateDevice (D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&lpD3DDevice9)))
return EXIT_FAILURE;

return EXIT_SUCCESS;

}

VOID Render()
{
if( NULL == lpD3DDevice9 )
return;

// Clear the backbuffer to a blue color
lpD3DDevice9->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 0, 0, 255 ), 1.0f, 0 );

// Begin the scene
if( SUCCEEDED( lpD3DDevice9->BeginScene() ) )
{
// Rendering of scene objects can happen here

// End the scene
lpD3DDevice9->EndScene();
}

// Present the backbuffer contents to the display
lpD3DDevice9->Present( NULL, NULL, NULL, NULL );

}

最佳答案

UpdateWindow 导致 WM_PAINT 再次触发。

另请查看:https://gamedev.stackexchange.com/questions/12901/wm-paint-and-direct3d

关于c++ - 发现缺陷 c++ 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9281041/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com