SED-P.NET

03 - Les surfaces (2/2)

samedi 20 décembre 2003, par Bech ()


Cette suite permet de donner des exemples d’utilisations des fonctions présentées dans l’article précédent.

Chargement d’une image

cet exemple explique le chargement d’une image depuis un fichier PNG. L’unité logger présente dans la distribution de JEDI-SDL permet de faire un log/trace du fonctionnement d’un soft.


program chargeIMG;

uses
  SDL,
  SDLUtils,
  SDL_Image,
  SysUtils,
  Logger;

var
  video_flags : integer;
  screen : PSDL_Surface;
  MainBoard : PSDL_Surface;

procedure checkLoadImg( var surface : PSDL_Surface; FileName : string );
Begin
    surface := IMG_Load( PCHAR(FileName) );
    if ( surface = nil ) then
          begin
          Log.LogError( Format(
          'Couldn''t load this image %s : %s',
          [FileName, SDL_GetError] ), 'InitializeImages' );
          SDL_Quit;
          halt( 1 );
          end;
End;

begin
// initialisation de la SDL
 if ( ( SDL_Init(SDL_INIT_VIDEO or SDL_INIT_AUDIO)= -1 ) )then begin        
    MessageBox(0, PChar(Format('Couldn''t initialize SDL : %s', [SDL_GetError])), 'Error', MB_OK or MB_ICONHAND);
    exit;    
    end;

 // Mettre un nom sur la fenetre
 SDL_WM_SetCaption('SDL_Test 2', nil);

 // Changement de la résolution de l'affichage (640x480x8)
 // Si le paramètre 'fs' ou 'fullscreen' est transmis au programme
 // alors passage en plein écran.
 if ( ParamStr( 1 ) = '-fullscreen' ) or ( ParamStr( 1 ) = '-fs' ) then
    video_flags := SDL_ANYFORMAT or SDL_SWSURFACE or SDL_HWPALETTE or SDL_FULLSCREEN
    else
    video_flags := SDL_ANYFORMAT or SDL_SWSURFACE or SDL_HWPALETTE;
 screen := SDL_SetVideoMode( 640, 480, 8, video_flags );

// chargement du fichier
checkLoadImg( MainBoard, 'grid.png');
MainBoard := SDL_DisplayFormat( MainBoard );

// affichage
SDL_BlitSurface(MainBoard, nil, screen, nil);
SDL_UpdateRect( screen, 0, 0, 0, 0);

// tempo ...
SDL_Delay(10000);

// déchargement
SDL_FreeSurface(MainBoard);

// quitter SDL
SDL_Quit;
end.

Transition Alpha

Cette procedure permet de passer d’un écran à un autre en utilisant un fondu. En paramètre il faut lui fournir la surface de l’écran et celle de la nouvelle surface à afficher (sur la totalité de l’écran), ainsi que le niveau min et max de la valeur Alpha (transparence) et le pas de changement. Enfin il faut lui donner le délais entre chaque pas de transition... bla bla bla... un test c’est toujours mieux qu’une explication :


procedure AlphaTransition( screen, image : PSDL_Surface; step, min, max : UInt8; Delay : UInt32 );
var
  Lev : UInt8;
  OldAlpha : UInt8;
begin
       OldAlpha := Image.format.alpha;

       lev := ((max - min) div step) + min;
       SDL_SetAlpha(image, SDL_SRCALPHA or SDL_RLEACCEL, 2*lev);

       for lev := 0 to step do
               begin
               SDL_BlitSurface(image, nil, screen, nil);
               SDL_UpdateRect( screen, 0, 0, 0, 0);
               SDL_Delay(Delay);
               end;

       SDL_SetAlpha(image, SDL_SRCALPHA, OldAlpha);
       SDL_BlitSurface(image, nil, screen, nil);
       SDL_UpdateRect( screen, 0, 0, 0, 0);
end;

Répondre à cet article