Question
Writing a program using C++ and DirectX11 and need so solve this error: Initial value of reference to a non-const must be an Ivalue The
Writing a program using C++ and DirectX11 and need so solve this error: Initial value of reference to a non-const must be an Ivalue
The line of code that shows the error are these two XMFLOATs: m_Sprites[1].SetPosition (XMFLOAT2(1200.0f, 600.0f)); and m_Sprites[0].SetScale(XMFLOAT2(0.5, 0.5));
Full section of code (this is not the entire code, as putting all that here would be much too long):
bool CDemoSprites::LoadContent() { // Compile vertex shader ID3DBlob* pVSBuffer = NULL; bool res = CompileShader(L"ShaderTexture.fx", "VS_Main", "vs_4_0", &pVSBuffer); if (res == false) { ::MessageBox(m_hWnd, L"Unable to load vertex shader", L"ERROR", MB_OK); return false; }
// Create vertex shader HRESULT hr; hr = m_pD3DDevice->CreateVertexShader( pVSBuffer->GetBufferPointer(), pVSBuffer->GetBufferSize(), 0, &m_pVS); if (FAILED(hr)) { if (pVSBuffer) pVSBuffer->Release(); return false; }
// Define input layout D3D11_INPUT_ELEMENT_DESC shaderInputLayout[] = { { "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, { "TEXCOORD", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 12, D3D11_INPUT_PER_VERTEX_DATA, 0 } }; UINT numLayoutElements = ARRAYSIZE(shaderInputLayout);
// Create input layout hr = m_pD3DDevice->CreateInputLayout( shaderInputLayout, numLayoutElements, pVSBuffer->GetBufferPointer(), pVSBuffer->GetBufferSize(), &m_pInputLayout); if (FAILED(hr)) { return false; }
// Release VS buffer pVSBuffer->Release(); pVSBuffer = NULL;
// Compile pixel shader ID3DBlob* pPSBuffer = NULL; res = CompileShader(L"ShaderTexture.fx", "PS_Main", "ps_4_0", &pPSBuffer); if (res == false) { ::MessageBox(m_hWnd, L"Unable to load pixel shader", L"ERROR", MB_OK); return false; }
// Create pixel shader hr = m_pD3DDevice->CreatePixelShader( pPSBuffer->GetBufferPointer(), pPSBuffer->GetBufferSize(), 0, &m_pPS); if (FAILED(hr)) { return false; }
// Cleanup PS buffer pPSBuffer->Release(); pPSBuffer = NULL;
// Load texture hr = ::D3DX11CreateShaderResourceViewFromFile( m_pD3DDevice, L"ball.dds", 0, 0, &m_pColorMap, 0); if (FAILED(hr)) { ::MessageBox(m_hWnd, L"Unable to load texture", L"ERROR", MB_OK); return false; }
// Texture sampler D3D11_SAMPLER_DESC textureDesc; ::ZeroMemory(&textureDesc, sizeof(textureDesc)); textureDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP; textureDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP; textureDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP; textureDesc.ComparisonFunc = D3D11_COMPARISON_NEVER; textureDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR; textureDesc.MaxLOD = D3D11_FLOAT32_MAX; hr = m_pD3DDevice->CreateSamplerState(&textureDesc, &m_pColorMapSampler); if (FAILED(hr)) { ::MessageBox(m_hWnd, L"Unable to create texture sampler state", L"ERROR", MB_OK); return false; }
// Get texture size ID3D11Resource* pColorMapRes; m_pColorMap->GetResource(&pColorMapRes); D3D11_TEXTURE2D_DESC colorMapDesc; ((ID3D11Texture2D*)pColorMapRes)->GetDesc(&colorMapDesc); pColorMapRes->Release(); float halfWidth = (float)colorMapDesc.Width / 2.0f; float halfHeight = (float)colorMapDesc.Height / 2.0f;
// Define triangle Vertex vertices[] = { { XMFLOAT3(halfWidth, halfHeight, 1.0f), XMFLOAT2(1.0f, 0.0f) }, { XMFLOAT3(halfWidth, -halfHeight, 1.0f), XMFLOAT2(1.0f, 1.0f) }, { XMFLOAT3(-halfWidth, -halfHeight, 1.0f), XMFLOAT2(0.0f, 1.0f) },
{ XMFLOAT3(-halfWidth, -halfHeight, 1.0f), XMFLOAT2(0.0f, 1.0f) }, { XMFLOAT3(-halfWidth, halfHeight, 1.0f), XMFLOAT2(0.0f, 0.0f) }, { XMFLOAT3(halfWidth, halfHeight, 1.0f), XMFLOAT2(1.0f, 0.0f) }, };
// Vertex description D3D11_BUFFER_DESC vertexDesc; ::ZeroMemory(&vertexDesc, sizeof(vertexDesc)); vertexDesc.Usage = D3D11_USAGE_DEFAULT; vertexDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; vertexDesc.ByteWidth = sizeof(Vertex) * 6;
// Resource data D3D11_SUBRESOURCE_DATA resourceData; ZeroMemory(&resourceData, sizeof(resourceData)); resourceData.pSysMem = vertices;
// Create vertex buffer hr = m_pD3DDevice->CreateBuffer(&vertexDesc, &resourceData, &m_pVertexBuffer); if (FAILED(hr)) { return false; }
// Create world matrix buffer D3D11_BUFFER_DESC constDesc; ::ZeroMemory(&constDesc, sizeof(constDesc)); constDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; constDesc.ByteWidth = sizeof(XMMATRIX); constDesc.Usage = D3D11_USAGE_DEFAULT; hr = m_pD3DDevice->CreateBuffer(&constDesc, 0, &m_MvpCB); if (FAILED(hr)) { return false; }
// Position and scale sprites
m_Sprites[1].SetPosition (XMFLOAT2(1200.0f, 600.0f)); m_Sprites[0].SetScale(XMFLOAT2(0.5, 0.5));
// Make world matrix XMMATRIX view = XMMatrixIdentity(); XMMATRIX projection = XMMatrixOrthographicOffCenterLH( 0.0f, 800.0f, 0.0f, 600.0f, 0.1f, 100.0f); m_vpMatrix = XMMatrixMultiply(view, projection);
// Set blend description D3D11_BLEND_DESC blendDesc; ::ZeroMemory(&blendDesc, sizeof(blendDesc)); blendDesc.RenderTarget[0].BlendEnable = TRUE; blendDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD; blendDesc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA; blendDesc.RenderTarget[0].DestBlend = D3D11_BLEND_ONE; blendDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD; blendDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ZERO; blendDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO; blendDesc.RenderTarget[0].RenderTargetWriteMask = 0x0F; float blendFactor[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
// Set blend state m_pD3DDevice->CreateBlendState(&blendDesc, &m_pAlphaBlendState); m_pD3DContext->OMSetBlendState(m_pAlphaBlendState, blendFactor, 0xFFFFFFFF);
return true; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started