Using an array of ptrs
I am working on a tile-based rpg. My world is represented with an array that looks like this:
I keep my world divided into maps of 100x100 tiles. One map file is 350k. And since my game will be using somewhere around 30 maps, the space really starts to add up. Now I do have a tilePalette which is bassically a lookup table and holds all of my tile definitions for my level editor. I was considering just having an array of TileStruct ptrs, so that I could save about 2/3 space on my maps. Would I be able to use an array of ptrs by setting all of the ptrs in my level editor, or would this cause some wierd memory thing. Or maybe there's a better solution?
Code:
#define kWorldWidth 100
#define kWorldHeight 100
#define kMaxLayers 3
TileStruct worldArray[kWorldWidth][kWorldHeight][kMaxLayers];I keep my world divided into maps of 100x100 tiles. One map file is 350k. And since my game will be using somewhere around 30 maps, the space really starts to add up. Now I do have a tilePalette which is bassically a lookup table and holds all of my tile definitions for my level editor. I was considering just having an array of TileStruct ptrs, so that I could save about 2/3 space on my maps. Would I be able to use an array of ptrs by setting all of the ptrs in my level editor, or would this cause some wierd memory thing. Or maybe there's a better solution?
Using pointers would be bad, at least when it comes to saving them. However, if you give each tile in the palette a unique id number, you can save a map in two parts: a tilePalette containing, say, 2000 tiles; and an array of 10,000 id numbers.
When it comes to loading it from disk, load the tilePalette first. You can store the tiles however you want (such as in an array). The unique ids give you the ability to see that square (0,0) on your map has tileID 6, search through the palette for tileID 6 and get its current address and there it is, a pointer to the tile you need. Your map data can now be easily represented as a 100x100 array of tilePointers.
ID numbers are sort of the same as pointers on some level, now come to I think of it.
[edit - if this isn't clear tell me, and I'll try to explain better]
When it comes to loading it from disk, load the tilePalette first. You can store the tiles however you want (such as in an array). The unique ids give you the ability to see that square (0,0) on your map has tileID 6, search through the palette for tileID 6 and get its current address and there it is, a pointer to the tile you need. Your map data can now be easily represented as a 100x100 array of tilePointers.
ID numbers are sort of the same as pointers on some level, now come to I think of it.
[edit - if this isn't clear tell me, and I'll try to explain better]
That made me think of something else. Okay, my tilePalette is bassically a large array holding a bunch of tile defs (which are declared in my tile editor). So if I wanted a particular tile def, I would use a specific index, for example:
Couldn't I just use a 2d array of short, such as:
and say I wanted to access a tiles passability,
is their anything wrong with doing it this way (it's a lot less space than the pts)?
Code:
#define kFloorTile1 5
TilePalette[kFloorTile1]Couldn't I just use a 2d array of short, such as:
Code:
short worldArray[kWorldWidth][kWorldHeight][kMaxLayers];and say I wanted to access a tiles passability,
Code:
short index = worldArray[h][v][layer];
if (TilePalette[index].passable == true)
// etc...is their anything wrong with doing it this way (it's a lot less space than the pts)?
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| memcpy(stuct array pointer struct array point) | unknown | 22 | 8,812 |
Sep 29, 2005 03:16 PM Last Post: unknown |
|

