Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Types

This thread is locked; no one can reply to it. rss feed Print
 1   2   3 
Types
Kauhiz
Member #4,798
July 2004

This is something I never learned, when I studied C, so be patient with me. I'd like to make a type called ENEMY to create an enemy easily. Now I know I should make the type like this:

typedef struct ENEMY{
     int x,y;
     int state;
}

I could then create a enemy by ENEMY *enemy1; But how do I read and change the variables? Also, please correct me, if I said something stupid...

---
It's Ridge Racer! RIIIIIDGE RAAAAACER!

Steve Terry
Member #1,989
March 2002
avatar

Fist off you only create a pointer.. you need to malloc an enemy for that.

enemey1 = malloc(sizeof(struct ENEMY));

enemy1->x = 10;
enemy1->y = SCREEN_H >> 1;
enemy1->state = ENEMY_ALIVE;

___________________________________
[ Facebook ]
Microsoft is not the Borg collective. The Borg collective has got proper networking. - planetspace.de
Bill Gates is in fact Shawn Hargreaves' ßî+çh. - Gideon Weems

Hrvoje Ban
Member #4,537
April 2004
avatar

Or you can make function to do it:

ENEMY *create_enemy(int new_x, int new_y, int new_state)
{
  ENEMY *tmp = malloc(sizeof(struct ENEMY));
  tmp->x = new_x;
  tmp->y = new_y;
  tmp->state = new_state;

  return tmp;
}

ENEMY *enemy1 = create_enemy(100, 100, ENEMY_ALIVE);

Oscar Giner
Member #2,207
April 2002
avatar

The syntax for the ENEMY struct is not correct. It should be:

typedef struct ENEMY{
     int x,y;
     int state;
} ENEMY;

You can create enemies by two ways:

statically:

{
    ENEMY enemy1;

    enemy1.x = 0;
    enemy1.x = 560;
    enemy1.state = 0;
}
// enemy1 does not longer exist, after the closing }

or

dinamically:

{
    ENEMY *enemy1;

    enemy1 = (ENEMY *)malloc(sizeof(ENEMY));

    enemy1->x = 0;  // equivalent to (*enemy).x = 0
    enemy1->x = 560;
    enemy1->state = 0;

    free enemy;  // destroys the enemy1 data.
}
// if you don't call free enemy1 is still in memory and can be accesed by anyone who knows its memory address (pointer).

ReyBrujo
Moderator
January 2001
avatar

You create a new type with the keyword typedef. You declare a new variable of some time on the fly with only the keyword struct:

typedef struct node_st {
    int data;
    struct node_st *next;
} node_t;

struct list_st {
    node_t *first;
    node_t *last;
} list;

/*  list is a variable, node_t is a type  */

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

spellcaster
Member #1,493
September 2001
avatar

I also highly recommand the use of calloc to create data for structs, since it will fill the memory with zeros.
Also, you shouldn't use ALL_CAPS for a type, since that's norally used for constant values only.

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

Yves Rizoud
Member #909
January 2001
avatar

Just a precision aboout one item:

typedef struct ENEMY
{
     int x,y;
     int state;
} ENEMY;

Kauhiz may not see it clearly why the word ENEMY appears twice.. in this precise case, the top one is unneeded. It's a kind of 'temporary' identifier, which is valid only until the struct is defined (until the } ENEMY; )
ReyBrujo shows one case where it's needed:

typedef struct node_st {
    int data;
    struct node_st *next;
} node_t;

This is a classic node element for a linked list of ints.
"next" is meant to be a pointer to a node_t, but the word "node_t" is not yet defined while the compiler parses the struct (it's farther down in the code) So you give a temporary name (node_st) at the beginning, and define "next" as a pointer to node_st.

If it's clear....

edit:
no CAPS? While learning C, I usually saw typedef structs in uppercase from books and sample code, though I cant really remember the origin... Well, it's a matter of norma. If you never succumb to the evil macros demons, uppercase is free for this use, right? ;D

X-G
Member #856
December 2000
avatar

(going somewhat off topic)

Quote:

Also, you shouldn't use ALL_CAPS for a type, since that's norally used for constant values only.

Most seasoned programmers know that this is true. Yet, Allegro has BITMAP * and a whole bunch of other things along the same line. Should that be dealth with, you think?

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

spellcaster
Member #1,493
September 2001
avatar

Allegro is code he can't influence. He can influence the code he writes, though :)

And I think that BITMAP and SAMPLE started as macros, which are the other case where you's ude ALL_CAPS.

EDIT:

Quote:

If you never succumb to the evil macros demons, uppercase is free for this use, right? ;D

Constants != macros.
While you can use the preprocessor for macros, it's better to use enums.

enum {
   I_AM_A_CONSTANT = 120,
   I_AM_A_CONSTANT_AS_WELL = 300
};

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

X-G
Member #856
December 2000
avatar

No, what I mean is... shouldn't Allegro's types be fixed? It's inevitable that newbs who get hold of Allegro code start believing that since Allegro does it, it's standard and they should too. Shouldn't Allegro use a better naming system to teach the right way to handle types?

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

spellcaster
Member #1,493
September 2001
avatar

Quote:

No, what I mean is... shouldn't Allegro's types be fixed?

Maybe, but this is not the right place to discuss that. But I guess you could suggest this on [AD] or the allegro-development board of this forum.

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

X-G
Member #856
December 2000
avatar

That's why I said I was going off-topic. ;) Okay, back to where we were. Uhm, wasn't the question answered already?

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

Johan Halmén
Member #1,550
September 2001

How about

struct Enemy
{
  int x,y;     
  int state;
};

or is this pure C++?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

X-G
Member #856
December 2000
avatar

In C, you would have to create a variable by doing "struct Enemy alien;". In C++, just "Enemy alien;" is enough.

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

Tobias Dammers
Member #2,604
August 2002
avatar

spellcaster said:

Also, you shouldn't use ALL_CAPS for a type, since that's normally used for constant values only.

I think this discussion is quite idle. I myself use ALL_CAPS for all types I define, and all_lowercase for instances. Since I work in C++ most of the time, this means that a class is in caps, and a class instance in lower case. I don't consider this "wrong" or "not done", but rather a convention, just as valid as others as long as you stick to it.
And, by the way, allegro is not the only API using it; the win api does pretty much the same thing, except for that evil Hungarian stuff. Anyway, as far as I'm concerned, I say let people name their identifiers the way they want to.
And X-G, please be so good and read the part of the allegro doc about "hacking allegro" or something like that. Shawn (I think it was him) defined the allegro code style there in detail, and AFAIK all the allegro code follows these rules neatly. Which means there is actually nothing to be fixed, unless by "fix" you mean "rewrite the library to fit your own coding style" (no offence pal).

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

X-G
Member #856
December 2000
avatar

Quote:

"rewrite the library to fit your own coding style

I do, because it's not just my coding style. It's the coding style of 90% of all C developers out there. Just because Shawn decided to use that nomenclature for Allegro doesn't make it a good thing.

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

Oscar Giner
Member #2,207
April 2002
avatar

And that 90% of people use a nomenclature, doesn't mean it's the good one :P

As long as you are coherent with your nomenclature, use whatever you want. Like if you want to use MiXeD cAsE, but do it in all your code (then you'll find that mixed case is not very optimal;)).

In C I use usually use capitalization in my projects for structs, like Enemy. This leaves me the enemy word free for other usage :)

ReyBrujo
Moderator
January 2001
avatar

I suffix types with _t, structures with _st, constants in uppercase, if bit positions WHATITISABOUT_I_NAME, if definitions (max something in example) WHATITISABOUT_D_NAME, if enums WHATISITABOUT_E_NAME, if a macro WHATITISABOUT_M_NAME. Constants are prefixed with CONST_WHATITISABOUT. Then I use lowercase for everything else. Easy for me to search all values with _t , _st , _D_, _I_, _E_, _M_ or CONST_ :)

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

X-G
Member #856
December 2000
avatar

Quote:

And that 90% of people use a nomenclature, doesn't mean it's the good one

No, but it's self-consistent and consistent with how it was done, so there's really no good reason to use anything else for no added benefit. The style with UPPERCASE_MACROS_WITH_UNDERSCORES is universally recognizable.

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

Yves Rizoud
Member #909
January 2001
avatar

still off-topic...

X-G said:

It's the coding style of 90% of all C developers out there

Not in my part of the world. C developers here have always been using uppercase typedef structs :the whole Allegro library (BITMAP, RGB, PALETTE) but it dates back to the libc library itself (or FILE is a constant ?)

X-G
Member #856
December 2000
avatar

Yes, Allegro uses it, but that's what we're discussing so you can't exactly use it as an example, heh. As for libc...

Yes, FILE is uppercase, and I don't know why. I can't find any other libc typedefs that are: time_t, size_t, sockaddr etc are all lowercase. Then again I'm not looking that closely. Feel free to prove me wrong.

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

Derezo
Member #1,666
April 2001
avatar

Meh. It's personal taste. I'd never in my wildest dreams write something like this:

   while (joy[0].flags & JOYFLAG_CALIBRATE) {
      if (calibrate_joystick(0) != 0) {
   set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
   allegro_message("Error calibrating joystick!\n");
   return 0;
      }
   }

Found in exjoy.c.

I'd write that like so:

  while (joy[0].flags & JOYFLAG_CALIBRATE)
   {
    if (calibrate_joystick(0) != 0)
     {
      set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
      allegro_message("Error calibrating joystick!\n");
      return 0;
     }
   }

Neither way is wrong. It's personal taste, just like using caps for structures.

Personally, I use ALL_CAPS for both classes and macro's. Wouldn't have it any other way.

"He who controls the stuffing controls the Universe"

X-G
Member #856
December 2000
avatar

Of course neither way is "wrong" in the strictest sense. I just believe, and so does many, many others (the majority, I wager), that sticking to conventions like UPPERCASE_MACROS is a good idea for code legibility. I think that should be encouraged.

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

Richard Phipps
Member #1,632
November 2001
avatar

And I'd write it like this:

  while (joy[0].flags & JOYFLAG_CALIBRATE)
  {
   if (calibrate_joystick(0) != 0)
   {
    set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
    allegro_message("Error calibrating joystick!\n");
    return 0;
   }
  }

Evert
Member #794
November 2000
avatar

And I'd write it as it is now, but with eight real spaces instead of one tab as Allegro's code uses.

Personally, I use uppercase names for defines and typedefs. I think that's how it was done in the book I read for the C programming course I followed. I like it.

 1   2   3 


Go to: