Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Comment every line of code to express your understanding of what this line of co

ID: 3835151 • Letter: C

Question

Comment every line of code to express your understanding of what this line of code is intended to do

#include"KeyboardDemo.h"
//#include<xnamath.h>
#include<DirectXMath.h>

using namespace DirectX;

struct VertexPos
{
XMFLOAT3 pos;
};


KeyboardDemo::KeyboardDemo( ) : customColorVS_( 0 ), customColorPS_( 0 ),
inputLayout_( 0 ), vertexBuffer_( 0 ),
                               colorCB_( 0 ), selectedColor_( 0 )
{

}


KeyboardDemo::~KeyboardDemo( )
{

}


bool KeyboardDemo::LoadContent( )
{
ID3DBlob* vsBuffer = 0;

bool compileResult = CompileD3DShader( L"CustomColor.hlsl", "VS_Main", "vs_4_0", &vsBuffer );

if( compileResult == false )
{
MessageBox( 0, "Error loading vertex shader!", "Compile Error", MB_OK );
return false;
}

HRESULT d3dResult;

d3dResult = d3dDevice_->CreateVertexShader( vsBuffer->GetBufferPointer( ),
vsBuffer->GetBufferSize( ), 0, &customColorVS_ );

if( FAILED( d3dResult ) )
{
if( vsBuffer )
vsBuffer->Release( );

return false;
}

D3D11_INPUT_ELEMENT_DESC solidColorLayout[] =
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }
};

unsigned int totalLayoutElements = ARRAYSIZE( solidColorLayout );

d3dResult = d3dDevice_->CreateInputLayout( solidColorLayout, totalLayoutElements,
vsBuffer->GetBufferPointer( ), vsBuffer->GetBufferSize( ), &inputLayout_ );

vsBuffer->Release( );

if( FAILED( d3dResult ) )
{
return false;
}

ID3DBlob* psBuffer = 0;

compileResult = CompileD3DShader( L"CustomColor.hlsl", "PS_Main", "ps_4_0", &psBuffer );

if( compileResult == false )
{
MessageBox( 0, "Error loading pixel shader!", "Compile Error", MB_OK );
return false;
}

d3dResult = d3dDevice_->CreatePixelShader( psBuffer->GetBufferPointer( ),
psBuffer->GetBufferSize( ), 0, &customColorPS_ );

psBuffer->Release( );

if( FAILED( d3dResult ) )
{
return false;
}

VertexPos vertices[] =
{
XMFLOAT3( 0.5f, 0.5f, 0.5f ),
XMFLOAT3( 0.5f, -0.5f, 0.5f ),
XMFLOAT3( -0.5f, -0.5f, 0.5f )
};

D3D11_BUFFER_DESC vertexDesc;
ZeroMemory( &vertexDesc, sizeof( vertexDesc ) );
vertexDesc.Usage = D3D11_USAGE_DEFAULT;
vertexDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vertexDesc.ByteWidth = sizeof( VertexPos ) * 3;

D3D11_SUBRESOURCE_DATA resourceData;
ZeroMemory( &resourceData, sizeof( resourceData ) );
resourceData.pSysMem = vertices;

d3dResult = d3dDevice_->CreateBuffer( &vertexDesc, &resourceData, &vertexBuffer_ );

if( FAILED( d3dResult ) )
{
return false;
}


   D3D11_BUFFER_DESC constDesc;
   ZeroMemory( &constDesc, sizeof( constDesc ) );
   constDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
   constDesc.ByteWidth = sizeof( XMFLOAT4 );
   constDesc.Usage = D3D11_USAGE_DEFAULT;

   d3dResult = d3dDevice_->CreateBuffer( &constDesc, 0, &colorCB_ );

   if( FAILED( d3dResult ) )
{
return false;
}

return true;
}


void KeyboardDemo::UnloadContent( )
{
if( customColorVS_ ) customColorVS_->Release( );
if( customColorPS_ ) customColorPS_->Release( );
if( inputLayout_ ) inputLayout_->Release( );
if( vertexBuffer_ ) vertexBuffer_->Release( );
   if( colorCB_ ) colorCB_->Release( );

customColorVS_ = 0;
customColorPS_ = 0;
inputLayout_ = 0;
vertexBuffer_ = 0;
   colorCB_ = 0;
}


void KeyboardDemo::Update( float dt )
{
   keyboardDevice_->GetDeviceState( sizeof( keyboardKeys_ ), ( LPVOID )&keyboardKeys_ );

// Button press event.
if( GetAsyncKeyState( VK_ESCAPE ) )
   {
       PostQuitMessage( 0 );
   }

// Button up event.
   if( KEYDOWN( prevKeyboardKeys_, DIK_DOWN ) && !KEYDOWN( keyboardKeys_, DIK_DOWN ) )
   {
       selectedColor_--;
   }

// Button up event.
   if( KEYDOWN( prevKeyboardKeys_, DIK_UP ) && !KEYDOWN( keyboardKeys_, DIK_UP ) )
   {
       selectedColor_++;
   }

memcpy( prevKeyboardKeys_, keyboardKeys_, sizeof( keyboardKeys_ ) );

if( selectedColor_ < 0 ) selectedColor_ = 2;
if( selectedColor_ > 2 ) selectedColor_ = 0;
}


void KeyboardDemo::Render( )
{
if( d3dContext_ == 0 )
return;

float clearColor[4] = { 0.0f, 0.0f, 0.25f, 1.0f };
d3dContext_->ClearRenderTargetView( backBufferTarget_, clearColor );

unsigned int stride = sizeof( VertexPos );
unsigned int offset = 0;

d3dContext_->IASetInputLayout( inputLayout_ );
d3dContext_->IASetVertexBuffers( 0, 1, &vertexBuffer_, &stride, &offset );
d3dContext_->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST );

   XMFLOAT4 redColor( 1.0f, 0.0f, 0.0f, 1.0f );
   XMFLOAT4 greenColor( 0.0f, 1.0f, 0.0f, 1.0f );
   XMFLOAT4 blueColor( 0.0f, 0.0f, 1.0f, 1.0f );

   if( selectedColor_ == 0 )
   {
       d3dContext_->UpdateSubresource( colorCB_, 0, 0, &redColor, 0, 0 );
   }
   else if( selectedColor_ == 1 )
   {
       d3dContext_->UpdateSubresource( colorCB_, 0, 0, &greenColor, 0, 0 );
   }
   else
   {
       d3dContext_->UpdateSubresource( colorCB_, 0, 0, &blueColor, 0, 0 );
   }

d3dContext_->VSSetShader( customColorVS_, 0, 0 );
d3dContext_->PSSetShader( customColorPS_, 0, 0 );
   d3dContext_->PSSetConstantBuffers( 0, 1, &colorCB_ );
d3dContext_->Draw( 3, 0 );

swapChain_->Present( 0, 0 );
}

Explanation / Answer

#include"KeyboardDemo.h" //Including the KeyboardDemo header file
//#include<xnamath.h> //Including xnamath header file
#include<DirectXMath.h> //Including DirectXMath header file
using namespace DirectX; //Using the namespace DirectX for to avoid naming collissions
struct VertexPos //declaring the structure VertexPos
{
XMFLOAT3 pos; //declaring a variable pos
}; //end of structure declaration
// memory allocation
KeyboardDemo::KeyboardDemo( ) : customColorVS_( 0 ), customColorPS_( 0 ),
inputLayout_( 0 ), vertexBuffer_( 0 ),
colorCB_( 0 ), selectedColor_( 0 ) //Initiating all the values of the keyboadrDemo
{
}

KeyboardDemo::~KeyboardDemo( ) /memory deallocation
{
}

bool KeyboardDemo::LoadContent( ) //loading content from the keyboard function
{
ID3DBlob* vsBuffer = 0; //declaring vsbuffer to 0
bool compileResult = CompileD3DShader( L"CustomColor.hlsl", "VS_Main", "vs_4_0", &vsBuffer );
//declaring and initializing the compileResult value
if( compileResult == false ) //if above compile result is false then
{
MessageBox( 0, "Error loading vertex shader!", "Compile Error", MB_OK ); //print the message in it
return false; //return false statement
}
HRESULT d3dResult; //declaring d3dResult
d3dResult = d3dDevice_->CreateVertexShader( vsBuffer->GetBufferPointer( ), //initalizing d3dResult
vsBuffer->GetBufferSize( ), 0, &customColorVS_ );
if( FAILED( d3dResult ) ) //calling the FAILED fuction with d3dResult as input then
{
if( vsBuffer ) //if is there any vsBuffer then
vsBuffer->Release( ); //release the buffer values
return false; //returnin the false
}
D3D11_INPUT_ELEMENT_DESC solidColorLayout[] =/*declaring the solidColorLayout array*/
{
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }
};
unsigned int totalLayoutElements = ARRAYSIZE( solidColorLayout );   
//intializing the totalLayoutElements to the size of the sloidColorLayout variable
d3dResult = d3dDevice_->CreateInputLayout( solidColorLayout, totalLayoutElements,
vsBuffer->GetBufferPointer( ), vsBuffer->GetBufferSize( ), &inputLayout_ );
/*initializing to d3dResult that creating user layout and stting the buffer
vsBuffer->Release( ); //releasing the buffer
if( FAILED( d3dResult ) ) //calling the FAILED function to know d3dResult is failed or not
{
return false; if d3dResult failed then return the false
}
ID3DBlob* psBuffer = 0; //declaring and intializing the psBuffer to 0
compileResult = CompileD3DShader( L"CustomColor.hlsl", "PS_Main", "ps_4_0", &psBuffer ); //intializing the compiling result to the variable
if( compileResult == false ) //if the above compileResult false then
{
MessageBox( 0, "Error loading pixel shader!", "Compile Error", MB_OK ); //print the message in the message box
return false; //return the false
}
d3dResult = d3dDevice_->CreatePixelShader( psBuffer->GetBufferPointer( ),
psBuffer->GetBufferSize( ), 0, &customColorPS_ );
/*intializing the d3dResult by value retrieved by the pixelShader
psBuffer->Release( ); /releasing the psBuffer
if( FAILED( d3dResult ) ) //calling the FAILED function with d3dResult variable passing to it
{
return false; //if in case the value in d3dResult failed then return false
}
VertexPos vertices[] =
{
XMFLOAT3( 0.5f, 0.5f, 0.5f ),
XMFLOAT3( 0.5f, -0.5f, 0.5f ),
XMFLOAT3( -0.5f, -0.5f, 0.5f )
};
/*declring and initializing the vertices array
D3D11_BUFFER_DESC vertexDesc; //declaring the vertices array variable
ZeroMemory( &vertexDesc, sizeof( vertexDesc ) ); calling the function ZeroMemory
vertexDesc.Usage = D3D11_USAGE_DEFAULT; //intializing the vertex.usage to usage default
vertexDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER; //intializing the blind flags to bind vertex buffer
vertexDesc.ByteWidth = sizeof( VertexPos ) * 3; //intializing the ByteWidth to the size of vertexPos multiplied with 3
D3D11_SUBRESOURCE_DATA resourceData; //declaring the resourceData
ZeroMemory( &resourceData, sizeof( resourceData ) ); //calling the ZeroMemory
resourceData.pSysMem = vertices; //intializing pSysMem to vertices array
d3dResult = d3dDevice_->CreateBuffer( &vertexDesc, &resourceData, &vertexBuffer_ ); ///initializing the d3dResult
if( FAILED( d3dResult ) ) //if the above d3dResult fails
{
return false; //return fasle if d3dResult fails
}

D3D11_BUFFER_DESC constDesc; //declaring the constDesc
ZeroMemory( &constDesc, sizeof( constDesc ) ); //calling the ZeroMemory with size of constDesc anf constDesc
constDesc.BindFlags = D3D11_BIND_CONSTANT_BUFFER; //intializing the blind flags to bind vertex buffer
constDesc.ByteWidth = sizeof( XMFLOAT4 );//intializing the ByteWidth to the size of XMFLOAT4
constDesc.Usage = D3D11_USAGE_DEFAULT; //intializing the vertex.usage to usage default
d3dResult = d3dDevice_->CreateBuffer( &constDesc, 0, &colorCB_ ); initializing the dedDevice buffer values to dedResult
if( FAILED( d3dResult ) ) //if the above d3dResult fails
{
return false; //return fasle if d3dResult fails
}
return true; //return true if dedResult not failed
}

void KeyboardDemo::UnloadContent( ) //method UnloadContent defintion
{
if( customColorVS_ ) customColorVS_->Release( ); // if customColorVS then release customColorVS
if( customColorPS_ ) customColorPS_->Release( ); // if customColorPS then release customColorPS
if( inputLayout_ ) inputLayout_->Release( ); //if InputLayout then release inputLayout
if( vertexBuffer_ ) vertexBuffer_->Release( ); // if vertexBuffer then release vertexBuffer
if( colorCB_ ) colorCB_->Release( ); // if ColorCB then release colorCB
customColorVS_ = 0; //intializing customColorVS to 0
customColorPS_ = 0; //intializingcustomColorPS to 0
inputLayout_ = 0; //intializing inputLayout to 0
vertexBuffer_ = 0; //intializing vertexBuffer to 0
colorCB_ = 0; //intializing colorCB to 0
}

void KeyboardDemo::Update( float dt ) //methode Update with float variable dt definition
{
keyboardDevice_->GetDeviceState( sizeof( keyboardKeys_ ), ( LPVOID )&keyboardKeys_ );
// Button press event.
if( GetAsyncKeyState( VK_ESCAPE ) )
{
PostQuitMessage( 0 );
}
// Button up event.
if( KEYDOWN( prevKeyboardKeys_, DIK_DOWN ) && !KEYDOWN( keyboardKeys_, DIK_DOWN ) )
{
selectedColor_--;
}
// Button up event.
if( KEYDOWN( prevKeyboardKeys_, DIK_UP ) && !KEYDOWN( keyboardKeys_, DIK_UP ) )
{
selectedColor_++; //increase selecteColor by 1
}
memcpy( prevKeyboardKeys_, keyboardKeys_, sizeof( keyboardKeys_ ) ); //calling memcpy method
if( selectedColor_ < 0 ) selectedColor_ = 2; //selectedColor_ < 0 then set selectedColor_ to 2
if( selectedColor_ > 2 ) selectedColor_ = 0; selectedColor_ >2 then set selectedColor_ to 0
}

void KeyboardDemo::Render( ) //method Render defintion
{
if( d3dContext_ == 0 ) //if d3dContext is 0 then
return;
float clearColor[4] = { 0.0f, 0.0f, 0.25f, 1.0f }; //initializing the variable clearColor array to the values
d3dContext_->ClearRenderTargetView( backBufferTarget_, clearColor );
//calling ClearRenderTargetView by d3dContext
unsigned int stride = sizeof( VertexPos ); //initializing stride to the sizeof the vertexPos
unsigned int offset = 0; //initializing offset to the sizeof the 0
d3dContext_->IASetInputLayout( inputLayout_ ); //calling the pointer IASetIputLayout
d3dContext_->IASetVertexBuffers( 0, 1, &vertexBuffer_, &stride, &offset );
//calling the pointer IASetVertexBuffer
d3dContext_->IASetPrimitiveTopology( D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST );
//calling the pointer IASetPrimitiveTopology
XMFLOAT4 redColor( 1.0f, 0.0f, 0.0f, 1.0f ); //declaring and intializing the redColor
XMFLOAT4 greenColor( 0.0f, 1.0f, 0.0f, 1.0f ); //declaring and intializing the greenColor
XMFLOAT4 blueColor( 0.0f, 0.0f, 1.0f, 1.0f ); //declaring and intializing the blueColor
if( selectedColor_ == 0 ) //if the selectedColor is 0
{
d3dContext_->UpdateSubresource( colorCB_, 0, 0, &redColor, 0, 0 ); //update the source color to red
}
else if( selectedColor_ == 1 ) //or if the selectedColor is 1
{
d3dContext_->UpdateSubresource( colorCB_, 0, 0, &greenColor, 0, 0 );//update the source color to green
}
else
{
d3dContext_->UpdateSubresource( colorCB_, 0, 0, &blueColor, 0, 0 );//update the source color to blue
}
d3dContext_->VSSetShader( customColorVS_, 0, 0 ); //calling the pointer function VSSetShader
d3dContext_->PSSetShader( customColorPS_, 0, 0 ); //calling the pointer function PSSetShader
d3dContext_->PSSetConstantBuffers( 0, 1, &colorCB_ ); //calling the pointer function VSSetConstantBuffer
d3dContext_->Draw( 3, 0 ); //calling the pointer function Draw
swapChain_->Present( 0, 0 ); //calling the pointer function Present
}