Sign in to follow this  
haora

SDL issues...

Recommended Posts

Hi all, after trying allegro, I decided I want something more, and I'm trying SDL out...., I have to say it's pretty cool, very good speed, and a lot of control over things..., but I have some thing to ask: 1) Any good tutorial?, 'cause I've only found one, and not very good... 2) I have a problem with double buffering and dirty rectangles....., first, if I load a bitmap into a surface, lets call it "back", and then I blit it to "screen" (which is the main screen), when I flip "screen" to see the background the screen flickers..., I read that that's because of the double buffer..., and that to solve it, you have to blit the back into the screen, flip the screen, and then, blit back into screen again...I did that and the problem went away..., but when I start putting sprites into the mix, and move them around, I get trails, which are not supposed to be there, but appear because of the double buffer... Now..., has anyone encounter this error??? This (http://www.google.com/search?hl=en&lr=&q=sdl+tutorials+c%2B%2B+&btnG=Search) is the tutorial I'm following....the only difference is that this guy doesn't use double buffer... Well, I know it's kind of a lot to ask, but hopefully someone's been in my situation before... Thanks in advance....

Share this post


Link to post
Share on other sites
I followed the same tutorials when I started out, after I finished those I pretty much just started experimenting, besides the author never actually completed the series. I'd really like to see some code to understand a bit better.

Share this post


Link to post
Share on other sites
I don't have the code here (i'm at work), but basically, is the same as the one in that tutorial, the only difference is I use the "SDL_DOUBLEBUF|SDL_FULLSCREEN" flag to initialize the video mode...

Share this post


Link to post
Share on other sites
My drawing loop usually looks something like this:


...

SDL_Blit( some_surface, 0, screen, &pos );
SDL_Flip( screen );
SDL_FillRect( screen, 0 );

...



That will draw 'some_surface' to the screen, then flip the buffers to make it visible, then clear the backbuffer black, so if you move the images around then you shouldn't see trails. Now if you're using dirty rectangles I'm not familiar with that method, but I'm assuming that you'd have to keep track of the old position of the image so you can blit that part of the background back after the image or sprite has moved. Is that right? If it is then you may need to have something like 'old_pos' and 'new_pos'. I'm guessing you could take the old position and how far the image has moved and create a rectangle that you could use to blit the background back in that spot so no trails.

Hope that atleast gives you some ideas. [embarrass]

Share this post


Link to post
Share on other sites
What I do is:

for every sprite, I keep a copy of the background in which I'm standing (only the rectangle that the sprites takes) (without the sprite), and a reference to the background image...

So, for every frame I:
- blit the copy of where I'm standing in my old x and y into the screen (the place I'm leaving)
- update my copy of the background (the rectangle I mentioned), with my new coordinates
- draw the sprite into the screen

THEN flip the screen so I can see what I did...
Now, if I do this..., I get trails of the sprites..., BUT, if I blit the background image into the screen, BEFORE any of that..., the trails are gone....

Even though the problem is solved, I don't like the solution..., since bliting the background image into the screen is more time consuming that..., say..., not doing it..., right? [grin]

So..., any ideas as to what might be happening???

Thanks!!

Share this post


Link to post
Share on other sites
Quote:
Original post by haora
1) Any good tutorial?, 'cause I've only found one, and not very good...


Cone3D's Tutorials
NeHe's SDL Ports
Aaron's Game Tutorials
Ryan Clark's Game Programming Wiki's SDL Tutorials

Quote:
but appear because of the double buffer...
Now..., has anyone encounter this error???


You must clear the screen before hand as clockwise has mentioned, but with:
SDL_FillRect( screen, 0, 0x000000);

Where 0x000000 represents the 0xRRGGBB value you want. 000000 is black, FFFFFF is white, etc...

Quote:
THEN flip the screen so I can see what I did...
Now, if I do this..., I get trails of the sprites..., BUT, if I blit the background image into the screen, BEFORE any of that..., the trails are gone....


Because you are inexplicity clearing the entire screen when you blit a bitmap onto the surface. Ideally what you want to do is this:

Blit Background (fills entire screen)
Blit Everything Else
Blit Cursor (if applicable)
Flip


Then you are done, no need to clear the screen because by drawing the background, it automatically clears everything already on the screen [wink]

If you don't like that solution, what you can do is similar to what you just described. I had an older post that went over code that does what you want it to do, but it was not complete. I'll see if I can find that.

[edit] Here's that post. Note that you will need to do some improvments per stated in that thread.

Share this post


Link to post
Share on other sites
Drew_Benton: Thanks for the links.., I'll check them out..., and as for my problem with the trails leaved by the sprites, I solved it... using SLD_UpdateRects...., but I will check your way anyway....

wazoo69: great!, now all I need is a PDF free version, so I can download it and read it....[grin]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this