开发者

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

开发者 https://www.devze.com 2023-02-23 08:47 出处:网络
Okay, so I\'m trying to draw a tilemap on the screen with SDL, and my fps is really bad cause of it. And I\'ve tried doing it in SFML a different way, but that way makes my ram useage go way up.

Okay, so I'm trying to draw a tilemap on the screen with SDL, and my fps is really bad cause of it. And I've tried doing it in SFML a different way, but that way makes my ram useage go way up.

the way I'm trying to draw it in SDL is having a SDL_Surface *tilemap; that I chop up, and render like this:

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

This way brings my CPU way up, and drops my FPS.

I tried in SFML to draw the level to a Image, and than display that image (with cropping) but that made my ram go up to 50,000k.

so what I am asking is how can I draw a tilemap (in SDL) without making my cpu or ram go way up?


  • Make sure that when you load tilemap, that you convert it to the same format as the screen, so that blitting doesn't have to perform any conversion. SDL_DisplayFormat does this for you.
  • Where are you calling render()? Make sure it's called once per frame, no more, no less.
0

精彩评论

暂无评论...
验证码 换一张
取 消