Jump to content
  • Advertisement
Sign in to follow this  
thejelmega

DX12 [Solved][D3D11] D3D not presenting

This topic is 734 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So after some people telling me it would be better do start with dx11, before dx12, I decided to do it, but now I have a problem I've hadn't had before: dx11 is not presenting anything to the screen.

I've been following this tutorial http://www.braynzarsoft.net/viewtutorial/q16390-3-initializing-direct3d-11

 

I'm already gonna thank you for the help.

 

Code:

 

Renderer.h

[spoiler]

#pragma once
 
#include "Helpers\Util.h"
 
namespace VEH
{
class Window;
}
 
namespace VE
{
struct Vertex
{
Vertex() {}
Vertex(float x, float y, float z) : pos(x, y, z) {}
 
DirectX::XMFLOAT3 pos;
};
 
 
class Renderer
{
public:
Renderer(VEH::Window* window);
~Renderer();
 
bool Init(HINSTANCE hInstance);
bool InitScene();
void Update();
void Render();
void Shutdown();
 
private:
 
IDXGISwapChain* m_pSwapChain = nullptr;
ID3D11Device* m_pDevice = nullptr;
ID3D11DeviceContext* m_pDevCon = nullptr;
ID3D11RenderTargetView* m_pRenderTargetView = nullptr;
 
VEH::Window* m_pWindow = nullptr;
};
}

[/spoiler]

 

Renderer.cpp

[spoiler]

#include "Renderer.h"
#include "Handlers\Window.h"
 
namespace VE
{
Renderer::Renderer(VEH::Window* window) : m_pWindow(window)
{
}
 
Renderer::~Renderer()
{
}
 
bool Renderer::Init(HINSTANCE hInstance)
{
HRESULT hr;
 
// describe the back buffer
DXGI_MODE_DESC bufferDesc;
ZeroMemory(&bufferDesc, sizeof(DXGI_MODE_DESC)); 
 
bufferDesc.Width = m_pWindow->GetWidth();
bufferDesc.Height = m_pWindow->GetHeight();
bufferDesc.RefreshRate.Numerator = 60;
bufferDesc.RefreshRate.Denominator = 1;
bufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
bufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
bufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
 
//Describe the swapchain
DXGI_SWAP_CHAIN_DESC swapChainDesc;
ZeroMemory(&swapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC));
 
swapChainDesc.BufferDesc = bufferDesc;
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.SampleDesc.Quality = 0;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.BufferCount = 1;
swapChainDesc.OutputWindow = m_pWindow->GetHwnd();
swapChainDesc.Windowed = !m_pWindow->IsFullscreen();
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
 
UINT creationFlags = 0;
 
#ifdef _DEBUG
creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif
 
D3D_FEATURE_LEVEL featureLevel = D3D_FEATURE_LEVEL_11_0;
 
//Create swap chain and device
hr = D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, creationFlags, &featureLevel, 1, D3D11_SDK_VERSION, &swapChainDesc, &m_pSwapChain, &m_pDevice, NULL, &m_pDevCon);
if (FAILED(hr))
{
MessageBox(NULL, L"Swapchain and Device creation failed!", L"Error", MB_OK | MB_ICONERROR);
return false;
}
 
//Create the back bbuffer
ID3D11Texture2D* backBuffer;
hr = m_pSwapChain->GetBuffer(0, IID_PPV_ARGS(&backBuffer));
if (FAILED(hr))
{
return false;
}
 
//Create the render target
hr = m_pDevice->CreateRenderTargetView(backBuffer, NULL, &m_pRenderTargetView);
backBuffer->Release();
if (FAILED(hr))
{
return false;
}
 
//set the render target view
m_pDevCon->OMSetRenderTargets(1, &m_pRenderTargetView, NULL);
 
return true;
}
 
bool Renderer::InitScene()
{
 
 
return true;
}
 
void Renderer::Update()
{
 
}
 
void Renderer::Render()
{
//Clear our back buffer to the updated color
float clearColor[4] = {0.0f, 0.1f, 0.2f, 1.0f};
m_pDevCon->ClearRenderTargetView(m_pRenderTargetView, clearColor);
 
//Present the backbuffer to the screen
m_pSwapChain->Present(0, 0);
}
 
void Renderer::Shutdown()
{
// release COM objects
SAFE_RELEASE(m_pRenderTargetView);
SAFE_RELEASE(m_pSwapChain);
SAFE_RELEASE(m_pDevice);
SAFE_RELEASE(m_pDevCon)
}
}

[/spoiler]

 

Util.h

[spoiler]

#pragma once
 
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN // Exclude rarely used stuff from the windows header
#endif
 
#pragma comment(lib, "d3d11.lib")
#pragma comment(lib, "dxgi.lib")
#pragma comment(lib, "d3dcompiler.lib")
 
#include <windows.h>
#include <d3d11.h>
#include <dxgi.h>
#include <d3dcompiler.h>
#include <DirectXMath.h>
 
#include <string>
 
#define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p) = nullptr; } }
#define SAFE_DELETE(a) { if((a) != nullptr) { delete (a); (a) = nullptr; } }

[/spoiler]

 

Main.cpp

[spoiler]

#include "Handlers\Window.h"
#include "Renderer.h"
 
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR cmdLine, int nCmdShow)
{
VEH::Window* pWindow = new VEH::Window(hInstance, nCmdShow, 800, 600, L"Voxel Engine DX11 v0.000.00.00001", false);
 
if (!pWindow->Init())
{
MessageBox(NULL, L"Window creation failed!", L"Error", MB_OK);
return 0;
}
 
VE::Renderer* pRenderer = new VE::Renderer(pWindow);
if (!pRenderer->Init(hInstance))
{
MessageBox(NULL, L"Renderer creation failed!", L"Error", MB_OK);
return 0;
}
if (!pRenderer->InitScene())
{
MessageBox(NULL, L"Scene creation failed!", L"Error", MB_OK);
return 0;
}
 
while (pWindow->Update())
{
pRenderer->Update();
pRenderer->Render();
}
 
pRenderer->Shutdown();
pWindow->Close();
 
delete pRenderer;
delete pWindow;
 
return 0;
}

[/spoiler]

Share this post


Link to post
Share on other sites
Advertisement

Yes, that was my problem.

 

I've just used AdjustWindowRect() during the creation of my window and now it works.

 

Thank you very much.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!