//------------------------------------------------------------ // К проекту необходимо добавить следующие библиотечные файлы: // d3d9.lib // dinput8.lib // dxguid.lib //------------------------------------------------------------ #include #include #include LRESULT __stdcall WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); struct vertex { float x,y,z; unsigned long color; }; int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASS wc; wc.style = CS_OWNDC; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hbrBackground = (HBRUSH)(6); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hInstance = hInstance; wc.lpszClassName = L"class"; wc.lpszMenuName = NULL; RegisterClass(&wc); HWND hWnd = CreateWindow(L"class",L"программа", WS_OVERLAPPEDWINDOW,200,100, 500,500,NULL,NULL,hInstance,NULL); ShowWindow(hWnd,nCmdShow); UpdateWindow(hWnd); //--------------------------------- // Инициализация Direct3D //--------------------------------- IDirect3D9* d3d = NULL; IDirect3DDevice9* dev = NULL; IDirect3DVertexBuffer9* vb = NULL; d3d = Direct3DCreate9(D3D_SDK_VERSION); D3DPRESENT_PARAMETERS pp; ZeroMemory(&pp,sizeof(pp)); pp.BackBufferWidth = 500; pp.BackBufferHeight = 500; pp.BackBufferFormat = D3DFMT_UNKNOWN; pp.BackBufferCount = 1; pp.SwapEffect = D3DSWAPEFFECT_DISCARD; pp.hDeviceWindow = hWnd; pp.Windowed = true; d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,hWnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, &pp, &dev); dev->CreateVertexBuffer( 6* sizeof(vertex), D3DUSAGE_WRITEONLY, D3DFVF_XYZ|D3DFVF_DIFFUSE, D3DPOOL_DEFAULT, &vb,NULL); dev->SetRenderState(D3DRS_LIGHTING, false); //---------------------------------------------- // Инициализация DirectInput //---------------------------------------------- IDirectInput8* di; IDirectInputDevice8* didev; DirectInput8Create(hInstance,DIRECTINPUT_VERSION, IID_IDirectInput8,(void**)&di, NULL); di->CreateDevice(GUID_SysKeyboard,&didev,NULL); didev->SetDataFormat(&c_dfDIKeyboard); didev->SetCooperativeLevel(hWnd,DISCL_FOREGROUND | DISCL_NONEXCLUSIVE); didev->Acquire(); //---------------------------------------------- // конец инициализации DirectInput //---------------------------------------------- // вершины квадрата vertex vertices[] = { { -2.5, -2.5, 10, 0xff000000}, { -2.5, 2.5, 10, 0xff000000}, { 2.5, -2.5, 10, 0xff000000}, { -2.5, 2.5, 10, 0xff000000}, { 2.5, 2.5, 10, 0xff000000}, { 2.5, -2.5, 10, 0xff000000} }; MSG msg; void* vb_vertices; int dx=0,dy=0; // Переменные с помощью которых передвигается квадрат. char buffer[256]; // Буфер. Хранит состояние клавиатуры. HRESULT hr; D3DMATRIX matWorld; matWorld._11=1; matWorld._12=0; matWorld._13=0; matWorld._14=0; matWorld._21=0; matWorld._22=1; matWorld._23=0; matWorld._24=0; matWorld._31=0; matWorld._32=0; matWorld._33=1; matWorld._34=0; matWorld._41=0; matWorld._42=0; matWorld._43=0; matWorld._44=1; dev->SetTransform(D3DTS_WORLD, &matWorld); D3DMATRIX matCam; matCam._11=1; matCam._12=0; matCam._13=0; matCam._14=0; matCam._21=0; matCam._22=1; matCam._23=0; matCam._24=0; matCam._31=0; matCam._32=0; matCam._33=1; matCam._34=0; matCam._41=0; matCam._42=0; matCam._43=10; matCam._44=1; dev->SetTransform(D3DTS_VIEW, &matCam); D3DMATRIX matProj; matProj._11=1; matProj._12=0; matProj._13=0; matProj._14=0; matProj._21=0; matProj._22=1; matProj._23=0; matProj._24=0; matProj._31=0; matProj._32=0; matProj._33=1; matProj._34=1; matProj._41=0; matProj._42=0; matProj._43=-1; matProj._44=0; dev->SetTransform( D3DTS_PROJECTION, &matProj ); while (1) { if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { if (msg.message == WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } dev->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(255,255,255),1.0f,0); //----------------------------------- // Ввод с клавиатуры. //----------------------------------- dx = 0; dy = 0; hr = didev->GetDeviceState(sizeof(buffer),buffer); if FAILED(hr) return 0; if (buffer[DIK_RIGHT] & 0x80) dx += 1; else if(buffer[DIK_LEFT] & 0x80) dx -= 1; if (buffer[DIK_UP]) dy+=1; else if (buffer[DIK_DOWN]) dy-=1; // Смена координат вершин квадрата. for (int i = 0; i < 6; i++) { vertices[i].x += dx; vertices[i].y += dy; } //----------------------------------- // Конец ввода с клавиатуры //----------------------------------- vb->Lock(0,sizeof(vertices),(void**)&vb_vertices,0); memcpy(vb_vertices,vertices,sizeof(vertices)); vb->Unlock(); dev->SetStreamSource(0,vb,0,sizeof(vertex)); dev->SetFVF(D3DFVF_XYZ|D3DFVF_DIFFUSE); dev->BeginScene(); dev->DrawPrimitive(D3DPT_TRIANGLELIST,0,2); dev->EndScene(); dev->Present(NULL,NULL,NULL,NULL); } if (didev != 0) { didev->Unacquire(); didev->Release(); } if (di != NULL) di->Release(); if (vb != NULL) vb->Release(); if (d3d != NULL) d3d->Release(); if (dev != NULL) dev->Release(); UnregisterClass(L"class",hInstance); return 0; } LRESULT __stdcall WndProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch (msg) { case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hWnd,msg,wParam,lParam); }