Jump to content
  • Advertisement
Sign in to follow this  
dragon22483

Dynamic Map

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

Hi, I'm kinda new to programming so bear with me. I'm trying to write a tile-based map for an rpg but I want the maps to be dynamic, vary in size and layer. I read a tutorial that said I should write it something like this struct tileStruct { char id; // tile id bool open; }; //walkable or nor tileStruct ***map; Could someone please help me figure out what to do with that or if there is a better way please tell me. I'm not the greatest with pointers. This is what my code looks like: I'm not even really sure what it does. Well, kinda but not enough to know how to fix it. Pleas help. :) void rpgmap::loadMap( std::string filename ) { ifstream infile;// file input infile.open( filename.c_str() );// open the file for reading infile >> myRows >> myCols >> myLayers; infile.ignore(1,' '); myMap = malloc(sizeof(tileStruct**) * myLayers); for(int layer = 0; layer < myLayers; layer++) { myMap[layer] = malloc(sizeof(tileStruct*) * myCols); for(int col = 0; col < myCols; col++) myMap[layer][col] = malloc(sizeof(tileStruct) * myRows); } // // load the tiles from the file // infile.close();// close the file so another one can be opened }

Share this post


Link to post
Share on other sites
Advertisement
You can use a ** but in this particular case it probably isn't worth it - I think it is faster and easier to do it like this (sorry, I've made up the variable names instead of using yours) (sorry again - edited to use plain C):

mapsquare *data;
int width;
int height;

void SetSize(int w, int h)
{
width = w;
height = h;
if(data)
free(data);
data = (mapsquare *)malloc(sizeof(mapsquare) * w * h);
}

mapsquare * GetSquareAt(int x, int y)
{
assert(x < width && y < height);
return (data + (y * width) + x);
}

Share this post


Link to post
Share on other sites
Quote:
Original post by dragon22483
Hi, I'm kinda new to programming so bear with me. I'm trying to write a tile-based map for an rpg but I want the maps to be dynamic, vary in size and layer. I read a tutorial that said I should write it something like this


Looks like you're mixing C with C++. In C++ you would use the keyword new an new[] instead of malloc() and you would then free the memory with delete or delete[].

First of all you probably want to create a header structure for your map files. The header should contain atleast the width and height of the map and perhaps the amount of enemies etc. on the map. Write the header structure to the beginning of the map file.


struct MapHeader {
unsigned int width;
unsigned int height;
char name[32]; // "The town"
..
};


How to dynamically allocate and deallocate a two-dimensional array:


// Load the header structure from the beginning of the file and place width and height to m_width and m_height..

Tile **m_tiles = 0;

m_tiles = new Tile*[m_width];
for (int i = 0; i < m_width; ++i)
m_tiles = new Tile[m_height];



for (int i = 0; i < m_width; ++i)
delete[] m_tiles;
delete[] m_tiles;


You can develop that code further to create multiple layers for your maps.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
thanks guys i got the first part to load but i'm having troble with the second part. i think i have the loading from file down, i just don't really know how to put the values into the arrays. thanks :)

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
thanks guys i got the first part to load but i'm having troble with the second part. i think i have the loading from file down, i just don't really know how to put the values into the arrays. thanks :)


You read and write the values like this:


for(int i = 0; i<width; i++)
for(int j = 0; j<height; j++)
tiles[j] = read_value_from_file();


for(int i = 0; i<width; i++)
for(int j = 0; j<height; j++)
write_value_to_file(tiles[j]);

Share this post


Link to post
Share on other sites
i think the problem was already solved, but if you`re interested - i have a tutorial series on tilebased games: http://jnrdev.kbs-design.net

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!