Hey. I've gotten a little more work on this done ( finally ). I haven't had too much time to put into it, though. So, this may be a disappointing amount of progress.
Right now, I'm stuck with making an editor to at least edit the vector so I can start working on some scrolling techniques and such. I just wanted to make sure I'm doing this right. I'm very new to this.
This is the class file for the tile map:
#include <vector>
//Self explanatory
struct sTile
{
int type;
bool solid;
};
class cTileMap
{
protected:
//These are various var's used for
//drawing functions and such.
int screen_tile_w;
int screen_tile_h;
int tile_width;
int tile_height;
bool initialized;
//This is the vector of vectors for storing
//the tile values.
std::vector<std::vector<sTile>> vTileMap;
public:
//Constructors
cTileMap ( ) { };
cTileMap ( int tile_width, int tile_height );
//Size the initial vector. I'm hoping
//to improve the system soon.
void size_map ( int size );
//Duh.
void draw_map ( int x_start, int y_start );
//Also duh ( it is if you read the above posts ).
void save_map ( char* file_name );
void load_map ( char* file_name );
};
Then, divided among a few .cpp files, are the definitions:
Constructor definition:
cTileMap::cTileMap ( int tile_width, int tile_height )
{
this->tile_width = tile_width;
this->tile_height = tile_height;
screen_tile_w = dbScreenWidth ( ) / tile_width;
screen_tile_h = dbScreenHeight ( ) / tile_height;
}
Definition of size_map:
void cTileMap::size_map ( int size )
{
if ( vTileMap.size ( ) < size )
{
vTileMap.resize ( size );
for ( int i = 0; i < size; i++ )
vTileMap[i].resize ( size );
initialized = true;
}
else
dbText ( 10, 10, "Error. Tile map is larger than specified size." );
}
EDIT: I know that the method I used for handling the size error is not the best way to do it. Just temporary.
Drawing code:
void cTileMap::draw_map ( int x_start, int y_start )
{
if ( vTileMap.size ( ) )
{
int n = 50;
int map_size = ( int ) vTileMap.size ( );
for ( int x = 0; x < map_size; x++ )
{
for ( int y = 0; y < map_size; y++ )
{
dbSprite ( n, x * tile_width, y * tile_height, vTileMap[y][x].type );
n++;
}
}
}
}
And, finally, saving and loading code:
void cTileMap::save_map ( char* file_name )
{
ofstream output ( file_name, ios_base::binary | ios_base::trunc );
int map_size = ( int ) vTileMap.size ( );
for ( int x = 0; x < map_size; x++ )
{
for ( int y = 0; y < map_size; y++ )
output.write ( ( char * ) &vTileMap[x][y], sizeof vTileMap[x][y] );
}
output.close ( );
}
void cTileMap::load_map ( char* file_name )
{
ifstream input ( file_name, ios_base::binary );
long start, end, length;
start = input.tellg ( );
input.seekg ( 0, ios::end );
end = input.tellg ( );
length = end - start;
int num_tiles = length / ( sizeof sTile );
double grid_size = sqrt ( ( double ) num_tiles );
input.seekg ( 0 );
for ( int x = 0; x < grid_size; x++ )
{
for ( int y = 0; y < grid_size; y++ )
input.read ( ( char * ) &vTileMap[x][y], sizeof vTileMap[x][y] );
}
input.close ( );
initialized = true;
}
Of course, there are a few debugging functions and such that I removed, but that is it so far. I was just updating all of you that helped me on where I was and, as a beginner, hoping to get some pointers as to where and how I sucked. Thanks.