2011-04-03 34 views
0

好吧,我試圖讓我的fps達到60,但現在它在20左右。我能做些什麼來加速這個代碼?注意:這是使用sfml的C++。繪製地圖正在降低我的fps爬行

App.Clear(); 
    for(int x = 0; x < lv.width; x++){ 
     for(int y = 0; y < lv.height; y++){ 

      int tileXCoord = 0; 
      int tileYCoord = 0; 
      int tileSheetWidth = tilemapImage.GetWidth()/lv.tileSize; 

      if (lv.tile[x][y] != 0) 
      { 
       tileXCoord = lv.tile[x][y] % tileSheetWidth; 
       tileYCoord = lv.tile[x][y]/tileSheetWidth; 
      } 

      tilemap.SetSubRect(sf::IntRect(tileXCoord * lv.tileSize, tileYCoord * lv.tileSize, (tileXCoord * lv.tileSize) + lv.tileSize, (tileYCoord * lv.tileSize) + lv.tileSize)); 
      tilemap.SetPosition(x * lv.tileSize, y * lv.tileSize); 
      App.Draw(tilemap); 
     } 
    } 

    playerSprite.SetSubRect(sf::IntRect(player.width * player.frame, player.height * player.state, 
            (player.width * player.frame) + player.width, (player.height * player.state) + player.height)); 
    playerSprite.SetPosition(player.x, player.y); 

    App.Draw(playerSprite); 

    if(player.walking){ 
     if(player.frameDelay >= 0) 
      player.frameDelay--; 

     if(player.frameDelay <= 0){ 

      player.frame++; 
      player.frameDelay = 10; 

      if(player.frame >= 4) 
       player.frame = 0; 
     } 
    } 


    for(int x = 0; x < lv.width; x++){ 
     for(int y = 0; y < lv.height; y++){ 

      int tileXCoord = 0; 
      int tileYCoord = 0; 
      int tileSheetWidth = tilemapImage.GetWidth()/lv.tileSize; 

      if (lv.ftile[x][y] != 0) 
      { 
       tileXCoord = lv.ftile[x][y] % tileSheetWidth; 
       tileYCoord = lv.ftile[x][y]/tileSheetWidth; 
      } 

      tilemap.SetSubRect(sf::IntRect(tileXCoord * lv.tileSize, tileYCoord * lv.tileSize, (tileXCoord * lv.tileSize) + lv.tileSize, (tileYCoord * lv.tileSize) + lv.tileSize)); 
      tilemap.SetPosition(x * lv.tileSize, y * lv.tileSize); 
      App.Draw(tilemap); 
     } 
    } 


    App.Display(); 

回答

0

我缺乏專業知識的領域,但你繪製你的tilemap的,每瓦的基礎上,它的服用,它看起來像它的重繪即使它至多一個單一瓦改變了整個tilemap的參數。

怎麼只會調用App.Draw(tilemap);在每一行之後,或者也許在設置了整個屏幕之後影響事物。

+0

然後,我只得到1行繪製每行或屏幕。 – CyanPrime 2011-04-03 14:37:40

+0

再次,我缺乏這方面的專業知識。但它看起來像兩個tilemap.Set ...行配置了tilemap對象的一部分。 App.Draw(瓷磚地圖)然後繪製它。除非在迭代之間對tilemap對象的更改丟失,否則我不明白爲什麼會這樣。如果我完全錯了,只要不理我,我現在就會安靜下來。 – preinheimer 2011-04-03 14:41:59

1

它看起來像你迭代你的關卡的像素,而不是在瓷磚上。重寫它像

///get the width of a tile 
// get the height of a tile 
int tileWidth = tilemapImage.getWidth(); 
int tileHeight = tilemapImage.getHeight(); 

//find the number of tiles vertically and horizontally, by dividing 
// the level width by the number of tiles 
int xTiles = lv.width/tileWidth; 
int yTiles = lv.height/tileHeight(); 

for (int x = 0; x < xTiles; x++) { 
    for (int y = 0; y < yTiles; y++) { 
     // Do your calculations here 

     //ie: if(Walking) { draw_walk_anim; } 
     // draw_tile[x][y]; 

     tilemap.SetPosition(x * tileWidth, y * tileHeight); 
    } 
} 
+0

你能告訴我你的意思嗎? – CyanPrime 2011-04-03 14:55:07