Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "MMPMap.h"
- MMPMap::MMPMap(int sizeX, int sizeY):
- sizeX(sizeX), sizeY(sizeY)
- {
- //tworzenie danych mapy
- this->mapData = new int[sizeX * sizeY];
- //czyszczenie mapy
- for (int y = 0; y != this->getSizeY(); ++y)
- {
- for (int x = 0; x != this->getSizeX(); ++x)
- {
- this->setField(x,y,0);
- }
- }
- }
- MMPMap::MMPMap(std::string fileName)
- {
- std::ifstream plik(fileName.c_str());
- //sprawdzanie formatu pliku
- std::string bufor;
- std::getline(plik, bufor);
- if (bufor != "MMP1") /*return false*/;
- //ladowanie rozmiaru i parametrow mapy
- std::getline(plik, bufor);
- while (bufor != "DATA")
- {
- int pos = bufor.find('=');
- if (pos == std::string::npos) /*return false*/;
- std::string key = bufor.substr(0, pos);
- std::string value = bufor.substr(pos + 1);
- if (key == "sizeX") this->sizeX = stoi(value);
- if (key == "sizeY") this->sizeY = stoi(value);
- else this->properties[key] = value;
- std::getline(plik, bufor);
- }
- //ladowanie danych mapy
- this->mapData = new int[this->sizeX * this->sizeY];
- for (int i = 0; i != this->sizeX * this->sizeY; ++i)
- {
- plik >> this->mapData[i];
- }
- }
- MMPMap::~MMPMap()
- {
- //usuniecie tabeli z danymi mapy
- delete[] this->mapData;
- }
- int MMPMap::getSizeX()
- {
- return this->sizeX;
- }
- int MMPMap::getSizeY()
- {
- return this->sizeY;
- }
- int MMPMap::getField(int x, int y)
- {
- //sprawdzanie poprawnosci
- if (x >= 0 && x < this->sizeX && y >= 0 && y < this->sizeY)
- {
- return this->mapData[y * this->sizeX + x];
- }
- else
- {
- return -1;
- }
- }
- void MMPMap::setField(int x, int y, int value)
- {
- //sprawdzanie poprawnosci
- if (x >= 0 && x < this->sizeX && y >= 0 && y < this->sizeY)
- {
- this->mapData[y * this->sizeX + x] = value;
- }
- }
- void MMPMap::setProperty(std::string propertyName, std::string propertyValue)
- {
- this->properties[propertyName] = propertyValue;
- }
- std::string MMPMap::getProperty(std::string propertyName)
- {
- return this->properties[propertyName];
- }
- bool MMPMap::saveToFile(std::string fileName)
- {
- std::ofstream plik(fileName.c_str());
- if (!plik.is_open()) return false;
- plik << "MMP1\n";
- plik << "sizeX=" << this->getSizeX() << '\n';
- plik << "sizeY=" << this->getSizeY() << '\n';
- for(std::map<std::string, std::string>::iterator it = this->properties.begin(); it != this->properties.end(); ++it)
- {
- plik << it->first << '=' << it->second << '\n';
- }
- plik << "DATA\n";
- for (int y = 0; y != this->getSizeY(); ++y)
- {
- for (int x = 0; x != this->getSizeX(); ++x)
- {
- plik << this->getField(x,y) << ' ';
- }
- plik << '\n';
- }
- return true;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement