CC65, array of structures supposedly bad?

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems.

Moderator: Moderators

User avatar
DRW
Posts: 1908
Joined: Sat Sep 07, 2013 2:59 pm

Re: CC65, array of structures supposedly bad?

Post by DRW » Mon Jun 25, 2018 12:34 am

Banshaku wrote:without passing parameter from the C side and how much it would be worth it.
I do it like this:

Code: Select all

void MyFunction(int a, int b, int c);
becomes:

Code: Select all

int MyFunction_A;
int MyFunction_B;
int MyFunction_C;

void MyFunction_(void);

#define MyFunction(a, b, c)\
{\
    MyFunction_A = a;\
    MyFunction_B = b;\
    MyFunction_C = c;\
    MyFunction_();\
}
Available now: My game "City Trouble".
Website: https://megacatstudios.com/products/city-trouble
Trailer: https://youtu.be/IYXpP59qSxA
Gameplay: https://youtu.be/Eee0yurkIW4
German Retro Gamer article: http://i67.tinypic.com/345o108.jpg

tepples
Posts: 21713
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: CC65, array of structures supposedly bad?

Post by tepples » Mon Jun 25, 2018 6:48 am

You appear to be using a preprocessor macro as a workaround for lack of static inline. For this to work, you will need to change the { and } in your macro to do { and } while (0) respectively in order to keep the following semicolon from interfering with the parsing of if...else constructions. Otherwise, the macro will expand undesirably as follows:

Code: Select all

if (some_condition)
    MyFunction(a, b, c);
else
    SomethingElse();

/* becomes, after preprocessing and reindenting */

if (some_condition)
    {
        MyFunction_A = a;
        MyFunction_B = b;
        MyFunction_C = c;
        MyFunction_();
    }

;  /* This ends the if statement because it is something other than else that directly follows its first statement or compound statement. */
else  /* This is a syntax error because it does not follow an open if statement. */
    SomethingElse();

/* But with my suggestion, it would become */

if (some_condition)
    do {
        MyFunction_A = a;
        MyFunction_B = b;
        MyFunction_C = c;
        MyFunction_();
    } while (0);  /* the do...while swallows up the semicolon */
else  /* properly recognized as belonging to the if statement */
    SomethingElse();

User avatar
DRW
Posts: 1908
Joined: Sat Sep 07, 2013 2:59 pm

Re: CC65, array of structures supposedly bad?

Post by DRW » Mon Jun 25, 2018 8:02 am

Yeah, I'm already doing this.
Or rather, I did macros for this as well:

Code: Select all

#define MACRO_START do {
#define MACRO_END } while (false)
I used the brackets in the example for simplicity's sake.

One thing to keep in mind in this construct:
If you ever feel the desire to include a break out of a loop inside a macro, you cannot do this if the loop exists outside the macro:

Code: Select all

#define IfErrorThenBreak(errorCode)\
do\
{\
    if ((errorCode) != 0)\
        break; /* Will not work as intended since it refers\
                  to the do while loop, not to some outside loop. */\
}\
while (false)
Available now: My game "City Trouble".
Website: https://megacatstudios.com/products/city-trouble
Trailer: https://youtu.be/IYXpP59qSxA
Gameplay: https://youtu.be/Eee0yurkIW4
German Retro Gamer article: http://i67.tinypic.com/345o108.jpg

Post Reply