• entries
    33
  • comments
    37
  • views
    38675

[Development] Hooded Development #2

Sign in to follow this  
superman3275

602 views

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?
Sign in to follow this  


0 Comments


Recommended Comments

There are no comments to display.

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