Advertisement
Guest User

Untitled

a guest
Dec 13th, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. #include "MMPMap.h"
  2.  
  3. MMPMap::MMPMap(int sizeX, int sizeY):
  4.     sizeX(sizeX), sizeY(sizeY)
  5. {
  6.     //tworzenie danych mapy
  7.     this->mapData = new int[sizeX * sizeY];
  8.  
  9.     //czyszczenie mapy
  10.     for (int y = 0; y != this->getSizeY(); ++y)
  11.     {
  12.         for (int x = 0; x != this->getSizeX(); ++x)
  13.         {
  14.             this->setField(x,y,0);
  15.         }
  16.     }
  17. }
  18.  
  19. MMPMap::MMPMap(std::string fileName)
  20. {
  21.     std::ifstream plik(fileName.c_str());
  22.  
  23.     //sprawdzanie formatu pliku
  24.     std::string bufor;
  25.     std::getline(plik, bufor);
  26.     if (bufor != "MMP1") /*return false*/;
  27.  
  28.     //ladowanie rozmiaru i parametrow mapy
  29.     std::getline(plik, bufor);
  30.     while (bufor != "DATA")
  31.     {
  32.         int pos = bufor.find('=');
  33.         if (pos == std::string::npos) /*return false*/;
  34.         std::string key = bufor.substr(0, pos);
  35.         std::string value = bufor.substr(pos + 1);
  36.         if (key == "sizeX") this->sizeX = stoi(value);
  37.         if (key == "sizeY") this->sizeY = stoi(value);
  38.         else this->properties[key] = value;
  39.         std::getline(plik, bufor);
  40.     }
  41.  
  42.     //ladowanie danych mapy
  43.     this->mapData = new int[this->sizeX * this->sizeY];
  44.     for (int i = 0; i != this->sizeX * this->sizeY; ++i)
  45.     {
  46.         plik >> this->mapData[i];
  47.     }
  48. }
  49.  
  50. MMPMap::~MMPMap()
  51. {
  52.     //usuniecie tabeli z danymi mapy
  53.     delete[] this->mapData;
  54. }
  55.  
  56. int MMPMap::getSizeX()
  57. {
  58.     return this->sizeX;
  59. }
  60.  
  61. int MMPMap::getSizeY()
  62. {
  63.     return this->sizeY;
  64. }
  65.  
  66. int MMPMap::getField(int x, int y)
  67. {
  68.     //sprawdzanie poprawnosci
  69.     if (x >= 0 && x < this->sizeX && y >= 0 && y < this->sizeY)
  70.     {
  71.         return this->mapData[y * this->sizeX + x];
  72.     }
  73.     else
  74.     {
  75.         return -1;
  76.     }
  77. }
  78.  
  79. void MMPMap::setField(int x, int y, int value)
  80. {
  81.     //sprawdzanie poprawnosci
  82.     if (x >= 0 && x < this->sizeX && y >= 0 && y < this->sizeY)
  83.     {
  84.         this->mapData[y * this->sizeX + x] = value;
  85.     }
  86. }
  87.  
  88. void MMPMap::setProperty(std::string propertyName, std::string propertyValue)
  89. {
  90.     this->properties[propertyName] = propertyValue;
  91. }
  92.  
  93. std::string MMPMap::getProperty(std::string propertyName)
  94. {
  95.     return this->properties[propertyName];
  96. }
  97.  
  98. bool MMPMap::saveToFile(std::string fileName)
  99. {
  100.     std::ofstream plik(fileName.c_str());
  101.     if (!plik.is_open()) return false;
  102.     plik << "MMP1\n";
  103.     plik << "sizeX=" << this->getSizeX() << '\n';
  104.     plik << "sizeY=" << this->getSizeY() << '\n';
  105.  
  106.     for(std::map<std::string, std::string>::iterator it = this->properties.begin(); it != this->properties.end(); ++it)
  107.     {
  108.         plik << it->first << '=' << it->second << '\n';
  109.     }
  110.  
  111.     plik << "DATA\n";
  112.     for (int y = 0; y != this->getSizeY(); ++y)
  113.     {
  114.         for (int x = 0; x != this->getSizeX(); ++x)
  115.         {
  116.             plik << this->getField(x,y) << ' ';
  117.         }
  118.         plik << '\n';
  119.     }
  120.     return true;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement