Advertisement

Redraw 2D backgrounds every frame?

Started by January 13, 2002 10:01 AM
8 comments, last by brewknowc 22 years, 11 months ago
Hi, I''m new to directX and am only about half way through lamothe''s book, but a question came to mind. When you have a character walking around on a pre rendered background image, do you have to draw that background to the back surface every frame? I was wondering because there seems like there would be a more efficient way. I know lamothe did this for one of his small demos in chap 7. Which is the faster part of the process? Drawing the bitmap to the back buffer or flipping it? - Free Your Mind -
- Free Your Mind -
Flipping is faster : it just involves changing in memory the pointer that tells the video bios where the image is stored.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
You have to redraw the entire screen otherwise your last frame would still be stuck on the screen, and the character would leave a trail of sprites behind him. There are a number of efficient ways to do this, like Fruny said, page flipping.
It sounds like you''re a little confused on the processes, I may be wrong though. First you draw everything to the backbuffer, then you flip the backbuffer with the frontbuffer, repeat.


Make games, it''s fun
You draw your frame in some off-screen buffer. Then you either flip it or blit it to the front buffer. You only get page flipping in full-screen apps, remember, so blitting is still necessary to display the frame when your app is running in a window.

Also, you don''t have to redraw the background every frame. There''s such a thing as dirty rectangles (or regions) that can be used to squeeze some more performance when actual pixel setting is a bottleneck.
Dirty rectangles? I never heard of them. How do those work?

-------------------------GBGames' Blog: An Indie Game Developer's Somewhat Interesting ThoughtsStaff Reviewer for Game Tunnel
Advertisement
1) Draw the background image on the back buffer

2) Before you blt your character copy the part of the backbuffer that the character is going to cover into an offscreen surface
3) blt your character
4) flip the surfaces
5) blt your offscreen surface onto the backbuffer to restore it
go back to step 2

Now instead of drawing a surface the size of your render target and another surface the size of your character, you are just drawing two surface the size of your character. Of course if you have a whole crap load of objects moving every frame and being drawn over your background (and possibly each other) than you might as well screw the whole thing and draw your background every frame. 2d games don''t really have the speed problems 3d games have so I wouldn''t worry about it.
Yeah, dirty rectangles are of limited use these days. But if you''re writing a GDI app, dirty rectangles (or--better yet--masking regions) ase the way to go.
guys! directly rectangles aren''t implemented that easy, i''ve read books saying about using clippers, that is, you use a cliplist to hold the rectangles, anyway i posted a thread here, asking for an article about this so if anybody here is kind-hearted to submit an article...
http://www.dualforcesolutions.comProfessional website designs and development, customized business systems, etc.,
I only brought up dirty rectangles to point out that you don''t have to redraw everything for every frame like someone here asserted. Yes, they are a pain in the neck, and the only time they''d really be of much use these days is—as I said before—for a GDI game. In such a case, you can just use Windows'' implementation of dirty rectangles, saving yourself a lot of work.

This topic is closed to new replies.

Advertisement