[ Pobierz całość w formacie PDF ]
.By defining the location, and a pointthe user is looking at, you can then create an appropriate viewing matrix.This informa-tion will be contained in the variables defined in Listing 12.11.LISTING 12.11 Storage of Viewer Location and Direction1: //----- Rotation position and speed -----//2:3: float rotAngle=g_PI; // current angle4: float rotVel=0.0f; // current velocity of spin5:6: //----- Elevation of viewer and vertical speed -----//7:8: float elevation=350.0f; // current elevation9: float liftVel=0.0f; // rate of rise / decentcontinues 18 1634xCH12 11/13/99 11:02 AM Page 226226 Hour 12LISTING 12.11 continued10:11: //----- Define distance of viewer orbit from target ------//12:13: #define ORBIT 800.0fNext, you will create an array of pointers that will reference a set of CCube objects thatyou will create.The blocks will be spaced along a two dimensional grid, four blockswide and four blocks deep.The size specification and array declaration shown in Listing12.12 will contain these objects.LISTING 12.12 Object Storage for an Array of Cubes1: //------ Storage for Cube Objects -----//2:3: #define NUM_ROWS 44: #define NUM_COLUMNS 45:6: CCube *cubes[NUM_ROWS][NUM_COLUMNS];Finally, you will define a list of error strings to describe possible failure modes, as youhave performed in your previous applications and shown in Listing 12.13.You will alsocreate a list of function prototypes so that you do not have to depend on a specific orderof function definition within the source code.LISTING 12.13 Error Strings and Function Prototypes1: //------ Error Return String ------//2:3: const char *ErrStr=NULL;4:5: //------ Error Messages ------//6:7: const char Err_Reg_Class[] =  Error Registering Window Class ;8: const char Err_Create_Win[] =  Error Creating Window ;9: const char Err_DirectDrawCreate[] =  DirectDrawCreate FAILED ;10: const char Err_Query[] =  QueryInterface FAILED ;11: const char Err_Coop[] =  SetCooperativeLevel FAILED ;12: const char Err_CreateSurf[] =  CreateSurface FAILED ;13: const char Err_DispMode[] =  Error Setting Display Mode ;14: const char Err_Device[] =  Device Creation Failed ;15: const char Err_SetView[] =  Viewport settings failed ;16:17: //------ Function Prototypes -----//18:19: void Cleanup(); 18 1634xCH12 11/13/99 11:02 AM Page 227Creating Our First Direct3D Application 22720: void create_objects();21: static BOOL Init(HINSTANCE hInstance, int nCmdShow);22: BOOL init_d3d();23: BOOL init_ddraw(HWND hWnd);24: void render_frame(float elapsed);Initializing the ApplicationThe first function we will write is our program initialization.We will begin as we have inpast applications by generating a window and initializing DirectDraw.Because our ini-tialization function is growing, we will split the DirectDraw initialization into a separatefunction, init_ddraw().This portion of the Init() function is shown in Listing 12.14.LISTING 12.14 The Initialization of the Application Window and DirectDraw1: //------ Function to Initialize DirectDraw and the Application ------//2:3: static BOOL Init(HINSTANCE hInstance, int nCmdShow)4: {5: WNDCLASS wc;6:7: // Set up and register window class8:9: wc.style = CS_HREDRAW | CS_VREDRAW;10: wc.lpfnWndProc = (WNDPROC) WindowProc;11: wc.cbClsExtra = 0;12: wc.cbWndExtra = sizeof(DWORD);13: wc.hInstance = hInstance;1214: wc.hIcon = NULL;15: wc.hCursor = LoadCursor(NULL, IDC_ARROW);16: wc.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);17: wc.lpszMenuName = NULL;18: wc.lpszClassName = szClass;19: if (!RegisterClass(&wc)) {20: ErrStr=Err_Reg_Class;21: return FALSE;22: }23:24: // Get dimensions of display25:26: int ScreenWidth = GetSystemMetrics(SM_CXSCREEN);27: int ScreenHeight = GetSystemMetrics(SM_CYSCREEN);28:29: // Create a window and display30:31: HWND hWnd;32:continues 18 1634xCH12 11/13/99 11:02 AM Page 228228 Hour 12LISTING 12.14 continued33: hWnd = CreateWindow(szClass, // class34: szCaption, // caption35: WS_VISIBLE|WS_POPUP, // style36: 0, // left37: 0, // top38: ScreenWidth, // width39: ScreenHeight, // height40: NULL, // parent window41: NULL, // menu42: hInstance, // instance43: NULL); // parms44: if (!hWnd) {45: ErrStr=Err_Create_Win;46: return FALSE;47: }48: ShowWindow(hWnd, nCmdShow);49: UpdateWindow(hWnd);50:51: // initialize DirectDraw52:53: if (!init_ddraw(hWnd)) return FALSE;To complete the initialization, we must initialize Direct3D and create the objects that willexist in our scene.These tasks will be performed in separate functions, which will becalled from the Init() functions as shown in Listing 12.15.LISTING 12.15 Initialization of Direct3D and the 3D Scene1: // initialize Direct3D2:3: if (!init_d3d()) return FALSE;4:5: // create 3D objects6:7: create_objects();8:9: // return success to caller10:11: return TRUE;12: }Initializing DirectDraw for Use with Direct3DTo use DirectDraw with Direct3D, one simple consideration must be made.For theDirectDraw surfaces to be compatible with the 3D device, the primary surface must becreated with the DDSCAPS_3DDEVICE flag. 18 1634xCH12 11/13/99 11:02 AM Page 229Creating Our First Direct3D Application 229The init_ddraw() function that we call from Init() is listed in Listing 12.16.With theexception of being moved to a separate function, the only modification is the addition ofthe proper flag to inform DirectDraw that it will be used by Direct3D.Listing 12.16 Initializing DirectDraw to be Compatible with Direct3D1: BOOL init_ddraw(HWND hWnd)2: {3: // Create the main DirectDraw object4:5: HRESULT ddrval = DirectDrawCreateEx(NULL, (void**)&lpDD,IID_IDirectDraw7, NULL);6: if (ddrval != DD_OK) {7: ErrStr=Err_DirectDrawCreate;8: return FALSE;9: }10:11: // Set our cooperative level12:13: ddrval = lpDD->SetCooperativeLevel(hWnd,DDSCL_EXCLUSIVE | DDSCL_FULLSCREEN );14: if (ddrval != DD_OK) {15: ErrStr=Err_Coop;16: return FALSE;17: }18:19: // Set the display mode20:21: ddrval = lpDD->SetDisplayMode( 640, 480, 16, 0, 0);22: if (ddrval !=DD_OK) {1223: ErrStr=Err_DispMode;24: return FALSE;25: }26:27: // Create the primary surface with 1 back buffer28:29: DDSURFACEDESC2 ddsd;30: ZeroMemory(&ddsd,sizeof(ddsd));31: ddsd.dwSize = sizeof( ddsd );32: ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT;33: ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE |34: DDSCAPS_FLIP | DDSCAPS_3DDEVICE |35: DDSCAPS_COMPLEX;36: ddsd [ Pobierz całość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • czarkowski.pev.pl
  •