rate my directx code

This is a discussion on rate my directx code within the Game Programming forums, part of the General Programming Boards category; since i saw a lot of posts say that directx has to do with games, so ima post this here. ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    221

    rate my directx code

    since i saw a lot of posts say that directx has to do with games, so ima post this here. all this does is inits directx. let me know what u think. i dont wanna get started with more directx stuff if the basics isnt good.
    Code:
    #include <windows.h>
    #include <d3d9.h>
    //d3d9.lib d3dx9.lib winmm.lib       mental note: always link
    HWND MainWindowHandle = 0;
    
    D3DCAPS9 caps;
    IDirect3D9 *d3d9 = 0;
    
    int Run()
    {
    	MSG msg;
    	::ZeroMemory(&msg,sizeof(MSG));
    
    	while(::GetMessage(&msg,0,0,0))
    	{
    		::TranslateMessage(&msg);
    		::DispatchMessage(&msg);
    	}
    	return (INT)msg.wParam;
    }
    
    LRESULT CALLBACK WndProc(HWND windowHandle, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    	switch(msg)
    	{
    	case WM_KEYDOWN:
    		{
    		if(wParam == VK_ESCAPE)
    			::DestroyWindow(MainWindowHandle);
    	  /*if(wParam == VK_RETURN)
    			MessageBox(0,"Hello Steph","You Swamper",MB_OK);
    		if(wParam == 'M')
    			MessageBox(0,"Button M pressed","Button hit",MB_OK);
    		if(wParam == 'N')
    			MessageBox(0,"Button n pressed","Button hit",MB_OK);
    		else
    			MessageBox(0,"Unknown Command","Button hit",MB_OK);*/
    		break;
    		}
    	case WM_DESTROY:
    		{
    			::PostQuitMessage(0);
    			break;
    		}
    	}
    	return ::DefWindowProc(windowHandle,msg,wParam,lParam);
    }
    
    bool InitWindowsApp(HINSTANCE instanceHandle, int nCmdShow)
    {
    	WNDCLASS wc;												//windows class
    	wc.style		= CS_HREDRAW | CS_VREDRAW;
    	wc.lpfnWndProc  = WndProc;
    	wc.cbClsExtra   = 0;
    	wc.cbWndExtra	= 0;
    	wc.hInstance	= instanceHandle;
    	wc.hCursor		= ::LoadCursor(0, IDC_ARROW);
    	wc.hIcon		= ::LoadIcon(0, IDI_APPLICATION);
    	wc.hbrBackground= 
    		static_cast<HBRUSH>(::GetStockObject(WHITE_BRUSH));
    	wc.lpszMenuName = 0;
    	wc.lpszClassName= "Test";
    
    	if(!::RegisterClass(&wc))
    	{
    		::MessageBox(0,"Regester class failed", "FAILED1", MB_OK);
    		return false;
    	}
    
    	MainWindowHandle = ::CreateWindow("Test",
    									"Hello World", 
    									WS_OVERLAPPEDWINDOW,
    									CW_USEDEFAULT,
    									CW_USEDEFAULT,
    									CW_USEDEFAULT,
    									CW_USEDEFAULT,
    									0,
    									0,
    									instanceHandle,
    									0);
    	if(MainWindowHandle == 0)
    	{
    		MessageBox(0,"Create Window failed", "FAILED", MB_OK);
    		return false;
    	}
    	::ShowWindow(MainWindowHandle,nCmdShow);
    	::UpdateWindow(MainWindowHandle);
    	return true;
    }
    
    bool initDirect3d()
    {
    	d3d9 = Direct3DCreate9(D3D_SDK_VERSION);    //pointer to direct3d interface
    	if(d3d9 == 0)
    	{
    		MessageBox(0,"error - direct3dcreate","error",MB_OK);
    		return false;
    	}
    	d3d9->GetDeviceCaps(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,&caps);
    	
    	//hardware/software vertex processing check
    	int vp;
    	if(caps.DevCaps && D3DDEVCAPS_HWTRANSFORMANDLIGHT)
    		vp = D3DCREATE_HARDWARE_VERTEXPROCESSING;
    	else
    		vp = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
    
    
    	D3DPRESENT_PARAMETERS d3dpp;
    	d3dpp.BackBufferWidth	= 800;
    	d3dpp.BackBufferHeight	= 600;
    	d3dpp.BackBufferFormat	= D3DFMT_A8R8G8B8;
    	d3dpp.BackBufferCount	= 1;
    	d3dpp.MultiSampleType	= D3DMULTISAMPLE_NONE;
    	d3dpp.MultiSampleQuality= 0;
    	d3dpp.SwapEffect		= D3DSWAPEFFECT_DISCARD;
    	d3dpp.hDeviceWindow		= MainWindowHandle;
    	d3dpp.Windowed			= false;
    	d3dpp.EnableAutoDepthStencil = true;
    	d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
    	d3dpp.Flags				= 0;
    	d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
    	d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
    
    	IDirect3DDevice9 *device = 0;
    	HRESULT hr = d3d9->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,MainWindowHandle,vp,
    						&d3dpp,&device);
    	if(FAILED(hr))
    	{
    		MessageBox(0,"createdevice failed","error",MB_OK);
    		return false;
    	}
    
    	return true;
    
    }
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    				   LPSTR lpCmdLine, int nCmdShow)
    {
    	//init the window
    	InitWindowsApp(hInstance, nCmdShow);
    	
    	if(!initDirect3d())
    	{
    		MessageBox(0,"Direct3d init error","Error!",MB_OK);
    		return 0;
    	}
    	Run();
    	d3d9->Release();
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Jul 2003
    Posts
    59
    Ratings:

    Sweden: 10, Findland: 8, Norway: 9, Denmark: 10
    Total: 9



    Seriosuly it looks good, nice and clean.

    About your mental note. You can use this at the beginning of your program.

    #pragma comment ( lib, "d3d9.lib" );
    #pragma comment ( lib, "d3dx9.lib" );
    #pragma comment ( lib, "winmm.lib" );

    Now you don't have to remember to link them =)

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    AFAIK, the #pragma lib only works in MSVC++, so if you're using a compiler like MingW32 (GCC), it won't work.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    #pragma comment ( lib, "d3d9.lib" );
    #pragma comment ( lib, "d3dx9.lib" );
    #pragma comment ( lib, "winmm.lib" );

    Now you don't have to remember to link them =)
    haha
    THANK YOU, i was trying to remember that command
    yea, im using msvc++

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Not too bad, I do see some things that could be better.

    Code:
    if(caps.DevCaps && D3DDEVCAPS_HWTRANSFORMANDLIGHT)
    You want bitwise AND ( & ) not logical ( && ).

    Also, you need to check formats that are supported by the video card and not have them hard-coded. For instance, some of the old cards don't have 8-bit stencil buffer capabilities so D24S8 wouldn't work. If you are just doing this for your machine it doesn't matter much but you'll run into trouble later, trust me. Good start though.

    Edit: Also consider a peek-based message pump instead of the standard one for performance issues. If you don't know what one is either google for it or ask.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  6. #6
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    why the bitwise?

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Otherwise, it becomes if( caps.DevCaps != 0 && D3DDEVCAPS_HWTRANSFORMANDLIGHT != 0 ).
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  8. #8
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    thank you

  9. #9
    Registered User
    Join Date
    Dec 2002
    Posts
    221
    Originally posted by MrWizard
    Not too bad, I do see some things that could be better.

    [CODE]

    Also, you need to check formats that are supported by the video card and not have them hard-coded. For instance, some of the old cards don't have 8-bit stencil buffer capabilities so D24S8 wouldn't work. If you are just doing this for your machine it doesn't matter much but you'll run into trouble later, trust me. Good start though.
    which is the best way to do this?

  10. #10
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Use the following method of IDirect3D9 to get the adapter's display information:

    Code:
    IDirect3D9::GetAdapterDisplayMode( UINT Adapter, D3DDISPLAYMODE *pMode )
    If you want a specific format you can call the following method of IDirect3D9 to test it:

    Code:
    IDirect3D9::CheckDeviceFormat( ... params ... )
    You can lookup the parameters to it in the DirectX SDK Help documentation.

    This should get you started.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 23
    Last Post: 04-20-2009, 07:35 AM
  2. DirectSound header issues
    By dxfoo in forum C++ Programming
    Replies: 0
    Last Post: 03-19-2006, 06:16 PM
  3. Getting Frame Rate in DirectX
    By Rune Hunter in forum Game Programming
    Replies: 4
    Last Post: 11-02-2005, 01:57 PM
  4. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 03:28 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

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