//------------------------------------------------------------ // К проекту необходимо добавить следующие библиотечные файлы: // 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; DWORD 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_SysMouse,&didev,NULL); didev->SetDataFormat(&c_dfDIMouse); didev->SetCooperativeLevel(hWnd,DISCL_FOREGROUND | DISCL_NONEXCLUSIVE); didev->Acquire(); //---------------------------------------------- // конец инициализации DirectInput //---------------------------------------------- // вершины квадрата vertex vertices[] = { { -2.5, -2.5, 0, 0xff000000}, { -2.5, 2.5, 0, 0xff000000}, { 2.5, -2.5, 0, 0xff000000}, { -2.5, 2.5, 0, 0xff000000}, { 2.5, 2.5, 0, 0xff000000}, { 2.5, -2.5, 0, 0xff000000} }; MSG msg; void* vb_vertices; HRESULT hr = NULL; DIMOUSESTATE dims; ZeroMemory(&dims,sizeof(dims)); 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=100; matWorld._44=1; 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=0; 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); //----------------------------------- // Ввод с мышки //----------------------------------- didev->GetDeviceState(sizeof(DIMOUSESTATE),&dims); if (dims.rgbButtons[0] & 0x80) for (int i = 0; i < 6; i++) vertices[i].color = 0xffff0000; if (dims.rgbButtons[1] & 0x80) for (int i = 0; i < 6; i++) vertices[i].color = 0xff00ff00; if (dims.rgbButtons[2] & 0x80) for (int i = 0; i < 6; i++) vertices[i].color = 0xff0000ff; for (int i = 0; i < 6; i++) { vertices[i].x += dims.lX; vertices[i].y -= dims.lY; } matWorld._43 += dims.lZ/10; dev->SetTransform(D3DTS_WORLD, &matWorld); //----------------------------------- // Конец ввода с мышки //----------------------------------- vb->Lock(0,sizeof(vertices),(void**)&vb_vertices,0); memcpy(vb_vertices,vertices,sizeof(vertices)); vb->Unlock(); dev->BeginScene(); dev->SetStreamSource(0,vb,0,sizeof(vertex)); dev->SetFVF(D3DFVF_XYZ|D3DFVF_DIFFUSE); 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); }