Sign in to follow this  
Cosmic R

class arrays

Recommended Posts

hi there. I've been programming using C(self taught) for 10 years and have finally decided to force myself into learning c++. Anyway, I've hit a problem already. I'm trying to make a 2d array of a class. I'm sure you can do this, but I dont think I'm doing it right. Can anyone offer some advice? Here is some of my code:
class Ctile {    
public:
    int base;
} tileset[100][100];
    
void Gloadtileset()
{
    int x,y;

    PACKFILE *pfp;
    pfp = pack_fopen("map.pak","r");

    if(!pfp) allegro_message(allegro_error);

    for(y=0;y<100;y++)
    {
        for(x=0;x<100;x++)
        {
            tileset[x][y].base = pack_getc(pfp);
        }
    }

    pack_fclose(pfp);
}


int main() {
    int x,y;
	init();

    for(y=0;y<100;y++)
    {
        for(x=0;x<100;x++)
        {
            if(tileset[x][y].base) putpixel(screen,x,y,WHITE);
        }
    }

	while (!key[KEY_ESC]) {

	}

	deinit();
	return 0;
}
END_OF_MAIN()

my 'map.pak' is fine, I checked it with a hex editor. its just a file with 10000 bytes of 1's. however, the program is supposed to read in each byte or tile if you will and simply put a pixel accordingly. but its not doing it. for some reason tileset[x][y].base is always equal to 0.

Share this post


Link to post
Share on other sites
You never call Gloadtileset() from what I see.

Also, you don't have a constructor or a destructor for your class.

Oh, and in C++ you can declare variables from 'for' if you just need them to exist for those loops.
Example:
for(int x = 0; x<100; x++)


Share this post


Link to post
Share on other sites
That is C not C++. You just replaced 'struct' with 'class/public:'. Here is a real C++ solution:


// Make a class for the whole map
class TileSet
{
private:
struct Tile
{
int base;
};

// You shold encapsulate your data in classes as much as possible
// Another improvement would be to set up the 100 as a constant
// variable, so you can change sizes easier.
Tile tiles[100][100];

public:
// Pass the file name to the constructor so you can reuse this
// code with many different map files
TileSet( const std::string & file_name )
{
PACKFILE *pfp;
pfp = pack_fopen( file_name.c_str( ), "r" );

if( !pfp ) allegro_message( allegro_error );

for( int y=0; y<100; y++ )
{
for( int x=0; x<100; x++ )
{
tiles[x][y].base = pack_getc( pfp );
}
}

pack_fclose( pfp );
}

// You can fill in this code for whatever
// drawing logic you need
void draw( )
{
for( int y=0; y<100; y++ )
{
for( int x=0; x<100; x++ )
{
if( tiles[x][y].base )
putpixel( screen, x, y, WHITE );
}
}
}
};




Then your main will be much simpeler:


int main()
{
TileSet ts( "map.pak" );

ts.draw( );

while( !key[KEY_ESC] )
{
// do nothing
}

return 0;
}
END_OF_MAIN()


Share this post


Link to post
Share on other sites
thanks for the helpful replies. And yes, I'm stupid - I never called Gloadtileset. I guess I got all caught up in the fact that it was c++ and overlooked a simple error. As for it not being proper C++, I had planned on building the class up after I had had some sort of functionality in the program. But that other solution is exactly what I was looking for. Thanks again.

Share this post


Link to post
Share on other sites
Quote:
Original post by Cosmic R
thanks for the helpful replies. And yes, I'm stupid - I never called Gloadtileset. I guess I got all caught up in the fact that it was c++ and overlooked a simple error. As for it not being proper C++, I had planned on building the class up after I had had some sort of functionality in the program. But that other solution is exactly what I was looking for. Thanks again.


Glad you got it working, good luck!

Share this post


Link to post
Share on other sites

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