#define???
#define is a C preprocessor directive. Subsequent instances of the defined symbol in the source code will be replaced with the value.
#define MYSYMBOL 666
int x = 10 * MYSYMBOL;
The preprocessor will replace MYSYMBOL with 666. x will equal 6660.
RTFM for details
‘But truth's a menace, science a public danger.’ Brave New World, Aldous Huxley
#define MYSYMBOL 666
int x = 10 * MYSYMBOL;
The preprocessor will replace MYSYMBOL with 666. x will equal 6660.
RTFM for details
‘But truth's a menace, science a public danger.’ Brave New World, Aldous Huxley
You can also put code there. Such as:
#define MyFunction cout<<"My Macro Function Has Been Called"
Then you could just say:
MyFunction;
Er, I think that''s right.
Alex Broadwin
A-Tronic Software & Design
-----
"if you fail in life, you were destined to fail. If you suceed in life, call me."
"The answer is out there."
"Please help, I''m using Windows!"
#define MyFunction cout<<"My Macro Function Has Been Called"
Then you could just say:
MyFunction;
Er, I think that''s right.
Alex Broadwin
A-Tronic Software & Design
-----
"if you fail in life, you were destined to fail. If you suceed in life, call me."
"The answer is out there."
"Please help, I''m using Windows!"
A really handy thing is something like
#define Wally(x,y,z) {x=z*(y*z+0.5f)}
so you wouldn't have to retype the same thing over and over
Another example is this.
It saves a whole lot of typing to draw the same cube.
all you have to do is type GlCube(x,y,z,w); where x,y,z is the position of the cube, and w is half the width of any side
#define GlCube(x,y,z,w); {\
glVertex3f(x-w,y-w,z-w); glVertex3f(x-w,y+w,z-w); glVertex3f(x+w,y+w,z-w); glVertex3f(x+w,y-w,z-w);\
glVertex3f(x-w,y-w,z-w); glVertex3f(x-w,y-w,z+w); glVertex3f(x+w,y-w,z+w); glVertex3f(x+w,y-w,z-w);\
glVertex3f(x-w,y-w,z-w); glVertex3f(x-w,y-w,z+w); glVertex3f(x-w,y+w,z+w); glVertex3f(x-w,y+w,z-w);\
glVertex3f(x-w,y-w,z+w); glVertex3f(x-w,y+w,z+w); glVertex3f(x+w,y+w,z+w); glVertex3f(x+w,y-w,z+w);\
glVertex3f(x-w,y+w,z-w); glVertex3f(x-w,y+w,z+w); glVertex3f(x+w,y+w,z+w); glVertex3f(x+w,y+w,z-w);\
glVertex3f(x+w,y-w,z-w); glVertex3f(x+w,y-w,z+w); glVertex3f(x+w,y+w,z+w); glVertex3f(x+w,y+w,z-w);}
You can also use it if you want to change (for example) the window size/screen resolution by doing something like
#define WINDOW_SIZE 2
#if WINDOW_SIZE == 0
#define WINDOW_X 320
#define WINDOW_Y 240
#elif WINDOW_SIZE == 1
#define WINDOW_X 640
#define WINDOW_Y 480
#elif WINDOW_SIZE == 2
#define WINDOW_X 800
#define WINDOW_Y 600
#else
#define WINDOW_X 1024
#define WINDOW_Y 768
#endif
This will make the window 800x600, if nothing is defined or something else is, the window will be 1024x768.
Of course you will have to put WINDOW_X,WINDOW_Y into your window creating function .
Another is for error checking and stuff
#define LookupSize 1000
#define LookupRealSize 1002
#if LookupSize!=LookupRealSize-1
#error LookupRealSize must be equal to LookupSize + 1
#endif
LookupRealSize should actually be 1001, but since it isn't, the compiler will call up the line above with the #error in it and display the above message
Hope this helped you.
Beer - the love catalyst
good ol' homepage
Edited by - Dredge-Master on January 10, 2002 10:07:29 AM
#define Wally(x,y,z) {x=z*(y*z+0.5f)}
so you wouldn't have to retype the same thing over and over
Another example is this.
It saves a whole lot of typing to draw the same cube.
all you have to do is type GlCube(x,y,z,w); where x,y,z is the position of the cube, and w is half the width of any side
#define GlCube(x,y,z,w); {\
glVertex3f(x-w,y-w,z-w); glVertex3f(x-w,y+w,z-w); glVertex3f(x+w,y+w,z-w); glVertex3f(x+w,y-w,z-w);\
glVertex3f(x-w,y-w,z-w); glVertex3f(x-w,y-w,z+w); glVertex3f(x+w,y-w,z+w); glVertex3f(x+w,y-w,z-w);\
glVertex3f(x-w,y-w,z-w); glVertex3f(x-w,y-w,z+w); glVertex3f(x-w,y+w,z+w); glVertex3f(x-w,y+w,z-w);\
glVertex3f(x-w,y-w,z+w); glVertex3f(x-w,y+w,z+w); glVertex3f(x+w,y+w,z+w); glVertex3f(x+w,y-w,z+w);\
glVertex3f(x-w,y+w,z-w); glVertex3f(x-w,y+w,z+w); glVertex3f(x+w,y+w,z+w); glVertex3f(x+w,y+w,z-w);\
glVertex3f(x+w,y-w,z-w); glVertex3f(x+w,y-w,z+w); glVertex3f(x+w,y+w,z+w); glVertex3f(x+w,y+w,z-w);}
You can also use it if you want to change (for example) the window size/screen resolution by doing something like
#define WINDOW_SIZE 2
#if WINDOW_SIZE == 0
#define WINDOW_X 320
#define WINDOW_Y 240
#elif WINDOW_SIZE == 1
#define WINDOW_X 640
#define WINDOW_Y 480
#elif WINDOW_SIZE == 2
#define WINDOW_X 800
#define WINDOW_Y 600
#else
#define WINDOW_X 1024
#define WINDOW_Y 768
#endif
This will make the window 800x600, if nothing is defined or something else is, the window will be 1024x768.
Of course you will have to put WINDOW_X,WINDOW_Y into your window creating function .
Another is for error checking and stuff
#define LookupSize 1000
#define LookupRealSize 1002
#if LookupSize!=LookupRealSize-1
#error LookupRealSize must be equal to LookupSize + 1
#endif
LookupRealSize should actually be 1001, but since it isn't, the compiler will call up the line above with the #error in it and display the above message
Hope this helped you.
Beer - the love catalyst
good ol' homepage
Edited by - Dredge-Master on January 10, 2002 10:07:29 AM
#define macros are useful, but they can be hazardous if you overuse them. take this macro:
If you were to use call that with a parameter like 3+2, the result would be 11 instead of the 25 you would expect. This is because the program interprets numSquared(3+2) as 3+2*3+2, so the order of operations would multiply the 2 and 3 first. This would usually be solved by putting parenthesis around the x.
This way, x is evaluated before it is multiplied. Another problem is if you put in a parameter like this:
After running those two lines, you would expect n to be 4 and n_squared to be 9. However, n would be 5 and n_squared would be 12. This is because it is interpreted as:
As you can see, n is incremented twice. These kind of problems can be hard to track down.
You could do something like that, but I would strongly advise against it. This would be prone to a lot of errors. Use a function instead.
No, HTML is not an OO language.
#define numSquared(x) x * x
If you were to use call that with a parameter like 3+2, the result would be 11 instead of the 25 you would expect. This is because the program interprets numSquared(3+2) as 3+2*3+2, so the order of operations would multiply the 2 and 3 first. This would usually be solved by putting parenthesis around the x.
#define numSquared(x) (x) * (x)
This way, x is evaluated before it is multiplied. Another problem is if you put in a parameter like this:
int n = 3;int n_squared = numSquared(n++);
After running those two lines, you would expect n to be 4 and n_squared to be 9. However, n would be 5 and n_squared would be 12. This is because it is interpreted as:
int n = 3;int n_squared = n++ * n++;
As you can see, n is incremented twice. These kind of problems can be hard to track down.
quote:Original post by Dredge-Master
It saves a whole lot of typing to draw the same cube.
all you have to do is type GlCube(x,y,z,w); where x,y,z is the position of the cube, and w is half the width of any side
#define GlCube(x,y,z,w); {\
glVertex3f(x-w,y-w,z-w); glVertex3f(x-w,y+w,z-w); glVertex3f(x+w,y+w,z-w); glVertex3f(x+w,y-w,z-w);\
glVertex3f(x-w,y-w,z-w); glVertex3f(x-w,y-w,z+w); glVertex3f(x+w,y-w,z+w); glVertex3f(x+w,y-w,z-w);\
glVertex3f(x-w,y-w,z-w); glVertex3f(x-w,y-w,z+w); glVertex3f(x-w,y+w,z+w); glVertex3f(x-w,y+w,z-w);\
glVertex3f(x-w,y-w,z+w); glVertex3f(x-w,y+w,z+w); glVertex3f(x+w,y+w,z+w); glVertex3f(x+w,y-w,z+w);\
glVertex3f(x-w,y+w,z-w); glVertex3f(x-w,y+w,z+w); glVertex3f(x+w,y+w,z+w); glVertex3f(x+w,y+w,z-w);\
glVertex3f(x+w,y-w,z-w); glVertex3f(x+w,y-w,z+w); glVertex3f(x+w,y+w,z+w); glVertex3f(x+w,y+w,z-w);}
You could do something like that, but I would strongly advise against it. This would be prone to a lot of errors. Use a function instead.
No, HTML is not an OO language.
a program which infested with many MACROS will also be harder to debug. Because the debugger will have to reference to the post processed code.
One thing macros are really good for is creating a debugging macro; something like:
#define assertMsg(expr, msg)\ if(!(expr))\ {\ I3D::DebugTrace(msg, __LINE__, __FILE__, "Assertion Failure");\ _asm{int 3}\ }//and you''d use it as soassertMsg(pStuff!=NULL, "Deferencing a NULL pointer!");
quote:Original post by masonium
You could do something like that, but I would strongly advise against it. This would be prone to a lot of errors. Use a function instead.
The one I posted was just an example, but in the end they can proove to be faster than a macro.
Eg, I use them (more complex) in the last voxel program I wrote when rendering and lighting 20000 quads to raise it from 57fps to 59fps.
Okay, it isn''t that much, but it still helps for speed increases (less function calls - passing a single pointer is slower than passing nothing at all and having it coded into the program).
Beer - the love catalyst
good ol'' homepage
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement