Spot the flaw c++ code

强颜欢笑 提交于 2019-12-11 05:37:04

问题


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

}

回答1:


UpdateWindow causes WM_PAINT to fire again.

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




回答2:


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



来源:https://stackoverflow.com/questions/9281041/spot-the-flaw-c-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!