Jump to content

  • Log In with Google      Sign In   
  • Create Account

SDL Image Transparency


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 munchor   Members   -  Reputation: 101

Like
0Likes
Like

Posted 17 December 2011 - 03:59 PM

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.


SDL_RWops *gold_rwop = SDL_RWFromFile("img/gold.png", "rb");
SDL_Surface *gold = IMG_LoadPNG_RW(gold_rwop);


This is the code I'm using to load a .png file. And this is the code I am using to display it:


// TILE_SIZE is defined to 25

SDL_SetAlpha(gold, SDL_SRCALPHA, SDL_ALPHA_TRANSPARENT);
SDL_DisplayFormatAlpha(gold);
SDL_Rect tile = {250 + x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE};
SDL_BlitSurface(gold, NULL, screen, &tile);

However, the image doesn't have transparency, the transparent part is just black.

Am I doing anything wrong? Thanks.

EDIT: I don't have any more SDL_Image related code, so I might be missing something (do I have to init something?)

Sponsor:

#2 rip-off   Moderators   -  Reputation: 8340

Like
0Likes
Like

Posted 17 December 2011 - 04:27 PM

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.

#3 munchor   Members   -  Reputation: 101

Like
0Likes
Like

Posted 18 December 2011 - 05:42 AM

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.

#4 rip-off   Moderators   -  Reputation: 8340

Like
0Likes
Like

Posted 18 December 2011 - 06:41 AM

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>

#include "SDL.h"
#include "SDL_image.h"

int main(int, char**)
{
	if(SDL_Init(SDL_INIT_VIDEO) < 0)
	{
		std::cerr << "Failed to initialise SDL: " << SDL_GetError() << '\n';
		return 1;
	}

	std::atexit(&SDL_Quit);

	SDL_Surface *screen = SDL_SetVideoMode(800, 600, 0, SDL_SWSURFACE);
	if(!screen)
	{
		std::cerr << "Failed to set video mode: " << SDL_GetError() << '\n';
		return 1;
	}

	SDL_Surface *image = IMG_Load("foo.png");
	if(!image)
	{
		std::cerr << "Failed to load imae: " << IMG_GetError() << '\n';
		return 1;
	}

#if 1
	SDL_Surface *temp = SDL_DisplayFormatAlpha(image);
	if(temp)
	{
		std::swap(temp, image);
		SDL_FreeSurface(temp);
	}
	else
	{
		std::cerr << "Failed to format image to display: " << SDL_GetError() << '\n';
		return 1;
	}

#if 1
	int result = SDL_SetAlpha(image, SDL_SRCALPHA, SDL_ALPHA_TRANSPARENT);
	if(result < 0)
	{
		std::cerr << "Failed to set image alpha: " << SDL_GetError() << '\n';
		return 1;
	}
#endif
#endif

	int x = 0;
	int y = 0;
	int vx = 1;
	int vy = 1;
	bool running = true;
	while(running)
	{
		SDL_Event event;
		while(SDL_PollEvent(&event))
		{
			if(event.type == SDL_QUIT)
			{
				running = false;
			}
			else if(event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE)
			{
				running = false;
			}
		}

		x += vx;
		y += vy;

		if(x < 0 || x + image->w > screen->w)
		{
			vx *= -1;
		}

		if(y < 0 || y + image->h > screen->h)
		{
			vy *= -1;
		}

		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.

#5 munchor   Members   -  Reputation: 101

Like
0Likes
Like

Posted 18 December 2011 - 05:05 PM

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?

#6 rip-off   Moderators   -  Reputation: 8340

Like
0Likes
Like

Posted 18 December 2011 - 05:18 PM

My problem was in another part of the code.

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?

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).

#7 munchor   Members   -  Reputation: 101

Like
0Likes
Like

Posted 19 December 2011 - 04:46 AM


My problem was in another part of the code.

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?

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).


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.

#8 rip-off   Moderators   -  Reputation: 8340

Like
0Likes
Like

Posted 19 December 2011 - 06:08 AM

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.

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS