Khaotic Engine Reborn
Loading...
Searching...
No Matches
d_3d_class Class Reference

Public Member Functions

 d_3d_class ()
 Default constructor for d_3d_class.
 
 d_3d_class (const d_3d_class &)
 
virtual bool initialize (int, int, bool, HWND, bool, float, float)
 Initializes the Direct3D device and related resources.
 
void shutdown ()
 Releases Direct3D resources.
 
virtual void begin_scene (float, float, float, float)
 Begins the rendering process for a new frame.
 
virtual void end_scene ()
 Ends the rendering process for the current frame.
 
ID3D11Device * get_device ()
 Gets the Direct3D device.
 
ID3D11DeviceContext * get_device_context ()
 Gets the Direct3D device context.
 
IDXGISwapChain * get_swap_chain ()
 
void resize_swap_chain (int width, int height)
 
void set_vsync (bool vsync)
 Sets the vertical sync state.
 
XMMATRIX get_projection_matrix () const
 
XMMATRIX get_world_matrix () const
 
XMMATRIX get_ortho_matrix () const
 
void get_video_card_info (char *description, int &memory)
 
void set_back_buffer_render_target ()
 
void reset_viewport ()
 
void release_resources ()
 
void reset_resources (int newWidth, int newHeight)
 
void turn_z_buffer_on ()
 
void turn_z_buffer_off ()
 
void enable_alpha_blending ()
 
void disable_alpha_blending ()
 

Public Attributes

IDXGISwapChain * swap_chain
 

Detailed Description

Definition at line 30 of file d_3d_class.h.

Constructor & Destructor Documentation

◆ d_3d_class() [1/2]

d_3d_class::d_3d_class ( )

Default constructor for d_3d_class.

Definition at line 7 of file d_3d_class.cpp.

8{
9 swap_chain = 0;
10 device_ = 0;
11 device_context_ = 0;
12 render_target_view_ = 0;
13 depth_stencil_buffer_ = 0;
14 depth_stencil_state_ = 0;
15 depth_stencil_view_ = 0;
16 raster_state_ = 0;
17 depth_disabled_stencil_state_ = 0;
18 alpha_enable_blending_state_ = 0;
19 alpha_disable_blending_state_ = 0;
20}

◆ d_3d_class() [2/2]

d_3d_class::d_3d_class ( const d_3d_class & other)

Definition at line 23 of file d_3d_class.cpp.

24{
25}

◆ ~d_3d_class()

d_3d_class::~d_3d_class ( )

Definition at line 28 of file d_3d_class.cpp.

29{
30}

Member Function Documentation

◆ begin_scene()

void d_3d_class::begin_scene ( float red,
float green,
float blue,
float alpha )
virtual

Begins the rendering process for a new frame.

Parameters
redRed component of the clear color.
greenGreen component of the clear color.
blueBlue component of the clear color.
alphaAlpha component of the clear color.

Definition at line 516 of file d_3d_class.cpp.

517{
518 float color[4];
519
520
521 // Setup the color to clear the buffer to.
522 color[0] = red;
523 color[1] = green;
524 color[2] = blue;
525 color[3] = alpha;
526
527 // Clear the back buffer.
528 device_context_->ClearRenderTargetView(render_target_view_, color);
529
530
531
532 // Clear the depth buffer.
533 device_context_->ClearDepthStencilView(depth_stencil_view_, D3D11_CLEAR_DEPTH, 1.0f, 0);
534
535 return;
536}

◆ disable_alpha_blending()

void d_3d_class::disable_alpha_blending ( )

Turn off alpha blending to disable transparency effects.

Definition at line 754 of file d_3d_class.cpp.

755{
756 float blendFactor[4];
757
758
759 // Setup the blend factor.
760 blendFactor[0] = 0.0f;
761 blendFactor[1] = 0.0f;
762 blendFactor[2] = 0.0f;
763 blendFactor[3] = 0.0f;
764
765 // Turn off the alpha blending.
766 device_context_->OMSetBlendState(alpha_disable_blending_state_, blendFactor, 0xffffffff);
767
768 return;
769}

◆ enable_alpha_blending()

void d_3d_class::enable_alpha_blending ( )

Turn on alpha blending for transparency effects.

Definition at line 737 of file d_3d_class.cpp.

738{
739 float blendFactor[4];
740
741
742 // Setup the blend factor.
743 blendFactor[0] = 0.0f;
744 blendFactor[1] = 0.0f;
745 blendFactor[2] = 0.0f;
746 blendFactor[3] = 0.0f;
747
748 // Turn on the alpha blending.
749 device_context_->OMSetBlendState(alpha_enable_blending_state_, blendFactor, 0xffffffff);
750
751 return;
752}

◆ end_scene()

void d_3d_class::end_scene ( )
virtual

Ends the rendering process for the current frame.

Definition at line 539 of file d_3d_class.cpp.

540{
541 // Present the back buffer to the screen since rendering is complete.
542 if (vsync_enabled_)
543 {
544 // Lock to screen refresh rate.
545 swap_chain->Present(1, 0);
546 }
547 else
548 {
549 // Present as fast as possible.
550 swap_chain->Present(0, 0);
551 }
552
553 return;
554}

◆ get_device()

ID3D11Device * d_3d_class::get_device ( )

Gets the Direct3D device.

Returns
Pointer to the ID3D11Device interface.

Definition at line 557 of file d_3d_class.cpp.

558{
559 return device_;
560}

◆ get_device_context()

ID3D11DeviceContext * d_3d_class::get_device_context ( )

Gets the Direct3D device context.

Returns
Pointer to the ID3D11DeviceContext interface.

Definition at line 563 of file d_3d_class.cpp.

564{
565 return device_context_;
566}

◆ get_ortho_matrix()

XMMATRIX d_3d_class::get_ortho_matrix ( ) const
inline

Get the orthographic matrix.

Returns
XMMATRIX representing the orthographic matrix.

Definition at line 121 of file d_3d_class.h.

121{ return ortho_matrix_; };

◆ get_projection_matrix()

XMMATRIX d_3d_class::get_projection_matrix ( ) const
inline

Get the projection matrix.

Returns
XMMATRIX representing the projection matrix.

Definition at line 109 of file d_3d_class.h.

109{ return projection_matrix_; };

◆ get_swap_chain()

IDXGISwapChain * d_3d_class::get_swap_chain ( )

Get the swap chain associated with the Direct3D device.

Returns
Pointer to the IDXGISwapChain interface.

Definition at line 690 of file d_3d_class.cpp.

691{
692 return swap_chain;
693}

◆ get_video_card_info()

void d_3d_class::get_video_card_info ( char * description,
int & memory )

Get the Video Card information.

Parameters
descriptionPointer to a character array to store the video card description.
memoryReference to an integer to store the video card memory size.

Definition at line 568 of file d_3d_class.cpp.

569{
570 strcpy_s(cardName, 128, video_card_description_);
571 memory = video_card_memory_;
572 return;
573}

◆ get_world_matrix()

XMMATRIX d_3d_class::get_world_matrix ( ) const
inline

Get the world matrix.

Returns
XMMATRIX representing the world matrix.

Definition at line 115 of file d_3d_class.h.

115{ return world_matrix_;};

◆ initialize()

bool d_3d_class::initialize ( int screenWidth,
int screenHeight,
bool vsync,
HWND hwnd,
bool fullscreen,
float screenDepth,
float screenNear )
virtual

Initializes the Direct3D device and related resources.

Parameters
screenWidthWidth of the screen.
screenHeightHeight of the screen.
vsyncWhether to enable vertical sync.
hwndHandle to the window.
fullscreenWhether to run in fullscreen mode.
screenDepthDepth of the screen.
screenNearNear clipping plane distance.
Returns
True if initialization was successful, false otherwise.

Definition at line 33 of file d_3d_class.cpp.

34{
35 Logger::Get().Log("Initializing D3Dclass", __FILE__, __LINE__, Logger::LogLevel::Initialize);
36
37 HRESULT result;
38 IDXGIFactory* factory;
39 IDXGIAdapter* adapter;
40 IDXGIOutput* adapterOutput;
41 unsigned int numModes, i, numerator, denominator;
42 unsigned long long stringLength;
43 DXGI_MODE_DESC* displayModeList;
44 DXGI_ADAPTER_DESC adapterDesc;
45 int error;
46 DXGI_SWAP_CHAIN_DESC swapChainDesc;
47 D3D_FEATURE_LEVEL featureLevel;
48 ID3D11Texture2D* backBufferPtr;
49 D3D11_TEXTURE2D_DESC depthBufferDesc;
50 D3D11_DEPTH_STENCIL_DESC depthStencilDesc;
51 D3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc;
52 D3D11_RASTERIZER_DESC rasterDesc;
53 float fieldOfView, screenAspect;
54 D3D11_DEPTH_STENCIL_DESC depthDisabledStencilDesc;
55 D3D11_BLEND_DESC blendStateDescription;
56
57 // Store the vsync setting.
58 vsync_enabled_ = vsync;
59
60 // Create a DirectX graphics interface factory.
61 result = CreateDXGIFactory(__uuidof(IDXGIFactory), (void**)&factory);
62 if (FAILED(result))
63 {
64 Logger::Get().Log("Failed to create DXGIFactory", __FILE__, __LINE__, Logger::LogLevel::Error);
65 return false;
66 }
67
68 // Use the factory to create an adapter for the primary graphics interface (video card).
69 result = factory->EnumAdapters(0, &adapter);
70 if (FAILED(result))
71 {
72 Logger::Get().Log("Failed to create adapter", __FILE__, __LINE__, Logger::LogLevel::Error);
73 return false;
74 }
75
76 // Enumerate the primary adapter output (monitor).
77 result = adapter->EnumOutputs(0, &adapterOutput);
78 if (FAILED(result))
79 {
80 Logger::Get().Log("Failed to create adapter output", __FILE__, __LINE__, Logger::LogLevel::Error);
81 return false;
82 }
83
84 // Get the number of modes that fit the DXGI_FORMAT_R8G8B8A8_UNORM display format for the adapter output (monitor).
85 result = adapterOutput->GetDisplayModeList(DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_ENUM_MODES_INTERLACED, &numModes, NULL);
86 if (FAILED(result))
87 {
88 Logger::Get().Log("Failed to get display mode list", __FILE__, __LINE__, Logger::LogLevel::Error);
89 return false;
90 }
91
92 // Create a list to hold all the possible display modes for this monitor/video card combination.
93 displayModeList = new DXGI_MODE_DESC[numModes];
94 if (!displayModeList)
95 {
96 Logger::Get().Log("Failed to create display mode list", __FILE__, __LINE__, Logger::LogLevel::Error);
97 return false;
98 }
99
100 // Now fill the display mode list structures.
101 result = adapterOutput->GetDisplayModeList(DXGI_FORMAT_R8G8B8A8_UNORM, DXGI_ENUM_MODES_INTERLACED, &numModes, displayModeList);
102 if (FAILED(result))
103 {
104 Logger::Get().Log("Failed to fill display mode list", __FILE__, __LINE__, Logger::LogLevel::Error);
105 return false;
106 }
107
108 // Now go through all the display modes and find the one that matches the screen width and height.
109 // When a match is found store the numerator and denominator of the refresh rate for that monitor.
110 for (i = 0; i < numModes; i++)
111 {
112 if (displayModeList[i].Width == (unsigned int)screenWidth)
113 {
114 if (displayModeList[i].Height == (unsigned int)screenHeight)
115 {
116 numerator = displayModeList[i].RefreshRate.Numerator;
117 denominator = displayModeList[i].RefreshRate.Denominator;
118 }
119 }
120 }
121
122 // Get the adapter (video card) description.
123 result = adapter->GetDesc(&adapterDesc);
124 if (FAILED(result))
125 {
126 Logger::Get().Log("Failed to get adapter description", __FILE__, __LINE__, Logger::LogLevel::Error);
127 return false;
128 }
129
130 // Store the dedicated video card memory in megabytes.
131 video_card_memory_ = (int)(adapterDesc.DedicatedVideoMemory / 1024 / 1024);
132
133 // Convert the name of the video card to a character array and store it.
134 error = wcstombs_s(&stringLength, video_card_description_, 128, adapterDesc.Description, 128);
135 if (error != 0)
136 {
137 Logger::Get().Log("Failed to convert video card name to character array", __FILE__, __LINE__, Logger::LogLevel::Error);
138 return false;
139 }
140
141 // Release the display mode list.
142 delete[] displayModeList;
143 displayModeList = 0;
144
145 // Release the adapter output.
146 adapterOutput->Release();
147 adapterOutput = 0;
148
149 // Release the adapter.
150 adapter->Release();
151 adapter = 0;
152
153 // Release the factory.
154 factory->Release();
155 factory = 0;
156
157 // initialize the swap chain description.
158 ZeroMemory(&swapChainDesc, sizeof(swapChainDesc));
159
160 // Set to a single back buffer.
161 swapChainDesc.BufferCount = 1;
162
163 // Set the width and height of the back buffer.
164 swapChainDesc.BufferDesc.Width = screenWidth;
165 swapChainDesc.BufferDesc.Height = screenHeight;
166
167 // Set regular 32-bit surface for the back buffer.
168 swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
169
170 // Set the refresh rate of the back buffer.
171 if (vsync_enabled_)
172 {
173 swapChainDesc.BufferDesc.RefreshRate.Numerator = numerator;
174 swapChainDesc.BufferDesc.RefreshRate.Denominator = denominator;
175 }
176 else
177 {
178 swapChainDesc.BufferDesc.RefreshRate.Numerator = 0;
179 swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
180 }
181
182 // Set the usage of the back buffer.
183 swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
184
185 // Set the handle for the window to render to.
186 swapChainDesc.OutputWindow = hwnd;
187
188 // Turn multisampling off.
189 swapChainDesc.SampleDesc.Count = 1;
190 swapChainDesc.SampleDesc.Quality = 0;
191
192 // Set to full screen or windowed mode.
193 if (fullscreen)
194 {
195 swapChainDesc.Windowed = false;
196 }
197 else
198 {
199 swapChainDesc.Windowed = true;
200 }
201
202 // Set the scan line ordering and scaling to unspecified.
203 swapChainDesc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
204 swapChainDesc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
205
206 // Discard the back buffer contents after presenting.
207 swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
208
209 // Don't set the advanced flags.
210 swapChainDesc.Flags = 0;
211
212 // Set the feature level to DirectX 11.
213 featureLevel = D3D_FEATURE_LEVEL_11_0;
214
215 // Create the swap chain, Direct3D device, and Direct3D device context.
216 result = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, 0, &featureLevel, 1,
217 D3D11_SDK_VERSION, &swapChainDesc, &swap_chain, &device_, NULL, &device_context_);
218 if (FAILED(result))
219 {
220 Logger::Get().Log("Failed to create swap chain, device and device context", __FILE__, __LINE__, Logger::LogLevel::Error);
221 return false;
222 }
223
224 // Get the pointer to the back buffer.
225 result = swap_chain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&backBufferPtr);
226 if (FAILED(result))
227 {
228 Logger::Get().Log("Failed to get pointer to back buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
229 return false;
230 }
231
232 // Create the render target view with the back buffer pointer.
233 result = device_->CreateRenderTargetView(backBufferPtr, NULL, &render_target_view_);
234 if (FAILED(result))
235 {
236 Logger::Get().Log("Failed to create render target view", __FILE__, __LINE__, Logger::LogLevel::Error);
237 return false;
238 }
239
240 // Release pointer to the back buffer as we no longer need it.
241 backBufferPtr->Release();
242 backBufferPtr = 0;
243
244 // initialize the description of the depth buffer.
245 ZeroMemory(&depthBufferDesc, sizeof(depthBufferDesc));
246
247 // Set up the description of the depth buffer.
248 depthBufferDesc.Width = screenWidth;
249 depthBufferDesc.Height = screenHeight;
250 depthBufferDesc.MipLevels = 1;
251 depthBufferDesc.ArraySize = 1;
252 depthBufferDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
253 depthBufferDesc.SampleDesc.Count = 1;
254 depthBufferDesc.SampleDesc.Quality = 0;
255 depthBufferDesc.Usage = D3D11_USAGE_DEFAULT;
256 depthBufferDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
257 depthBufferDesc.CPUAccessFlags = 0;
258 depthBufferDesc.MiscFlags = 0;
259
260 // Create the texture for the depth buffer using the filled out description.
261 result = device_->CreateTexture2D(&depthBufferDesc, NULL, &depth_stencil_buffer_);
262 if (FAILED(result))
263 {
264 Logger::Get().Log("Failed to create texture for depth buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
265 return false;
266 }
267
268 // initialize the description of the stencil state.
269 ZeroMemory(&depthStencilDesc, sizeof(depthStencilDesc));
270
271 // Set up the description of the stencil state.
272 depthStencilDesc.DepthEnable = true;
273 depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
274 depthStencilDesc.DepthFunc = D3D11_COMPARISON_LESS;
275
276 depthStencilDesc.StencilEnable = true;
277 depthStencilDesc.StencilReadMask = 0xFF;
278 depthStencilDesc.StencilWriteMask = 0xFF;
279
280 // Stencil operations if pixel is front-facing.
281 depthStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
282 depthStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
283 depthStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
284 depthStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
285
286 // Stencil operations if pixel is back-facing.
287 depthStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
288 depthStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR;
289 depthStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
290 depthStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
291
292 // Create the depth stencil state.
293 result = device_->CreateDepthStencilState(&depthStencilDesc, &depth_stencil_state_);
294 if (FAILED(result))
295 {
296 Logger::Get().Log("Failed to create depth stencil state", __FILE__, __LINE__, Logger::LogLevel::Error);
297 return false;
298 }
299
300 // Set the depth stencil state.
301 device_context_->OMSetDepthStencilState(depth_stencil_state_, 1);
302
303 // initialize the depth stencil view.
304 ZeroMemory(&depthStencilViewDesc, sizeof(depthStencilViewDesc));
305
306 // Set up the depth stencil view description.
307 depthStencilViewDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
308 depthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
309 depthStencilViewDesc.Texture2D.MipSlice = 0;
310
311 // Create the depth stencil view.
312 result = device_->CreateDepthStencilView(depth_stencil_buffer_, &depthStencilViewDesc, &depth_stencil_view_);
313 if (FAILED(result))
314 {
315 Logger::Get().Log("Failed to create depth stencil view", __FILE__, __LINE__, Logger::LogLevel::Error);
316 return false;
317 }
318
319 // Bind the render target view and depth stencil buffer to the output render pipeline.
320 device_context_->OMSetRenderTargets(1, &render_target_view_, depth_stencil_view_);
321
322 // Setup the raster description which will determine how and what polygons will be drawn.
323 rasterDesc.AntialiasedLineEnable = false;
324 rasterDesc.CullMode = D3D11_CULL_BACK;
325 rasterDesc.DepthBias = 0;
326 rasterDesc.DepthBiasClamp = 0.0f;
327 rasterDesc.DepthClipEnable = true;
328 rasterDesc.FillMode = D3D11_FILL_SOLID;
329 rasterDesc.FrontCounterClockwise = false;
330 rasterDesc.MultisampleEnable = false;
331 rasterDesc.ScissorEnable = false;
332 rasterDesc.SlopeScaledDepthBias = 0.0f;
333
334 // Create the rasterizer state from the description we just filled out.
335 result = device_->CreateRasterizerState(&rasterDesc, &raster_state_);
336 if (FAILED(result))
337 {
338 Logger::Get().Log("Failed to create rasterizer state", __FILE__, __LINE__, Logger::LogLevel::Error);
339 return false;
340 }
341
342 // Now set the rasterizer state.
343 device_context_->RSSetState(raster_state_);
344
345 // Setup the viewport for rendering.
346 viewport_.Width = (float)screenWidth;
347 viewport_.Height = (float)screenHeight;
348 viewport_.MinDepth = 0.0f;
349 viewport_.MaxDepth = 1.0f;
350 viewport_.TopLeftX = 0.0f;
351 viewport_.TopLeftY = 0.0f;
352
353 // Create the viewport.
354 device_context_->RSSetViewports(1, &viewport_);
355
356 // Setup the projection matrix.
357 fieldOfView = 3.141592654f / 4.0f;
358 screenAspect = (float)screenWidth / (float)screenHeight;
359
360 // Create the projection matrix for 3D rendering.
361 projection_matrix_ = XMMatrixPerspectiveFovLH(fieldOfView, screenAspect, screenNear, screenDepth);
362
363 // initialize the world matrix to the identity matrix.
364 world_matrix_ = XMMatrixIdentity();
365
366 // Create an orthographic projection matrix for 2D rendering.
367 ortho_matrix_ = XMMatrixOrthographicLH((float)screenWidth, (float)screenHeight, screenNear, screenDepth);
368
369 // Clear the second depth stencil state before setting the parameters.
370 ZeroMemory(&depthDisabledStencilDesc, sizeof(depthDisabledStencilDesc));
371
372 // Now create a second depth stencil state which turns off the Z buffer for 2D rendering. The only difference is
373 // that DepthEnable is set to false, all other parameters are the same as the other depth stencil state.
374 depthDisabledStencilDesc.DepthEnable = false;
375 depthDisabledStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
376 depthDisabledStencilDesc.DepthFunc = D3D11_COMPARISON_LESS;
377 depthDisabledStencilDesc.StencilEnable = true;
378 depthDisabledStencilDesc.StencilReadMask = 0xFF;
379 depthDisabledStencilDesc.StencilWriteMask = 0xFF;
380 depthDisabledStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
381 depthDisabledStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
382 depthDisabledStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
383 depthDisabledStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
384 depthDisabledStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
385 depthDisabledStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR;
386 depthDisabledStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
387 depthDisabledStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
388
389 // Create the state using the device.
390 result = device_->CreateDepthStencilState(&depthDisabledStencilDesc, &depth_disabled_stencil_state_);
391 if (FAILED(result))
392 {
393 Logger::Get().Log("Failed to create depth disabled stencil state", __FILE__, __LINE__, Logger::LogLevel::Error);
394 return false;
395 }
396
397 // Clear the blend state description.
398 ZeroMemory(&blendStateDescription, sizeof(D3D11_BLEND_DESC));
399
400 // Create an alpha enabled blend state description.
401 blendStateDescription.RenderTarget[0].BlendEnable = TRUE;
402 blendStateDescription.RenderTarget[0].SrcBlend = D3D11_BLEND_ONE;
403 blendStateDescription.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
404 blendStateDescription.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
405 blendStateDescription.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
406 blendStateDescription.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
407 blendStateDescription.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
408 blendStateDescription.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
409
410 // Create the blend state using the description.
411 result = device_->CreateBlendState(&blendStateDescription, &alpha_enable_blending_state_);
412 if (FAILED(result))
413 {
414 Logger::Get().Log("Failed to create alpha enabled blend state", __FILE__, __LINE__, Logger::LogLevel::Error);
415 return false;
416 }
417
418 // Modify the description to create an alpha disabled blend state description.
419 blendStateDescription.RenderTarget[0].BlendEnable = FALSE;
420
421 // Create the blend state using the description.
422 result = device_->CreateBlendState(&blendStateDescription, &alpha_disable_blending_state_);
423 if (FAILED(result))
424 {
425 Logger::Get().Log("Failed to create alpha disabled blend state", __FILE__, __LINE__, Logger::LogLevel::Error);
426 return false;
427 }
428
429 return true;
430}
static Logger & Get()
Definition Logger.h:20
void Log(const std::string &message, const std::string &fileName, int lineNumber, LogLevel level=LogLevel::Info)
Definition Logger.h:158

◆ release_resources()

void d_3d_class::release_resources ( )

Release all Direct3D resources.

Definition at line 593 of file d_3d_class.cpp.

594{
595 Logger::Get().Log("Releasing D3D resources", __FILE__, __LINE__);
596
597 // libere la vue
598 if (render_target_view_)
599 {
600 render_target_view_->Release();
601 render_target_view_ = 0;
602 }
603
604 // libere le buffer de profondeur
605 if (depth_stencil_buffer_)
606 {
607 depth_stencil_buffer_->Release();
608 depth_stencil_buffer_ = 0;
609 }
610
611 // libere la vue de profondeur
612 if (depth_stencil_view_)
613 {
614 depth_stencil_view_->Release();
615 depth_stencil_view_ = 0;
616 }
617
618 Logger::Get().Log("D3D resources released", __FILE__, __LINE__);
619}

◆ reset_resources()

void d_3d_class::reset_resources ( int newWidth,
int newHeight )

Reset Direct3D resources based on the new width and height.

Parameters
newWidth
newHeight

Definition at line 622 of file d_3d_class.cpp.

623{
624 Logger::Get().Log("Resetting D3D resources", __FILE__, __LINE__);
625
626 HRESULT result;
627
628 ID3D11Texture2D* backBuffer;
629 result = swap_chain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&backBuffer);
630 if (FAILED(result))
631 {
632 Logger::Get().Log("Failed to get back buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
633 return;
634 }
635
636 result = device_->CreateRenderTargetView(backBuffer, NULL, &render_target_view_);
637 if (FAILED(result))
638 {
639 Logger::Get().Log("Failed to create render target view", __FILE__, __LINE__, Logger::LogLevel::Error);
640 return;
641 }
642
643 result = backBuffer->Release();
644 if (FAILED(result))
645 {
646 Logger::Get().Log("Failed to release back buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
647 return;
648 }
649
650 // Recreate the depth/stencil buffer and view
651 D3D11_TEXTURE2D_DESC depthBufferDesc;
652 ZeroMemory(&depthBufferDesc, sizeof(depthBufferDesc));
653 depthBufferDesc.Width = newWidth;
654 depthBufferDesc.Height = newHeight;
655 depthBufferDesc.MipLevels = 1;
656 depthBufferDesc.ArraySize = 1;
657 depthBufferDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
658 depthBufferDesc.SampleDesc.Count = 1;
659 depthBufferDesc.SampleDesc.Quality = 0;
660 depthBufferDesc.Usage = D3D11_USAGE_DEFAULT;
661 depthBufferDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
662 depthBufferDesc.CPUAccessFlags = 0;
663 depthBufferDesc.MiscFlags = 0;
664
665 D3D11_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc;
666 ZeroMemory(&depthStencilViewDesc, sizeof(depthStencilViewDesc));
667 depthStencilViewDesc.Format = depthBufferDesc.Format;
668 depthStencilViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
669 depthStencilViewDesc.Texture2D.MipSlice = 0;
670
671 // Other depthStencilDesc settings...
672 result = device_->CreateTexture2D(&depthBufferDesc, NULL, &depth_stencil_buffer_);
673 if (FAILED(result))
674 {
675 Logger::Get().Log("Failed to create depth stencil buffer", __FILE__, __LINE__, Logger::LogLevel::Error);
676 return;
677 }
678
679 result = device_->CreateDepthStencilView(depth_stencil_buffer_, &depthStencilViewDesc, &depth_stencil_view_);
680 if (FAILED(result))
681 {
682 Logger::Get().Log("Failed to create depth stencil view", __FILE__, __LINE__, Logger::LogLevel::Error);
683 return;
684 }
685
686 // Set the new render target and depth/stencil views for rendering
687 device_context_->OMSetRenderTargets(1, &render_target_view_, depth_stencil_view_);
688}

◆ reset_viewport()

void d_3d_class::reset_viewport ( )

Resets the viewport to the default settings.

Definition at line 585 of file d_3d_class.cpp.

586{
587 // Set the viewport.
588 device_context_->RSSetViewports(1, &viewport_);
589
590 return;
591}

◆ resize_swap_chain()

void d_3d_class::resize_swap_chain ( int width,
int height )

Resizes the swap chain to the specified width and height.

Parameters
widthNew width of the swap chain.
heightNew height of the swap chain.

Definition at line 695 of file d_3d_class.cpp.

696{
697
698 // log the new width and height
699 Logger::Get().Log("Resizing swap chain to " + std::to_string(newWidth) + "x" + std::to_string(newHeight), __FILE__, __LINE__);
700
701 HRESULT result;
702
703 // Release existing DirectX resources
705 device_context_->Flush();
706
707 // Resize the swap chain
708 result = swap_chain->ResizeBuffers(0, newWidth, newHeight, DXGI_FORMAT_UNKNOWN, 0);
709 if (FAILED(result))
710 {
711 Logger::Get().Log("Failed to resize swap chain", __FILE__, __LINE__, Logger::LogLevel::Error);
712 return;
713 }
714
715 // Reset the resources
716 reset_resources(newWidth, newHeight);
717
718 // Update the viewport
719 viewport_.Width = static_cast<float>(newWidth);
720 viewport_.Height = static_cast<float>(newHeight);
721 device_context_->RSSetViewports(1, &viewport_);
722}
void release_resources()
void reset_resources(int newWidth, int newHeight)

◆ set_back_buffer_render_target()

void d_3d_class::set_back_buffer_render_target ( )

Set the render target to the back buffer.

Definition at line 576 of file d_3d_class.cpp.

577{
578 // Bind the render target view and depth stencil buffer to the output render pipeline.
579 device_context_->OMSetRenderTargets(1, &render_target_view_, depth_stencil_view_);
580
581 return;
582}

◆ set_vsync()

void d_3d_class::set_vsync ( bool vsync)

Sets the vertical sync state.

Parameters
vsyncTrue to enable vertical sync, false to disable.

Definition at line 771 of file d_3d_class.cpp.

772{
773 vsync_enabled_ = vsync;
774}

◆ shutdown()

void d_3d_class::shutdown ( )

Releases Direct3D resources.

Definition at line 433 of file d_3d_class.cpp.

434{
435
436 Logger::Get().Log("Shutting down D3Dclass", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
437
438 // Before shutting down set to windowed mode or when you release the swap chain it will throw an exception.
439 if (swap_chain)
440 {
441 swap_chain->SetFullscreenState(false, NULL);
442 }
443
444 if (alpha_enable_blending_state_)
445 {
446 alpha_enable_blending_state_->Release();
447 alpha_enable_blending_state_ = 0;
448 }
449
450 if (alpha_disable_blending_state_)
451 {
452 alpha_disable_blending_state_->Release();
453 alpha_disable_blending_state_ = 0;
454 }
455
456 if (depth_disabled_stencil_state_)
457 {
458 depth_disabled_stencil_state_->Release();
459 depth_disabled_stencil_state_ = 0;
460 }
461
462 if (raster_state_)
463 {
464 raster_state_->Release();
465 raster_state_ = 0;
466 }
467
468 if (depth_stencil_view_)
469 {
470 depth_stencil_view_->Release();
471 depth_stencil_view_ = 0;
472 }
473
474 if (depth_stencil_state_)
475 {
476 depth_stencil_state_->Release();
477 depth_stencil_state_ = 0;
478 }
479
480 if (depth_stencil_buffer_)
481 {
482 depth_stencil_buffer_->Release();
483 depth_stencil_buffer_ = 0;
484 }
485
486 if (render_target_view_)
487 {
488 render_target_view_->Release();
489 render_target_view_ = 0;
490 }
491
492 if (device_context_)
493 {
494 device_context_->Release();
495 device_context_ = 0;
496 }
497
498 if (device_)
499 {
500 device_->Release();
501 device_ = 0;
502 }
503
504 if (swap_chain)
505 {
506 swap_chain->Release();
507 swap_chain = 0;
508 }
509
510 Logger::Get().Log("D3Dclass shutdown", __FILE__, __LINE__, Logger::LogLevel::Shutdown);
511
512 return;
513}

◆ turn_z_buffer_off()

void d_3d_class::turn_z_buffer_off ( )

Turn off the Z-buffer to disable depth.

Definition at line 731 of file d_3d_class.cpp.

732{
733 device_context_->OMSetDepthStencilState(depth_disabled_stencil_state_, 1);
734 return;
735}

◆ turn_z_buffer_on()

void d_3d_class::turn_z_buffer_on ( )

Turn on the Z-buffer to enable depth.

Definition at line 724 of file d_3d_class.cpp.

725{
726 device_context_->OMSetDepthStencilState(depth_stencil_state_, 1);
727 return;
728}

Member Data Documentation

◆ swap_chain

IDXGISwapChain* d_3d_class::swap_chain

Definition at line 85 of file d_3d_class.h.


The documentation for this class was generated from the following files: