Incorrect Window made with D3D

This is a discussion on Incorrect Window made with D3D within the Game Programming forums, part of the General Programming Boards category; Hi, I'm just trying to make a simple window with Direct3D 9. I get no errors, but the window made ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    216

    Incorrect Window made with D3D

    Hi, I'm just trying to make a simple window with Direct3D 9. I get no errors, but the window made isn't a valid window. It's just a window that is always the same size, and any key exits (Not just escape) Here is the code without the resource and header files.

    Windows.cpp
    Code:
    // INCLUDES ///////////////////////////////////////////////
    
    #define WIN32_LEAN_AND_MEAN  
    
    #include "header.h"
    
    // GLOBAL VARIABLES ///////////////////////////////
    
    HWND g_hwnd                     = NULL; // save the window handle
    
    LPDIRECT3D9 g_pD3D              = NULL; // Direct3D 9 interface object pointer
    LPDIRECT3DDEVICE9 g_pd3dDevice  = NULL; // Direct3D 9 rendering component pointer
    
    
    const char g_szClassName[] = "myWindowClass";
    
    // Step 4: the Window Procedure
    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    	switch(msg)
    	{
            case WM_KEYDOWN:
            {
                 switch(wParam)
                 {
                               case VK_ESCAPE:
                                    PostQuitMessage(0);
                                    break;
                 }
            }
            
    		case WM_CLOSE:
    			DestroyWindow(hwnd);
    		break;
    		case WM_DESTROY:
    			PostQuitMessage(0);
    		break;
    		default:
    			return DefWindowProc(hwnd, msg, wParam, lParam);
    	}
    	return 0;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    	LPSTR lpCmdLine, int nCmdShow)
    {
    	WNDCLASSEX wc;
    	MSG Msg;
    
    	//Step 1: Registering the Window Class
    	wc.cbSize		 = sizeof(WNDCLASSEX);
    	wc.style		 = 0;
    	wc.lpfnWndProc	 = WndProc;
    	wc.cbClsExtra	 = 0;
    	wc.cbWndExtra	 = 0;
    	wc.hInstance	 = hInstance;
    	wc.hIcon		 = LoadIcon(hInstance, (LPCTSTR)IDI_DIRECTX_ICON);
    	wc.hCursor		 = LoadCursor(NULL, IDC_ARROW);
    	wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    	wc.lpszMenuName  = NULL;
    	wc.lpszClassName = g_szClassName;
    	wc.hIconSm		 = LoadIcon(hInstance, (LPCTSTR)IDI_DIRECTX_ICON);
    
    	if(!RegisterClassEx(&wc))
    	{
    		MessageBox(NULL, "Window Registration Failed!", "Error!",
    			MB_ICONEXCLAMATION | MB_OK);
    		return 0;
    	}
    
    	// Step 2: Creating the Window
    	g_hwnd = CreateWindowEx(
    		WS_EX_CLIENTEDGE,
    		g_szClassName,
    		WINDOW_TITLE,
    		WS_POPUP | WS_SYSMENU | WS_VISIBLE,
    		0, 0, WINDOW_WIDTH, WINDOW_HEIGHT,
    		NULL, NULL, hInstance, NULL);
    
    	if(g_hwnd == NULL)
    	{
    		MessageBox(NULL, "Window Creation Failed!", "Error!",
    			MB_ICONEXCLAMATION | MB_OK);
    		return 0;
    	}
    
    	ShowWindow(g_hwnd, nCmdShow);
    	UpdateWindow(g_hwnd);
    
        // perform all game console specific initialization
        init();
    
        // enter main event loop
        while(1)
    	{
    	        if (PeekMessage(&Msg,NULL,0,0,PM_REMOVE))
    		    { 
                   // test if this is a quit
                   if (Msg.message == WM_QUIT)
                   break;
    	
    		       // translate any accelerator keys
    		       TranslateMessage(&Msg);
    
    		       // send the message to the window proc
    		       DispatchMessage(&Msg);
    		     } // end if
        
                 // main game processing goes here
                 render();
    
    	} // end while
    
        // shutdown game and release all resources
        shutdown();
    
        // return to Windows like this
        return(Msg.wParam);
    
    } // end WinMain
    Directx.cpp
    Code:
    #include "header.h"
    
    void init()
    {
        g_pD3D = Direct3DCreate9( D3D_SDK_VERSION );
    
    	if( g_pD3D == NULL )
    	    return;
    
        D3DDISPLAYMODE d3ddm;
    
        if( FAILED( g_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm ) ) )
    	    return;
    
    	HRESULT hr;
    
    	if( FAILED( hr = g_pD3D->CheckDeviceFormat( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, 
    												d3ddm.Format, D3DUSAGE_DEPTHSTENCIL,
    												D3DRTYPE_SURFACE, D3DFMT_D16 ) ) )
    	{
    		if( hr == D3DERR_NOTAVAILABLE )
    			// POTENTIAL PROBLEM: We need at least a 16-bit z-buffer!
    			return;
    	}
    
    	//
    	// Do we support hardware vertex processing? if so, use it. 
    	// If not, downgrade to software.
    	//
    
    	D3DCAPS9 d3dCaps;
    
    	if( FAILED( g_pD3D->GetDeviceCaps( D3DADAPTER_DEFAULT, 
    		                               D3DDEVTYPE_HAL, &d3dCaps ) ) )
                   return;
    
    	DWORD dwBehaviorFlags = 0;
    
    	if( d3dCaps.VertexProcessingCaps != 0 )
    		dwBehaviorFlags |= D3DCREATE_HARDWARE_VERTEXPROCESSING;
    	else
    		dwBehaviorFlags |= D3DCREATE_SOFTWARE_VERTEXPROCESSING;
    
    	//
    	// Everything checks out - create a simple, windowed device.
    	//
    
    	D3DPRESENT_PARAMETERS d3dpp;
    	memset(&d3dpp, 0, sizeof(d3dpp));
    
        d3dpp.BackBufferFormat       = d3ddm.Format;
    	d3dpp.SwapEffect             = D3DSWAPEFFECT_DISCARD;
    	d3dpp.Windowed               = TRUE;
        d3dpp.EnableAutoDepthStencil = TRUE;
        d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
        d3dpp.PresentationInterval   = D3DPRESENT_INTERVAL_IMMEDIATE;
    
        if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_hwnd,
                                          dwBehaviorFlags, &d3dpp, &g_pd3dDevice ) ) )
                 return;
    }
    
    void shutdown()
    {
        if( g_pd3dDevice != NULL )
            g_pd3dDevice->Release();
    
        if( g_pD3D != NULL )
            g_pD3D->Release();
    }
    
    void render()
    {
        g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
                             D3DCOLOR_COLORVALUE(0.0f,0.0f,0.0f,1.0f), 1.0f, 0 );
    
        g_pd3dDevice->BeginScene();
    
    	// Render geometry here...
    
        g_pd3dDevice->EndScene();
    
        g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
    }
    Thanks for the help.

  2. #2
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    You need a break statement for "case WM_KEYDOWN":
    Code:
    switch(msg)
    {
        case WM_KEYDOWN:
        {
             switch(wParam)
             {
                           case VK_ESCAPE:
                                PostQuitMessage(0);
                                break;
             }
        }
        //here
    
        ....
    }
    It's just a window that is always the same size
    I'm not totally sure what you mean here; I'm able change the window's size by changing WINDOW_WIDTH and WINDOW_HEIGHT.

    If you mean you want to resize the window while it's on-screen, I think the right style of window is WS_OVERLAPPEDWINDOW. That will add a caption, border, maximize/close buttons, etc.
    In the code:
    Code:
    g_hwnd = CreateWindowEx(
            WS_EX_CLIENTEDGE,
            g_szClassName,
            "hello",
            WS_OVERLAPPEDWINDOW, //<--
            0, 0, 800, 600,
            NULL, NULL, hInstance, NULL);
    Consider this post signed

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    216
    Ok, the break could be the problem thanks.

    About the window style, I read a tutorial where it said this was mandatory (WS_POPUP | WS_SYSMENU | WS_VISIBLE) for Direct3D to work? Is that false, can the WS_OVERLAPPEDWINDOW be used?

    The window that was being made was just a black box in the corner of the screen, without any sort of border. (I'm assuming the styles I had were the cause of this)

  4. #4
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    Is that false, can the WS_OVERLAPPEDWINDOW be used?
    Indeed.

    Maybe the tutorial was implying that WS_POPUP | WS_VISIBLE will get you the minimum that's required for Direct3D?
    Consider this post signed

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    216
    Well, that did the trick. Thanks so much for the help!

  6. #6
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,597
    On a side note I would specifically assign a value to each member of D3DPRESENT_PARAMETERS so it is clear in the code what you are doing instead of relying on zeroes set by the ZeroMemory() function. It will make your code more clear and could avoid future bugs related to this structure.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    216
    So VirtualAce, your saying I should assign 0's myself in case ZeroMemory doesn't do so? And if the value shouldn't be 0, ZeroMemory will change it.

  8. #8
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,597
    ZeroMemory only zeroes out each member of the struct. This does not mean that your final presentation config in the struct will be accurate. Windowed and full-screen modes require specific configurations and so it is best to specifically set each member so it is clear from the code what you are doing. I recommend doing this for all structures you may encounter ...especially for Win32 structures. Zeroing out structures is only half the problem b/c some methods may not work correctly if you do not set the structure data members to the correct values. It is a simple thing to remember and could hide potentially nasty bugs later.

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    216
    Ok, but what exactly would I be filling in the structures with? Data regarding the window?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Value is incorrect??
    By Myca in forum C Programming
    Replies: 3
    Last Post: 02-11-2011, 10:20 AM
  2. RPN incorrect output
    By csharp100 in forum C Programming
    Replies: 5
    Last Post: 10-14-2010, 01:36 PM
  3. Window closes when edit is made
    By eam in forum Windows Programming
    Replies: 7
    Last Post: 11-06-2003, 09:11 PM
  4. I made a window... now how do I fill it?
    By SMB3Master in forum Windows Programming
    Replies: 24
    Last Post: 06-10-2003, 07:00 PM
  5. Incorrect Output
    By Nutshell in forum C Programming
    Replies: 2
    Last Post: 01-07-2002, 09:11 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21