连续捕获鼠标输入后,即。左右点击,切换到其他软件(如谷歌浏览器)时电脑会变得非常滞后,但当你结束框架几秒钟后会恢复正常,没有滞后。
在我创建 Graphics 对象之前,该框架工作正常。因此,我怀疑 Graphics 类有问题,但我仍然无法找出导致此性能问题的部分。
图形类
图形.h
#ifndef GRAPHICS_H
#define GRAPHICS_H
#include <d3d9.h>
class Graphics
{
public:
Graphics();
~Graphics();
void initialize(HWND hWnd, int backBufferWidth, int backBufferHeight);
void BeginFrame();
void EndFrame();
private:
IDirect3D9* direct3D9;
IDirect3DDevice9* d3dDevice;
};
#endif
图形.cpp
#include "Graphics.h"
Graphics::Graphics()
{
}
Graphics::~Graphics()
{
// Release the device when exiting.
d3dDevice->Release();
// Reset pointer to NULL.
d3dDevice = NULL;
}
void Graphics::initialize(HWND hWnd, int backBufferWidth, int backBufferHeight)
{
// Define Direct3D 9.
direct3D9 = Direct3DCreate9(D3D_SDK_VERSION);
// Define how the screen presents.
D3DPRESENT_PARAMETERS d3dPP;
ZeroMemory(&d3dPP, sizeof(d3dPP));
d3dPP.Windowed = true;
d3dPP.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dPP.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dPP.BackBufferCount = 1;
d3dPP.BackBufferWidth = backBufferWidth;
d3dPP.BackBufferHeight = backBufferHeight;
d3dPP.hDeviceWindow = hWnd;
// Create a Direct3D 9 device.
direct3D9->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dPP, &d3dDevice);
}
void Graphics::BeginFrame()
{
// Clear the back buffer.
d3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
// Begin the scene
d3dDevice->BeginScene();
}
void Graphics::EndFrame()
{
// End the scene
d3dDevice->EndScene();
// Present the back buffer to screen
d3dDevice->Present(NULL, NULL, NULL, NULL);
}
如果您想检查所有代码,这是我的整个项目:https ://www.dropbox.com/s/p4nm57fzqse12t7/Zero%20DirectX%20Framework.rar