CPU使用率很高DirectX 9 [英] Very high CPU usage directx 9

查看:105
本文介绍了CPU使用率很高DirectX 9的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将使用DirectX,因此我使用的是directxtutorial.com教程。当我运行游戏时,它的CPU使用率很高。它需要处理器第一核的所有功能。我有Celeron e3400。我认为这远远超过了我一无所有运行游戏所需要的。

I'm going to use DirectX, so I'm using directxtutorial.com tutorial. When I run my game, it has very high cpu usage. It takes all power of the first core of my processor. I have Celeron e3400. I think it's much more than I need to run my game without anything.

对不起,那堆代码。我不知道有什么问题。
这是我的代码:

Sorry for that bunch of code. I have no idea what could be wrong. Here is my code:

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


推荐答案

而不是在每个框架中渲染帧循环,您应该在没有以下消息要处理的情况下呈现框架。

Instead of rendering frame in each loop, you should render your frame when there is no message to handle as below.

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

这篇关于CPU使用率很高DirectX 9的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆