DirectX. Использование возможностей по выводу графики

контрольная работа

Исходный текст отлаженной программы

#include <d3d9.h>

#include <d3dx9.h>

#include <strsafe.h>

struct C_VERTEX

{

FLOAT x, y, z, rhw;

DWORD color;

};

#define D3DFVF_C_VERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)

LPDIRECT3D9 g_pD3D = NULL;

LPDIRECT3DDEVICE9 g_pd3dDevice = NULL;

LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL;

LPD3DXFONT g_Font = NULL;

C_VERTEX Vertices[] =

{

{ 150.0f, 50.0f, 0.5f, 1.0f, 0xff00bdff, },

{ 250.0f, 150.0f, 0.5f, 0.0f, 0xff00ff00, },

{ 50.0f, 150.0f, 0.5f, 0.0f, 0xff00ffff, },

};

int WINDOW_WIDTH = 400;

int WINDOW_HEIGHT = 300;

int textx = 10;

int texty = 10;

bool res = false;

HRESULT InitD3D( HWND hWnd )

{

if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )

return E_FAIL;

D3DPRESENT_PARAMETERS d3dpp;

ZeroMemory( &d3dpp, sizeof( d3dpp ) );

d3dpp.Windowed = TRUE;

d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;

d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;

if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,

D3DCREATE_SOFTWARE_VERTEXPROCESSING,

&d3dpp, &g_pd3dDevice ) ) )

{

return E_FAIL;

}

SUCCEEDED(D3DXCreateFont(g_pd3dDevice, 15, 0, FW_BOLD, 1, 0, DEFAULT_CHARSET,

OUT_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH, L"Arial", &g_Font));

return S_OK;

}

void DrawString()

{

RECT FontPos = { textx, texty, textx + 100, texty + 40 };

if (res)

g_Font->DrawText(NULL, L"Point in the polygon.", -1, &FontPos, DT_NOCLIP, D3DCOLOR_ARGB( 255, 255, 0, 0 ));

else

g_Font->DrawText(NULL, L"Point out of the polygon.", -1, &FontPos, DT_NOCLIP, D3DCOLOR_ARGB( 255, 0, 0, 0 ));

}

bool IsPointInPolygon(int x, int y)

{

bool result = false;

int j = 2;

for (int i = 0; i < 3; i++)

{

if ((Vertices[i].y < y

&& Vertices[j].y >= y)

|| (Vertices[j].y < y

&& Vertices[i].y >= y))

{

if (Vertices[i].x + (y - Vertices[i].y) / (Vertices[j].y - Vertices[i].y) * (Vertices[j].x - Vertices[i].x) < x)

{

result = !result;

}

}

j = i;

}

return result;

}

HRESULT InitPolygon()

{

if( FAILED( g_pd3dDevice->CreateVertexBuffer( 3 * sizeof( C_VERTEX ),

0, D3DFVF_C_VERTEX,

D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )

{

return E_FAIL;

}

VOID* pVertices;

if( FAILED( g_pVB->Lock( 0, sizeof( Vertices ), ( void** )&pVertices, 0 ) ) )

return E_FAIL;

memcpy( pVertices, Vertices, sizeof( Vertices ) );

g_pVB->Unlock();

return S_OK;

}

VOID Cleanup()

{

if( g_pd3dDevice != NULL )

g_pd3dDevice->Release();

if( g_pD3D != NULL )

g_pD3D->Release();

if(g_Font)

g_Font->Release();

}

VOID Render()

{

if( NULL == g_pd3dDevice )

return;

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

if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )

{

g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof( C_VERTEX ) );

g_pd3dDevice->SetFVF( D3DFVF_C_VERTEX );

g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 1 );

DrawString();

g_pd3dDevice->EndScene();

}

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

}

LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )

{

switch( msg )

{

case WM_DESTROY:

Cleanup();

PostQuitMessage( 0 );

return 0;

case WM_PAINT:

Render();

ValidateRect( hWnd, NULL );

return 0;

case WM_MOUSEMOVE:

int x = LOWORD(lParam);

inty = HIWORD(lParam);

res = IsPointInPolygon(x, y);

Render();

break;

}

return DefWindowProc( hWnd, msg, wParam, lParam );

}

INT WINAPI wWinMain( HINSTANCE hInst, HINSTANCE, LPWSTR, INT )

{

WNDCLASSEX wc =

{

sizeof( WNDCLASSEX ), CS_CLASSDC, MsgProc, 0L, 0L,

GetModuleHandle( NULL ), NULL, NULL, NULL, NULL,

L"C_WORK", NULL

};

RegisterClassEx( &wc );

HWND hWnd = CreateWindow( L"C_WORK", L"POINT IN POLYGON",

WS_OVERLAPPEDWINDOW, 100, 100, WINDOW_WIDTH, WINDOW_HEIGHT,

NULL, NULL, wc.hInstance, NULL );

if( SUCCEEDED( InitD3D( hWnd ) ) )

{

if (SUCCEEDED( InitPolygon()))

{

ShowWindow( hWnd, SW_SHOWDEFAULT );

UpdateWindow( hWnd );

MSG msg;

while( GetMessage( &msg, NULL, 0, 0 ) )

{

TranslateMessage( &msg );

DispatchMessage( &msg );

}

}

}

UnregisterClass( L"C_WORK", wc.hInstance );

return 0;

}

графика функция программа

Делись добром ;)