Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

MadProgrammer

SDL & Alpha Blending

This topic is 6158 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i need to be able to change the alpha value for a surface every frame. To do this i need to do SDL_SetAlpha() every frame. If i do that, however, nothing shows up. So, after doing the SetAlpha(), i have to do SDL_DisplayFormat(surface). That slows things down about 75 fps! How can i fix this? Here's the sprite class (homemade) i'm using:
    
struct SPRIZITE
{
	SPRIZITE() { surface=NULL; }
	SPRIZITE(const char *file) { surface=NULL; loadFromFile(file); }
	~SPRIZITE() {}
	void loadFromFile(const char *file)
	{
		out << "Loading Sprite From File - " << file << ", version " << FileVersion(file) << endl;
		SDL_Surface *stemp;
		stemp = SDL_LoadBMP(file);
		surface = SDL_DisplayFormat(stemp);
		SDL_FreeSurface(stemp);
		width = surface->w;
		height = surface->h;
		currAlpha = 255;
	}
	void Draw(int x, int y)
	{
		if(x >= SCREEN_WIDTH || x + width <= 0 || y >= SCREEN_HEIGHT || y + height <= 0)
			return;
		SDL_Rect r;
		r.x = x;
		r.y = y;
		r.w = 0;
		r.h = 0;
		SDL_BlitSurface(surface, NULL, pSurface, &r);
	}
	void SetColorKey(Uint8 r, Uint8 g, Uint8 b)
	{
		SDL_SetColorKey(surface, SDL_SRCCOLORKEY, SDL_MapRGB(surface->format, r, g, b));
	}
	void SetAlpha(Uint8 fade)
	{
		if(currAlpha == fade)
			 return;
		SDL_SetAlpha(surface, SDL_SRCALPHA, fade);
		SDL_Surface *stemp = SDL_DisplayFormat(surface);
		SDL_FreeSurface(surface);
		surface = stemp;
		currAlpha = fade;
	}

	SDL_Surface *surface;
	int height;
	int width;
	Uint8 currAlpha;
};
    
[EDIT] Posted Wrong Code :-) Edited by - MadProgrammer on February 1, 2002 8:23:45 PM

Share this post


Link to post
Share on other sites
Advertisement

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!