DirectX Tutorial 1: Getting Started
DirectX Tutorial 1: Getting Started
What you will need
What do you need to prepare?
DirectX 8.0 SDK (Downloadable from http://msdn.microsoft.com/directx)
DirectX 8.0 SDK (available from http://msdn.microsoft.com/directx) Microsoft Visual C 6 (SP5)
Microsoft Visual C 6 (upgrade package 5) General Knowledge of Windows Programming
For Windows programming, General Knowledge of C and Object-Oriented Programming, General Knowledge Of C And Object-Oriented Programming
Overall knowledge about C and object-oriented programming
Introduction
preface
Welcome to my DirectX tutorials. This is the first in a number of tutorials that should at least help you on the way to make Windows games using Microsoft DirectX 8. I have decided to write these tutorials for two reasons. Firstly, I'm a complete beginner when it comes to DirectX. So, the idea is that as I learn, I can write a short tutorial that should reinforce my knowledge. Secondly, the SDK is not the most helpful thing in the world for complete beginners starting out in Game Development. Also, There isn't a Great Deal of Stuff Out There on the Internet for Beginners and DirectX 8, So this stay, as i said, i am a beginner. so, if you spot something That IS INCORRECT IN TUTORIALIALIALIALIALIALILING ME AT: Webmaster@andypike.com.
Welcome to read my DirectX tutorial. This is the first in many tutorials that should be at least at least in your use of Microsoft DirectX8 to help your tutorial on the way of making Windows games. I decided to write these things for a lot of reasons. First of all, when I picked up DirectX, I was a thorough beginner. Therefore, in the process of learning, I have an idea, I can write a small tutorial to deepen my understanding. Second, the SDK is not the most useful for complete beginners who have just entered the game development. Moreover, there are no many materials about DirectX8 on the Internet to beginners, so this thing may be helpful. On the other hand, as I said, I am an initiator. So if you find some incorrect things in these tutorials, please let me know by sending a letter to Webmaster@andypike.com.
COM
What is COM? Well, the Component Object Model is basically a library of methods. You can create COM objects in your program and then call the methods that they expose to you. Methods are grouped together in collections of related methods. These collections are known as Interfaces. you could think of a COM object as a library of functions arranged by subject. DirectX provides a whole host of these libraries that will enable you to create 3D games. The best part is, that DirectX takes care of a lot of the Hard stuff for you, SO it is pretty easy to get Something Simple Up and running. What is COM? Hey, the component object model is basically just a library. You can create a COM object in your program and call him to give your function. Functions and related functions are set together. These guitars are called interfaces. You can imagine COM objects into functions arranged in different topics. DirectX provides a complete Host that allows you to create a 3D game. The best part is that DirectX made a lot of bitter things for you, so it is easy to make some simple things move. There is a lot more to COM than that, for a full description take a look in the SDK. All you really need to worry about is that you release all of your COM objects / interfaces before your program terminates. You should make sure that you Release Them in The Reverse Order To That Which You Created Them. for Example:
About COM has many more content, to get a full description to see SDK. All you really need to care is to release all your COM objects / interfaces before you terminate. You should make sure you release them in order to create them when you create them. For example: 1. Create Interface A.2. Create Interface B.3. Release Interface B.4. Release Interface A. You Release The Com Object by Calling Their Release Method.
You release the COM object by calling the Release function.
Page flipping
Turn the page
What is Page Flipping? Well, think of a flipbook. This is a number of pages with a slightly different drawing on each page. Then, when you hold the corner and "flip" the pages, it looks like the picture is moving. This is how DirectX Graphics works. you draw all of your objects onto a hidden page, known as the "Back Buffer". Then when you have finished, flip it to the Front Buffer and repeat the process. As the user is looking at the new Front Buffer, Your Program Will Be Drawing Onto The Back Buffer. What is Page Flipping? Hey, think about Flipbook. He is a book with a lot of books that have a slightly different drawings. Then, when you mention one angle and then "flip" page, you look at the image is moving. This is how DirectX Graphics works. You draw on your object on a hidden page. The hidden page is something called "back buffer". Then when you paint it, turn it to your front and repeat this process. When the user is watching the front desk, your program is painted behind. What Would Happen WITHOUT Page Flipping? WITHOUT Page Flipping, The User Would See each Object Appear AS IT WAS DRAWN, WANT ALL.
What happens if there is no page flipping? Without Page Flipping, users may see the drawing process of each object, and this is not that you are not what you want. So, your game will basically consist of a loop, known as the "Game Loop". Each time around the loop you process your game logic so you know where your objects will be. Next, you clear the Back Buffer. Then draw the current Scene ONTO It. When this is done, flip it to the front and start the loop again. This Will Continue Until The Game is Shut Down. You May Have a Number of Back Buffers, this is know, this is know.
Therefore, your game basically contains a loop, called "game loop", each time you are in loop, and you deal with your game logic, you know where your object will go. Then you remove the Back Buffer. Then use the current scene. When this is completed, the FLIP starts to cycle again again. This must be done until your game is over. You may have a lot of back buffer, which is also called "swap chain".
DEVICES
equipment
What is a device? Basically, a device (as far as DirectX Graphics is concerned) is your machines 3D card. You can create an interface that represents your device and then use it to draw objects onto the back buffer. What is the device? Simply, a device (for DirectX Graphic) is a 3D card for your machine. You can create an interface that represents your device and use it to take the object in Back Buffer.
Game loop
Game cycle
What is the game loop Well, the game loop is a code loop that loops until the program is shut down Inside the game loop is where it all happens:?. Objects are drawn (rendered), game logic is processed (AI, moving objects And scoring etc) and windows messages are processed. The it's all done again untric the program is closed down.
What is the game cycle? Hey, the game loop is a code loop knows your program to end. The following things have occurred in the game cycle: the object is drawn (rendered), the game logic is processed (artificial intelligence, moving object, statistical score, etc.) and processes Windows messages. Then there is a repetition of these knows the end of the program.
CREANG YOUR FIRST Project
Create your first project
Okay, That's enough theory letts get started. FOLLOW TO CREATE YOUR FIRECT DIRECTX GRAPHICS Project.
Ok, theory is enough, let's get started. Follow the steps step by step to create your first DirectX Graphics Project 1. In Visual C Create A New Win32 Application. A. File> New B. From The Projects Tab SELECT WIN32 Application C. Enter a name for Your Project such as "DX Project 1" d. Select a folder for the location of your source code files e. Click Next f. Select the empty project option. g. Click Finish2. Make sure that your project settings are correct. a. Project> Settings ... b. On the link tab, makess "D3D8.LIB" is in the list of object / library modules. If it isn't simply type it in. Make su consisting your search path. a. Tools> Options> Directories Tab B. in the "show directories for" DROP-DOWN, SELECT "incrude file". c. if it does not exist already, add the following path:
LPDIRECT3D8 g_pD3D = NULL; LPDIRECT3DDEVICE8 g_pD3DDevice = NULL;. HRESULT InitialiseD3D (HWND hWnd) {// First of all, create the main D3D object If it is created successfully we // should get a pointer to an IDirect3D8 interface.// First, Create a primary D3D object. If it was successfully created, we got a pointer to the iDirect3d8 interface. G_pd3d = Direct3dcreate8 (D3D_SDK_VERSION); if (g_pd3d == null) {Return E_FAIL;} // Get The Current Display Mode
// Get the current display mode D3DDisplayMode D3DDM; if (failed (g_pd3d-> getadapterdisplaymode (D3DADAPTER_DEFAULT, & D3DDM))) {Return E_FAIL;} // Create A Structure To Hold The Settings for Our Device
// Create a structure to save our device set information D3DPresent_Parameters D3dpp; ZeromeMory (& D3DPP, SIZEOF (D3DP)); // Fill the structure. // We wantur program to be windowed, and set the back buffer to a format / / That Matches Our Current Display Mode
// Fill structure.
// We want our program to display, and set back buffer to match our current display mode. D3dpp.windowed = true; d3dpp.swapeffect = d3dswapeffect_copy_vsync; d3dpp.backbufferformat = d3ddm.format; // CREATE A DIRECT3D DEVICE.
// Create a Direct3D device if (FAILED (g_pD3D-> CreateDevice (D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, & d3dpp, & g_pD3DDevice))) {return E_FAIL;} return S_OK;} void Render () {if (g_pD3DDevice == NULL) {Return;} // clear the backbuffer to a Green Color
// Clear the Backbuffer as green G_PD3DDEvice-> Clear (0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB (0, 25, 0), 1.0F, 0); // Begin The Scene
// Start the scene g_pd3ddevice-> beginscene (); // rendering of ia get Objects Will Go Here
// We will be rendered here // end the scene // End Scene G_PD3DDEVICE-> endscene (); // filp the back and front buffers so trat whatver Has Been rendered on the back buffer // Will Now Be visible On Screen (Front Buffer).
// Turning back and Front Buffer Thus on how to render in Back Buffer can be seen on the screen (Front // Buffer). g_pd3ddevice-> present (null, null, null, null);} void cleanup () {if (g_pd3ddevice! = null) {g_pd3ddevice-> release (); g_pd3ddevice = null;} if (g_pd3d! = null) {g_pd3d- > Release (); g_pd3d = null;}} void gameloop () {// enter the game loop
// Enter the game cycle 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
// Processing Message TranslateMessage (& MSG); DispatchMessage (& MSG);} else {// no message to process, so render theurrent scene
// There is no message to process, so render the current scene render ();}}} // the Windows Message Handler
// window message handler LRESULT WINAPI WinProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {switch (msg) {case WM_DESTROY: PostQuitMessage (0); return 0; break; case WM_KEYUP: switch (wParam) {case VK_EScape: // User Has Pressed The Escape Key, So Quit
/ / The user presses the ESCAPE button to exit DestroyWindow (hwnd); return 0; Break;} Break;} Return DefWindowProc (HWND, MSG, WPARAM, LPARAM);} // Application Entry Point
// program entry point INT WINAPI WinMain (HINSTANCE hInst, HINSTANCE, LPSTR, INT) {// Register the window class // register window class WNDCLASSEX wc = {sizeof (WNDCLASSEX), CS_CLASSDC, WinProc, 0L, 0L, GetModuleHandle (NULL ), NULL, NULL, NULL, NULL, "DX Project 1", NULL}; RegisterClassex (& WC); // Create The Application's Window
// Create a program window HWND HWND = CreateWindow ("DX Project 1", "www.andypike.com: Tutorial 1", WS_OVERLAPPEDWINDOW, 50, 50, 500, 500, GetDesktopWindow (), NULL, WC.HINSTANCE, NULL ; // Initialize Direct3D
// Initialization Direct3D IF (Succeeded (Initialiad3D (HWND))) {// showur window
// Show our window showWindow (hwnd, sw_showdefault); UpdateWindow (hwnd); // start game running: Enter the game loop
// Start the run: Enter the game loop gameloop ();} cleanup (); unregisterclass ("DX Project 1", wc.hinstance); return 0;}
You Should Finish Up with a window with a Green Background (Shown Below). Okay, It's Not Much i Know, But Everyone Has To Start Somewhere.
You will see a window of a green background to display it (as follows). Ok, know much, but everyone must start from somewhere.
Image slightly SO, What is going online?
Hey, what is it here? WinMainThis is The Applications Entry Point. Cade Execution Will Start Here. This is where we register, create and show our window. Once That is Complete, WeinitiSe Direct3d and Enter Our Game Loop.
This is the entry point of the program. The program is executed from here. This is where we register, create and display our window. Once these, we initialize Direct3D and enter our game cycle. WinProcThis is the applications message handler Whenever Windows sends a message to our application, it will be handled by this function Notice that there are two messages that our application will handle:.. WM_DESTROY and WM_KEYUP, all other messages are passed to DefWindowProc for default message This is the processing function of the program message. Whenever Windows sends a message to our program, it will be processed by this function. Note that our program is to handle two messages: WM_DESTROY and WM_KEYUP, all other messages are sent to DefWindowProc to make default messages. G_pd3dthis is a pointer to an idirect3d8 interface. from this interface We will create our direct3d device.
This will be a pointer to the IDirect3D8 interface. We will create our Direct3D devices with this interface. G_pd3ddevicethis is a pointer to an idirect3ddevice8 interface. this will actually represent your hardware graphics card.
This is an interface pointing to iDirect3dDevice8. This actually represents your hardware graphics card. InitialiseD3DThis does exactly that: initialise Direct3D First of all, we create the IDirect3D8 object From this object we can determine the users current display mode Finally, we use this information to create a compatible device....
This is actually doing this: initialize Direct3D. First, we create IDirect3D8 objects. With this object, we can determine the current display mode of the user. Finally, we use this information to create a compatible device. Gamelooponce Our Window Is Created this function is caled. This function contains the main game. If the all, it callsur render () function.
Once our window is created, this function is called. This function contains a primary game loop. If there is no Windows message to process, it calls our render () function. RenderFirstly we clear the back buffer ready for drawing. Then we use the BeginScene method of our device object to tell DirectX that we are about to start drawing. We can then start to draw our game objects (Tutorial 2). Once we have finished drawing , we use the EndScene method of our device object to tell DirectX that we have finished drawing. the final step is to "flip" (present) the back buffer, this will display our game objects to the user. first we clear the back buffer To prepare painting. Then we use our device object's Beginscene function to tell DirectX We have to start painting. Then we can start painting the objects of our game (tutorial 2). Once we complete the drawing, we use our equipment object's EndScene function to tell DirectX we have completed the drawing. The last step is "FLIP" (presentation) Back Buffer, which will display our game object to the user. Cleanupsimply Cleans Up by release our objects.
Simply clean up by releasing our objects.
Summary
to sum up
OK, That's it for the first tutorial. I know the name the MOSHED Program Wasn't The Most Spectacular Thing in The Next Tutorial When We Well Be Drawing Some Shapes!
Ok, the first chapter is this. I know that the completed procedure is not the most striking program in the world, but as long as we wait until the next tutorial, we can draw some graphics!