0

I am currently trying to learn DirectX 11 using C++ in Visual Studio 2017 and have run into a problem.

#include <Windows.h>
#include <windowsx.h>
#include <d3d11.h>
#include <d3d12.h>
#include <d3dcompiler.h>

// include D3D Librararies
#pragma comment (lib, "d3d11.lib")
#pragma comment (lib, "d3d12.lib")
#pragma comment (lib, "D3DCompiler.lib")

// Definitions
#define SCREEN_WIDTH    800
#define SCREEN_HEIGHT   600

// Global Declarations
IDXGISwapChain *swapchain;              // Pointer to Swap Chain Buffer
ID3D11DeviceContext *devcon;            // Pointer to managing Device for GPU
ID3D11Device *dev;                      // Pointer to GPU
ID3D11RenderTargetView *backBuffer;     // A pointer to an object that holds info about the render target
ID3D11VertexShader *pVS;                // The Vertex shader
ID3D11PixelShader *pPS;                 // The Pixel Shader


struct VERTEX {
   FLOAT X, Y, Z;           // Position
   D3DCOLORVALUE Color; // Color
};

// float color2[4] = { 1.0f, 0.0f, 0.0f, 1.0f };
VERTEX OurVertex = { 0.0f, 0.5f, 0.0f,  D3DCOLORVALUE(1.0f, 0.0f, 0.0f, 1.0f)}; // No suitable constructor exists to convert from "float" to "_D3DCOLORVALUE"

There is an error inside of OurVertex, where C++ can't seem to convert the float numbers in D3DCOLORVALUE() into a D3DCOLORVALUE item. I tried looking at other solutions, and couldn't find any to solve my case.

Is there a solution to this?

3 Answers3

0

D3DCOLORVALUE doesn't have a constructor taking 4 floats. You should use list initialization:

VERTEX OurVertex = {0.0f, 0.5f, 0.0f, D3DCOLORVALUE{1.0f, 0.0f, 0.0f, 1.0f}}; 

Or simpler:

VERTEX OurVertex = {0.0f, 0.5f, 0.0f,  {1.0f, 0.0f, 0.0f, 1.0f}};
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
0

@TedLyngmo Ted basically said it all about how you should do this.

However based on your comment earlier,

Curly Braces are used for D3DCOLORVALUE. I will keep that in mind

You seem to be thinking this as a special case. Instead, the way of understanding that should be whether you are calling a function or you are creating an object.

With modern C++s, list initialization are preferred in most cases. Check this question: Why is list initialization (using curly braces) better than the alternatives?

Basically, often time you could use myClass objectName { myValues }; to declare an object, or myClass { myValues }; if you are to create a temporary one. So that parentheses could be reserved for either function calls or objects with std::initializer_list ctors.

Ranoiaetep
  • 5,872
  • 1
  • 14
  • 39
0

Keep in mind that D3DCOLORVALUE is specifically a Direct3D 9 construct. Many people will use four floats for per-vertex color (at least when starting out), but if you wanted to use a packed color format the DirectXMath type XMCOLOR in the DirectX::PackedVector namespace matches the old D3DCOLORVALUE.

For Direct3D 11 functions that take an array of four floats for the color (such as ClearRenderTargetView, DirectXMath also defines a number of color constants in the DirectXColors.h header.

As you are new to DirectX 11, you should take a look at DirectX Tool Kit.

Chuck Walbourn
  • 38,259
  • 2
  • 58
  • 81