DirectX 8. Начинаем работу с DirectX Graphics (Ваткин, Dempski) - страница 20

Getting Started

Assuming you have the DX8 SDK, there are a couple tutorials that present how to create a D3D device and set up a render loop, so I don't want to spend alot of time on that. For the purposes of this article, I'll talk about the tutorial found in [DX8SDK]\samples\Multimedia\Direct3D\Tutorials\Tut01_CreateDevice, although you can add it to anything. To that sample, I'll add the following functions:

>void PostInitialize(float WindowWidth, float WindowHeight) — this function is called by the app after everything else is set up. You've created your device and initialized everything. If you're following along with the Tutorial code, WinMain looks like this:

>…

>if (SUCCEEDED(InitD3D(hWnd))) {

> PostInitialize(200.0f, 200.0f);

> // This is my added line. The values of

> // 200.0f were chosen based on the sizes

> // used in the call to CreateWindow.

> ShowWindow(hWnd, SW_SHOWDEFAULT);

> …

>void Render2D() — This function is called each time you want to render your scene. Again, the Render function of the Tutorial now looks like this:

>VOID Render() {

> if (NULL == g_pd3dDevice) return;

> // Clear the backbuffer to a blue color

> g_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0);

> // Begin the scene

> g_pd3dDevice->BeginScene();

> Render2D(); //My added line…

> // End the scene

> g_pd3dDevice->EndScene();

> // Present the backbuffer contents to the display

> g_pd3dDevice->Present(NULL, NULL, NULL, NULL);

>}

OK, that's our shell of an application. Now for the good stuff…

Setting Up for 2D drawing in D3D

NOTE: This is where we start talking about some of the nasty math involved with D3D. Don't be alarmed - if you want to, you can choose to ignore most of the detailsЕ Most Direct3D drawing is controlled by three matrices: the projection matrix, the world matrix, and the view matrix. The first one we'll talk about is the projection matrix. You can think of the projection matrix as defining the properties of the lens of your camera. In 3D applications, it defines things like perspective, etc. But, we don't want perspective - we are talking about 2D!! So, we can talk about orthogonal projections. To make a long story very short, this allows us to draw in 2D without all the added properties of 3D drawing. To create an orthogonal matrix, we need to call >D3DXMatrixOrthoLH and this will create a matrix for us. The other matrices (view and world) define the position of the camera and the position of the world (or an object in the world). For our 2D drawing, we don't need to move the camera, and we don't want to move the world for now, so we'll use an identity matrix, which basically sets the camera and world in a default position. We can create identity matrices with