Jump to content
  • Advertisement
Sign in to follow this  
Joshnathan

Fading image

This topic is 5034 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

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
Advertisement
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
::laughs really hard::

I'm a OpenGL programmer and fortunately for me I don't have to worry about no per-pixel interpolation crap. Alpha blending ownz your face!

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
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!