Skip to main content
gamedev.net
🔒 Locked

Refresh the background

Started by Clawer Sep 4, 2006 at 3:08 PM 2 replies 950+ views
Original Post
Clawer
Clawer
Hi all, I was enjoying myself with SDL, and now I have a little doubt. I want to blit on the background a Rect ( using an interesting code portion found on gamedev! ) then show the background again and blit another shape ( . . . ) ( check out the EXAMPLE 1 ) Example I absolutely don't want the situation shown on the EXAMPLE 2. So I was thinking how to refresh the background. Is that a nice solution?

void render()
{
     SDL_Rect rect;
     rect.x = rand( ) % ( WIDTH );
     rect.y = rand( ) % ( HEIGHT );
     rect.w = rand( ) % ( WIDTH - rect.x );
     rect.h = rand( ) % ( HEIGHT - rect.y );
     
     // Let's fill a nice rect over the background!
     SDL_FillRect( screen, ▭, SDL_MapRGB( screen->format, rand( ) % 256,
                                              rand( ) % 256, rand( ) % 256 ) );
                           
                                              
     SDL_Flip( screen );
     SDL_Delay(200);
     
     // I'm blitting the background over the "old" Rect!
     applySurface( 0, 0, image, screen );
}

Do you know any other more efficient ways ( in terms of memory etc. etc. ) to solve this little problem? Thanks in advance! [Edited by - Clawer on March 2, 2007 2:50:46 PM]
evillive2
evillive2
Your drawing order should always be back to front so that things in the front appear to block out things in the back.

If you are using a double buffer (as it appears since the SDL_Flip() call is there) you would draw like so:

void render(){     // draw the background first     applySurface( 0, 0, image, screen );     SDL_Rect rect;     rect.x = rand( ) % ( WIDTH );     rect.y = rand( ) % ( HEIGHT );     rect.w = rand( ) % ( WIDTH - rect.x );     rect.h = rand( ) % ( HEIGHT - rect.y );          // then fill in the rectangle     SDL_FillRect( screen, ▭, SDL_MapRGB( screen->format, rand( ) % 256,                                              rand( ) % 256, rand( ) % 256 ) );                               // that was all done on the back buffer so we want to flip the back buffer    // to the front so we can see what we did     SDL_Flip( screen );     SDL_Delay(200); // about 5 random rectangles per second? Stroby :)     }


Since your background is the entire size of the screen you don't have to worry about clearing it first as it will overwrite everything anyway.


Evillive2
Clawer
Clawer
Quote:
Original post by evillive2
Since your background is the entire size of the screen you don't have to worry about clearing it first as it will overwrite everything anyway.


My spider sense is tingling! :)
Let's suppose this:

void render(){     // draw the background first     applySurface( 200, 200, image, screen );     //.....  


The background isn't blitted on x = 0, y = 0
so it cannot cover the entire size of the screen.
How can I "clear" the surface?.. Maybe using another "blitting"
to mask the "not-refreshed" area? Is that right?

Anyway thank you very much for the quick answer! :)
evillive2
evillive2
Clearing a surface can be as simple as:
// clear the screen to black
SDL_FillRect( screen, ▭, 0 );

or you can do a dirty rectangle system where you only erase and re-draw in certain areas of the screen using SDL_UpdateRect.

There are many other different ways to do this as well (ok, mostly variations on those 2 ideas). My suggestion is to try a bunch of ideas and see how each one behaves. That is how you will learn what scenario works best for you, not by asking others.
Evillive2

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.