找出缺陷 c++代码

发布于 2025-01-05 22:06:09 字数 3501 浏览 0 评论 0原文

有人可以发现这个代码部分中的缺陷吗?我花了几个小时试图找出它出了什么问题。我基本上在一个基本的 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 );

}

Could someone please spot the flaw in this code section i have spent hours trying to figure out what's wrong with it.I am bassicaly reciving massive lag on a basic directx app could someone explain to me what's wrong and why this is causing it.

#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 );

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

江南月 2025-01-12 22:06:09

UpdateWindow 导致 WM_PAINT 再次触发。

另请查看:https://gamedev.stackexchange.com/questions/12901/wm-绘制和直接3d

UpdateWindow causes WM_PAINT to fire again.

Also take a look at: https://gamedev.stackexchange.com/questions/12901/wm-paint-and-direct3d

仅此而已 2025-01-12 22:06:09

也许是小问题,但有更好的方法来编写主游戏循环。您可以使用 PeekMessage 来避免休眠,而不是使用 while(GetMessage(...)) {...},如下所述:
http://www.mvps.org/directx/articles/writing_the_game_loop.htm

Small point, perhaps, but there is a better way to write the main game loop. Instead of using while(GetMessage(...)) {...}, you can use PeekMessage to avoid sleeping, as described here:
http://www.mvps.org/directx/articles/writing_the_game_loop.htm

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文