绘制tilemap会将我的fps降低到爬行状态

时间:2011-04-03 14:29:54

标签: c++ frame-rate sfml

好吧,我正在努力让我的fps达到60,但现在它在20左右。我能对这段代码做些什么来加快速度呢?注意:这是使用sfml的c ++。

    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();

2 个答案:

答案 0 :(得分: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);
    }
}

答案 1 :(得分:0)

我缺乏该领域的专业知识,但是您根据所拍摄的参数为每个图块绘制瓷砖地图,看起来它正在重新绘制整个图块图,即使它最多只更改了一个图块。

如何只调用App.Draw(tilemap);在每一行之后,或者在您设置完整个屏幕后影响事物。