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?
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)...
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.
I don't want to write to much code because I don't like it.
Thank you all.
How can I initialize a matrix of structures?
I use the method of the third post.
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
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}} };
And for a huge structure it'll be a pain in the ass.
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.
Haaa, long time since my first datafile ^^
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.