The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
about SDL make translucent effect,how make it?
gamehook


Joined: 25 Jun 2012
Posts: 3
Location: cn
I'm sorry ,my English is not good Sad



Im SDL beginner,i try make some image Effects,i got a game resource file ,in this game have a effect like this


i try make it,i uses follow code:

Code:

//The headers
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <string>

//Screen attributes
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
const int SCREEN_BPP = 32;
SDL_Color BGC={125,120,255};

//The surfaces
SDL_Surface *screen = NULL;
SDL_Surface *bg = NULL;
SDL_Surface *tm = NULL;
SDL_Surface *yf = NULL;
SDL_Surface *wp = NULL;
SDL_Surface *mz = NULL;
//The event structure
SDL_Event event;


SDL_Surface *load_image( std::string filename )
{
    //The image that's loaded
    SDL_Surface* loadedImage = NULL;

    //The optimized surface that will be used
    SDL_Surface* optimizedImage = NULL;

    //Load the image
    loadedImage = IMG_Load( filename.c_str() );

    //If the image loaded
    if( loadedImage != NULL )
    {
        //Create an optimized surface
        optimizedImage = SDL_DisplayFormat( loadedImage );

        //Free the old surface
        SDL_FreeSurface( loadedImage );

        //If the surface was optimized
        if( optimizedImage != NULL )
        {
            //Color key surface
            SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0, 0 ) );
        }
    }

    //Return the optimized surface
    return optimizedImage;
}

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
    //Holds offsets
    SDL_Rect offset;

    //Get offsets
    offset.x = x;
    offset.y = y;

    //Blit
    SDL_BlitSurface( source, clip, destination, &offset );
}

bool init()
{
    //Initialize all SDL subsystems
    if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
    {
        return false;
    }

    //Set up the screen
    screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );

    //If there was an error in setting up the screen
    if( screen == NULL )
    {
        return false;
    }


    SDL_WM_SetCaption( "alpha", NULL );

    //If everything initialized fine
    return true;
}

bool load_files()
{
    //Load the background image
    bg = load_image( "mbg.bmp" );
    tm = load_image( "tc2.bmp" );
    yf = load_image( "tc1.bmp" );
    wp = load_image( "lxd.bmp" );
    mz = load_image( "kltk.bmp" );

    //If there was a problem in loading the background
    if( bg == NULL )
    {
        return false;
    }

    //If everything loaded fine
    return true;
}

void clean_up()
{
    //Free the surfaces
    SDL_FreeSurface( bg );


    //Quit SDL
    SDL_Quit();
}

int main( int argc, char* args[] )
{
    int ti=200;//设置透明度
   
    //Quit flag
    bool quit = false;

    //Initialize
    if( init() == false )
    {
        return 1;
    }

    //Load the files
    if( load_files() == false )
    {
        return 1;
    }
    SDL_FillRect(screen,0,SDL_MapRGB( screen->format,BGC.r,BGC.g,BGC.b )) ;
    //Apply the background
    apply_surface( -7,44, bg, screen );//属性框背景原坐标 7 -44,按原坐标贴图不能显示完整,修改为0 0显示
    SDL_SetAlpha( tm, SDL_SRCALPHA|SDL_RLEACCEL, ti );//半透明显示图片
 
    apply_surface( 145, 170, yf, screen );//衣服和属性框坐标不在一个坐标系,两者坐标无参考性,故以衣服坐标为基准 (原图x=-22 y=-123)
    apply_surface( 102, 107, wp, screen );// (原图x=-65 y=-186)新贴图坐标为(基准当前坐标 +(当前贴图原坐标-(基准原坐标))此处为:145+(-65-(-22))=102    170+(-186-(-123))=107
    apply_surface( 176, 160, mz, screen );//(原图x=9 y=-133)此处坐标为:145+(9-(-22))=176   170+(-133-(-123))=160
    apply_surface( 82,76, tm, screen );//(-85 -217)  145+(-85-(-22))= 82    170+(-217-(-123))=76


    //Update the screen
    if( SDL_Flip( screen ) == -1 )
    {
        return 1;
    }

    //While the user hasn't quit
    while( quit == false )
    {
        //While there's events to handle
        while( SDL_PollEvent( &event ) )
        {
            //If a key was pressed
                       //If the user has Xed out the window
            SDL_Delay(100);
            if( event.type == SDL_QUIT )
            {
                //Quit the program
                quit = true;
            }
        }
    }

    //Free surfaces, fonts and sounds
    //then quit SDL_mixer, SDL_ttf and SDL
    clean_up();

    return 0;
}
 
 

i got effect like this:

the resource file is BMP,like this


how get the effect like in the game?

all resource file is here http://fuwensoft.99k.org/pic/alpha.zip
gamehook


Joined: 25 Jun 2012
Posts: 3
Location: cn
Why can not display pictures??????
gamehook


Joined: 25 Jun 2012
Posts: 3
Location: cn
the image file link is fixed!