非常高的CPU使用率directx 9

时间:2013-11-27 16:02:52

标签: directx cpu-usage directx-9

我将使用DirectX,所以我使用的是directxtutorial.com教程。当我运行我的游戏时,它具有非常高的CPU使用率。它占用了处理器第一个核心的所有功能。我有Celeron e3400。我认为这比我没有任何东西需要运行我的游戏要多得多。

对不起那堆代码。我不知道会出现什么问题。 这是我的代码:

#include <windows.h>
#include <windowsx.h>
#include <d3d9.h>

#pragma (lib, "d3d9.lib")

#define Width_X 1024
#define Height_Y 768


LPDIRECT3D9 d3d;
LPDIRECT3DDEVICE9 d3d_device;

void d3d_initialize(HWND hWnd);
void render_frame();
void d3d_clear();

void d3d_initialize(HWND hWnd)
{
d3d = Direct3DCreate9(32); // value for dx9 = 32, i think
D3DPRESENT_PARAMETERS d3dpp;

ZeroMemory(&d3dpp,sizeof(d3dpp));
d3dpp.Windowed = false;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = hWnd;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;    // set the back buffer format to 32-bit
d3dpp.BackBufferWidth = Width_X;    // set the width of the buffer
d3dpp.BackBufferHeight = Height_Y;

d3d->CreateDevice(D3DADAPTER_DEFAULT,
                  D3DDEVTYPE_HAL,
                  hWnd,
                  D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                  &d3dpp,
                  &d3d_device);
}

void render_frame()
{
d3d_device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(54, 222, 10), 1.0f, 0);
d3d_device->BeginScene();


d3d_device->EndScene();
d3d_device->Present(NULL, NULL, NULL, NULL);
}

void cleanD3D(void)
{
d3d_device->Release();    // close and release the 3D device
d3d->Release();    // close and release Direct3D
}


LRESULT CALLBACK WindowProc(HWND hWnd,
                     UINT message,
                     WPARAM wParam,
                     LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance,
               HINSTANCE hPrevInstance,
               LPSTR lpCmdLine,
               int nCmdShow)
{
HWND hWnd;
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));

wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
//wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpszClassName = "WindowClass1";

RegisterClassEx(&wc);

hWnd = CreateWindowExW(NULL,
                      L"WindowClass1",    // name of the window class
                      L"Test",   // title of the window
                      WS_EX_TOPMOST ,//| WS_POPUP, // window style //
                      0,    // x-position of the window
                      0,    // y-position of the window
                      Width_X,    // width of the window
                      Height_Y,    // height of the window
                      NULL,    // we have no parent window, NULL
                      NULL,    // we aren't using menus, NULL
                      hInstance,    // application handle
                      NULL);    // used with multiple windows, NULL

ShowWindow(hWnd, nCmdShow);
d3d_initialize(hWnd);
MSG msg;

    while(TRUE)
    {
        while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }

        if(msg.message == WM_QUIT)
            break;
        render_frame();
    }
        return msg.wParam;
        cleanD3D();
}

LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
    case WM_DESTROY:
        {
            PostQuitMessage(0);
            return 0;
        } break;
}

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

1 个答案:

答案 0 :(得分:2)

不应在每个循环中渲染帧,而是在没有要处理的消息时渲染帧,如下所示。

MSG msg;
while (msg.message != WM_QUIT)
{
    if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE) != 0)
    {
        TranslateMessage (&msg) ;
        DispatchMessage (&msg) ;
    }
    else // Render the scene if there is no message to handle
    {
        Render() ;
    }
}