gpt4 book ai didi

c++ - 绘制 tilemap 使我的 fps 降低到爬行

转载 作者:行者123 更新时间:2023-11-28 08:21:53 25 4
gpt4 key购买 nike

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

最佳答案

看起来您正在遍历关卡的像素,而不是遍历图 block 。像这样重写

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

关于c++ - 绘制 tilemap 使我的 fps 降低到爬行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5530174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com