绘制图块地图使我的 fps 降低得很慢

发布于 2024-10-30 04:53:00 字数 2160 浏览 1 评论 0原文

好的,我正在尝试将 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();

Okay, I'm trying to get my fps to 60, but right now it's at around 20. What can I do to this code to speed it up? Note: this is c++ using sfml.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海螺姑娘 2024-11-06 04:53:00

看起来您正在迭代关卡的像素,而不是图块。重写它就像

 ///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);
    }
}

It looks like you're iterating over the pixels of your level, instead of over the tiles. Rewrite it like

 ///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);
    }
}
给妤﹃绝世温柔 2024-11-06 04:53:00

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

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

I lack expertise in the area, but you're drawing your tilemap for every tile, based on the parameters it's taking, it looks like it's redrawing the entire tilemap even though it's changed at most a single tile.

How would only calling App.Draw(tilemap); after every row, or perhaps after you've set the entire screen affect things.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文