none
count # of characters in a line of a .txt file while excluding some characters

    Question

  • 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

    Monday, July 01, 2013 6:27 PM

Answers

  • 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];
    

    This does not create a 2D array.  In a 2D array, you are guaranteed that level[0][14] is immediately folowed in memory by level[1][0].  While the current construction allows you to use the syntax level[i][j] AS IF it were a 2D array, you cannot use the row*width+column alternate method of accessing element.  You have no idea where row 1 is located in memory compared to row 0.  (Yes, I know you only show this "trick" for lvlbox but it is important to realilze it is not available for level.)

    	//Open the level
    	ifstream lvlFile;
    	lvlFile.open( levelFile.c_str() );
    
    	//If the level fails to open
    	if( !lvlFile )
    	{
    		cout << "File open failed!\n";
    	}

    You have been advised repeatedly that is is a logic error.  If the file fails to open, you cannot procede to process it as if it did and expect anything useful to happen.  This block needs to terminate the function or perhaps the entire program.

    	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] );
    		}

    You created the simulated array with the dimensions reversed.  lvlWidth is 20.  level[0] points to memory for 15 int.  As soon as j gets to 15, you are updating memory you don't own.  This is undefined behavior.  The first use of new should have specified lvlHeight and the second should specify lvlWidth.

    The %*c in your format string serves no purpose (but does no harm).  It is apparently a holdover from previous code that used scanf instead of sscanf.  In the first call to sscanf, the %d will interpret the int at line+0 and stop at the comma in line[1].  The next call to sscanf will interpret the int at line+2 (skipping the previous comma on its own) and stop at the comma in line[3].

    		++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

    Even though you attempt to correct the indeces, the limits are still wrong.

    			{
    				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].

    Why are you showing us code you know you needs to change?

    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.

    It can't work out since level is broken.

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

    You actually need two character counts, one to size the input buffer and one for lvlWidth.  There are couple options:

         Since line is sized at 300, each level[i] could point to allocated space for 300 (or only 151) int and later you could set lvlWidth to what you find in the the first line.  This would waste some space for each row but then the space is soon to be released (see later in this comment).

         You could read the first line and use strlen to determine its length.  the number of commas is length/2 and the number of integers is length/2+1.  Then rewind or close and reopen the file to process for real.

         You could read the file with fgetc counting each comma until you hit the '\n'.  The number of integers is count_of_commas+1.

    YOU HAVE MEMORY LEAKS.  level is a local variable.  When you exit this function, it is destroyed.  The memory it points to and all the memory pointed to the level[i] pointers remain allocated and unreachable.  You need to delete each level[i] and then delete level.

     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.

    For the line count:

         You could pre-process the file reading each character in an fgetc loop counting each '\n'.

         You could pre-process the file reading each line in a getline loop.

         Allocate space for an estimated number of line pointers and assign the address to level.  As you process each line, allocate the space for the integers to the corresponding level[i].  When you have used up all the line pointers currently allocated, allocate a larger set, copy the pointers from level to this set, delete the space for level, set level to point to the new space and continue processing.  Repeat as needed till all lines processed.  This is a little easier if you use malloc and realloc instead of new and delete.

    2. what if I decide to have double digits between the commas? like 01 instead of 1.

    If you count commas as suggested above, the number of digits per integer doesn't matter.

     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?)

    Since lvlWidth and lvlHeight are local, if you compute these on the fly you need some method or letting everyone else know what values you ended up with.  A global variable seems like the easiest option.  As discussed in previous threads, delcare the globals external in a header and define them exactly once in a single .cpp file

    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

    If its not the end, what will follow.  If the data that follows does not affect level, does it really belong in this file or processed by this function?  In any event, after reading a line, you can easily determine if it starts with the string "end" (strcmp, strncmp, etc).  Once you determine that it does, proceed on to whatever the new processing is.
    Monday, July 01, 2013 11:44 PM

All replies

  • This would be much easier if you used STL.  By using a vector of vectors, your structures could be flexible and you'd avoid j*20+i stuff.  For example:

    #include <iostream>
    #include <fstream>
    #include <vector>
    using namespace std;
    
    int main()
    {
        ifstream lvlFile;
        lvlFile.open( "level.txt" );
        char line[300];
    
        vector< vector<int> > grid;
        while( lvlFile.getline(line, sizeof(line) ) )
        {
            vector<int> row;
            int accum = 0;
            for( char * pj = line; *pj; pj++ )
            {
                if( *pj >= '0' && *pj <= '9' )
                {
                    accum = accum * 10 + *pj - '0';
                    continue;
                }
    
                row.push_back(accum);
            }
            printf( "Row length %d\n", row.size() );
            grid.push_back( row );
        }
        printf( "%d rows\n", grid.size() );
    }
    



    Tim Roberts, VC++ MVP Providenza & Boekelheide, Inc.

    Monday, July 01, 2013 6:40 PM
  • 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).

    #include "stdafx.h"
    #include <algorithm>
    #include <iostream>
    
    using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
      char line[] = "1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1";
      size_t len = strlen(line);
      int commas = count(&line[0], &line[len], ',');
      cout << "There are " << commas << " commas in the line.\n";
      cout << "There are " << len-commas 
           << " non-commas in the line.\n";
    
      return 0;
    }
    

    Output:

    There are 19 commas in the line.
    There are 20 non-commas in the line.

    - Wayne

    Monday, July 01, 2013 7:22 PM
  • Thanks, I tried incorporating it into that code, I wasn't entirely sure how to do it though. You think you can show me?
    Monday, July 01, 2013 7:28 PM
  • Thanks, I tried incorporating it into that code, I wasn't entirely sure how to do it though. You think you can show me?

    Were you replying to Tim's suggestion or mine? Including a small quote helps us read your
    intentions.

    If mine, the time stamps suggest that you spent 5 minutes or less trying.
    Programming usually requires more of a concerted effort than that. Give
    it some more thought. Most of us have jobs, families, social obligations,
    etc. and don't have time (or inclination) to do programming for others when
    it appears that with some effort they could do it themselves.

    Do your best to incorporate it, and if you don't succeed after a few hours
    of trying then post the code you have tried and we may be able to suggest
    how to get it to work as desired.

    - Wayne

    Monday, July 01, 2013 8:34 PM
  • I was replying to Tim's suggestion, we must have posted replies at the same time.
    Monday, July 01, 2013 8:41 PM
  • 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).

    #include "stdafx.h"
    #include <algorithm>
    #include <iostream>
    
    using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
      char line[] = "1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1";
      size_t len = strlen(line);
      int commas = count(&line[0], &line[len], ',');
      cout << "There are " << commas << " commas in the line.\n";
      cout << "There are " << len-commas 
           << " non-commas in the line.\n";
    
      return 0;
    }

    Output:

    There are 19 commas in the line.
    There are 20 non-commas in the line.

    - Wayne


    can you show me how to put that into lvlWidth while reading things into level[][]? Thanks
    Monday, July 01, 2013 9:07 PM
  • 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];
    

    This does not create a 2D array.  In a 2D array, you are guaranteed that level[0][14] is immediately folowed in memory by level[1][0].  While the current construction allows you to use the syntax level[i][j] AS IF it were a 2D array, you cannot use the row*width+column alternate method of accessing element.  You have no idea where row 1 is located in memory compared to row 0.  (Yes, I know you only show this "trick" for lvlbox but it is important to realilze it is not available for level.)

    	//Open the level
    	ifstream lvlFile;
    	lvlFile.open( levelFile.c_str() );
    
    	//If the level fails to open
    	if( !lvlFile )
    	{
    		cout << "File open failed!\n";
    	}

    You have been advised repeatedly that is is a logic error.  If the file fails to open, you cannot procede to process it as if it did and expect anything useful to happen.  This block needs to terminate the function or perhaps the entire program.

    	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] );
    		}

    You created the simulated array with the dimensions reversed.  lvlWidth is 20.  level[0] points to memory for 15 int.  As soon as j gets to 15, you are updating memory you don't own.  This is undefined behavior.  The first use of new should have specified lvlHeight and the second should specify lvlWidth.

    The %*c in your format string serves no purpose (but does no harm).  It is apparently a holdover from previous code that used scanf instead of sscanf.  In the first call to sscanf, the %d will interpret the int at line+0 and stop at the comma in line[1].  The next call to sscanf will interpret the int at line+2 (skipping the previous comma on its own) and stop at the comma in line[3].

    		++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

    Even though you attempt to correct the indeces, the limits are still wrong.

    			{
    				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].

    Why are you showing us code you know you needs to change?

    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.

    It can't work out since level is broken.

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

    You actually need two character counts, one to size the input buffer and one for lvlWidth.  There are couple options:

         Since line is sized at 300, each level[i] could point to allocated space for 300 (or only 151) int and later you could set lvlWidth to what you find in the the first line.  This would waste some space for each row but then the space is soon to be released (see later in this comment).

         You could read the first line and use strlen to determine its length.  the number of commas is length/2 and the number of integers is length/2+1.  Then rewind or close and reopen the file to process for real.

         You could read the file with fgetc counting each comma until you hit the '\n'.  The number of integers is count_of_commas+1.

    YOU HAVE MEMORY LEAKS.  level is a local variable.  When you exit this function, it is destroyed.  The memory it points to and all the memory pointed to the level[i] pointers remain allocated and unreachable.  You need to delete each level[i] and then delete level.

     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.

    For the line count:

         You could pre-process the file reading each character in an fgetc loop counting each '\n'.

         You could pre-process the file reading each line in a getline loop.

         Allocate space for an estimated number of line pointers and assign the address to level.  As you process each line, allocate the space for the integers to the corresponding level[i].  When you have used up all the line pointers currently allocated, allocate a larger set, copy the pointers from level to this set, delete the space for level, set level to point to the new space and continue processing.  Repeat as needed till all lines processed.  This is a little easier if you use malloc and realloc instead of new and delete.

    2. what if I decide to have double digits between the commas? like 01 instead of 1.

    If you count commas as suggested above, the number of digits per integer doesn't matter.

     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?)

    Since lvlWidth and lvlHeight are local, if you compute these on the fly you need some method or letting everyone else know what values you ended up with.  A global variable seems like the easiest option.  As discussed in previous threads, delcare the globals external in a header and define them exactly once in a single .cpp file

    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

    If its not the end, what will follow.  If the data that follows does not affect level, does it really belong in this file or processed by this function?  In any event, after reading a line, you can easily determine if it starts with the string "end" (strcmp, strncmp, etc).  Once you determine that it does, proceed on to whatever the new processing is.
    Monday, July 01, 2013 11:44 PM
  • @Barry-Schwarz

    How do I have memory leaks? I look at the memory usage on the program and it doesn't go up a single bit. level[][] is only used by create, but it creates lvlBox that everything else collides with. lvlWidth and lvlHeight are external in other processes, and appear to work right now. That part is fine. I see you put alot into helping me, however I am really sorry, I didn't understand it. I need some code to help me with that.

    Tuesday, July 02, 2013 4:14 PM
  • Memory leaks don't necessarily mean memory consumption grows.  It means you can no longer access the memory once the function ends.  It remains allocated for the life of your program even though you are no longer using it.  And you can never free it.  It constitutes sloppy programming.  It may not have an impact on this particular project but consuming resources you no longer need will hurt you eventually.

    lvlWidth land lvlHeight are defined in your function.  They cease to exist as soon as your function ends.  Even if other functions declare external variables with those names, they are not accessing the variables this function is using.

    "I don't understand" doesn't tell us anything.  WHAT don't you understand?  Be specific.

    Wednesday, July 03, 2013 4:17 AM
  • I have cleaned alot of code up. And things do use lvlWidth and lvlHeight for checking, such as player and enemy, all the time. Anyways, now my levels look like this:

    01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 
    01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 01 01 
    01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 01 01 
    01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 01 01 
    01 00 00 01 01 01 01 00 00 00 00 00 01 01 01 01 01 00 00 01 01 01 
    01 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 01 01 01 
    01 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 01 01 01 
    01 00 00 01 00 00 00 01 01 01 00 00 00 00 00 00 01 00 00 01 01 01 
    01 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 01 01 01 
    01 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 01 01 01 
    01 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 01 00 00 01 01 01 
    01 00 00 01 01 01 01 01 01 01 01 01 01 01 01 01 01 00 00 01 01 01 
    01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 01 01 
    01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 01 01 
    01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 01 

    the lvlWidth for this should be 22 and the lvlHeight is 15. Those however, are hardcoded in, I would like for the game to decide the lvlWidth and lvlHeight based off of the rows and columns. I understand putting in a max size for the levels, which is why I may make the maximum resolution 3840 × 2160 (while unsafe, you never know; lvlWidth would be 240 and lvlHeight would be 135). The maximum amount of tiles is the lvlWidth * lvlHeight (so right now its 330, and with the max settings it would be 32400). I have a constant int called MAX_TILES which is 330 right now. However when placing tiles, the code only goes as far as the lvlWidth * lvlHeight.

    Do you think you can help me with that? Thanks


    • Edited by DoctorNicholas Thursday, July 04, 2013 12:05 AM I figured something out
    Thursday, July 04, 2013 12:02 AM
  • Most of us are not in the business of writing applications for you.  You need to develop you own code and when it doesn't do what you want, post it with and description of what you wanted and what actually occurred.

    One question that I l have is why are placing a tile at every possible pixel?

    While you are at it, when you do this you might want to start a new thread because you are no longer concerned with counting some characters and excluding others.

    Friday, July 05, 2013 1:31 AM
  • I'm not quite sure what you mean.  The code I posted does the whole first half of your function.  When it's done, "grid" is a replacement for "level" in your code.  The next part is to allocate "lvlBox" to be the same size, but since you didn't show us the declaration of lvlBox, I have to guess.

    std::vector<boxtype> lvlBox( grid.size() );
    for( int i = 0; i < grid.size(); i++ )
    {
        lvlBox[i].resize(grid[i].size());
        for( int j = 0; j < grid[i].size() j++ )
        {
    if( level[i][j] )
    { lvlbox[i][j].x = (i*16); lvlbox[i][j].y = (j*16); lvlbox[i][j].w = 16; lvlbox[i][j].h = 16;
    }

    }
    }



    Tim Roberts, VC++ MVP Providenza & Boekelheide, Inc.

    Monday, July 08, 2013 4:42 PM