|
|
| != 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. if(map[y][x] != 1 && map[y][x] != 2 ......)etc... 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
|
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
|
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. -- |
|
Mark Oates
Member #1,146
March 2001
|
New_sun said: I don't want to write to much code because I don't like it.
-- |
|
New_sun
Member #10,816
March 2009
|
Thank you all. How can I initialize a matrix of structures? |
|
GullRaDriel
Member #3,861
September 2003
|
like that ?
"Code is like shit - it only smells if it is not yours" |
|
New_sun
Member #10,816
March 2009
|
But after the allocation I should initiliaze every element of the matrix by hand... I read that with C you can initialize in this way: STRUCTURE map[3][3]= y = 0 is passable,y = 1 is not passable. I'd like something more visual. 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. This should work also |
|
LennyLen
Member #5,313
December 2004
|
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
|
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" |
|
LennyLen
Member #5,313
December 2004
|
GullRaDriel said: 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
|
Haaa, long time since my first datafile ^^ "Code is like shit - it only smells if it is not yours" |
|
OnlineCop
Member #7,919
October 2006
|
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.
|
|
|