Thread: DirectX always this intensive?

  1. #1
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483

    DirectX always this intensive?

    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?
    My Website
    010000110010101100101011
    Add Color To Your Code!

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    You're probably just in an infinite loop...so high cpu usage isn't a problem.

  3. #3
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    Quote Originally Posted by MadCow257
    You're probably just in an infinite loop...so high cpu usage isn't a problem.
    Well yeah, the message loop....

    Here is my code
    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 );
    }
    Am I doing something wrong?
    My Website
    010000110010101100101011
    Add Color To Your Code!

  4. #4

    Join Date
    May 2005
    Posts
    1,042
    Sleep()
    I'm not immature, I'm refined in the opposite direction.

  5. #5
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    Sleep after rendering?
    My Website
    010000110010101100101011
    Add Color To Your Code!

  6. #6
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    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...

  7. #7
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    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
    Last edited by Epo; 01-03-2006 at 10:06 PM.
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  8. #8
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    DX does use a lot of CPU but you want it that way.

  9. #9
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    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.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  10. #10
    Registered User mrafcho001's Avatar
    Join Date
    Jan 2005
    Posts
    483
    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.
    My Website
    010000110010101100101011
    Add Color To Your Code!

  11. #11
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    Quote Originally Posted by mrafcho001
    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!
    Current Setup: Win 10 with Code::Blocks 17.12 (GNU GCC)

  12. #12
    Call me AirBronto
    Join Date
    Sep 2004
    Location
    Indianapolis, Indiana
    Posts
    195
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with my DirectX program
    By fighter92 in forum Game Programming
    Replies: 1
    Last Post: 01-23-2007, 06:28 PM
  2. Isometric Tile Engine using DirectX
    By Wraithan in forum Game Programming
    Replies: 3
    Last Post: 07-17-2006, 12:16 PM
  3. DirectSound header issues
    By dxfoo in forum C++ Programming
    Replies: 0
    Last Post: 03-19-2006, 07:16 PM
  4. DirectX - Starting Guide?
    By Zeusbwr in forum Game Programming
    Replies: 13
    Last Post: 11-25-2004, 12:49 AM
  5. DirectX 8 with Borland
    By Tommaso in forum Game Programming
    Replies: 11
    Last Post: 08-12-2003, 09:30 AM