Skip to main content
gamedev.net
🔒 Locked

[SDL]Refreshing Background Image

Started by WhoCares357 3 replies 1.9k views
Original Post
WhoCares357
WhoCares357
I was wondering how one would go about refreshing the background image with SDL. I know that if one only has a blank background (not an image) one can use SDL_FillRect to clear the screen. But is there a method for clearing a background composed of an image?
Drew_Benton
Drew_Benton
Quote:
Original post by WhoCares357
I was wondering how one would go about refreshing the background image with SDL. I know that if one only has a blank background (not an image) one can use SDL_FillRect to clear the screen. But is there a method for clearing a background composed of an image?


By not drawing the background image [wink].

Right now you will have some code that draws an image to the screen, your background. If you have your conventional loop, you will be clearing the screen, drawing your stuff, then flipping the buffer. If you want to change the background image, you need to target the code that is actually drawing the image. Otherwise, it will always be drawn. Hope that makes sense.
WhoCares357
WhoCares357
I am somewhat confused by that. This is how I'm drawing the screen:

1) I create a blank surface (the buffer) that is the main background.

2) I creat two more surfaces. One will be the background image that goes over the main blank surface and the second image is the object on the screen that goes over the second image.

3) I load the two images.

4) In the main loop I first use SDL_FillRect to fill the first blank surface (the main background) with black.

5) I process the user input and set the coordinates of the object (the third image) accordingly.

6) I draw the second image (the second background) on top of the main background.

7) I draw the third image (the object) on top of the second image (the second background).

8) I flip the buffer.

9) The result is that the second image (the second background) doesn't refresh itself. Each time I draw the third image (the object) at a new place the old object remains on the screen.

[Edited by - WhoCares357 on October 5, 2008 9:04:42 PM]
Drew_Benton
Drew_Benton
Quote:
7) I draw the third image (the object) on top of the second image (the second background).


I think this step is what is causing the "no refresh" behavior. All drawing should be done to the "main background". Right now you are drawing to an image, so that process is "permanent" until you reload that background image.

Consider if you had a rubber stamp, one of the ones you press on an ink pad and then press on paper. Let's say you ink the stamp, stamp the piece of paper and write something on the paper. The stamp stays the same and the ink stays the same. The only thing that changes is the paper. Now let's say you get a new piece of paper (clearing the screen), but you cut off half the rubber stamp. When you go and ink the stamp and press it on the paper, you now have modified your template so the first page (screen) and second page (screen) look different.

So, any changes or drawing that needs to be done, should be done on the primary screen buffer, and not the template images you are using -- granted you are not purposefully modifying those graphics to change how they look. An example would be if you added blood spots on the source image to represent a different health state and reloaded the image as health recovered. That's a case where you would have use to draw to the image directly to get an effect of a "decal".

Alternatively though, you can just reload the surface each frame change, but that would be a graphics killer unless it were a small image and you loaded it from a memory location and not from a file. I'd revise the design to not draw directly on the image though and hit the primary screen instead.
WhoCares357
WhoCares357
Okay, I understand that now. Thank you very much.

Edit: I drew the background then the object (both on the buffer) and it works perfectly. Thank you again.

Topic Locked

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

Sign in to reply to this topic.