2013-01-22 22 views
2

我從一個關卡文件中加載地圖,這些文件只是與瓷磚中的一個圖塊對應的數字。如何讓我的TileSheet包裝?

這裏是一級文件

[Map] 
0 1 0 0 0 0 0 0 
0 0 20 0 0 0 0 0 
0 0 0 40 0 0 0 0 
0 0 0 0 63 0 0 0 
0 0 0 0 0 79 0 0 
0 0 0 0 0 0 0 0 

這裏是解釋它

void LoadMap(const char *filename, std::vector< std::vector <int> > &map) 
{ 
    std::ifstream openfile(filename); 
    if(openfile.is_open()) 
    { 
     std::string line, value; 
     int space; 

     while(!openfile.eof()) 
     { 
      std::getline(openfile, line); 

      if(line.find("[TileSet]") != std::string::npos) 
      { 
       state = TileSet; 
       continue; 
      } 
      else if (line.find("[Map]") != std::string::npos) 
      { 
       state = Map; 
       continue; 
      } 

      switch(state) 
      { 
      case TileSet: 
       if(line.length() > 0) 
        tileSet = al_load_bitmap(line.c_str()); 
       break; 
      case Map: 

       std::stringstream str(line); 
       std::vector<int> tempVector; 

       while(!str.eof()) 
       { 
        std::getline(str, value, ' '); 
        if(value.length() > 0) 
         tempVector.push_back(atoi(value.c_str())); 
       } 
       map.push_back(tempVector); 
       break; 
      } 
     } 
    } 
    else 
    { 
    } 
    } 

的代碼,這是它的外觀 http://i.imgur.com/6W49eWf.jpg

好了,所以我Tilesheet是1000由200,看起來像這樣http://i.imgur.com/Y83zBxj.png

如何在地圖文件中放入20或40時將其放到20或40?

void DrawMap(std::vector <std::vector <int> > map) 
{  
    for(int i, j = 0; i < map.size(); i ++) 
    { 
     for(j = 0; j < map[i].size(); j ++) 
     { 
      al_draw_bitmap_region(tileSet, map[i][j] * TileSizeX, 0, TileSizeX, TileSizeY, j * TileSizeX, i * TileSizeX, NULL); 
     } 
    } 
} 

另外,TileSizeX和TileSizeY是50

+0

不知道你在問什麼 – dchhetri

+0

你的瓷磚總是會變成1000 X的常數?如在,你將永遠有一個瓷磚中的20列瓷磚,無限數量的行? – crush

+0

@ user814628那麼我問的是如何讓它環繞瓷磚表面。也就是說,如果我在關卡文件中放入20個字符,我希望它回到開始處,然後沿着一個瓦片並顯示「20」瓦片。因爲現在它只是在一條線上。 – Manl400

回答

1

你需要計算你的目標瓷磚坐在地形設置的什麼細胞。你有tileset索引。根據拼圖的尺寸使用一些數學算法來確定拼塊的列和行。

//This is how many columns your tileset can have. 
//You could even dynamically calculate this if you wanted. 
static const int TILESET_COLCOUNT = 20; 

void DrawMap(std::vector<std::vector<int> > map) 
{  
    int mapRowCount = map.size(); 

    for (int i = 0; i < mapRowCount; ++i) 
    { 
     int mapColCount = map[i].size(); 

     for (int j = 0; j < mapColCount; ++j) 
     { 
      //This is your tileset index in your level map. 
      int tilesetIndex = map[i][j]; 

      //The tileset row can be calculated by dividing the tileset index by the number of columns in a tileset row. 
      int tilesetRow = floor(tilesetIndex/TILESET_COLCOUNT); 

      //The tileset column can be calculated by retrieving the remainder of the modulus operation on the total number of columns in a row. 
      int tilesetCol = tilesetIndex % TILESET_COLCOUNT; 

      al_draw_bitmap_region(
       tileSet, //The tileset 
       tilesetCol * TileSizeX, //multiply the tileset column by the size of a tile to get the source x 
       tilesetRow * TileSizeY, //multiply the tileset row by the size of a tile to get the source y 
       TileSizeX, //width 
       TileSizeY, //height 
       j * TileSizeX, //destination x 
       i * TileSizeX, //destination y 
       NULL //flags 
      ); 
     } 
    } 
} 
+1

謝謝!這工作完美!並且非常翔實。 – Manl400

相關問題