Sign in to follow this  

Dynamic Map Sizes

This topic is 4691 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

Hello, I am currently working on a simple Pacman clone with VC++ and SDL. I am in the process of creating a Map Class for my game. I want this class to be scaleable so that it can load maps of various sizes simply by reading the header in a map file. Currently I use a simple struct to hold my tiledata, at the moment it looks like this: stuct sTile { int tile_id; bool walkable; } I am then using a 2 dimensional array to store my map itself: #define LEVELWIDTH 19 #define LEVELHEIGHT 22 sTile gLevel[LEVELWIDTH * LEVELHEIGHT]; Now in the context of a map object..how could I create it so that when the map object is called, the contructor loads a passed map file name, pulls the LEVELWIDTH and LEVELHEIGHT and creates an array based on that information, then loads in the map data? I've been thinking of possibly using a dynamic array class which is capable of creating, resizing, deleting etc.. however, with my array stored in the "private" section of my mapclass, would I be able to use my mapclass methods to resize it with the level data? Any help would be greatly appreciated. Thanks!

Share this post


Link to post
Share on other sites
boost has a class for this, multi_array. It would look like this:

typedef boost::multi_array<sTile, 2> array_type;
array_type gLevel;

void some_function()
{
gLevel.resize(boost::extents[19][22]);
for (int x=0 ; x<19 ; ++x) {
for (int y=0 ; x<22 ; ++y) {
use(gLevel[x][y]);
}
}
}

It doesn't matter if the instance is a private member of some class, as long as you have access to it where you want to use it.

Share this post


Link to post
Share on other sites
You could use std::vector like this:


class Map
{
public:
Map(std::string mapfile):
{
std::ifstream fin(mapfile.c_str());

// ...read width & height from map file...
mMapData.resize(mWidth * mHeight);

// read map data...
}

...

// This could be used in renderer...
const sTile& getMapCell(int x, int y)
{
return mMapData[y * mWidth + x];
}

private:
std::vector<sTile> mMapData;
int mWidth;
int mHeight;
};

// somewhere
Map test("test.map");
if (test.getMapCell(x, y).walkable)
crash_and_burn();


Share this post


Link to post
Share on other sites
You should think about just using a 2d-array with a maximum width and height a map can have:

#define MAX_WIDTH 200
#define MAX_HEIGHT 200

sTile MapTiles[MAX_WIDTH][MAX_HEIGHT];

Your game will probably not support maps of unlimited size, so this really shouldnt be a problem.

You read your mapwidth and height from mapfile as usual, but when you loop through tiles or access a tile from the array you just need to make sure not to access non-used tiles. Simply check for the width and height you read from file before. For example only tiles from MapTiles[0][0] to MapTiles[99][49] are valid for a mapwidth of 100 and mapheight of 50.


This should be easiest way to do it. Hope that helps.

Share this post


Link to post
Share on other sites
Why not dynamically allocate memory and use a pointer to index it ?
Load the size of the map from a file, allocate memory, then load the rest of the map.

Share this post


Link to post
Share on other sites

This topic is 4691 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this