2013-11-27 56 views
1

我打算使用DirectX,所以我使用的是directxtutorial.com教程。當我運行我的遊戲時,它具有非常高的CPU使用率。它需要我的處理器的第一個核心的所有權力。我有賽揚e3400。我認爲這遠遠超過我需要無任何運行我的遊戲。非常高的CPU使用率directx 9

對不起,這堆的代碼。我不知道什麼是錯的。 這裏是我的代碼:

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

如果沒有明確的框架上限技術,directx應用程序將使用100%的處理器,儘可能多地渲染幀​​數。 – GSerg

+0

哦。然後我還有兩個問題:我怎樣才能看到我的fps,以及如何限制他們?謝謝。 – Jancis

+0

然後參見例如http://stackoverflow.com/q/5508922/11683。 – GSerg

回答

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