Skip to main content
GameDev.net gamedev.net
🔒 Locked

NULL in functions

Started by luxitan Jan 6, 2003 at 4:28 PM 4 replies 450+ views
Original Post
luxitan
luxitan
how can i allow myself to put NULL instead of the predefined value to the function ie:
  
D3DMATERIAL9 CNothing::MakeMaterial(D3DCOLORVALUE diff, D3DCOLORVALUE amb, D3DCOLORVALUE emi, D3DCOLORVALUE spec, float power)
{
D3DCOLORVALUE black = MakeColorA(0.0f, 0.0f, 0.0f);
D3DMATERIAL9 temp = 
{
  diff==NULL?black:diff, 
  amb==NULL?black:amb, 
  spec==NULL?black:spec, 
  emi==NULL?black:emi, 
  power
}; 
return temp;
}
  
for example if diff is NULL i put the default value in if it isn't i put the value. thx [edited by - luxitan on January 6, 2003 5:34:07 PM]
To be considered a genius you just have to say what everybody knows in a way very few understand
CWizard
CWizard
I cannot easily see your question (i'm a bit drunk ), but if yyou replaced your == with a single =, I think it would do what you want.

EDIT: hehe, I was aparently too drunk... I cannot see any problem with your function. Is there a problem, and if so, what?




[edited by - CWizard on January 6, 2003 5:36:08 PM]
luxitan
luxitan
the problem is :

MakeMaterial(MakeColorA(0.1f,0.1f,1.0f),NULL,NULL,NULL);

gives me an error saying that const int cannot be converted to color, ie doesnt accept my NULL

i think the only option is to use pointers instead of the value

[edited by - luxitan on January 6, 2003 6:18:53 PM]
To be considered a genius you just have to say what everybody knows in a way very few understand
Cedric
Cedric
Why don''t you create a dummy color?

const D3DCOLORVALUE NULLCOLOR = MakeColorA(0.666f,0.666f,0.666f);

It''s very bad style, but I don''t think there''s a good way to do what you want in C++. You could always pass a structure, and only fill the required members.

Cédric
sardak
sardak
Are the D3DCOLORVALUE parameters pointers or actual values? If they''re pointers, NULL should work fine in that, but if they''re actual values, then there''d be a parameter size conflict I would think.
SabreMan
SabreMan
quote:
Original post by luxitan
MakeMaterial(MakeColorA(0.1f,0.1f,1.0f),NULL,NULL,NULL);

gives me an error saying that const int cannot be converted to color, ie doesnt accept my NULL

Yes, and that's an accurate description of the problem. Presumably MakeColorA is a macro or function returning some sort of "Color" object, no? In that case, the infinite wisdom of C++ dictates that you cannot pass null where an object is expected. You have a few choices:

a) pass the parameters by pointer, so that you can check for absence of a parameter.

b) work around the C++ type system by use of the "Null Object Idiom". Basically, the Null Object Idiom involves identifying a set of values for a Color object which are to be interpreted as giving null identity. You might do this by using a combination of values which will never be viable for normal usage. For a Color object, I suspect there is no such set of values, so you might consider modifying the Color class to have an "is_null" flag, and add a constructor that accepts "Color(NULL)".

c) provide an overload of MakeMaterial which omits the extraneous values, possibly delegating to a common function which contains the shared implementation between the extant and new functions.

[edited by - SabreMan on January 7, 2003 6:39:51 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.