1. [OT] a couple of C macro queries

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

new topic     » topic index » view message » categorize

2. Re: [OT] a couple of C macro queries

petelomax said...

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.

petelomax said...

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.

new topic     » goto parent     » topic index » view message » categorize

3. Re: [OT] a couple of C macro queries

petelomax said...

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> 
new topic     » goto parent     » topic index » view message » categorize

4. Re: [OT] a couple of C macro queries

jimcbrown said...
petelomax said...

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.

new topic     » goto parent     » topic index » view message » categorize

5. Re: [OT] a couple of C macro queries

jimcbrown said...

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.

jimcbrown said...

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

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu