1. [OT] a couple of C macro queries
- Posted by petelomax May 28, 2016
- 1320 views
One for the C fans. Translating this bit of C code:
#define INVERT_Y(_y) (height-y) static int motion_cb(Ihandle *ih,int x,int y,char* status) { (void)status; if (move) { double dif_x, dif_y; int height = IupGetInt2(ih, "RASTERSIZE"); dif_x = x - pos_x; dif_y = y - pos_y; pos_x = x; pos_y = y; gluUnProject(pos_x, INVERT_Y(pos_y), 0.0,...
Should I really just replace INVERT_Y(pos_y) with (height-y)?
Is the use of pos_y a complete red herring?
Also, while I'm here, is (void)status; just a way of suppressing unused errors/warnings?
Pete
2. Re: [OT] a couple of C macro queries
- Posted by jimcbrown (admin) May 28, 2016
- 1301 views
One for the C fans. Translating this bit of C code:
#define INVERT_Y(_y) (height-y) static int motion_cb(Ihandle *ih,int x,int y,char* status) { (void)status; if (move) { double dif_x, dif_y; int height = IupGetInt2(ih, "RASTERSIZE"); dif_x = x - pos_x; dif_y = y - pos_y; pos_x = x; pos_y = y; gluUnProject(pos_x, INVERT_Y(pos_y), 0.0,...
Should I really just replace INVERT_Y(pos_y) with (height-y)?
Is the use of pos_y a complete red herring?
I think so. I can't find any reference to the underscore having a special meaning to the preprocessor, and _y is a valid identifier in C (and to the C preprocessor itself).
Make you wonder why they didn't simply just write "height-y" in that spot in the first place... it's less typing for one.
Also, while I'm here, is (void)status; just a way of suppressing unused errors/warnings?
Pete
I don't know, but that's my guess.
3. Re: [OT] a couple of C macro queries
- Posted by ne1uno May 28, 2016
- 1305 views
One for the C fans. Translating this bit of C code:
#define INVERT_Y(_y) (height-y) static int motion_cb(Ihandle *ih,int x,int y,char* status) { (void)status; if (move) { double dif_x, dif_y; int height = IupGetInt2(ih, "RASTERSIZE"); dif_x = x - pos_x; dif_y = y - pos_y; pos_x = x; pos_y = y; gluUnProject(pos_x, INVERT_Y(pos_y), 0.0,...
Should I really just replace INVERT_Y(pos_y) with (height-y)?
Is the use of pos_y a complete red herring?
Also, while I'm here, is (void)status; just a way of suppressing unused errors/warnings?
Pete
-E is preprocess only $ gcc -E INVERT_Y.c
gluUnProject(pos_x, (height-y), 0.0, ... <eucode>
4. Re: [OT] a couple of C macro queries
- Posted by jimcbrown (admin) May 28, 2016
- 1291 views
One for the C fans. Translating this bit of C code:
#define INVERT_Y(_y) (height-y) static int motion_cb(Ihandle *ih,int x,int y,char* status) { (void)status; if (move) { double dif_x, dif_y; int height = IupGetInt2(ih, "RASTERSIZE"); dif_x = x - pos_x; dif_y = y - pos_y; pos_x = x; pos_y = y; gluUnProject(pos_x, INVERT_Y(pos_y), 0.0,...
Should I really just replace INVERT_Y(pos_y) with (height-y)?
Is the use of pos_y a complete red herring?
I think so. I can't find any reference to the underscore having a special meaning to the preprocessor, and _y is a valid identifier in C (and to the C preprocessor itself).
Make you wonder why they didn't simply just write "height-y" in that spot in the first place... it's less typing for one.
Thinking it over, I think the original C coder screwed up the macro, but it just so happened the resulting statement was equivalent to the intended one.
I suspect that this was the C code that was intended:
#define INVERT_Y(_y) (height-(_y)) static int motion_cb(Ihandle *ih,int x,int y,char* status) { (void)status; if (move) { double dif_x, dif_y; int height = IupGetInt2(ih, "RASTERSIZE"); dif_x = x - pos_x; dif_y = y - pos_y; pos_x = x; pos_y = y; gluUnProject(pos_x, INVERT_Y(pos_y), 0.0,...
which, postprocessing, would have become this:
static int motion_cb(Ihandle *ih,int x,int y,char* status) { (void)status; if (move) { double dif_x, dif_y; int height = IupGetInt2(ih, "RASTERSIZE"); dif_x = x - pos_x; dif_y = y - pos_y; pos_x = x; pos_y = y; gluUnProject(pos_x, height-(pos_y), 0.0,...
It just so happens that pos_y and y have the same value (since pos_y was set to y right before the call), so the bad C macro ended up coming up with the desired result by accident.
I guess pos_y is a module level or file level variable that is initialize elsewhere, as I don't see it declared or initialized in this snippet.
5. Re: [OT] a couple of C macro queries
- Posted by petelomax May 28, 2016
- 1269 views
Thinking it over, I think the original C coder screwed up the macro, but it just so happened the resulting statement was equivalent to the intended one.
I suspect that this was the C code that was intended:
It just so happens that pos_y and y have the same value (since pos_y was set to y right before the call), so the bad C macro ended up coming up with the desired result by accident.
Oh, you're a star! That suddenly all makes perfect sense.
I guess pos_y is a module level or file level variable that is initialize elsewhere, as I don't see it declared or initialized in this snippet.
Yes, sorry, it is, at top-level
integer pos_x, pos_y;
Thanks, Pete