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: 3835148 • Letter: C

Question

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

#include"Dx11DemoBase.h"

#include<D3Dcompiler.h>

Dx11DemoBase::Dx11DemoBase( ) : driverType_( D3D_DRIVER_TYPE_NULL ), featureLevel_( D3D_FEATURE_LEVEL_11_0 ),
d3dDevice_( 0 ), d3dContext_( 0 ), swapChain_( 0 ), backBufferTarget_( 0 ),
directInput_( 0 ), keyboardDevice_( 0 )
{

}


Dx11DemoBase::~Dx11DemoBase( )
{
Shutdown( );
}


bool Dx11DemoBase::Initialize( HINSTANCE hInstance, HWND hwnd )
{
hInstance_ = hInstance;
hwnd_ = hwnd;

RECT dimensions;
GetClientRect( hwnd, &dimensions );

unsigned int width = dimensions.right - dimensions.left;
unsigned int height = dimensions.bottom - dimensions.top;

D3D_DRIVER_TYPE driverTypes[] =
{
D3D_DRIVER_TYPE_HARDWARE, D3D_DRIVER_TYPE_WARP,
D3D_DRIVER_TYPE_REFERENCE, D3D_DRIVER_TYPE_SOFTWARE
};

unsigned int totalDriverTypes = ARRAYSIZE( driverTypes );

D3D_FEATURE_LEVEL featureLevels[] =
{
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0
};

unsigned int totalFeatureLevels = ARRAYSIZE( featureLevels );

DXGI_SWAP_CHAIN_DESC swapChainDesc;
ZeroMemory( &swapChainDesc, sizeof( swapChainDesc ) );
swapChainDesc.BufferCount = 1;
swapChainDesc.BufferDesc.Width = width;
swapChainDesc.BufferDesc.Height = height;
swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
swapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.OutputWindow = hwnd;
swapChainDesc.Windowed = true;
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.SampleDesc.Quality = 0;

unsigned int creationFlags = 0;

#ifdef _DEBUG
creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif

HRESULT result;
unsigned int driver = 0;

for( driver = 0; driver < totalDriverTypes; ++driver )
{
result = D3D11CreateDeviceAndSwapChain( 0, driverTypes[driver], 0, creationFlags,
featureLevels, totalFeatureLevels,
D3D11_SDK_VERSION, &swapChainDesc, &swapChain_,
&d3dDevice_, &featureLevel_, &d3dContext_ );

if( SUCCEEDED( result ) )
{
driverType_ = driverTypes[driver];
break;
}
}

if( FAILED( result ) )
{
//DXTRACE_MSG( "Failed to create the Direct3D device!" );
return false;
}

ID3D11Texture2D* backBufferTexture;

result = swapChain_->GetBuffer( 0, __uuidof( ID3D11Texture2D ), ( LPVOID* )&backBufferTexture );

if( FAILED( result ) )
{
//DXTRACE_MSG( "Failed to get the swap chain back buffer!" );
return false;
}

result = d3dDevice_->CreateRenderTargetView( backBufferTexture, 0, &backBufferTarget_ );

if( backBufferTexture )
backBufferTexture->Release( );

if( FAILED( result ) )
{
//DXTRACE_MSG( "Failed to create the render target view!" );
return false;
}

d3dContext_->OMSetRenderTargets( 1, &backBufferTarget_, 0 );

D3D11_VIEWPORT viewport;
viewport.Width = static_cast<float>(width);
viewport.Height = static_cast<float>(height);
viewport.MinDepth = 0.0f;
viewport.MaxDepth = 1.0f;
viewport.TopLeftX = 0.0f;
viewport.TopLeftY = 0.0f;

d3dContext_->RSSetViewports( 1, &viewport );

ZeroMemory( keyboardKeys_, sizeof( keyboardKeys_ ) );
ZeroMemory( prevKeyboardKeys_, sizeof( prevKeyboardKeys_ ) );

result = DirectInput8Create( hInstance_, DIRECTINPUT_VERSION, IID_IDirectInput8, ( void** )&directInput_, 0 );

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

result = directInput_->CreateDevice( GUID_SysKeyboard, &keyboardDevice_, 0 );

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

result = keyboardDevice_->SetDataFormat( &c_dfDIKeyboard );

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

result = keyboardDevice_->SetCooperativeLevel( hwnd_, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE );

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

result = keyboardDevice_->Acquire( );

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

return LoadContent( );
}


//bool Dx11DemoBase::CompileD3DShader( char* filePath, char* entry, char* shaderModel, ID3DBlob** buffer )
bool Dx11DemoBase::CompileD3DShader(LPCWSTR filePath, char* entry, char* shaderModel, ID3DBlob** buffer)
{
DWORD shaderFlags = D3DCOMPILE_ENABLE_STRICTNESS;

#if defined( DEBUG ) || defined( _DEBUG )
shaderFlags |= D3DCOMPILE_DEBUG;
#endif

ID3DBlob* errorBuffer = 0;
HRESULT result;

/* result = D3DX11CompileFromFile( filePath, 0, 0, entry, shaderModel,
shaderFlags, 0, 0, buffer, &errorBuffer, 0 );*/
   result = D3DCompileFromFile(filePath, 0, 0, entry, shaderModel, shaderFlags, 0, buffer, &errorBuffer);

if( FAILED( result ) )
{
if( errorBuffer != 0 )
{
OutputDebugStringA( ( char* )errorBuffer->GetBufferPointer( ) );
errorBuffer->Release( );
}

return false;
}
  
if( errorBuffer != 0 )
errorBuffer->Release( );

return true;
}


bool Dx11DemoBase::LoadContent( )
{
// Override with demo specifics, if any...
return true;
}


void Dx11DemoBase::UnloadContent( )
{
// Override with demo specifics, if any...
}


void Dx11DemoBase::Shutdown( )
{
UnloadContent( );

if( backBufferTarget_ ) backBufferTarget_->Release( );
if( swapChain_ ) swapChain_->Release( );
if( d3dContext_ ) d3dContext_->Release( );
if( d3dDevice_ ) d3dDevice_->Release( );

if( keyboardDevice_ )
{
keyboardDevice_->Unacquire( );
keyboardDevice_->Release( );
}

if( directInput_ ) directInput_->Release( );

backBufferTarget_ = 0;
swapChain_ = 0;
d3dContext_ = 0;
d3dDevice_ = 0;
keyboardDevice_ = 0;
directInput_ = 0;
}

Explanation / Answer

#include"Dx11DemoBase.h" // include Dx11DemoBase.h header file
#include<D3Dcompiler.h> // include D3Dcompiler.h header file

// below line --- Dx11DemoBase is class and Dx11DemoBase() is a constructor of this class, and after : functions and their default values to initialize.
Dx11DemoBase::Dx11DemoBase( ) : driverType_( D3D_DRIVER_TYPE_NULL ), featureLevel_( D3D_FEATURE_LEVEL_11_0 ),
d3dDevice_( 0 ), d3dContext_( 0 ), swapChain_( 0 ), backBufferTarget_( 0 ),
directInput_( 0 ), keyboardDevice_( 0 )
{
}

// below line -- Dx11DemoBase is class :: ~Dx11DemoBase() is a destructor function and it calls shutdown() function to clean memory
Dx11DemoBase::~Dx11DemoBase( )
{
Shutdown( );
}

bool Dx11DemoBase::Initialize( HINSTANCE hInstance, HWND hwnd ) // return bool value, Dx11DemoBase class, function name - initialize and its arguments
{
hInstance_ = hInstance; // hInstance_ variable assigned with value of hInstance
hwnd_ = hwnd; // hwnd_ variable assigned with value hwnd
RECT dimensions; // create a RECT type variable name dimensions
GetClientRect( hwnd, &dimensions ); // call GetClientRect function with arguments hwnd, &dimensions
unsigned int width = dimensions.right - dimensions.left; // create width variable of type unsigned int and assign with value dimensions.right - dimensions.left
unsigned int height = dimensions.bottom - dimensions.top; // create height variable of type unsigned int and assign with value dimensions.bottom - dimensions.top
D3D_DRIVER_TYPE driverTypes[] = // driveTypes is a array variable of type DRD_DRIVER_TYPE, and assign with two given values between { .. };
{
D3D_DRIVER_TYPE_HARDWARE, D3D_DRIVER_TYPE_WARP,
D3D_DRIVER_TYPE_REFERENCE, D3D_DRIVER_TYPE_SOFTWARE
};
unsigned int totalDriverTypes = ARRAYSIZE( driverTypes ); // create totalDriverTypes variable of type unsigned int and assign with size of drivertypes
D3D_FEATURE_LEVEL featureLevels[] = // create featureLevels variable to store type D3D_FEATURE_LEVEL with values between { ... };
{
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0
};
unsigned int totalFeatureLevels = ARRAYSIZE( featureLevels ); // totalFeatureLevels is a variable which holds total features value
DXGI_SWAP_CHAIN_DESC swapChainDesc; // swapChainDesc variable of type DXGI_SWAP_CHAIN_DESC
ZeroMemory( &swapChainDesc, sizeof( swapChainDesc ) ); // called ZeroMemory with arguments &swapChainDesc, sizeof( swapChainDesc )
swapChainDesc.BufferCount = 1; // buffer assign to swapChainDesc is 1
swapChainDesc.BufferDesc.Width = width; // width of swapChainDesc.BufferDesc is equal to width
swapChainDesc.BufferDesc.Height = height; // Height swapChainDesc.BufferDesc is equal to height
swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; // format specify to swapChainDesc.BufferDesc
swapChainDesc.BufferDesc.RefreshRate.Numerator = 60; // assign swapChainDesc.BufferDesc.RefreshRate numerator value
swapChainDesc.BufferDesc.RefreshRate.Denominator = 1; // assign swapChainDesc.BufferDesc.RefreshRate denominator value
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; // set Target output swapChainDesc.BufferDesc
swapChainDesc.OutputWindow = hwnd; // set its window size
swapChainDesc.Windowed = true;
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.SampleDesc.Quality = 0;
unsigned int creationFlags = 0; // create variable creationFlags of type unsigned int
#ifdef _DEBUG // if _DEBUF is defined then next instruction executes upto #endif
creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif
HRESULT result;
unsigned int driver = 0; // create variable driver of unsigned int
for( driver = 0; driver < totalDriverTypes; ++driver ) // for every driver
{
result = D3D11CreateDeviceAndSwapChain( 0, driverTypes[driver], 0, creationFlags,
featureLevels, totalFeatureLevels,
D3D11_SDK_VERSION, &swapChainDesc, &swapChain_,
&d3dDevice_, &featureLevel_, &d3dContext_ ); // call D3D11CreateDeviceAndSwapChain function and store its return value to result
if( SUCCEEDED( result ) ) // if result is successfull then go inside
{
driverType_ = driverTypes[driver]; // it means driver is found which you are looking for , store it in driverType_
break; // break the for loop
}
}
if( FAILED( result ) ) // if result is failed
{
//DXTRACE_MSG( "Failed to create the Direct3D device!" );
return false; // return to callee function with false value
}
ID3D11Texture2D* backBufferTexture; // create pointer variable (which holds address) backBufferTecture of type ID3D11Texture2D
result = swapChain_->GetBuffer( 0, __uuidof( ID3D11Texture2D ), ( LPVOID* )&backBufferTexture ); // Try to allocate buffer by call swapChain_->GetBuffer function with arguments and store return value to variable result
if( FAILED( result ) ) // if result is failed go inside it
{
//DXTRACE_MSG( "Failed to get the swap chain back buffer!" );
return false; // return value false
}
result = d3dDevice_->CreateRenderTargetView( backBufferTexture, 0, &backBufferTarget_ ); // Try to create render view by calling d3dDevice_->CreateRenderTargetView and store return value to result
if( backBufferTexture ) // if backBufferTexture is not zero, go inside if block
backBufferTexture->Release( ); // Release the texture by calling backBufferTexture->Release( )
if( FAILED( result ) ) // if result is not succeded
{
//DXTRACE_MSG( "Failed to create the render target view!" );
return false; // return false
}
d3dContext_->OMSetRenderTargets( 1, &backBufferTarget_, 0 ); // call d3dContext_->OMSetRenderTargets function to render targets
D3D11_VIEWPORT viewport; // create a variable viewport of type D3D11_VIEWPORT
viewport.Width = static_cast<float>(width); // set viewport width
viewport.Height = static_cast<float>(height); // set viewport height
viewport.MinDepth = 0.0f; // set viewport minimum depth
viewport.MaxDepth = 1.0f; // set viewport minimum depth
viewport.TopLeftX = 0.0f; // set its top left X corner value
viewport.TopLeftY = 0.0f; // set viewport's top left Y value
d3dContext_->RSSetViewports( 1, &viewport ); // set d3dContext_->RSSetViewports with viewport value
ZeroMemory( keyboardKeys_, sizeof( keyboardKeys_ ) ); // call ZeroMemory with current arguments to set all values to zero
ZeroMemory( prevKeyboardKeys_, sizeof( prevKeyboardKeys_ ) ); // call ZeroMemory of prevKeyboardKeys and set all values to zero
result = DirectInput8Create( hInstance_, DIRECTINPUT_VERSION, IID_IDirectInput8, ( void** )&directInput_, 0 ); // creating DirectInput8 with function DirectInput8Create()
if( FAILED( result ) ) // if result is failed go inside if block
{
return false; // return false
}
result = directInput_->CreateDevice( GUID_SysKeyboard, &keyboardDevice_, 0 ); // create direct input device by directInput_->CreateDevice() and get result back to result variable
if( FAILED( result ) )
{
return false;
}
result = keyboardDevice_->SetDataFormat( &c_dfDIKeyboard ); // set keyboard device data format by calling keyboardDevice_->SetDataFormat and store return value to result variable
if( FAILED( result ) ) // if result is failed execute inside it block
{
return false; // return value false
}
result = keyboardDevice_->SetCooperativeLevel( hwnd_, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE ); // call keyboardDevice_->SetCooperativeLevel()
if( FAILED( result ) ) // if failed
{
return false; // return false value
}
result = keyboardDevice_->Acquire( ); //keyboardDevice_->Acquire to take input from device
if( FAILED( result ) )// if not acquired
{
return false; //return false
}
return LoadContent( ); // now all things are set up -- load the content by calling LoadContent() function
}

//bool Dx11DemoBase::CompileD3DShader( char* filePath, char* entry, char* shaderModel, ID3DBlob** buffer )
bool Dx11DemoBase::CompileD3DShader(LPCWSTR filePath, char* entry, char* shaderModel, ID3DBlob** buffer)
{
DWORD shaderFlags = D3DCOMPILE_ENABLE_STRICTNESS; // create variable to hold type DWORD and assign D3DCOMPILE_ENABLE_STRICTNESS flags value
#if defined( DEBUG ) || defined( _DEBUG ) // if define DEBUG or _DEBUG then go inside if block upto #endif
shaderFlags |= D3DCOMPILE_DEBUG; // Add D3DCOMPILE_DEBUG flag to shaderFlags
#endif
ID3DBlob* errorBuffer = 0; // create pointer variable errorBuffer of type ID3DBlob and assign value 0
HRESULT result; // create variale result of type HRESULT
/* result = D3DX11CompileFromFile( filePath, 0, 0, entry, shaderModel,
shaderFlags, 0, 0, buffer, &errorBuffer, 0 );*/
result = D3DCompileFromFile(filePath, 0, 0, entry, shaderModel, shaderFlags, 0, buffer, &errorBuffer); // compile file whose path is stored in filepath, and return value to result variable
if( FAILED( result ) ) // if result is failed
{
if( errorBuffer != 0 ) // check error, if error is caught
{
OutputDebugStringA( ( char* )errorBuffer->GetBufferPointer( ) ); // Debug the error from errorBuffer
errorBuffer->Release( ); // print error message
}
return false; // return false -- failed
}
  
if( errorBuffer != 0 ) // if some error is there
errorBuffer->Release( ); // print the error
return true; // return ture -- function ran successfully
}

bool Dx11DemoBase::LoadContent( ) // LoadContent function of class Dx11DemoBase that returns bool value
{
// Override with demo specifics, if any...
return true; // return true value to callee
}

void Dx11DemoBase::UnloadContent( ) // UnloadContent function of class Dx11DemoBase that return nothing
{
// Override with demo specifics, if any...
}

void Dx11DemoBase::Shutdown( ) // Shutdown function of class Dx11DemoBase and it return nothing
{ // clean the acquired resources by this function
UnloadContent( ); // call UnloadContent function
if( backBufferTarget_ ) backBufferTarget_->Release( ); // if backBufferTarget_ is present, release it
if( swapChain_ ) swapChain_->Release( ); // if swapChain_ is present, release it
if( d3dContext_ ) d3dContext_->Release( ); // if dedContext_ is present, release it
if( d3dDevice_ ) d3dDevice_->Release( ); // if d3dDevice_ is present, release it
if( keyboardDevice_ ) // if keyboardDevice_ is there
{
keyboardDevice_->Unacquire( ); // unacquire it
keyboardDevice_->Release( ); // and release it
}
if( directInput_ ) directInput_->Release( ); // if directInput_ is there, release it
backBufferTarget_ = 0; // set all variable to 0
swapChain_ = 0;
d3dContext_ = 0;
d3dDevice_ = 0;
keyboardDevice_ = 0;
directInput_ = 0;
}