Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "dxm.h"
- void DXM::initD3D(HWND hwnd)
- {
- //Create device & swap chain
- DXGI_SWAP_CHAIN_DESC scd;
- memset(&scd, 0, sizeof(DXGI_SWAP_CHAIN_DESC));
- scd.BufferCount = 1;
- scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
- scd.BufferDesc.Width = 800;
- scd.BufferDesc.Height = 600;
- scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
- scd.OutputWindow = hwnd;
- scd.SampleDesc.Count = 1;
- scd.SampleDesc.Quality = 0;
- scd.Windowed = TRUE;
- D3D10CreateDeviceAndSwapChain(NULL, D3D10_DRIVER_TYPE_HARDWARE,
- NULL, 0, D3D10_SDK_VERSION, &scd, &swapchain, &device);
- ID3D10Texture2D *backBuffer;
- swapchain->GetBuffer(0, __uuidof(ID3D10Texture2D),
- (LPVOID*)&backBuffer);
- device->CreateRenderTargetView(backBuffer, NULL, &rtv);
- backBuffer->Release();
- device->OMSetRenderTargets(1, &rtv, NULL);
- //Viewport
- D3D10_VIEWPORT viewport;
- memset(&viewport, 0, sizeof(D3D10_VIEWPORT));
- viewport.TopLeftX = 0;
- viewport.TopLeftY = 0;
- viewport.Width = 800;
- viewport.Height = 600;
- device->RSSetViewports(1, &viewport);
- //Create effect
- D3DX10CreateEffectFromFile(L"effect1.fx", NULL, NULL, "fx_4_0",
- D3D10_SHADER_ENABLE_STRICTNESS, 0, device, 0, 0, &effect, 0, 0);
- technique = effect->GetTechniqueByName("Render");
- //Define input layout
- D3D10_INPUT_ELEMENT_DESC elementDesc[] = {
- {"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0},
- {"COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 0, 12, D3D10_INPUT_PER_VERTEX_DATA, 0}
- };
- UINT numElements = arrysize(elementDesc);
- //Create input layout
- ID3D10InputLayout *vertexLayout = NULL;
- D3D10_PASS_DESC passdesc;
- technique->GetPassByIndex(0)->GetDesc(&passdesc);
- device->CreateInputLayout(elementDesc, numElements, passdesc.pIAInputSignature,
- passdesc.IAInputSignatureSize, &vertexLayout);
- device->IASetInputLayout(vertexLayout);
- //Create vertex buffer
- cvertex verts[4];
- verts[0].position = D3DXVECTOR3(-1.0f, -1.0f, 0.0f);
- verts[0].color = 0xffff0000;
- verts[1].position = D3DXVECTOR3(-1.0f, 1.0f, 0.0f);
- verts[1].color = 0xff0000ff;
- verts[2].position = D3DXVECTOR3(1.0f, -1.0f, 0.0f);
- verts[2].color = 0xff00ff00;
- verts[3].position = D3DXVECTOR3(1.0f, 1.0f, 0.0f);
- verts[3].color = 0xffffffff;
- //Fill buffer description
- D3D10_BUFFER_DESC bd;
- bd.Usage = D3D10_USAGE_DEFAULT;
- bd.ByteWidth = sizeof(cvertex)*4;
- bd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
- bd.CPUAccessFlags = 0;
- bd.MiscFlags = 0;
- D3D10_SUBRESOURCE_DATA initData;
- initData.pSysMem = verts;
- device->CreateBuffer(&bd, &initData, &vertexBuffer);
- //Set vertex buffer
- UINT stride = sizeof(cvertex);
- UINT offset = 0;
- device->IASetVertexBuffers(0, 1, &vertexBuffer, &stride, &offset);
- //Set prim topology
- device->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
- }
- void DXM::render()
- {
- device->ClearRenderTargetView(rtv, D3DXCOLOR(0.0f, 0.2f, 0.4f, 1.0f));
- D3D10_TECHNIQUE_DESC techDesc;
- technique->GetDesc(&techDesc);
- for (UINT p = 0; p < techDesc.Passes; ++p)
- {
- technique->GetPassByIndex(p)->Apply(0);
- device->Draw(4, 0);
- }
- swapchain->Present(0, 0);
- }
- void DXM::cleanD3D()
- {
- swapchain->SetFullscreenState(FALSE, NULL);
- swapchain->Release();
- rtv->Release();
- effect->Release();
- vertexBuffer->Release();
- device->Release();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement