The InitApp Function

The initialization function in Helworld.c creates the window class and main application window, just as in most Windows applications. After that, however, it does some work that is unique to applications using DirectDraw and Direct3D.

Now InitApp retrieves the current display's bits per pixel. This information is used to help set the rendering quality for the application. For more information, see Setting the Render State.

InitApp then calls the locally defined EnumDrivers function to determine what Direct3D drivers are available and to choose one. For more information about enumerating drivers, see Enumerating Device Drivers.

Next, the code calls the Direct3DRMCreate function to create an IDirect3DRM interface. It uses this interface in the calls to IDirect3DRM::CreateFrame and IDirect3DRMFrame::SetPosition that create the scene and camera frames, and position the camera in the scene.

A DirectDrawClipper object makes it simple to manage the clipping planes that control which parts of a 3-D scene are visible. Helworld.c calls the DirectDrawCreateClipper function to create an interface, and then uses the IDirectDrawClipper::SetHWnd method to set the window handle that obtains the clipping information.

Now the InitApp function calls the locally defined CreateDevAndView function to create the Direct3D device and viewport. For more information about this function, see Creating the Device and Viewport.

When the entire supporting structure of a Direct3D application has been put into place, the details of the 3-D scene can be constructed. The MyScene function does this work. For more information about MyScene, see Creating the Device and Viewport.

Finally, just as in a standard initialization function, InitApp shows and updates the window.

/////////////////////////////////////////////////////////////////////

//

// InitApp

// Creates window and initializes all objects necessary to begin

// rendering.

//

/////////////////////////////////////////////////////////////////////

static BOOL

InitApp(HINSTANCE this_inst, int cmdshow)

{

HWND win;

HDC hdc;

WNDCLASS wc;

RECT rc;

// Set up and register the window class.

wc.style = CS_HREDRAW | CS_VREDRAW;

wc.lpfnWndProc = WindowProc;

wc.cbClsExtra = 0;

wc.cbWndExtra = sizeof(DWORD);

wc.hInstance = this_inst;

wc.hIcon = LoadIcon(this_inst, "AppIcon");

wc.hCursor = LoadCursor(NULL, IDC_ARROW);

wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);

wc.lpszMenuName = NULL;

wc.lpszClassName = "D3DRM Example";

if (!RegisterClass(&wc))

return FALSE;

// Initialize the global variables.

memset(&myglobs, 0, sizeof(myglobs));

// Create the window.

win =

CreateWindow

( "D3DRM Example", // Class

"Hello World (Direct3DRM)", // Title bar

WS_VISIBLE | WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU |

WS_MINIMIZEBOX | WS_MAXIMIZEBOX,

CW_USEDEFAULT, // Init. x pos

CW_USEDEFAULT, // Init. y pos

300, // Init. x size

300, // Init. y size

NULL, // Parent window

NULL, // Menu handle

this_inst, // Program handle

NULL // Create parms

);

if (!win)

return FALSE;

// Record the current display bits-per-pixel.

hdc = GetDC(win);

myglobs.BPP = GetDeviceCaps(hdc, BITSPIXEL);

ReleaseDC(win, hdc);

// Enumerate the D3D drivers and select one.

if (!EnumDrivers(win))

return FALSE;

// Create the D3DRM object and the D3DRM window object.

lpD3DRM = NULL;

Direct3DRMCreate(&lpD3DRM);

// Create the master scene frame and camera frame.

lpD3DRM->lpVtbl->CreateFrame(lpD3DRM, NULL, &myglobs.scene);

lpD3DRM->lpVtbl->CreateFrame(lpD3DRM, myglobs.scene,

&myglobs.camera);

myglobs.camera->lpVtbl->SetPosition(myglobs.camera, myglobs.scene,

D3DVAL(0.0), D3DVAL(0.0), D3DVAL(0.0));

// Create a DirectDrawClipper object and associate the

// window with it.

DirectDrawCreateClipper(0, &lpDDClipper, NULL);

lpDDClipper->lpVtbl->SetHWnd(lpDDClipper, 0, win);

// Create the D3DRM device by using the selected D3D driver.

GetClientRect(win, &rc);

if (!CreateDevAndView(lpDDClipper, myglobs.CurrDriver, rc.right,

rc.bottom)) {

return FALSE;

}

// Create the scene to be rendered.

if (!MyScene(myglobs.dev, myglobs.view, myglobs.scene,

myglobs.camera))

return FALSE;

myglobs.bInitialized = TRUE; // Initialization completed

// Display the window.

ShowWindow(win, cmdshow);

UpdateWindow(win);

return TRUE;

}