Jump to content
  • Advertisement
Sign in to follow this  
overeasy

Pointer to array.

This topic is 3000 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
Advertisement
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.


Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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::vector

int 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;
}

Share this post


Link to post
Share on other sites
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::vector

int 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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!