Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » need help with declaring a 2d int array

This thread is locked; no one can reply to it. rss feed Print
 1   2   3 
need help with declaring a 2d int array
spellcaster
Member #1,493
September 2001
avatar

Quote:

All preprocessor stuff is evil, including the header guards.

Why is that?

Quote:

Not familiar with what you mean

The # operator puts it's operand in quotes.

#define DEBUG_CASE(value) case value: log(#value)

enum {
   ERROR_SOMETHING
};

switch (returnCode) {
    DEBUG_CASE( ERROR_SOMETHING )
         // your code here
    break;
}

will evalute into:


enum {
   ERROR_SOMETHING
};

switch (returnCode) {
    case ERROR_SOMETHING:
         log("ERROR_SOMETHING");
         // your code here
    break;
}

You can also use it to create string representations for your constants:

1// events.h
2EVENT( MSG_PlayerHit )
3EVENT( MSG_EnemySpawned )
4 
5// events.cpp
6#define EVENT(msg) msg,
7enum Events {
8 #include "events.h"
9};
10#undef EVENT
11#define EVENT(msg) #x,
12 
13static const char* EventStrings[] = {
14 #include "events.h"
15};
16#undef EVENT

You can also use it if you're too lazy to write your own embedded language parser. I used the preprocessor to create a little "embedded language" so createing the JS interface is more easy.
I could have written something like luaBind, but why should I? The cpp is tried and proven and works perfectly well :)

So, code like this:

GetterStart
    GetterAttributesStart

        Get( width )
            // return width as JS var
     
        Get( height )
           // return height as JS var
       
    GetterAttributesEnd
GetterEnd

Is translated into the correct interface calls:

1JSBool gimlJSImageGetFunc(JSContext *jsContext, JSObject *thisObj, jsval id, jsval *vp) {
2 JSBool result = JS_TRUE;
3 
4 if (JSVAL_IS_INT(id)) {
5 switch(JSVAL_TO_INT(id)) {
6 case 0xEfffffff: {
7 }
8 case GIML_JS_IMG_WIDTH: {
9 // return width as JS var
10 } break;
11 case GIML_JS_IMG_HEIGHT: {
12 // return height as JS var
13 } break;
14 }
15 }
16}

The Image and IMG parts are of course dynamic :)

So I didn't had to write a special parser. And writing interface code got more readable.
I got that idea from an artcile in Game Programming GemsI, where a complete state machine was developed that way. You should check it out, the resulting code is quite neat.

--
There are no stupid questions, but there are a lot of inquisitive idiots.

23yrold3yrold
Member #1,134
March 2001
avatar

Quote:

I got that idea from an artcile in Game Programming GemsI, where a complete state machine was developed that way. You should check it out, the resulting code is quite neat.

Not sure I curently have need for such a thing, but it's not like I can afford any of the GPG books anyway. :)

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

ReyBrujo
Moderator
January 2001
avatar

spellcaster, it is useful, but when you create complex macros like in MFC, you are abusing the language features :) Especially if you add bunch of code in a macro.

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

spellcaster
Member #1,493
September 2001
avatar

Quote:

spellcaster, it is useful, but when you create complex macros like in MFC, you are abusing the language features

If it saves me several days of coding and testing, is more easy to read and doesn't require any new utilities, it's not abuse.
Esp. not since the preprocessor was made to do that job. I'm not abusing it, I'm using it.

I agree that inline functions and constants / enums should be used where it is possible. But if you want to output a linenumber, you should better use the _LINE_ macro :)

Know your tools, and use them well. That includes cpp.

--
There are no stupid questions, but there are a lot of inquisitive idiots.

ReyBrujo
Moderator
January 2001
avatar

Sure, but having something like this is evil:

int main() {
    INITIALIZE_ALLEGRO();
    PLAY_GAME();
    TERMINATE_ALLEGRO();
}
END_OF_MAIN()

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

X-G
Member #856
December 2000
avatar

No, making a game almost entirely out of templates is evil. ;)

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Rash
Member #2,374
May 2002
avatar

spellcaster said:

As long as there's no way to replace the # and ## macro operations, there's a need for defines. It's that easy.

spellcaster, you're seriously pissing me off with this straw man. 95% of all instances of macros I ever saw at Allegro.cc were unjustified and then you bring up a few exotic uses of it where it might be useful supposedly as a rebuttal?

I stand by the statement that macros should be minimized AS MUCH AS POSSIBLE and so should you. Frankly, you're not helping changing the general mindset here.

X-G
Member #856
December 2000
avatar

Quote:

spellcaster, you're seriously pissing me off with this straw man

Who's strawmanning here, eh? Allegro's use of macros was not mentioned once prior to spellcaster saying that line, and he didn't mention it either.

EDIT: For the record, Rash's post said "... I ever saw in Allegro ..." before he edited it. Not that it matters; the topic of "95% of instances of macros on Allegro.cc" wasn't brought up either. One broad statement was made, and a casual comment was made disfavoring that statement. Someone needs to lay off the alkaloids...

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

Richard Phipps
Member #1,632
November 2001
avatar

I already mentioned MIN, MID, MAX are useful to me. So what's wrong with using them?

23yrold3yrold
Member #1,134
March 2001
avatar

Quote:

I already mentioned MIN, MID, MAX are useful to me. So what's wrong with using them?

Inlined versions are safer. ;) A Google for the MIN macro (just for the sake of double checking what I was about to say) coughs up "MIN macro in numbers.c causes build problems on HP-UX" as the first hit. ;D Fourth hit demonstrates some problems ("In this small example you can already see several of the dangers of macro arguments ...")

Macros have uses, but very specialized ones. For the purposes of this discussion, in regards to the above code, macros are evil.

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Richard Phipps
Member #1,632
November 2001
avatar

These are the same as those defined by allegro?

23yrold3yrold
Member #1,134
March 2001
avatar

The MIN() macro is pretty much the same everywhere ...

   #define min(X, Y)  ((X) < (Y) ? (X) : (Y))

Makes me sick, it does. ;) An inlined template function is much better. ;) IMHO, of course ....

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

ReyBrujo
Moderator
January 2001
avatar

DJGPP has another, using the __typeof to prevent errors this macro has.

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

Richard Phipps
Member #1,632
November 2001
avatar

23yrold3yrold
Member #1,134
March 2001
avatar

   a = MIN(--b, ++c);

Depending on whether you're using the macro or an inlined function, you'll get two different results for 'a'.

EDIT: The "expensive_computation" argument is another good one ...

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

X-G
Member #856
December 2000
avatar

Quote:

looks ok to me.

min(expensive_computation(), another_expensive_computation())

...after cpp...

(expensive_computation() < another_expensive_computation() ? expensive_computation() : another_expensive_computation())

Just one example of why it's evil. Either way the "winner" is going to have to be executed twice. If either has side effects, you're even further up sh*t creek...

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

ReyBrujo
Moderator
January 2001
avatar

int a = 9;
int b = 10;
int c = 0;

c = MIN(a++, b);

Should return 9, returns 10.

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

Richard Phipps
Member #1,632
November 2001
avatar

ok.. it is 12:40am here. :)

But what about normal variables:
c = MIN(a, b);

any problems here?

23yrold3yrold
Member #1,134
March 2001
avatar

Quote:

looks ok to me. :)

The fact this comment got thrice bum-rushed tells you this is a classic example of evil macros. ;)

Quote:

any problems here?

There? No. But wouldn't you prefer a function that NEVER gives you problems? See the first post; you can shoot yourself in the foot reall easy like ...

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

Richard Phipps
Member #1,632
November 2001
avatar

ReyBrujo
Moderator
January 2001
avatar

Should return 9, but returns 10.

--
RB
光子「あたしただ…奪う側に回ろうと思っただけよ」
Mitsuko's last words, Battle Royale

X-G
Member #856
December 2000
avatar

Quote:

unsensible use of the macro

Just by making it an inline function instead, this kind of misbehaviour is made impossible. You don't have to care any more, and the function will execute just as fast. Why give you the chance to shoot yourself in the foot if preventing it is just as powerful?

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

23yrold3yrold
Member #1,134
March 2001
avatar

Quote:

Or... unsensible use of the macro.

Would you consider it better practice to make bulletproof code that always works, or code that relies on you not to screw it up every time you reference it? The macro has pitfalls, and zero advantages over the inlined template function.

--
Software Development == Church Development
Step 1. Build it.
Step 2. Pray.

spellcaster
Member #1,493
September 2001
avatar

rash said:

spellcaster, you're seriously pissing me off with this straw man. 95% of all instances of macros I ever saw at Allegro.cc were unjustified and then you bring up a few exotic uses of it where it might be useful supposedly as a rebuttal?

I think I got that covered, eh?

myself said:

I agree that inline functions and constants / enums should be used where it is possible.

I think you should apologize. And maybe you should read the posts you're replying to, and maybe even think twice before you post once. Just an idea.

Anyway, saying that macros are "evil, evil, evil" is singleminded. And not very clever. Due to the idea that all a macro can do is just text replacement, nobody cares about the cpp anymore. Most don't even know about the # and ## operators and how to use them.

But, from time to time macros are extremly useful, make your code more easy to read and more easy to maintain.

unsigned char flags = BINARY2( 1001, 0011);

is more easy to read and less error prone than the decimal or hexadecimal version of that number.

assertmsg( value > 0, "The value must be > 0");

will give you a better error message than a pure assert().

Macros allow to create a string containing both file name and line number... which is quite neat, IMO.

The point is: cpp is a tool. If you use it wisely, it'll help you. If you use it poorly, well, than you're not clever enough.

Always use the right tool for the right job. Should one use a #define for constants or should one prefer enums or consts? In that case the define looses.
Samething for macros and inline functions (most of the time).

But: Just because there are better ways for these two jobs doesn't mean that "macros are evil". And whoever telling you that macros are evil never read the cpp description :)
All it means that you shouldn't use macros for constants and inline methods. It's as easy as that.

--
There are no stupid questions, but there are a lot of inquisitive idiots.

Richard Phipps
Member #1,632
November 2001
avatar

<shrugs>

I only use it as: c = MIN(a, b); anyway.

And on that note, I'm off to bed. If I can sleep in this stifling heat..

 1   2   3 


Go to: