2

好的,所以我正在尝试使用 SDL 在屏幕上绘制一个 tilemap,而我的 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 中将关卡绘制到图像,然后显示该图像(带有裁剪),但这使我的内存上升到 50,000k。

所以我要问的是如何在不让我的 cpu 或 ram 上升的情况下绘制一个 tilemap(在 SDL 中)?

4

1 回答 1

4
  • 确保在加载 tilemap 时将其转换为与屏幕相同的格式,这样 blitting 就不必执行任何转换。SDL_DisplayFormat 为您执行此操作。
  • 你在哪里调用渲染()?确保每帧调用一次,不多也不少。
于 2011-04-09T11:29:42.633 回答