loading data from a file and putting it into a 2d array

Started by
3 comments, last by GameDev135 21 years, 8 months ago
Hi. Im writing a tile based scroller. The scrolling is workign properly, however, I would like to load from a file rather that hard code it. I am representing my world as a 2d array. What makes this difficult is that I don''t know the size of the array in advance so I need to dynamically allocate a 2d array, which I am unsure of how to do. Here is a simple sample array of mine. char [2][2] { {1,0}, {0,1}}; where the numbers 1 and 0 repreesent different types of tiles. I would like to be able to put at the top of the file the number of rows and columns and then in the rest of the file include ones and zeroes. How can I do this?
Feel free to email me at NYYanks432@hotmail.com if you have any questions
Advertisement
basically you need to "linearize" the data so its in one contigous array.

you first write the row count and column count (height and width). you should know how to do that. if not go look up how to do file access. its very important that you understand how to do file access otherwise it will be diffiuclt to debug the code if you mess something up.

now that the header is in place (with the row/column count), you need to write the data to the file.

for(y=0;yfor(x=0;x{
fwrite(buffer[y][x], 1, 1, filePointer);
}

thats it. to read the data you just first read the header, then read the data.
for(y=0;yfor(x=0;x{
fread(buffer[y][x], 1, 1, filePointer);
}

now dynamic allocation can be down in two ways. linear and planar. you seem to want planar.

char **buffer=NULL;
buffer = (char**)malloc(rowCount*sizeof(char*));
for(y=0; y buffer[y] = (char*)malloc(columnCount*sizeof(char));

to free the data you MUST do things in this order:
// first free the rows
for(y=0; y free(buffer[y]);
// then the columns
free(buffer);

// access is done by
buffer[row][column]

if you dont free and allocate in the order shown you WILL have problems. look up memory allocation for more in depth information.

linear method
char *buffer=NULL;
buffer = (char*)malloc(rowCount*columnCount*sizeof(char));

to free the memory you simply:
free(buffer);

// acess
buffer[row*columnCount+column];

its VERY important that you actually go through some dynamic memory tutorials and understand how it works. DONT just copy the code. i have shown the c style of memory allocation using malloc/free. you could use new/delete or the vector class which may be better. i have not shown them, and expect you to actually do some research on this. since you are already creating a side scroller then i expect that you have some basic programming skills already. if not, start with a simpler game and simpler apps to ensure a good foundation. it may be boring, but anything worth learning will have some boring component to it.
Couldn''t you set the maximum array size, say char [200][50].

And then when writing the code put an

if (char[j] != -1)
{
//do stuff
}
else
{
stop scrolling/end level or whatever goes her
}


NOTE: Everything within my post is subject to wrongness.
If the file you want to load is a 256*256*256 3D array (yes I have a situation where I may need that), is there a way to load it without buffer? Otherwise it needs twice as much RAM for loading the file :/
I am going to use a variation of the idea of "a person"

So far it is working well.

I don''t want to define a max world size b/c that will put a limitation on my world sizes. They could be very large and I dont want to have lots of memory being wasted if the world is small.
Feel free to email me at NYYanks432@hotmail.com if you have any questions

This topic is closed to new replies.

Advertisement