Pointer to array.

Started by
7 comments, last by overeasy 13 years, 9 months ago
I'm implementing a map paging system like such as and:

...|...|...
...|...|...
--+--+--
...|...|...
...|...|...
--+--+--
...|...|...
...|...|...

there is a macro array (3 by 3) and 9 micro arrays.

each macro array cell contains a micro array. great.

when the character moves outside the central bounding rectangle, i want to shift the pointers in the macro array so 6 out of the 9 micro arrays will not have to be reloaded.

how do i store the micro arrays in the macro array to facilitate this?
Advertisement
is that not a 3 dimensional array?

[3][6][3] for your example
Would this be a workable solution?

- Make the 3x3 macro array an array of pointers to your 'micro' object, rather than an array of micro objects themselves
- Create howerver many micro objects you need in a micro[x][y] array
- Set the first 9 pointers to the first nine micro objects, so
[currentX][currentY] is the micro cell the top left pointer points to
- Then do something like

void moveLeft(int currentX, int currentY){   if( currentX != 0 )   //Not at the far left       for(int i = 0; i < 3; i++){   //Run through the macro array X co-ords           for(int j = 0; j < 3; j++){  //Run through the macro array Y co-ords                macro[j] = micro[currentX + i - 1][currentY];                 //Every cell points to the micro array cell left of where it was           }       }        }}


with similar functions for up, right, down.


The high cost of living hasn't affected its popularity.
Quote:Original post by PrintFDebugger
Would this be a workable solution?

- Make the 3x3 macro array an array of pointers to your 'micro' object, rather than an array of micro objects themselves
- Create howerver many micro objects you need in a micro[x][y] array
- Set the first 9 pointers to the first nine micro objects, so
[currentX][currentY] is the micro cell the top left pointer points to
- Then do something like

*** Source Snippet Removed ***

with similar functions for up, right, down.


similar to what i came up with. it looks like using objects is the best solution. i was hoping i could bypass them entirely as they contain no information except the array
could i have a four dimensional array, and then just do the same operation as above?
Quote:Original post by overeasy
could i have a four dimensional array, and then just do the same operation as above?


my concern is that when i move a two dimensional sub-array in the macro array the entire array will be copied in memory. can someone confirm this?
the whole array will be in memory already anyway...

if you copy elements from

big[4][4]

to

small[4]

only the values you copy from one array to another will be copied in memory.

Using c style arrays you will have to iterate through the elements of the array that you want to use to copy the data from one array to the other, you cant simply do small[4] = big[4][], and a pointer to big[4] will not achieve what you hope.
Your best bet would be to use an appropriate indexing scheme into a one-dimensional map array. You'll have one coordinate for the room, and another coordinate for the individual cell. You don't have to copy anything around, and it can easily be factored into a single function that you just use everywhere:
const int ROOM_WIDTH = 3;const int ROOM_HEIGHT = 3;const int WORLD_WIDTH = 3;const int WORLD_HEIGHT = 3;const int MAP_WIDTH = ROOM_WIDTH * WORLD_WIDTH;const int MAP_HEIGHT = ROOM_HEIGHT * WORLD_HEIGHT;Foo mapArray[MAP_WIDTH * MAP_HEIGHT]; // or perhaps an std::vectorint getMapArrayIndex(Point room, Point cell) {   // todo: check coordinates are in range   return (room.Y * ROOM_HEIGHT * MAP_WIDTH) + (room.X * ROOM_WIDTH) +          (cell.Y * MAP_WIDTH) + cell.X;}
Quote:Original post by Zipster
Your best bet would be to use an appropriate indexing scheme into a one-dimensional map array. You'll have one coordinate for the room, and another coordinate for the individual cell. You don't have to copy anything around, and it can easily be factored into a single function that you just use everywhere:
const int ROOM_WIDTH = 3;const int ROOM_HEIGHT = 3;const int WORLD_WIDTH = 3;const int WORLD_HEIGHT = 3;const int MAP_WIDTH = ROOM_WIDTH * WORLD_WIDTH;const int MAP_HEIGHT = ROOM_HEIGHT * WORLD_HEIGHT;Foo mapArray[MAP_WIDTH * MAP_HEIGHT]; // or perhaps an std::vectorint getMapArrayIndex(Point room, Point cell) {   // todo: check coordinates are in range   return (room.Y * ROOM_HEIGHT * MAP_WIDTH) + (room.X * ROOM_WIDTH) +          (cell.Y * MAP_WIDTH) + cell.X;}


it's a paging system. the contents of it (the portion of map pre-loaded) changes. i think i'm going to use objects to be safe. doesn't hurt.

This topic is closed to new replies.

Advertisement