Sign in to follow this  
EzkimOrava

SDL Problem

Recommended Posts

Is there any tutorial or something that u can learn fade sdl_surface to other. I tried google but it didnt help, and if u are bored u can make one :D

Share this post


Link to post
Share on other sites
Here's an example from a game I'm working on ("desenha()" is the function I'm using to blit surfaces)


for (short int a=255;a>0;a--) {
if (SDL_PollEvent(&event) && event.type==SDL_KEYDOWN) {skip=true;}
if (SDL_PollEvent(&event) && event.type==SDL_QUIT) {skip=true;done=true;break;}
if (skip) {break;}
desenha(screen,fundo_estrelas,0,0);
SDL_SetAlpha(desenvolvimento,SDL_RLEACCEL|SDL_SRCALPHA,a);
desenha(screen,desenvolvimento,0,0);
SDL_Delay(1000/FPS);
SDL_Flip(screen);
}

Share this post


Link to post
Share on other sites
If there is back screen and i wand fade a picture in it (surface) and then i want it to disappear. Both with diffrent functions. Like FadeOn(surface, time); Fadeout(Surface, time);

Share this post


Link to post
Share on other sites
Quote:
Original post by EzkimOrava
If there is back screen and i wand fade a picture in it (surface) and then i want it to disappear. Both with diffrent functions. Like FadeOn(surface, time); Fadeout(Surface, time);


The function you are looking for is SDL_SetAlpha. You will have to implement the time part yourself (use SDL_GetTicks()).

Share this post


Link to post
Share on other sites
I have made something, but it doesnt work. Could someone fix it :)

void FadeIn(SDL_Surface *eka,SDL_Surface *naytto, int speed) {
int fade = 0;
SDL_SetAlpha(eka,SDL_RLEACCEL|SDL_SRCALPHA,fade);
if( fade != 255) {fade += speed;}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by EzkimOrava
I have made something, but it doesnt work. Could someone fix it :)

void FadeIn(SDL_Surface *eka,SDL_Surface *naytto, int speed) {
int fade = 0;
SDL_SetAlpha(eka,SDL_RLEACCEL|SDL_SRCALPHA,fade);
if( fade != 255) {fade += speed;}
}


The problem is that fade is always zero. Fade is a local variable, so it is set to zero every time you call it.

I don't know if you are using C or C++. You need to associate some state with the surface to know how much to fade it by. This example has no error checking.

class FadingSurface {
public:
enum FadeOptions {
FADE_IN, FADE_OUT
};
FadingSurface( SDL_Surface *surface, Uint32 milliseconds, FadeOptions options );
void draw( int x, int y ); // assumes drawing to screen
bool finished() const;
private:
SDL_Surface *surface;
Uint32 startTime, duration;
FadeOptions option;
};

FadingSurface::FadingSurface(SDL_Surface *surface, Uint32 milliseconds, FadeOptions options)
:
surface(surface),
startTime(SDL_GetTicks()),
duration(milliseconds),
option(options)
{
}

bool FadingSurface::finished() const
{
return (SDL_GetTicks() - startTime) / static_cast<float>(duration) > 1.0f;
}

void FadingSurface::draw( int x, int y )
{
SDL_Surface *screen = SDL_GetVideoSurface();
SDL_Rect dest = {x,y};

float value = (SDL_GetTicks() - startTime) / static_cast<float>(duration);
if( option == FADE_OUT )
{
value = 1 - value;
}

int fade = static_cast<int>(value * SDL_ALPHA_OPAQUE);

SDL_SetAlpha(surface,SDL_SRCALPHA,fade);
SDL_BlitSurface(surface,0,screen,&dest);
}

int main(int, char **)
{
SDL_Init(SDL_INIT_VIDEO);
SDL_Surface *screen = SDL_SetVideoMode(800,600,0,SDL_ANYFORMAT);
SDL_Surface *image = SDL_LoadBMP("samplem.bmp");

FadingSurface::FadeOptions options = FadingSurface::FADE_IN;
FadingSurface fader(image,2500,options);

bool running = true;
while(running)
{
SDL_FillRect(screen,0,0);

SDL_Event event;
while(SDL_PollEvent(&event))
{
if( event.type == SDL_QUIT )
{
running = false;
}
}

fader.draw(40,50);

if( fader.finished() )
{
if( options == FadingSurface::FADE_IN )
{
options = FadingSurface::FADE_OUT;
}
else
{
options = FadingSurface::FADE_IN;
}
fader = FadingSurface(image,2500,options);
}
SDL_Flip(screen);
}
SDL_FreeSurface(image);
SDL_Quit();
return 0;
}


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