I'm having a few troubles with this. I can get it to work fine, but I don't know how to do out-of-range checking. Here is the line of code accessing the vector:
thisTile = tile->tiles[0][leftTileIndex + i][topTileIndex + j];
for (int i = 0; i < SCREEN_WIDTH / tile->tileWidth + 1; i++)
{
for (int j = 0; j < SCREEN_HEIGHT / tile->tileHeight + 1; j++)
{
try
{
thisTile = tile->tiles[0][leftTileIndex + i][topTileIndex + j];
srcRect.x = (thisTile % tile->numTilesPerRow - 1) * tile->tileWidth;
srcRect.y = floor((float)(thisTile / tile->tileHeight));
destRect.x = i * tile->tileWidth;
destRect.y = j * tile->tileHeight;
SDL_BlitSurface(tile->image, &srcRect, screen, &destRect);
}
catch (std::out_of_range &)
{
// Vector was out of range, so just let the bg color be drawn
}
}
}
and this is the declaration in the TileSet class:
std::vector<std::vector<std::vector<int> > > tiles;
tile is a pointer to a TileSet object, the part I'm talking about is bolded.
Basically all I want to know is how I could somehow use .at instead of each set of brackets for exception handling, since at the edge of a room the tileset ends but the function still checks for more tiles.