Sign in to follow this  
Joshnathan

Fading image

Recommended Posts

hi all, I just want to ask how I could make an image fade away. I know that for a normal rect I could do something like: for(int i = 255, i > 0, i--) { SDL_FillRect(img, NULL i) } but how could I have the same effect for an image?

Share this post


Link to post
Share on other sites
You can fade from any image to any other (including black or white or red or green screens, etc) by doing a per-pixel linear interpolation. That's the way we used to do it about 5 years ago, and the formula for interpolating between source and destination pixel values is:
result = (1 - u) * source + u * destination
where u is the interpolation factor and varies from 0 to 1. For your purposes, you can multiply all scalar coefficients by 255 for simplicity.

As I said, that's the old way of doing it. These days you can blit an image with an alpha parameter, and by varying the value of the alpha you can fade toward a specific color. See the documentation for SDL_SetAlpha here for details.

Share this post


Link to post
Share on other sites
hmm, I try'd this:

void Draw()
{
SDL_Rect rect;
rect.y = 30;
rect.x = 30;
SDL_BlitSurface(image, NULL, screen, &rect);
SDL_SetAlpha(image, flag, alpha);
alpha--;
}


but it won't work. isn't this what I need to do to make the image fade?
BTW, I am offcourse updating the Draw() function constantly...

Share this post


Link to post
Share on other sites
First of all, I think you want to place SDL_SetAlpha() before SDL_BlitSurface(). What is the value of the flags variable? I'm not sure if this is true, but I think you can't use SDL_SetAlpha() if you already have an alpha channel.

Share this post


Link to post
Share on other sites
Trivial -> 1. What is type and color depth of image you are trying to blit?
Repeat -> 2. I second Gyrbo: what is flag value? and do you set alpha to anything before blitting? ;-)
Important -> 3. What exactly happens, what are the effects you see on screen?
Advice -> 4. Probably you should post more code...?

Share this post


Link to post
Share on other sites
this is the entire code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL/SDL.h>
#include <windows.h>

SDL_Surface *image;
SDL_Surface *screen;
int alpha = 255;
Uint32 flag;


void Draw()
{
SDL_Rect rect;
rect.y = 30;
rect.x = 30;
SDL_BlitSurface(image, NULL, screen, &rect);
SDL_SetAlpha(image, flag, alpha);
alpha--;
}

int main (int argc, char *argv[])
{
char *msg;
int done;

/* Initialize SDL */
if (SDL_Init (SDL_INIT_VIDEO) < 0)
{
sprintf (msg, "Couldn't initialize SDL: %s\n", SDL_GetError ());
MessageBox (0, msg, "Error", MB_ICONHAND);
free (msg);
exit (1);
}
atexit (SDL_Quit);

/* Set 640x480 16-bits video mode */
screen = SDL_SetVideoMode (640, 480, 16, SDL_SWSURFACE | SDL_DOUBLEBUF);
if (screen == NULL)
{
sprintf (msg, "Couldn't set 640x480x16 video mode: %s\n",
SDL_GetError ());
MessageBox (0, msg, "Error", MB_ICONHAND);
free (msg);
exit (2);
}
SDL_WM_SetCaption ("SDL MultiMedia Application", NULL);

image = SDL_LoadBMP("characters/elf.bmp");

done = 0;
while (!done)
{
SDL_Event event;

/* Check for events */
while (SDL_PollEvent (&event))
{
switch (event.type)
{
case SDL_KEYDOWN:
break;
case SDL_QUIT:
done = 1;
break;
default:
break;
}
}
Draw();
SDL_Flip(screen);
}

return 0;
}



flag is initiated, but not assigned any value.

Share this post


Link to post
Share on other sites
Have your mother told you that not initializing variables may give you stupid and weird bugs? :->

Of course, you must initialize flag with SDL_SRCALPHA and add missing FillRect:



void Draw()
{
SDL_Rect rect;
rect.y = 30;
rect.x = 30;
SDL_SetAlpha(image, flag, alpha);
SDL_FillRect(screen, 0, 0);
SDL_BlitSurface(image, NULL, screen, &rect);
alpha--;
}




And now it's working... and one more thing: change that ugly, old and evil


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL/SDL.h>



to


#include <cstdio> // that was old and nasty, use new C++ libraries
#include <cstdlib> // -||-
// #include <string.h> comment out, it's not needed
#include <SDL.h> // SDL *could* be pretty anywhere, so including it that way wasn't good idea



Also look out for sprintf (msg, "Couldn't initialize SDL: %s\n", SDL_GetError ()); and msg - you're not allocating it anywhere any memory, so in sprintf you'll end writing to wild pointer - segmentation fault anyone? Probably you should use std::string or define msg as static array;

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