DirectX ProgrammingThis area of the site is dedicated to DirectX programming. This page includes some general Q & A’s about DirectX along with links to notes and questions about the specific APIs. There are now more than 30 pages in this area of notes and answers. New items are added as I get asked questions - so keep asking! The Microsoft DirectX SDK contains a set of APIs for programming games on Microsoft Windows platforms. The APIs are Direct3D, DirectShow, DirectInput, DirectPlay, DirectSound and DirectSetup. These pages concentrate mainly on the largest API: Direct3D. There are also some getting started notes for some of the other APIs. Follow the links below or use the bar at the top of the page to navigate. Section IndexDirectX - General questions about DirectX (on this page). Direct3D - Notes describing the complete process of getting started with Direct3D plus answers to common questions. DirectShow - Notes about using DirectShow. DirectInput - Notes about using DirectInput. DirectSound - A brief introduction to using DirectSound
DirectX In General DirectX In General
Where can I get the DirectX SDK?The SDK is free to download, you can get it by going to this page: http://msdn.microsoft.com/library/default.asp?url=/downloads/list/directx.asp Be warned it is a huge download, close to 200 MB so you will need broadband. Make sure you uninstall previous versions of the SDK before installing the latest version (9.0a Summer Update) and install it in debug mode. I have heard there is going to be a summer update this coming summer as well.At the moment they are calling it DirectX 9.0c (Summer Update 2004) but hopefully that will change to something more sensible. Where to look for helpThe help system that comes with the DirectX SDK is actually very good and should be your first place to look for help. Additionally the sample applications are great at showing an example of how to do something. You will also be surprised as to how much DirectX can do for you. If you want to do some graphic functions have a look in the SDK help first, there's a good chance there is a helper function in there (look at the D3DX functions). There is a Microsoft FAQ on DirectX available as well here: DirectX FAQ. There are some newsgroups dedicated to DirectX, a list can be found here. What are the differences between DirectX and OpenGL?Firstly let me say that both are very useful to know. DirectX is a collection of APIs including APIs for graphics, music, audio, input, networking and multimedia. OpenGL is dedicated to graphics. So to compare the two makes no sense, we can however (tentatively) compare OpenGL with Direct3D: - Most people find OpenGL easier to learn to start with
- OpenGL uses standard C interfaces.
- Direct3D has a steeper initial learning curve and is based on C++ interfaces (COM).
- It is more difficult using OpenGL to do lower level operations than Direct3D, however this does mean you are less likely to crash an OpenGL app than a Direct3D one.
- Most PC games are written using Direct3D.
- OpenGL is on multiple platforms where as Direct3D is Windows based.
- Direct3D is updated frequently, allowing a standard interface to new features sooner than they appear in core OpenGL. However, OpenGL has an extension mechanism allowing immediate access to new features as gfx manufacturers create them, rather than having to wait for a new API version to appear.
- Direct3D has a lot of helper functions for common game coding issues (the D3DX set of functions)
- In terms of performance there is little difference between the two.
As you can see there are pros and cons for each and really you should just learn both and use whichever is appropriate. You could even, if you wish, use OpenGL for graphics and the other APIs of DirectX for input, networking etc. If you are looking for a career in the games industry I would advise you to learn DirectX. I get link errors! Firstly make sure you have the include and library paths set correctly in visual studio. Go to the tools menu - options - directories and make sure the paths are set correctly for the SDK, normally they will be c:\dx90sdk\include and c:\dx90sdk\lib. Also make sure they are first in their lists. Use the black arrows to move them to the top. More detailed steps are shown below: Other link errors can be due to you forgetting to add a library to the project - settings - link - object / library modules text box. See What are the different Direct3D library files. Visual C++ Settings For DirectX - Detailed StepsThese settings are to point Viz at the correct directories for using DirectX. I assume below that the DirectX SDK is installed in the default c:\DXSDK\ folder. If not then just replace it with the correct folder. - Start Visual C++
- Go to the tools menu and select options
- There are a number of tabs along the top, click on the ‘Directories’ one
- Firstly we set up the include directory:
- In the drop down list ‘Show directories for’ make sure it is set to ‘Include File’s’
- In the directories list box double click on the blank entry at the end where by an edit box will appear. Type c:\DX90SDK\Include or browse to find it and press return.
- It is important that this is the first entry in the list, so once it is entered click on the arrow (move item up) to move it to the top of the list.
- Now we need to set up the library directory
- In the drop down list ‘Show directories for’ change it to ‘Library File’s’
- Again, in the directories list box double click on the blank entry at the end and type c:\DX90SDK\Lib.
- Again this needs to be first in the list so move it up using the arrows.
And that’s it. You should only ever have to do this once. I have a DirectX related bug I cannot track down!The first thing to do is to check the debug output in Viz, DirectX puts warnings and errors there that can help you find a problem. This requires you have DirectX in debug mode (It can be changed via the control panel). The number of warnings you get is controlled by the slider. DirectX provides a means of getting a DirectX error string. Each DirectX function returns a HRESULT, you can pass that to an error function to get an error string and then output it to the debug pane or the window. Note: OutputDebugString sends output to the debug pane in Viz. The two functions are DXGetErrorString9(hr),DXGetErrorDescription9(hr) To use these functions you must include Dxerr9.h and link to Dxerr9.lib (put it in the project - settings - link - object / library modules text box). e.g. HRESULT hr; hr=D3DXCreateTextureFromFile( gDevice, d3dxMaterials[i].pTextureFilename, &m_meshTextures[i] ); if (FAILED(hr)) { char buf[2048]; sprintf(buf, "Error: %s error description: %s\n",DXGetErrorString9(hr),DXGetErrorDescription9(hr)); OutputDebugString(buf); } There are also functions in Dxerr9.h that will display the error for you without having to do the above. Simply call: DXTRACE_ERR(TEXT(“My error description”),hr) So the above example could now be written more neatly as: if (FAILED(hr=D3DXCreateTextureFromFile( gDevice, d3dxMaterials[i].pTextureFilename, &m_meshTextures[i] ))) DXTRACE_ERR(TEXT(“Failed to create texture”),hr); Since you are constantly having to check the HRESULT from DirectX calls it makes sense to write one error function to handle them all rather than repeating the above each time. I have a class that is full of static helper functions which includes a function to check if a hr value is failed. If you create a macro you can even pass in the file and line number that the problem occured on. e.g. #define CheckHr(hr) CheckForDxError(__FILE__,__LINE__,hr) The file and line are provided by the compiler and allow you to output where the problem occured. In addition if you output them correctly you can double click on the error in the output pane of Viz and be taken to the line in question. Your function may then look something like this: void CheckForDxError(const char *file, int line, HRESULT hr) { if (!FAILED(hr)) return; // Get the direct X error and description char desc[1024]; sprintf(desc,"(DX) %s - %s",DXGetErrorString9(hr),DXGetErrorDescription9(hr)); // Output the file and line number in the correct format + the above DX error char buf[2048]; sprintf(buf,"%s(%d) : Error: %s\n", file, line, desc); OutputDebugString(buf); } I get memory leak errors from DirectX when my application closes, how do I get rid of them?Every DirectX object you have obtained must be released before your application closes. This can include the Direct3D object, the device, textures, vertex buffers, index buffers, state blocks, sprites, fonts etc. Luckily every object implements the COM IUnknown set of interfaces which include a Release() function, so the process is the same to release everything e.g. device->Release() You must release things in the correct order. So the device and object are released last. Advanced: If you have searched and searched and still cannot find what it is that you have failed to release there is one further, advanced, trick that can help. You will have some sort of leak message like: Direct3D9: (ERROR) :Memory Address: 003b4b3c lAllocID=1 dwSize=00003524,ReturnAddr=00cb7346 (pid=00000108) Go to the DirectX properties dialog box in control panel and make sure it is in debug mode. Then in the ‘debugging box’, in the part which says ‘brake on AllocID’ fill in the IAllocId in your error message. Run the game and you should then see which items are not releasing. The IAllocID of 1 is probably the main DirectX object and is not being released due to something later so check against the higher id numbers first. How do I get auto-complete to work with DirectX?Auto complete is a capability of Viz that means when you type a pointer and the -> or a . from an instance you get a drop down list of variables and functions, which is very useful! Unfortunately it does not always work well with libraries. You can get it to work in most cases for DirectX by actually including in your project the DirectX header files. So under the file tab add the DirectX header files so they are included in the project. After doing this do a complete rebuild and auto complete will now work in most cases. The new .net environment handles auto complete fine already. I get lots of compile time errors in d3dtypes.hLook at the error where it shows the path of the d3dtypes.h, if it is indicating it is in the visual studio directory then this is the problem. Visual studio comes with a very old version of DirectX so you have to make sure you use the one in the c:\dx90sdk directory. To do this in visual studio go to Tools/Options and select the directories tab, select Include Files in the Show Directories For drop down list and make sure the DirectX path is first on the list, use the arrows to move it to the top if it is not. Do the same for Library Files. See also Link Errors.
|