Reputation: 45
'm coding a image viewer and converter. _D3DCOLORVALUE is defined in d3d8types.h file. I'm using DirectX 8 SDK. If I define a struct with _D3DCOLORVALUE occurred some errors like this :
1. error C2504: '_D3DCOLORVALUE' : base class undefined
2. error C2061: syntax error : identifier '_D3DCOLORVALUE'
3. _D3DCOLORVALUE not a class or struct name
What could be problem?
Note : this project perfectly working on VB6 but I tried build it on VS2012
My3DStruct.h
#include "d3d8types.h"
struct __ColorValue : public _D3DCOLORVALUE //problem is here
{
public:
void operator = (const D3DCOLORVALUE& cv) { r = cv.r; g = cv.g; b = cv.b; a = cv.a; }
void operator = (D3DCOLOR cr) { r = ((cr & 0x00ff0000)>>16)/255.0f; g = ((cr & 0x0000ff00)>>8)/255.0f; b = (cr & 0x000000ff)/255.0f; a = ((cr & 0xff000000) >> 24)/255.0f; }
void Set(float r2, float g2, float b2, float a2) { r = r2; g = g2; b = b2; a = a2; }
D3DCOLOR ToD3DCOLOR() { return (((DWORD)(a*255.0f))<<24) | (((DWORD)(r*255.0f))<<16) | (((DWORD)(g*255.0f))<<8) | (((DWORD)(b*255.0f))); }
__ColorValue() {}
__ColorValue(D3DCOLOR cr) { *this = cr; }
__ColorValue(float r2, float g2, float b2, float a2) { this->Set(r2, g2, b2, a2); }
void operator += (float fDelta) { r += fDelta; g += fDelta; b += fDelta; a += fDelta; }
void operator -= (float fDelta) { r -= fDelta; g -= fDelta; b -= fDelta; a -= fDelta; }
void operator *= (float fDelta) { r *= fDelta; g *= fDelta; b *= fDelta; a *= fDelta; }
void operator /= (float fDelta) { if(0 == fDelta) return; r /= fDelta; g /= fDelta; b /= fDelta; a /= fDelta; }
D3DCOLORVALUE operator + (float fDelta) { __ColorValue cv = *this; cv.r += fDelta; cv.g += fDelta; cv.b += fDelta; cv.a += fDelta; return cv; }
D3DCOLORVALUE operator - (float fDelta) { __ColorValue cv = *this; cv.r -= fDelta; cv.g -= fDelta; cv.b -= fDelta; cv.a -= fDelta; return cv; }
D3DCOLORVALUE operator * (float fDelta) { __ColorValue cv = *this; cv.r *= fDelta; cv.g *= fDelta; cv.b *= fDelta; cv.a *= fDelta; return cv; }
D3DCOLORVALUE operator / (float fDelta) { __ColorValue cv = *this; cv.r /= fDelta; cv.g /= fDelta; cv.b /= fDelta; cv.a /= fDelta; return cv; }
void operator += (const D3DCOLORVALUE& cv) { r += cv.r; g += cv.g; b += cv.b; a += cv.a; }
void operator -= (const D3DCOLORVALUE& cv) { r -= cv.r; g -= cv.g; b -= cv.b; a -= cv.a; }
void operator *= (const D3DCOLORVALUE& cv) { r *= cv.r; g *= cv.g; b *= cv.b; a *= cv.a; }
void operator /= (const D3DCOLORVALUE& cv) { r /= cv.r; g /= cv.g; b /= cv.b; a /= cv.a; }
D3DCOLORVALUE operator + (const D3DCOLORVALUE& cv) { __ColorValue cv2(cv.r, cv.g, cv.b, cv.a); cv2 += cv; return cv2; }
D3DCOLORVALUE operator - (const D3DCOLORVALUE& cv) { __ColorValue cv2(cv.r, cv.g, cv.b, cv.a); cv2 -= cv; return cv2; }
D3DCOLORVALUE operator * (const D3DCOLORVALUE& cv) { __ColorValue cv2(cv.r, cv.g, cv.b, cv.a); cv2 *= cv; return cv2; }
D3DCOLORVALUE operator / (const D3DCOLORVALUE& cv) { __ColorValue cv2(cv.r, cv.g, cv.b, cv.a); cv2 /= cv; return cv2; }
};
d3d8types.h
#ifndef D3DCOLORVALUE_DEFINED
typedef struct _D3DCOLORVALUE {
float r;
float g;
float b;
float a;
} D3DCOLORVALUE;
#define D3DCOLORVALUE_DEFINED
#endif
Upvotes: 0
Views: 941
Reputation: 2253
All identifiers beginning with two underscores (__
) are reserved in C++; you should never use them for your own code anywhere.
You should use D3DCOLORVALUE
instead of _D3DCOLORVALUE
. The DirectX 8 SDK is pretty old now, but since you're using that old version you can look at my free book The Direct3D Graphics Pipeline that is a mix of DirectX 8/9. Even with the mix in there, that material should be useful to you when using this old a version of DirectX. Chapter 1 discusses D3DCOLOR
and D3DCOLORVALUE
. Are you using DirectX 8 because you're converting VB6 code and Direct3D 8 was the last API exposed to VB? You're probably better off using Direct3D 9 when doing the port. Direct3D 10, 11 and 12 are significantly different from 9, but 9 is mostly similar to 8 and 8.1.
The idiom typedef struct _X X;
is a C-ism because C doesn't recognize the name of a structure as a complete type. In C you have to write struct X
all over the place, hence the common idiom to introduce the real structure as the name _X
and introduce X
as a synonym for struct _X
with typedef
. All of this is harmless in C++, but also completely unnecessary. In C++, saying struct X
means you can use X
as the type wherever you need it without forcing you to write struct X
or use a typedef
.
It looks like you're just trying to provide syntactic sugar for manipulating color values by creating operator+
members on your ColorValue
structure. You don't need to derive from D3DCOLORVALUE
in order to achieve this; simply define the operators as free functions with appropriate types:
inline D3DCOLORVALUE &operator+(D3DCOLORVALUE &lhs, float delta)
{
lhs.r += delta;
lhs.g += delta;
lhs.b += delta;
lhs.a += delta;
return lhs;
}
I believe Microsoft themselves introduced such free functions or member functions on the structures into the SDK at some point. I don't recall which version introduced that, but it was probably after DirectX 8.
Finally, it is generally not a good idea to write your own classes that derive from value types like D3DCOLORVALUE
. These are simple structures that don't have virtual destructors and deriving from them leaves you susceptible to object slicing errors. I recommend instead that you prefer aggregation over inheritance.
If your only motivation was to get nice operator+
behavior on D3DCOLORVALUE
values, then I suggest the free function approach instead of derivation.
Upvotes: 1