Fading image
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?
-----------------------------Sismondi GamesStarted c++ in October 2004...
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:
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.
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.
hmm, I try'd this:
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...
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...
-----------------------------Sismondi GamesStarted c++ in October 2004...
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.
::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!
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!
---http://www.michaelbolton.comI constantly dream about Michael Bolton.
thx really much for that last reply, it was very helpfull... Anyone have any ideas of how I could make this function work?
-----------------------------Sismondi GamesStarted c++ in October 2004...
lols Khaos
_______________________________ ________ _____ ___ __ _`By offloading cognitive load to the computer, programmers are able to design more elegant systems' - Unununium OS regarding Python
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...?
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...?
this is the entire code:
flag is initiated, but not assigned any value.
#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.
-----------------------------Sismondi GamesStarted c++ in October 2004...
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:
And now it's working... and one more thing: change that ugly, old and evil
to
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;
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;
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement