如何在没有使用ram的情况下绘制tilemap与cpu使用情况相比如何?

时间:2011-04-09 07:15:35

标签: c++ c sdl sfml

好的,所以我试图用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中尝试将关卡绘制到图像,然后显示该图像(使用裁剪),但这使我的ram达到50,000k。

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

1 个答案:

答案 0 :(得分:4)

  • 确保在加载tilemap时,将其转换为与屏幕相同的格式,以便blitting不必执行任何转换。 SDL_DisplayFormat为您完成此任务。
  • 你在哪里调用render()?确保每帧调用一次,不多也不少。
相关问题