• No se han encontrado resultados

Instrumentos para la recolección de datos

Capítulo 3. Metodología

3.2 Método de recolección de datos

3.2.1 Instrumentos para la recolección de datos

Before we can compile DirectX code, we need to configure our project.

Include directories. Right click the project name “DirectX Window” in the “Solution Explorer” and select “Properties.”

• Change the “Configuration:” in the top left to “All Configurations.”

• Select “VC++ Directories” under “Configuration Properties” in the left pane of the properties page.

• Click “Include Directories” in the right pane.

• Open the drop down menu and select “<Edit...>” (Figure 3.4).

Figure 3.3. Adding an existing item.

53 3.8. Our First DirectX Program

• Enter “$(DXSDK_DIR)\Include” as shown in Figure 3.5 then click “OK.” This tells Visual Studio where to look for the DirectX include files.

Figure 3.4. “Include Directories.”

Figure 3.5. Adding the included directory.

54 3. Introduction to DirectX

Library directories. Next, we need to tell the linker where to look for the DirectX library files.

• Click “Library Directories” in the right pane.

• Open the drop down menu and select “<Edit...>.”

• Enter “$(DXSDK_DIR)\Lib\x86” in the same manner as before. Click “OK” in the

“Library Directories” window, then click “Apply” in the “DirectX Window Property Pages.”

Runtime library.

• Change the “Configuration:” in the top left of the “DirectX Window Property Pag-es” to “Release” (see Figure 3.6).

• Expand C/C++ and select “Code Generation.”

• Change the “Runtime Library” to “Multi-threaded (/MT).” This change will stati-cally link in the C Runtime Libraries, which allows our program to run on systems that do not have the C Runtime Library DLL installed (MSVCP100.DLL or equiva-lent).

Figure 3.6. “Runtime Library.”

55 3.8. Our First DirectX Program

Additional dependencies. Now we tell Visual Studio which DirectX libraries to include.

• Change the “Configuration:” in the top left to “All Configurations.”

• Expand “Linker” in the left tab of the properties page.

• Select “Input” in the left pane.

• Click “Additional Dependencies” in the right pane.

• Open the drop down menu and select “<Edit...>.”

• Enter “d3d9.lib” and click “OK” in the “Additional Dependencies” window; then click “OK” in the “DirectX Property Pages” window (see Figure 3.7).

The configuration is now complete for this project. In future projects, we will add more .lib files to the “Additional Dependencies” as we add more features from DirectX to our programs. Now we can add our DirectX code to the project.

• Right click the “Source Files” folder in the “Solution Explorer” window. Point to

“Add,” then select “New Item...”

Figure 3.7. “Additional Dependencies” window.

56 3. Introduction to DirectX

• Select “C++ File (.cpp)” in the center window and replace “<Enter_name>” with

“graphics” then click “Add.” This creates a C++ source file where we can enter the code for graphics.cpp, shown earlier. Of course, in the interest of time and to avoid mistakes, just grab the code from the book’s web site: www.programming 2dgames.com.

• Right click the “Header Files” folder in the “Solution Explorer” window. Point to

“Add” then select “New Item...”

• Select “Header File (.h)” in the center window and replace “<Enter_name>” with

“graphics” then click “Add.” This creates a C++ header file where we can enter the code for graphics.h, shown earlier. Again, in the interest of time and to avoid mistakes, just grab the code from the book’s web site.

• The only step remaining is to modify a few lines in winmain.cpp.

• Add #include graphics.h at the top of winmain.cpp (see Listing 3.16).

// Programming 2D Games // Copyright (c) 2011 by:

// Charles Kelly

// Chapter 3 DirectX Window v1.0 // winmain.cpp

#define _CRTDBG_MAP_ALLOC // For detecting memory leaks

#define WIN32_LEAN_AND_MEAN

#include <Windows.h>

#include <stdlib.h> // For detecting memory leaks

#include <crtdbg.h> // For detecting memory leaks

#include "graphics.h"

// Function prototypes

int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int);

bool CreateMainWindow(HWND &, HINSTANCE, int);

LRESULT WINAPI WinProc(HWND, UINT, WPARAM, LPARAM);

// Global variable HINSTANCE hinst;

Listing 3.16. Including graphics.h in winmain.cpp. Create a Graphics pointer above the WinMainfunction (Listing 3.17).

// Graphics pointer Graphics *graphics;

Listing 3.17. Graphics pointer.

Create the Graphics object and call the graphics->initialize function after CreateMainWindow (Listing 3.18). GAME_WIDTH, GAME_HEIGHT and FULLSCREEN are defined in constants.h (Listing 3.21).

57 3.8. Our First DirectX Program

//========================================================================

// Starting point for a Windows application // Parameters are:

// hInstance. Handle to the current instance of the application // hPrevInstance. Always NULL, obsolete parameter

// lpCmdLine. Pointer to null-terminated string of command line arguments // nCmdShow. Specifies how the window is to be shown

//========================================================================

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // Check for memory leak if debug build

#if defined(DEBUG) | defined(_DEBUG)

_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );

#endif MSG msg;

HWND hwnd = NULL;

// Create the window

if (!CreateMainWindow(hwnd, hInstance, nCmdShow)) return 1;

try{

// Create Graphics object graphics = new Graphics;

// Initialize Graphics, throws GameError

graphics->initialize(hwnd, GAME_WIDTH, GAME_HEIGHT, FULLSCREEN);

Listing 3.18. Initializing the graphics.

The main message loop is updated to call the graphics->showBackbuffer() function if no Windows messages need processing. When the loop exits, we delete the Graphics object to release the memory that was reserved (Listing 3.19).

// Main message loop

catch(const GameError &err) {

58 3. Introduction to DirectX MessageBox(NULL, err.getMessage(), "Error", MB_OK);

}

catch(...) {

MessageBox(NULL, "Unknown error occurred in game.", "Error", MB_OK);

}

SAFE_DELETE(graphics); // Free memory before exit return 0;

}

Listing 3.19. Updated message loop.

The WinProc and CreateMainWindow functions are unchanged from our Chapter 2 ex-amples. We are now ready to compile and run the program. The result should be a very bright green window. Yay! Let’s take a break and play a few games; we’ve earned it.