Sign in to follow this  
madmaurice

SDL: Logo fade in out?

Recommended Posts

you know that fade in - fade out in games where logos of the developers are shown at the beginning. however i tried to create this in SDL. my idea was a black background and the fade in fade out with alpha but it fades in but it doesn't fade out and it fades in a little to fast. normally a picture is fully opaque with alpha 255 but somehow the logo is opaque with alpha at 10. Here's my code
#include "SDL/SDL.h"
#include <string>
#include "sdltimer.h"

#ifdef main
#undef main
#endif

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

using namespace std;

SDL_Surface *screen;
SDL_Surface *logo;
Uint8 *keys;

#include "useful_sdl_functions.cpp"

void Msg(string text) {
     MessageBox(NULL,text.c_str(),"SDLTimer",MB_OK);
}

void CleanUp() {
     if(logo == NULL) { SDL_FreeSurface(logo); }
     
     SDL_Quit();
}

int loadfiles() {
    //SDL Logo
    logo = loadbmp("logo.bmp");
    if(logo == NULL) {
         return 1;
    }
    return 0;
}

int main(int argc, char* argv[]) {
    bool game_running = true;
    bool showlogo = true;
    int alpha = 0;
    int dir = 1;
    SDL_Rect offset;
    SDL_Rect screenrect;
    SDLTimer alphaswitch;
    SDL_Event event;
    logo = NULL;
    screen = NULL;
    if(SDL_Init(SDL_INIT_VIDEO) == -1) {
         Msg("Could not initialize SDL!");
         exit(1);
    }
    atexit(CleanUp);

    screen = SDL_SetVideoMode(800,600,32,SDL_HWSURFACE | SDL_DOUBLEBUF);
    if(screen == NULL) {
         Msg("Error while setting sdl video mode!");
         exit(1);
    }
    if(loadfiles() != 0) {
         Msg("Error loading required files!");
         exit(1);
    }
    offset = CenterInScreen(screen,logo);
    screenrect = screen->clip_rect;
    alphaswitch.start();
    while(game_running) {
         while(SDL_PollEvent(&event)) {
              if(event.type == SDL_QUIT) {
                   game_running = false;
              }
         }
         keys = SDL_GetKeyState(NULL);
         if(keys[SDLK_ESCAPE]) {
              game_running = false;
         }
         if(alpha == SDL_ALPHA_OPAQUE && dir > 0) {
              dir = -1;
         }
         if(alpha == SDL_ALPHA_TRANSPARENT && dir < 0) {
              game_running = false;
         }
         if(alphaswitch.time() > 20) {
              alpha += dir;
              printf("alpha: %d",alpha);
              alphaswitch.reset();
         }
         SDL_SetAlpha(logo,SDL_SRCALPHA,alpha);
         SDL_BlitSurface(logo,NULL,screen,&offset);
         SDL_Flip(screen);
    }
    CleanUp();
    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