我怎么能画一个平铺图没有我的ram使用或cpu使用的方式上升

How can I draw a tilemap without my ram useage or cpu useage going way up?

本文关键字:ram 我的 cpu 方式 怎么能 一个      更新时间:2023-10-16

我试着用SDL在屏幕上画一个平铺图,我的fps很糟糕。我试过用一种不同的方式在SFML中做这件事,但这种方式使我的ram使用率大大提高。

我试图在SDL中绘制它的方式是有一个我切碎的SDL_Surface *tilemap;,并像这样渲染:

void apply_surface ( int sourceX, int sourceY, int sourceW, int sourceH, int x, int y, SDL_Surface* source, SDL_Surface* destination ){
     // make a temporary rectangle to hold the offsets
     SDL_Rect offset;
     SDL_Rect sourceRect;
     // give the offsets to the rectangle
     offset.x = x;
     offset.y = y;
     sourceRect.x = sourceX;
     sourceRect.y = sourceY;
     sourceRect.h = sourceH;
     sourceRect.w = sourceW;
     // blit the surface
     SDL_BlitSurface( source, &sourceRect, destination, &offset );
}

void render(){
    for(int x = 0; x < lv.width; x++){
        for(int y = 0; y < lv.height; y++){
            if(x * lv.tileSize < 640 && y * lv.tileSize < 480){
                int tileXCoord = 0;
                int tileYCoord = 0;
                int tileSheetWidth = tilemap->w / lv.tileSize;
                if (lv.tile[x][y] != 0)
                {
                    tileXCoord = lv.tile[x][y] % tileSheetWidth;
                    tileYCoord = lv.tile[x][y] / tileSheetWidth;
                }
                apply_surface(tileXCoord * lv.tileSize, tileYCoord * lv.tileSize, lv.tileSize, lv.tileSize, x * lv.tileSize, y * lv.tileSize, tilemap, screen);
                }
        }
    }
}

这种方式会提高我的CPU,降低我的FPS。

我尝试在SFML中绘制图像的水平,然后显示该图像(使用裁剪),但这使我的ram高达50,000k。

所以我问的是我怎么能画一个平铺图(在SDL)没有让我的cpu或ram走的方式?

  • 请确保在加载tilemap时,将其转换为与屏幕相同的格式,这样比特化就不必执行任何转换。SDL_DisplayFormat为你做这个。
  • 在哪里调用渲染()?确保每帧调用一次,不多也不少。