Sign in to follow this  
JoeMagoe

[SDL & C++] Blitting SDL_Surfaces onto another created with SDL_CreateRGBSurface...

Recommended Posts

OK, I tried to fix this problem, but I just couldn't find the solution. I'm afraid to post my question here because it makes me sound like such a nub. XD What I am trying to do is blit images (.png's) onto a surface previously created with SDL_CreateRGBSurface(). I am working hard to maintain the transparency of the images and then blit them onto a transparent surface, which I then proceed to blit onto the screen, but I'm failing. When I blit them directly on to the screen, they maintain their alpha and blit correctly. But, my project doesn't allow me to blit these images directly to the screen. I have to use the .png format for my images with their included alpha. If this was simply a matter of color keying out the magenta, I wouldn't be posting up here. Any help? And sorry if I sounded rude up there. I'm just a little frustrated :)

Share this post


Link to post
Share on other sites
Quote:
Original post by JoeMagoe
But, my project doesn't allow me to blit these images directly to the screen.


What exactly happens when you try to do this?

Share this post


Link to post
Share on other sites
Quote:
Original post by kratz
Quote:
Original post by JoeMagoe
But, my project doesn't allow me to blit these images directly to the screen.


What exactly happens when you try to do this?


By this I just mean that my project requires me to use .png's. I can blit directly to the screen, but I am not allowed due to the design of my project.

I would just like to know how to properly create a empty surface that I can clear later multiple times to a transparent state so I can properly blit my images to it. This, in my game's context, is being done to have tiles blitted on to a surface that is then blitted to the screen according to the camera's position.

Come to think about it, I could actually do this by blitting to the screen... But, still, I'd like to know how to do the above.

Share this post


Link to post
Share on other sites
Are you sure that all surfaces you create have an alpha channel? Most probably the surface you're creating with SDL_CreateRGBSurface doesn't have an alpha channel, as you stated that blitting directly to the screen works fine. This might be useful. Make sure that, when creating a surface, you specify the AMASK parameter, as in here.

Share this post


Link to post
Share on other sites
I have set the correct masks and have also declared the surface with an alpha. When I blit something to the surface I just created, it will not show unless I have filled the new surface with some color. When I do end up filling, if I try to give the color I'm filling with an alpha of zero, the images also get an alpha of zero. If I give it an alpha of, say, 128, the images are blitted onto the surface with an alpha of 128. Why do the images change alpha when all I have done is fill the destination surface with a color?

Share this post


Link to post
Share on other sites
I looked at the SDL documentation, and it turns out that this is the problem:

Quote:
Note that RGBA->RGBA blits (with SDL_SRCALPHA set) keep the alpha of the destination surface. This means that you cannot compose two arbitrary RGBA surfaces this way and get the result you would expect from "overlaying" them; the destination alpha will work as a mask.


How am I supposed to get around this? I want to create a transparent surface (RGBA) that I want to blit .png's to (RGBA). I don't see how this is possible without major workarounds. I'd like to avoid color keying. And thanks for all your help, guys. I find this a very supportive and helpful community.

[Edited by - JoeMagoe on July 21, 2009 12:04:08 PM]

Share this post


Link to post
Share on other sites
OK, solved. I've learned that SDL's own blitting system is horrible, with no extra support for these kind of "natural" things. We're making games, not abiding to rules or anything. Come on! Anyway, here's my solution:

While browsing the internet, I found a few people recommend SDL_gfx's custom blitting functions. Since I already had SDL_gfx, I decided to try them. I added the SDL_gfxBlitFunc.h to my program, but it turns out that it wasn't there. So, I downloaded the package from the site (since the one I had was from Ubuntu's repositories) and tried to make. Turns out that a few of SDL_gfx's files have problems with a 64-bit OS. This is where, once again, SPriG comes in to save the day. SPriG had its own custom blitting functions, with proper documentation. These functions even allowed me to set my own blitting modes!

So, with a quick and minor replacement of syntax, I started using RGBA->RGBA blitting. Yay!

By the way, SPriG is a great, free extension for SDL, which has most of SDL_gfx's capabilities and even more. Previously, I used SPriG to allow line thickness when drawing. I would recommend this extension to most over SDL_gfx (though, I don't know about both's speed costs. Feel free to correct me if SPriG's load is large enough to hinder a program's usability). SPriG is located here.

[Edited by - JoeMagoe on July 22, 2009 12:41:33 AM]

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