[Development] Hooded Development #2

posted in Retro Grade
Published October 27, 2012
Advertisement
Hello all! Since my last hooded development post I have succeeded in loading a map from a file! Here's how I did it:
Map.h:

#pragma once
class Map
{
public:
Map(std::string MapFileName);
~Map(void);
const TileType& GetTile(int PosX, int PosY);
const std::vector>& GetWholeMap();
private:
int MapSizeX;
int MapSizeY;
std::vector> MapVector;
std::ifstream MapFile;
};

Map.cpp:

#include "StdAfx.h"
#include "Map.h"

Map::Map(std::string MapFileName)
{
MapFile.open(MapFileName);
if (MapFile.is_open())
{
MapFile >> MapSizeX >> MapSizeY;
MapVector.resize(MapSizeX);
for (int index = 0; index < MapSizeX; ++index)
{
MapVector[index].resize(MapSizeY);
}
int LoadPositionX = 0;
int LoadPositionY = 0;
while (!MapFile.eof())
{
MapFile >> MapVector[LoadPositionX][LoadPositionY];
++LoadPositionX;
if (LoadPositionX >= MapSizeX)
{
LoadPositionX = 0;
++LoadPositionY;
}
}
}
else
{
std::cout << "Could Not Load Map File:" << MapFileName << std::endl;
}
}

Map::~Map(void)
{
}

const TileType& Map::GetTile(int PosX, int PosY)
{
return (TileType)MapVector[PosX][PosY];
}

const std::vector>& Map::GetWholeMap()
{
return MapVector;
}


So essentially, I have my map.txt files structured as such:

8 4
1 1 1 1 1 1 1 1
4 5 2 3 5 5 7 3
8 5 6 1 5 7 2 9
1 1 1 1 1 1 1 1

The top two numbers are the X size of the file (Number of Columns) and the Y size of the file (Number of rows). Then, the numbers under it specify certain block types! Currently you can request a certain tile in my std::vector of tiles, or you can get the whole map! What do you think of this, seasoned developers? Is it smart, or is there anything I'm forgetting / not planning for?
0 likes 0 comments

Comments

Nobody has left a comment. You can be the first!
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement