(DirectX programming) Modifying and debugging Allen Sherrod's program really makes me a headache

Source: Internet
Author: User

Preface: I recently spent time on my own course design program, so I have never showed up on csdn. The remaining time is spent on DirectX programming. Have you seen my recently uploaded GIF dynamic images? These are the results of my recent DirectX learning. For more information, see:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Of course, I still read ultimate game programming with dirextx. This book is written by Allen Sherrod. The author is 28 years old and quite young. He has a website: www.ultimategrameprogramming.com. But I cannot access this website anyway.

Now I have read this book and found many errors. The cover of this book.

The source program here has encountered many errors, which really gave me a headache. The simplest error is as follows:

The connection Action # pragma comment (Lib, "d3dx9. lib") is missing,

Then many people may encounter such an unknown error after the program is successfully compiled, that is, the program cannot run. To be exact, every time you click to run, the window will flash, and then there will be no movement. This is often confusing. In fact, people who know how to use VC should think that this issue can be easily solved, because from vc6.0 to vs2005, there are debug modules. I use the Chinese version of vs2005, which is easy to use. When you locate a program statement, you will know that this statement has encountered an error. Most of Allen Sherrod's programs are written in this way.

Code:
  1. // Create the d3ddevice
  2. If (failed (g_d3d-> createdevice (d3dadapter_default, d3ddevtype_hal, g_hwnd,
  3. D3dcreate_hardware_vertexprocessing | d3dcreate_puredevice,
  4. & D3dpp, & g_d3ddevice) return false;

In fact, I checked a lot of information about d3dcreate_hardware_vertexprocessing and d3dcreate_software_vertexprocessing. The conclusion is that most laptops do not support d3dcreate_hardware_vertexprocessing ). Therefore, you must change d3dcreate_hardware_vertexprocessing | d3dcreate_puredevice to d3dcreate_software_vertexprocessing, which cannot be used by d3dcreate_puredevice.

Next I will introduce how to debug a program that is difficult to use: Chapter 10 directinput demo program.

First of all, the program can be compiled successfully at first glance, but there is still a flash in the window, is the above problem so simple? No. This project also has the above problems. However, this error still occurs after you change hardware to software. What exactly is this? When I checked the main. cpp file, I found the following error:

Code:
  1. Bool initializeobjects ()
  2. {
  3. // Initialize input.
  4. If (! Input. initialize (g_hwnd, g_hinstance) // the error message returned when this error occurs.
  5. Return false;

Right-click and go to definition. Here is the source of the error. You can set the breakpoint here:

 

 

 

 

 

 

Then perform debugging. Debugging can identify errors, but it cannot completely solve them. In order to solve the problem, I even went to a foreign forum to get the story.

Paste the code first, and then analyze it one by one.

Code:
  1. Bool cdirectinputsystem: Initialize (hwnd, hinstance, bool mouseexclusive)
  2. {
  3. // Save copies.
  4. M_hwnd = hwnd;
  5. Gthis = this;
  6. // Create input system.
  7. If (failed (directinput8create (hinstance, directinput_version, iid_idirectinput8, (void **) & m_inputsystem, null) return false;
  8. // An error occurs. directinput_version is defined in directinput but has no value. That is to say, a null value is passed here. Of course, this is not allowed and will return false.
  9. // Initialize the keyboard.
  10. If (failed (m_inputsystem-> createdevice (guid_syskeyboard, & m_keyboard, null )))
  11. Return false;
  12. If (failed (m_keyboard-> setdataformat (& c_dfdikeyboard) return false;
  13. If (failed (m_keyboard-> setcooperativelevel (m_hwnd, discl_foreground | discl_nonexclusive )))
  14. Return false;
  15. /* If (failed (m_keyboard-> acquire () // an error occurs here because it is declared as a virtual function in directinput and does not run the function, of course, the returned results failed.
  16. {
  17. MessageBox (null, "An error occurred when calling the acquire function. The acquire member function may not exist. "," The system prompts an error. ", Null );
  18. System ("pause ");
  19. Return false;
  20. }*/
  21. // Clear keys will clear out the array of keys we have.
  22. Memset (m_keys, 0, sizeof (m_keys ));
  23. // Initialize the mouse.
  24. DWORD flags;
  25. If (failed (m_inputsystem-> createdevice (guid_sysmouse, & m_mouse, null) return false;
  26. If (failed (m_mouse-> setdataformat (& c_dfdimouse) return false;
  27. If (mouseexclusive) flags = discl_foreground | discl_exclusive | discl_nowinkey;
  28. Else flags = discl_foreground | discl_nonexclusive;
  29. If (failed (m_mouse-> setcooperativelevel (m_hwnd, flags) return false;
  30. /* An error occurs here because it is declared as a virtual function in directinput and does not run the function. Of course, the returned result fails.
  31. If (failed (m_mouse-> acquire () return false;
  32. */
  33. // Initialize the game controller.
  34. Diproprange range;
  35. Didevcaps caps;
  36. M_inputsystem-> enumdevices (di8devclass_gamectrl, (lpdienumdevicescallback) gjsenumdevicecallback,
  37. Null, diedfl_attachedonly );
  38. // If (! M_controllerfound) return false;
  39. Range. diph. dwsize = sizeof (diproprange );
  40. Range. diph. dwheadersize = sizeof (dipropheader );
  41. Range. diph. dwhow = diph_byoffset;
  42. Range. Lmin =-1000;
  43. Range. Lmax = 1000;
  44. Range. diph. dwobj = dijofs_x;
  45. /* An error occurs. m_gamecontrol does not assign an initial value. Therefore, an error occurs when a member function is called with a null value. */
  46. // M_gamecontrol-> setproperty (diprop_range, & range. diph );
  47. Range. diph. dwobj = dijofs_y;
  48. /* An error occurs. m_gamecontrol does not assign an initial value. Therefore, an error occurs when a member function is called with a null value. */
  49. // M_gamecontrol-> setproperty (diprop_range, & range. diph );
  50. /* An error occurs. m_gamecontrol does not assign an initial value. Therefore, an error occurs when a member function is called with a null value. */
  51. // If (succeeded (m_gamecontrol-> getcapabilities (& caps) m_numbuttons = caps. dwbuttons;
  52. /* Else */m_numbuttons = 4;
  53. Return true;
  54. }

Here I will explain it. I checked relevant information online, saying that directinput_version is not defined as a value and will appear during compilation
1> C:/program files/Microsoft DirectX SDK (February 2010)/include/dinput. h: directinput_version undefined. Defaulting to version 0x0800

This problem, so follow the solution on the gamedev website (please visit http://www.gamedev.net/community/forums/topic.asp? Topic_id = 116163). When the header file is defined (here it refers to the cdirectinput. h file header), add a statement: # define directinput_version 0x0800, and the information during compilation will be gone. I also checked the relevant materials for other problems. Unfortunately, I cannot solve the problem because I am not very knowledgeable (specifically, I cannot write Instance functions for virtual functions, so I simply commented out.

The results are good. The program can run, and there is no shortage of arms and legs. The program runs:

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Post Script: Great. I finally succeeded in debugging. Sometimes debugging is successful in other people's programs. Do you think so?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.