简体   繁体   中英

How to clear a part of screen in SDL 2.0

So, currently i'm writing a game and i have a small texture(20x20) that fills screen (1680x1050). My player moves on this backgroud, all in game loop. I need my backgound to be static and drawn just once, but SDL_RenderClear redraws all the area and this causes to lag. How can i draw it once, and then update it with my player figure?

There is really no way to "Draw it once and leave it like that" But there is a way you can make just a part of to draw in order to have the same result, just draw the part of the background that the character is, before drawing the new character.

In more details, draw all the "blocks" that are touched by the hero even by a little bit, then draw your hero over them.

Here is an example:

//LocationX is the location of hero on the X axis
//LocationX /20 is the number of the first texture that is drawn on X axis
//LocationX +Width (width of hero) +20 (width of texture) this is the number of the last texture on X axis

//Now draw everything from first to last texture that is touched the hero (just calculate the Y axis the same way as X axis!)
for (int xxx = LocationX /20; xxx < LocationX +Width + 20; xxx++)
{
    for (/*Do the same for Y axis*/)
    {
        draw(texture, xxx *20, yyy *20);
    }
}
//Draw Hero here, the background is clear!

Literally answering, you should use SDL_RenderDrawRect to 'clear a part of screen'.

However, you've mentioned that you have texture at background - RenderClear wouldn't draw a texture, so something in this description appears to be wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM