I have a .txt file
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 1,0,0,1,1,1,1,0,0,0,0,0,1,1,1,1,1,0,0,1 1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1 1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1 1,0,0,1,0,0,0,1,1,1,0,0,0,0,0,0,1,0,0,1 1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1 1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1 1,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1 1,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,1 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
and in my game, this is the code that reads the level
int lvlWidth; int lvlHeight; void create::render_level( string levelFile ) { //The width and height of the level lvlWidth = 20; lvlHeight = 15; //Create a 2D array based on the size of the level int** level; level = new int*[lvlWidth]; for (int i = 0;i<lvlWidth;i++) level[i] = new int[lvlHeight]; //Open the level ifstream lvlFile; lvlFile.open( levelFile.c_str() ); //If the level fails to open if( !lvlFile ) { cout << "File open failed!\n"; } int i = 0; int j = 0; char line[300]; //Read the file while( lvlFile.getline(line, sizeof line) ) { //cout << line << endl; for( j = 0; j < lvlWidth; ++j ) { sscanf( line+(j*2), "%d%*c", &level[i][j] ); }++i; } //Place things on the level for( i = 0; i < lvlWidth; i++ ) { for( j = 0; j < lvlHeight; j++ ) { if( level[j][i] == 1 ) //Place a block when level[i][j] is not 0 { lvlbox[j*20+i].x = (i*16); lvlbox[j*20+i].y = (j*16); lvlbox[j*20+i].w = 16; lvlbox[j*20+i].h = 16; //If there is a place for a block, say where it is //cout << "lvlbox[" << ((j*20+i+1)) << "] is at coordinates: " << lvlbox[j*20+i].x << "," << lvlbox[j*20+i].y << endl; } } } //Close the file lvlFile.close(); //Set the way for objects to be placed initialized = false; }
There are some things you have to know:
*Any time you see lvlbox[j*20+i], it should actually be lvlbox[j*lvlWidth+i]. In another process, I have:
for( int i = 0; i < lvlWidth; i++ ) { for( int j = 0; j < lvlHeight; j++ ) { SDL_FillRect( screen, &lvlbox[j*20+i], SDL_MapRGB( screen->format, 0x00, 0x00, 0x00 ) ); } }
in case any of you were wondering. But as long as that 20 is lvlWidth then it all works out.
Heres what I need help with:
1. counting the number of characters per line while excluding the comma (or counting the commas seperately and then subtracting the total by the number of commas, whatever is easier). I also need something to count the number of lines. The reason being is
I want to be able to put the number of characters in lvlWidth and the number of lines in lvlHeight.
2. what if I decide to have double digits between the commas? like 01 instead of 1. how would I be able to do that? (going back to no. 1, how could i still put it into lvlWidth, just divide the total by 2 or something?)
3. I may be getting ahead of myself, but if I were to make "-end-" the last line of the .txt file, how could I make the code understand that (while not counting -end- as another line, because I would like to put variables after -end-)?
Thanks