我正在尝试学习一些 DirectX API,目前,我只有一个 WINAPI 窗口和一个简单的渲染函数,可以在整个屏幕上显示位图。我的 WINMAIN 函数:
LPDIRECT3D9 pD3D; // the Direct3D object
LPDIRECT3DDEVICE9 pd3dDevice; // the Direct3D device
// This is winmain, the main entry point for Windows applications
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
int nCmdShow) {
hInst = hInstance;
// Initialize the window
if (!initWindow(hInstance)) return false;
// called after creating the window
if (!initDirect3D()) return false;
// main message loop:
MSG msg;
ZeroMemory( &msg, sizeof( msg ) );
while( msg.message != WM_QUIT) {
if( PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE)) {
TranslateMessage ( &msg );
DispatchMessage ( &msg );
} else {
render();
}
}
// Release the device and the Direct3D object
if( pd3dDevice != NULL ) pd3dDevice->Release( );
if( pD3D != NULL ) pD3D->Release( );
return (int) msg.wParam;
}
这是我的渲染功能:
void render(void) {
IDirect3DSurface9* surface;
pd3dDevice->CreateOffscreenPlainSurface(640, // the width of the surface to create
480, // the height of the surface to create
D3DFMT_X8R8G8B8, // the surface format
D3DPOOL_DEFAULT, // the memory pool to use
&surface, // holds the resulting surface
NULL); // reserved
D3DXLoadSurfaceFromFile(surface, NULL, NULL, L"test.bmp", NULL, D3DX_DEFAULT, 0, NULL);
// This will hold the back buffer
IDirect3DSurface9* backbuffer = NULL;
// Check to make sure you have a valid Direct3D device
if( NULL == pd3dDevice ) return;// Clear the back buffer to a blue color
pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 0,0,255 ), 1.0f, 0 );
// Get the back buffer
pd3dDevice->GetBackBuffer( 0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer );
// Copy the offscreen surface to the back buffer
// Note the use of NULL values for the source and destination RECTs
// This ensures a copy of the entire surface to the back buffer
pd3dDevice->StretchRect(surface, NULL, backbuffer, NULL, D3DTEXF_NONE );
// Present the back buffer contents to the display
pd3dDevice->Present( NULL, NULL, NULL, NULL );
}
因此,主要问题是,当启用(未注释)渲染功能时,应用程序使用的内存会在一秒钟内达到 400 - 600 Mb。现在,如果我从 WinMain 禁用(注释)该行,内存将保留为 5 Mb,但处理器会发疯,应用程序会使用大约 50% 的内存。所以,看起来WinMain()
使用了大量的处理器和render()
大量的内存。为什么?我忘记了什么?
谢谢!