Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » != from many things (tile engine)

This thread is locked; no one can reply to it. rss feed Print
!= from many things (tile engine)
New_sun
Member #10,816
March 2009

I'm working on a tile engine(with C language) and I'd like to make multiple obstacles.
For example 1 is a terrain tile and is an obstacle,2 is a block tile ecc...
I don't want to write to much code because I don't like it.
I know I could write something like:

if(map[y][x] != 1 && map[y][x] != 2 ......)etc...
then
don't move

Actually inside the [] there is a formula to obtain the tile coordinates from the real ones and so it's much longer.

Is there a way to write different from a lot of numbers in one line only?

LennyLen
Member #5,313
December 2004
avatar

Instead of storing your tiles as ints, you could make up a tile struct similar to this:

typedef struct {

   int ID;
   BITMAP *bmp;
   int passable;

} Tile;

And then have a map of tiles instead, then all you need to check is: if(!map[y][x].passable)...

Thomas Fjellstrom
Member #476
June 2000
avatar

I would probably extend that to:

typedef struct {
   int ID;
   int passable;
} Tile;

BITMAP *tiles[MAX_TILES];

// ...

draw_sprite(tiles[map[y][x].ID]...);

// ...

if(!map[y][x].passable) ...

could even pull out passable into a separate array too if you really wanted to. But that makes modifying passable per tile at run time inconvenient.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

Mark Oates
Member #1,146
March 2001
avatar

New_sun said:

I don't want to write to much code because I don't like it.

;D

--
Visit CLUBCATT.com for cat shirts, cat mugs, puzzles, art and more <-- coupon code ALLEGRO4LIFE at checkout and get $3 off any order of 3 or more items!

AllegroFlareAllegroFlare DocsAllegroFlare GitHub

New_sun
Member #10,816
March 2009

Thank you all.

How can I initialize a matrix of structures?
I use the method of the third post.

GullRaDriel
Member #3,861
September 2003
avatar

like that ?

typedef struct STRUCTURE
{
       int x , y ;
}STRUCTURE ;

STRUCTURE *my_struct  = NULL ;

my_struct = (STRUCTURE *)malloc( nb * sizeof( STRUCTURE ) ); /* where nb is the number of item you need */

if( !my_struct )
{
    fprintf( stderr , "There was a problem when allocating the struct\n" );
}

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

New_sun
Member #10,816
March 2009

But after the allocation I should initiliaze every element of the matrix by hand...
my_struct[0][0].x = 1;
my_struct[0][0].y = 0;

I read that with C you can initialize in this way:

STRUCTURE map[3][3]=
{
{1,1},{1,1},{1,1},
{2,0},{2,0},{2,0},
{1,1},{1,1},{1,1}
};

y = 0 is passable,y = 1 is not passable.
x = 1 is a block,x = 2 is the terrain.

I'd like something more visual.
I know it can be done because I've searched on google,but my compiler doesn't accept that syntax.

I'm thinking to create a function that say if a number is passable or not so I can continue to store the map of ints.
boolean isPassable(int x)
{
if(x== 1 || x == 2 || x == 3)
return TRUE;
else
return FALSE;
}

This should work also :)

LennyLen
Member #5,313
December 2004
avatar

What you want is:

STRUCTURE map[3][3]= 
{
    {{1,1},{1,1},{1,1}},
    {{2,0},{2,0},{2,0}},
    {{1,1},{1,1},{1,1}}
};

GullRaDriel
Member #3,861
September 2003
avatar

And for a huge structure it'll be a pain in the ass.

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

LennyLen
Member #5,313
December 2004
avatar

And for a huge structure it'll be a pain in the ass.

Indeed. That's when you want to start storing the data in a file and write a function to load it.

GullRaDriel
Member #3,861
September 2003
avatar

Haaa, long time since my first datafile ^^

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

OnlineCop
Member #7,919
October 2006
avatar

If you want to do it really simply, just let all of your tiles 0..127 (or 0x00..0x7F hex) be "non-obstacles" and tiles 128..255 (or 0x80..0xFF hex) be "obstacles".

EDIT: Whether you use tile[0] or tile[128], you'll draw the same tiles. It just determines whether or not you can walk onto that tile.

Then, simply check its MSB.

if (tile & 0x80 == 0) // not an obstacle
{
  ...
}
else // implies that (tile & 0x80 == 1), and that it IS an obstacle
{
  ...
}

Not as nice as all of ^ their suggestions, but it's a hack, nonetheless.

Go to: