I just created my first DirectX window, it is totaly blank. When ever I move a window over it or even just hover/move the mouse of the client area of the window, the processor usage goes up to 50%.
Is this normal, why is it so intensive?
Printable View
I just created my first DirectX window, it is totaly blank. When ever I move a window over it or even just hover/move the mouse of the client area of the window, the processor usage goes up to 50%.
Is this normal, why is it so intensive?
You're probably just in an infinite loop...so high cpu usage isn't a problem.
Well yeah, the message loop....Quote:
Originally Posted by MadCow257
Here is my code
Am I doing something wrong?Code:#include <D3d9.h>
#include <d3dx9.h>
#include <windows.h>
#define SAFE_RELEASE(x) if( x ) { (x)->Release(); (x) = NULL; }
LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam );
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow )
{
// Define the window
WNDCLASSEX wcex;
wcex.cbSize = sizeof( WNDCLASSEX );
wcex.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon( hInstance, IDI_APPLICATION );
wcex.hCursor = LoadCursor( NULL, IDC_ARROW );
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.lpszClassName = "NOOBN";
wcex.hIconSm = LoadIcon( hInstance, IDI_APPLICATION );
// Register the window
RegisterClassEx( &wcex );
// Create the window
HWND hWnd = CreateWindow( "NOOBN", "NOOBN", WS_OVERLAPPED | WS_MINIMIZEBOX | WS_SYSMENU,
CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, 0, 0, hInstance, 0 );
ShowWindow( hWnd, SW_SHOW );
UpdateWindow( hWnd );
// Initialize DirectX
LPDIRECT3D9 pD3D9 = Direct3DCreate9( D3D_SDK_VERSION );
if ( !pD3D9 )
{
MessageBox( 0, "Direct3DCreate9() - Failed", 0, 0 );
return 0;
}
// Fill out the presentation parameters
D3DPRESENT_PARAMETERS D3Dpp;
ZeroMemory( &D3Dpp, sizeof( D3Dpp ) );
D3Dpp.Windowed = TRUE;
D3Dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
// Create the device
LPDIRECT3DDEVICE9 pD3DDevice = NULL;
if ( FAILED( pD3D9->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING, &D3Dpp, &pD3DDevice ) ) )
{
MessageBox( 0, "CreateDevice() - Failed", 0, 0 );
SAFE_RELEASE( pD3D9 );
return 0;
}
// Main loop
MSG msg;
BOOL fMessage;
PeekMessage(&msg, NULL, 0U, 0U, PM_NOREMOVE);
while(msg.message != WM_QUIT)
{
fMessage = PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE);
if(fMessage)
{
//Process message
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
pD3DDevice->Clear( 0, 0, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 255, 0), 1.0f, 0 );
pD3DDevice->BeginScene();
// Render the frame here
pD3DDevice->EndScene();
pD3DDevice->Present( 0, 0, 0, 0 );
}
}
// Give back resources
SAFE_RELEASE( pD3DDevice );
SAFE_RELEASE( pD3D9 );
return 0;
}
LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
{
switch ( message )
{
case WM_DESTROY:
PostQuitMessage( 0 );
return 0;
case WM_PAINT:
// Render a frame then validate the client area
ValidateRect( hWnd, 0 );
return 0;
}
return DefWindowProc( hWnd, message, wParam, lParam );
}
Sleep()
Sleep after rendering?
Unless your program is a background type app, then you aren't doing anything wrong. If it is, then you might want to lower CPU priority, add a sleep, etc...
Your main loop is running as quickly as possible. For games, you want this loop as efficient and quick as possible in order to keep frame rates up. The faster it loops, the more frames you can render per second.
Since that loop's running as quickly as possible, it's taking as much of the resource pool as it can, so you're no doubt to see other things slow down, especially something like re-drawing a window that's moving around on top of your DX window.
If you don't need this speed, if your program isn't something that needs to be as quick as possible, then throwing in a sleep, or some other method of reducing how quickly the loop is running / how much of a demand for resources it has, that will free up resources to make other things (moving a window) run a little more quickly.
If you DO need this speed...stop draggin a window overtop your DX window :)
DX does use a lot of CPU but you want it that way.
The high CPU usage hasn't anything to do with DirectX, really. The reason is that you haven't got anything slowing your message loop down. This is exactly as it should be for games.
Normal windows applications use GetMessage instead of PeekMessage, which blocks (with 0% cpu usage) until a message is posted. PeekMessage returns immediately.
I see... I guess, there is quite a difference between normal Win32 programming and Game programming. You are right about that it needs to rerender as fast as possible, it makes a lot more scence when I look at it now.
Thanks guys.
That is quite possibly the best spelling mistake ever!Quote:
Originally Posted by mrafcho001
At first i took offence to your remark, i could of sworn i was the worst speller ever. But then i looked at it again and my god. :)