I know there are already many topics on this, but I need some help with this, since I any of the snippets/suggestions given in other topics worked for me.
Have you tried blitting it without calling SDL_SetAlpha on the surface? Note that your call to SDL_DisplayFormatAlpha simply leaks memory, as you do not capture the return value.
Have you tried blitting it without calling SDL_SetAlpha on the surface? Note that your call to SDL_DisplayFormatAlpha simply leaks memory, as you do not capture the return value.
I have tried blitting it without SetAlpha, and transparency worked. (I drew a brown block first, and then the 'gold' surface, and it worked). Thanks a lot! I don't get it though, everybody else fixed their problems by setting alpha, and the opposite happened with me.
IMG_Load prepares the image such that it is ready to blit. So you don't need to do anything special to get alpha transparency here. However, if you want the fastest blit, you might still want to (correctly) use SDL_DisplayFormatAlpha().
Anyway, that is the reason I suggested you try doing nothing to the image first. However I don't understand the behaviour you are describing. I have created a simple test program:
#include <iostream>
#include <cstdlib>
SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0x00, 0x00, 0xff));
SDL_Rect dest = { x, y };
SDL_BlitSurface(image, NULL, screen, &dest);
SDL_Flip(screen);
}
return 0;
}
You can use the preprocessor to enable the call to SDL_DisplayFormatAlpha(), and SDL_SetAlpha(). On my system, it looks the same regardess of whether none, one or both are enabled.
My problem was in another part of the code. Either way, how can I access different parts of an image (like if I have a spritesheet), how to access different parts of the image?
My problem was in another part of the code.
[/quote]
Why is why making a minimal example is really handy. It can isolate "SDL problems" from "weird bugs somewhere in your code". In making such a minimal example, you often will find the problem yourself.
Either way, how can I access different parts of an image (like if I have a spritesheet), how to access different parts of the image?
[/quote]
You can specify a "source rectangle" as the second parameter to SDL_BlitSurface(). The image data from the source surface at the given x,y and with the given width and height (clipping will be performed) will be rendered to the destination surface at the given x and y position (the destination with and height are ignored).
Why is why making a minimal example is really handy. It can isolate "SDL problems" from "weird bugs somewhere in your code". In making such a minimal example, you often will find the problem yourself.
Either way, how can I access different parts of an image (like if I have a spritesheet), how to access different parts of the image?
[/quote]
You can specify a "source rectangle" as the second parameter to SDL_BlitSurface(). The image data from the source surface at the given x,y and with the given width and height (clipping will be performed) will be rendered to the destination surface at the given x and y position (the destination with and height are ignored).
[/quote]
Very true, writing some code, in a new file helps a lot. I often do it, but since this was a very common problem, I immediately deduced it was an alpha problem, and not a code structure problem (which it ended up being).
Yes, I just usually put NULL when blitting a surface, but now I understand what that argument is for, thanks a lot for all the help!
When I started using SDL, I was told there was not much information on this library, and that I'd better use OpenGL, but I'm actually finding everything plain simple and straightforward.
When I started using SDL, I was told there was not much information on this library, and that I'd better use OpenGL, but I'm actually finding everything plain simple and straightforward.
[/quote]
SDL is very easy to use, it is one of the main selling points. I suspect the opposite would be true, I think most beginners to both would encounter more trouble getting OpenGL up and running than SDL.