本文介绍了找出缺陷C ++代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以找出这段代码中的缺陷吗?我花了数小时试图找出它的问题所在。我大惊小怪地发现,在基本DirectX应用程序上存在大量滞后,有人可以向我解释什么是错误的,以及这是什么原因导致的。

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

}


推荐答案

小点,也许吧,但是有一种更好的方法编写主游戏循环。您可以使用PeekMessage避免睡眠,而不必使用while(GetMessage(...)){...},如下所述:

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

这篇关于找出缺陷C ++代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-04 23:40