////////////////////////////////////////////////////////////////////////////////
// Filename: d3dclass.cpp
////////////////////////////////////////////////////////////////////////////////
#include "d_3d_class.h"


d_3d_class::d_3d_class()
{
	swap_chain = 0;
	device_ = 0;
	device_context_ = 0;
	render_target_view_ = 0;
	depth_stencil_buffer_ = 0;
	depth_stencil_state_ = 0;
	depth_stencil_view_ = 0;
	raster_state_ = 0;
	depth_disabled_stencil_state_ = 0;
	alpha_enable_blending_state_ = 0;
	alpha_disable_blending_state_ = 0;
}


d_3d_class::d_3d_class(const d_3d_class& other)
{
}


d_3d_class::~d_3d_class()
{
}


bool d_3d_class::initialize(int screenWidth, int screenHeight, bool vsync, HWND hwnd, bool fullscreen, float screenDepth, float screenNear)
{
	Logger::Get().Log("Initializing D3Dclass", __FILE__, __LINE__, Logger::LogLevel::Initialize);

	HRESULT result;
	IDXGIFactory* factory;
	IDXGIAdapter* adapter;
	IDXGIOutput* adapterOutput;
	unsigned int numModes, i, numerator, denominator;
	unsigned long long stringLength;
	DXGI_MODE_DESC* displayModeList;
	DXGI_ADAPTER_DESC adapterDesc;
	int error;
	DXGI_SWAP_CHAIN_DESC swapChainDesc;
	D3D_FEATURE_LEVEL featureLevel;
	ID3D11Texture2D* backBufferPtr;
	D3D11_TEXTURE2D_DESC depthBufferDesc;
	D3D11_DEPTH_STENCIL_DESC depthStencilDesc;
	D3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc;
	D3D11_RASTERIZER_DESC rasterDesc;
	float fieldOfView, screenAspect;
	D3D11_DEPTH_STENCIL_DESC depthDisabledStencilDesc;
	D3D11_BLEND_DESC blendStateDescription;

	// Store the vsync setting.
	vsync_enabled_ = vsync;

	// Create a DirectX graphics interface factory.
	result = CreateDXGIFactory(__uuidof(IDXGIFactory), (void**)&factory);
	if (FAILED(result))
	{
		Logger::Get().Log("Failed to create DXGIFactory", __FILE__, __LINE__, Logger::LogLevel::Error);
		return false;
	}

	// Use the factory to create an adapter for the primary graphics interface (video card).
	result = factory->EnumAdapters(0, &adapter);
	if (FAILED(result))
	{
		Logger::Get().Log("Failed to create adapter", __FILE__, __LINE__, Logger::LogLevel::Error);
		return false;
	}

	// Enumerate the primary adapter output (monitor).
	result = adapter->EnumOutputs(0, &adapterOutput);
	if (FAILED(result))
	{
		Logger::Get().Log("Failed to create adapter output", __FILE__, __LINE__, Logger::LogLevel::Error);
		return false;
	}

	// Get the number of modes that fit the DXGI_FORMAT_R8G8B8A8_UNORM display format for the adapter output (monitor).
	result = adapterOutput->GetDisplayModeList(DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_ENUM_MODES_INTERLACED, &numModes, NULL);
	if (FAILED(result))
	{
		Logger::Get().Log("Failed to get display mode list", __FILE__, __LINE__, Logger::LogLevel::Error);
		return false;
	}

	// Create a list to hold all the possible display modes for this monitor/video card combination.
	displayModeList = new DXGI_MODE_DESC[numModes];
	if (!displayModeList)
	{
		Logger::Get().Log("Failed to create display mode list", __FILE__, __LINE__, Logger::LogLevel::Error);
		return false;
	}

	// Now fill the display mode list structures.
	result = adapterOutput->GetDisplayModeList(DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_ENUM_MODES_INTERLACED, &numModes, displayModeList);
	if (FAILED(result))
	{
		Logger::Get().Log("Failed to fill display mode list", __FILE__, __LINE__, Logger::LogLevel::Error);
		return false;
	}

	// Now go through all the display modes and find the one that matches the screen width and height.
	// When a match is found store the numerator and denominator of the refresh rate for that monitor.
	for (i = 0; i < numModes; i++)
	{
		if (displayModeList[i].Width == (unsigned int)screenWidth)
		{
			if (displayModeList[i].Height == (unsigned int)screenHeight)
			{
				numerator = displayModeList[i].RefreshRate.Numerator;
				denominator = displayModeList[i].RefreshRate.Denominator;
			}
		}
	}

	// Get the adapter (video card) description.
	result = adapter->GetDesc(&adapterDesc);
	if (FAILED(result))
	{
		Logger::Get().Log("Failed to get adapter description", __FILE__, __LINE__, Logger::LogLevel::Error);
		return false;
	}

	// Store the dedicated video card memory in megabytes.
	video_card_memory_ = (int)(adapterDesc.DedicatedVideoMemory / 1024 / 1024);

	// Convert the name of the video card to a character array and store it.
	error = wcstombs_s(&stringLength, video_card_description_, 128, adapterDesc.Description, 128);
	if (error != 0)
	{
		Logger::Get().Log("Failed to convert video card name to character array", __FILE__, __LINE__, Logger::LogLevel::Error);
		return false;
	}

	// Release the display mode list.
	delete[] displayModeList;
	displayModeList = 0;

	// Release the adapter output.
	adapterOutput->Release();
	adapterOutput = 0;

	// Release the adapter.
	adapter->Release();
	adapter = 0;

	// Release the factory.
	factory->Release();
	factory = 0;

	// initialize the swap chain description.
	ZeroMemory(&swapChainDesc, sizeof(swapChainDesc));

	// Set to a single back buffer.
	swapChainDesc.BufferCount = 1;

	// Set the width and height of the back buffer.
	swapChainDesc.BufferDesc.Width = screenWidth;
	swapChainDesc.BufferDesc.Height = screenHeight;

	// Set regular 32-bit surface for the back buffer.
	swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;

	// Set the refresh rate of the back buffer.
	if (vsync_enabled_)
	{
		swapChainDesc.BufferDesc.RefreshRate.Numerator = numerator;
		swapChainDesc.BufferDesc.RefreshRate.Denominator = denominator;
	}
	else
	{
		swapChainDesc.BufferDesc.RefreshRate.Numerator = 0;
		swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
	}

	// Set the usage of the back buffer.
	swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;

	// Set the handle for the window to render to.
	swapChainDesc.OutputWindow = hwnd;

	// Turn multisampling off.
	swapChainDesc.SampleDesc.Count = 1;
	swapChainDesc.SampleDesc.Quality = 0;

	// Set to full screen or windowed mode.
	if (fullscreen)
	{
		swapChainDesc.Windowed = false;
	}
	else
	{
		swapChainDesc.Windowed = true;
	}

	// Set the scan line ordering and scaling to unspecified.
	swapChainDesc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
	swapChainDesc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;

	// Discard the back buffer contents after presenting.
	swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;

	// Don't set the advanced flags.
	swapChainDesc.Flags = 0;

	// Set the feature level to DirectX 11.
	featureLevel = D3D_FEATURE_LEVEL_11_0;

	// Create the swap chain, Direct3D device, and Direct3D device context.
	result = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, 0, &featureLevel, 1,
		D3D11_SDK_VERSION, &swapChainDesc, &swap_chain, &device_, NULL, &device_context_);
	if (FAILED(result))
	{
		Logger::Get().Log("Failed to create swap chain, device and device context", __FILE__, __LINE__, Logger::LogLevel::Error);
		return false;
	}

	// Get the pointer to the back buffer.
	result = swap_chain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&backBufferPtr);
	if (FAILED(result))
	{
		Logger::Get().Log("Failed to get pointer to back buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
		return false;
	}

	// Create the render target view with the back buffer pointer.
	result = device_->CreateRenderTargetView(backBufferPtr, NULL, &render_target_view_);
	if (FAILED(result))
	{
		Logger::Get().Log("Failed to create render target view", __FILE__, __LINE__, Logger::LogLevel::Error);
		return false;
	}

	// Release pointer to the back buffer as we no longer need it.
	backBufferPtr->Release();
	backBufferPtr = 0;

	// initialize the description of the depth buffer.
	ZeroMemory(&depthBufferDesc, sizeof(depthBufferDesc));

	// Set up the description of the depth buffer.
	depthBufferDesc.Width = screenWidth;
	depthBufferDesc.Height = screenHeight;
	depthBufferDesc.MipLevels = 1;
	depthBufferDesc.ArraySize = 1;
	depthBufferDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
	depthBufferDesc.SampleDesc.Count = 1;
	depthBufferDesc.SampleDesc.Quality = 0;
	depthBufferDesc.Usage = D3D11_USAGE_DEFAULT;
	depthBufferDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
	depthBufferDesc.CPUAccessFlags = 0;
	depthBufferDesc.MiscFlags = 0;

	// Create the texture for the depth buffer using the filled out description.
	result = device_->CreateTexture2D(&depthBufferDesc, NULL, &depth_stencil_buffer_);
	if (FAILED(result))
	{
		Logger::Get().Log("Failed to create texture for depth buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
		return false;
	}

	// initialize the description of the stencil state.
	ZeroMemory(&depthStencilDesc, sizeof(depthStencilDesc));

	// Set up the description of the stencil state.
	depthStencilDesc.DepthEnable = true;
	depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
	depthStencilDesc.DepthFunc = D3D11_COMPARISON_LESS;

	depthStencilDesc.StencilEnable = true;
	depthStencilDesc.StencilReadMask = 0xFF;
	depthStencilDesc.StencilWriteMask = 0xFF;

	// Stencil operations if pixel is front-facing.
	depthStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
	depthStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
	depthStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
	depthStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;

	// Stencil operations if pixel is back-facing.
	depthStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
	depthStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR;
	depthStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
	depthStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;

	// Create the depth stencil state.
	result = device_->CreateDepthStencilState(&depthStencilDesc, &depth_stencil_state_);
	if (FAILED(result))
	{
		Logger::Get().Log("Failed to create depth stencil state", __FILE__, __LINE__, Logger::LogLevel::Error);
		return false;
	}

	// Set the depth stencil state.
	device_context_->OMSetDepthStencilState(depth_stencil_state_, 1);

	// initialize the depth stencil view.
	ZeroMemory(&depthStencilViewDesc, sizeof(depthStencilViewDesc));

	// Set up the depth stencil view description.
	depthStencilViewDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
	depthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
	depthStencilViewDesc.Texture2D.MipSlice = 0;

	// Create the depth stencil view.
	result = device_->CreateDepthStencilView(depth_stencil_buffer_, &depthStencilViewDesc, &depth_stencil_view_);
	if (FAILED(result))
	{
		Logger::Get().Log("Failed to create depth stencil view", __FILE__, __LINE__, Logger::LogLevel::Error);
		return false;
	}

	// Bind the render target view and depth stencil buffer to the output render pipeline.
	device_context_->OMSetRenderTargets(1, &render_target_view_, depth_stencil_view_);

	// Setup the raster description which will determine how and what polygons will be drawn.
	rasterDesc.AntialiasedLineEnable = false;
	rasterDesc.CullMode = D3D11_CULL_BACK;
	rasterDesc.DepthBias = 0;
	rasterDesc.DepthBiasClamp = 0.0f;
	rasterDesc.DepthClipEnable = true;
	rasterDesc.FillMode = D3D11_FILL_SOLID;
	rasterDesc.FrontCounterClockwise = false;
	rasterDesc.MultisampleEnable = false;
	rasterDesc.ScissorEnable = false;
	rasterDesc.SlopeScaledDepthBias = 0.0f;

	// Create the rasterizer state from the description we just filled out.
	result = device_->CreateRasterizerState(&rasterDesc, &raster_state_);
	if (FAILED(result))
	{
		Logger::Get().Log("Failed to create rasterizer state", __FILE__, __LINE__, Logger::LogLevel::Error);
		return false;
	}

	// Now set the rasterizer state.
	device_context_->RSSetState(raster_state_);

	// Setup the viewport for rendering.
	viewport_.Width = (float)screenWidth;
	viewport_.Height = (float)screenHeight;
	viewport_.MinDepth = 0.0f;
	viewport_.MaxDepth = 1.0f;
	viewport_.TopLeftX = 0.0f;
	viewport_.TopLeftY = 0.0f;

	// Create the viewport.
	device_context_->RSSetViewports(1, &viewport_);

	// Setup the projection matrix.
	fieldOfView = 3.141592654f / 4.0f;
	screenAspect = (float)screenWidth / (float)screenHeight;

	// Create the projection matrix for 3D rendering.
	projection_matrix_ = XMMatrixPerspectiveFovLH(fieldOfView, screenAspect, screenNear, screenDepth);

	// initialize the world matrix to the identity matrix.
	world_matrix_ = XMMatrixIdentity();

	// Create an orthographic projection matrix for 2D rendering.
	ortho_matrix_ = XMMatrixOrthographicLH((float)screenWidth, (float)screenHeight, screenNear, screenDepth);

	// Clear the second depth stencil state before setting the parameters.
	ZeroMemory(&depthDisabledStencilDesc, sizeof(depthDisabledStencilDesc));

	// Now create a second depth stencil state which turns off the Z buffer for 2D rendering.  The only difference is 
	// that DepthEnable is set to false, all other parameters are the same as the other depth stencil state.
	depthDisabledStencilDesc.DepthEnable = false;
	depthDisabledStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
	depthDisabledStencilDesc.DepthFunc = D3D11_COMPARISON_LESS;
	depthDisabledStencilDesc.StencilEnable = true;
	depthDisabledStencilDesc.StencilReadMask = 0xFF;
	depthDisabledStencilDesc.StencilWriteMask = 0xFF;
	depthDisabledStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
	depthDisabledStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
	depthDisabledStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
	depthDisabledStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
	depthDisabledStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
	depthDisabledStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR;
	depthDisabledStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
	depthDisabledStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;

	// Create the state using the device.
	result = device_->CreateDepthStencilState(&depthDisabledStencilDesc, &depth_disabled_stencil_state_);
	if (FAILED(result))
	{
		Logger::Get().Log("Failed to create depth disabled stencil state", __FILE__, __LINE__, Logger::LogLevel::Error);
		return false;
	}

	// Clear the blend state description.
	ZeroMemory(&blendStateDescription, sizeof(D3D11_BLEND_DESC));

	// Create an alpha enabled blend state description.
	blendStateDescription.RenderTarget[0].BlendEnable = TRUE;
	blendStateDescription.RenderTarget[0].SrcBlend = D3D11_BLEND_ONE;
	blendStateDescription.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
	blendStateDescription.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
	blendStateDescription.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
	blendStateDescription.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
	blendStateDescription.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
	blendStateDescription.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;

	// Create the blend state using the description.
	result = device_->CreateBlendState(&blendStateDescription, &alpha_enable_blending_state_);
	if (FAILED(result))
	{
		Logger::Get().Log("Failed to create alpha enabled blend state", __FILE__, __LINE__, Logger::LogLevel::Error);
		return false;
	}

	// Modify the description to create an alpha disabled blend state description.
	blendStateDescription.RenderTarget[0].BlendEnable = FALSE;

	// Create the blend state using the description.
	result = device_->CreateBlendState(&blendStateDescription, &alpha_disable_blending_state_);
	if (FAILED(result))
	{
		Logger::Get().Log("Failed to create alpha disabled blend state", __FILE__, __LINE__, Logger::LogLevel::Error);
		return false;
	}

	return true;
}


void d_3d_class::shutdown()
{

	Logger::Get().Log("Shutting down D3Dclass", __FILE__, __LINE__, Logger::LogLevel::Shutdown);

	// Before shutting down set to windowed mode or when you release the swap chain it will throw an exception.
	if (swap_chain)
	{
		swap_chain->SetFullscreenState(false, NULL);
	}

	if (alpha_enable_blending_state_)
	{
		alpha_enable_blending_state_->Release();
		alpha_enable_blending_state_ = 0;
	}

	if (alpha_disable_blending_state_)
	{
		alpha_disable_blending_state_->Release();
		alpha_disable_blending_state_ = 0;
	}

	if (depth_disabled_stencil_state_)
	{
		depth_disabled_stencil_state_->Release();
		depth_disabled_stencil_state_ = 0;
	}

	if (raster_state_)
	{
		raster_state_->Release();
		raster_state_ = 0;
	}

	if (depth_stencil_view_)
	{
		depth_stencil_view_->Release();
		depth_stencil_view_ = 0;
	}

	if (depth_stencil_state_)
	{
		depth_stencil_state_->Release();
		depth_stencil_state_ = 0;
	}

	if (depth_stencil_buffer_)
	{
		depth_stencil_buffer_->Release();
		depth_stencil_buffer_ = 0;
	}

	if (render_target_view_)
	{
		render_target_view_->Release();
		render_target_view_ = 0;
	}

	if (device_context_)
	{
		device_context_->Release();
		device_context_ = 0;
	}

	if (device_)
	{
		device_->Release();
		device_ = 0;
	}

	if (swap_chain)
	{
		swap_chain->Release();
		swap_chain = 0;
	}

	Logger::Get().Log("D3Dclass shutdown", __FILE__, __LINE__, Logger::LogLevel::Shutdown);

	return;
}


void d_3d_class::begin_scene(float red, float green, float blue, float alpha)
{
	float color[4];


	// Setup the color to clear the buffer to.
	color[0] = red;
	color[1] = green;
	color[2] = blue;
	color[3] = alpha;

	// Clear the back buffer.
	device_context_->ClearRenderTargetView(render_target_view_, color);



	// Clear the depth buffer.
	device_context_->ClearDepthStencilView(depth_stencil_view_, D3D11_CLEAR_DEPTH, 1.0f, 0);

	return;
}


void d_3d_class::end_scene()
{
	// Present the back buffer to the screen since rendering is complete.
	if (vsync_enabled_)
	{
		// Lock to screen refresh rate.
		swap_chain->Present(1, 0);
	}
	else
	{
		// Present as fast as possible.
		swap_chain->Present(0, 0);
	}

	return;
}


ID3D11Device* d_3d_class::get_device()
{
	return device_;
}


ID3D11DeviceContext* d_3d_class::get_device_context()
{
	return device_context_;
}

void d_3d_class::get_video_card_info(char* cardName, int& memory)
{
	strcpy_s(cardName, 128, video_card_description_);
	memory = video_card_memory_;
	return;
}


void d_3d_class::set_back_buffer_render_target()
{
	// Bind the render target view and depth stencil buffer to the output render pipeline.
	device_context_->OMSetRenderTargets(1, &render_target_view_, depth_stencil_view_);

	return;
}


void d_3d_class::reset_viewport()
{
	// Set the viewport.
	device_context_->RSSetViewports(1, &viewport_);

	return;
}

void d_3d_class::release_resources()
{
	Logger::Get().Log("Releasing D3D resources", __FILE__, __LINE__);

	// libere la vue
	if (render_target_view_)
	{
		render_target_view_->Release();
		render_target_view_ = 0;
	}

	// libere le buffer de profondeur
	if (depth_stencil_buffer_)
	{
		depth_stencil_buffer_->Release();
		depth_stencil_buffer_ = 0;
	}

	// libere la vue de profondeur
	if (depth_stencil_view_)
	{
		depth_stencil_view_->Release();
		depth_stencil_view_ = 0;
	}

	Logger::Get().Log("D3D resources released", __FILE__, __LINE__);
}

// Reset the resources for the swap chain
void d_3d_class::reset_resources(int newWidth, int newHeight)
{
	Logger::Get().Log("Resetting D3D resources", __FILE__, __LINE__);

	HRESULT result;

	ID3D11Texture2D* backBuffer;
	result = swap_chain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&backBuffer);
	if (FAILED(result))
	{
		Logger::Get().Log("Failed to get back buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
		return;
	}

	result = device_->CreateRenderTargetView(backBuffer, NULL, &render_target_view_);
	if (FAILED(result))
	{
		Logger::Get().Log("Failed to create render target view", __FILE__, __LINE__, Logger::LogLevel::Error);
		return;
	}

	result = backBuffer->Release();
	if (FAILED(result))
	{
		Logger::Get().Log("Failed to release back buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
		return;
	}

	// Recreate the depth/stencil buffer and view
	D3D11_TEXTURE2D_DESC depthBufferDesc;
	ZeroMemory(&depthBufferDesc, sizeof(depthBufferDesc));
	depthBufferDesc.Width = newWidth;
	depthBufferDesc.Height = newHeight;
	depthBufferDesc.MipLevels = 1;
	depthBufferDesc.ArraySize = 1;
	depthBufferDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
	depthBufferDesc.SampleDesc.Count = 1;
	depthBufferDesc.SampleDesc.Quality = 0;
	depthBufferDesc.Usage = D3D11_USAGE_DEFAULT;
	depthBufferDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
	depthBufferDesc.CPUAccessFlags = 0;
	depthBufferDesc.MiscFlags = 0;

	D3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc;
	ZeroMemory(&depthStencilViewDesc, sizeof(depthStencilViewDesc));
	depthStencilViewDesc.Format = depthBufferDesc.Format;
	depthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
	depthStencilViewDesc.Texture2D.MipSlice = 0;

	// Other depthStencilDesc settings...
	result = device_->CreateTexture2D(&depthBufferDesc, NULL, &depth_stencil_buffer_);
	if (FAILED(result))
	{
		Logger::Get().Log("Failed to create depth stencil buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
		return;
	}

	result = device_->CreateDepthStencilView(depth_stencil_buffer_, &depthStencilViewDesc, &depth_stencil_view_); 
	if (FAILED(result))
	{
		Logger::Get().Log("Failed to create depth stencil view", __FILE__, __LINE__, Logger::LogLevel::Error);
		return;
	}

	// Set the new render target and depth/stencil views for rendering
	device_context_->OMSetRenderTargets(1, &render_target_view_, depth_stencil_view_);
}

IDXGISwapChain* d_3d_class::get_swap_chain()
{
	return swap_chain;
}

void d_3d_class::resize_swap_chain(int newWidth, int newHeight)
{

	// log the new width and height
	Logger::Get().Log("Resizing swap chain to " + std::to_string(newWidth) + "x" + std::to_string(newHeight), __FILE__, __LINE__);

	HRESULT result;

	// Release existing DirectX resources
	release_resources();
	device_context_->Flush();

	// Resize the swap chain
	result = swap_chain->ResizeBuffers(0, newWidth, newHeight, DXGI_FORMAT_UNKNOWN, 0);
	if (FAILED(result))
	{
		Logger::Get().Log("Failed to resize swap chain", __FILE__, __LINE__, Logger::LogLevel::Error);
		return;
	}

	// Reset the resources
	reset_resources(newWidth, newHeight);
	
	// Update the viewport
	viewport_.Width = static_cast<float>(newWidth);
	viewport_.Height = static_cast<float>(newHeight);
	device_context_->RSSetViewports(1, &viewport_);
}

void d_3d_class::turn_z_buffer_on()
{
	device_context_->OMSetDepthStencilState(depth_stencil_state_, 1);
	return;
}


void d_3d_class::turn_z_buffer_off()
{
	device_context_->OMSetDepthStencilState(depth_disabled_stencil_state_, 1);
	return;
}

void d_3d_class::enable_alpha_blending()
{
	float blendFactor[4];


	// Setup the blend factor.
	blendFactor[0] = 0.0f;
	blendFactor[1] = 0.0f;
	blendFactor[2] = 0.0f;
	blendFactor[3] = 0.0f;

	// Turn on the alpha blending.
	device_context_->OMSetBlendState(alpha_enable_blending_state_, blendFactor, 0xffffffff);

	return;
}

void d_3d_class::disable_alpha_blending()
{
	float blendFactor[4];


	// Setup the blend factor.
	blendFactor[0] = 0.0f;
	blendFactor[1] = 0.0f;
	blendFactor[2] = 0.0f;
	blendFactor[3] = 0.0f;

	// Turn off the alpha blending.
	device_context_->OMSetBlendState(alpha_disable_blending_state_, blendFactor, 0xffffffff);

	return;
}

void d_3d_class::set_vsync(bool vsync)
{
	vsync_enabled_ = vsync;
}