1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
| /*******************************************************/
/** Programme de PERRUCHON Romain pour developpez.com **/
/** Chapitre V : SDL_TTF **/
/*******************************************************/
//Les fichiers d'entête
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "SDL/SDL_ttf.h"
#include <string>
//Les attributs de l'ecran (640 * 480)
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;
//Les surfaces
SDL_Surface *background = NULL;
SDL_Surface *message = NULL;
SDL_Surface *screen = NULL;
//La structure d'evenement
SDL_Event event;
//Le font qu'on va utiliser
TTF_Font *font;
//La couleur du font
SDL_Color textColor = { 255, 255, 255 };
SDL_Surface *load_image( std::string filename )
{
//L'image qui est chargée
SDL_Surface* loadedImage = NULL;
//L'image optimisée que nous utiliserons par la suite
SDL_Surface* optimizedImage = NULL;
//Chargement de l'image
loadedImage = IMG_Load( filename.c_str() );
//Si l'image est chargée correctement
if( loadedImage != NULL )
{
//creation de l'image optimisée
optimizedImage = SDL_DisplayFormat( loadedImage );
//liberation de l'ancienne image
SDL_FreeSurface( loadedImage );
//si l'image optimisée créé est bonne
if( optimizedImage != NULL )
{
//transparence
SDL_SetColorKey( optimizedImage, SDL_RLEACCEL | SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 0xFF, 0xFF ) );
}
}
//on retourne l'image optimisé
return optimizedImage;
}
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
//on blit la surface
SDL_BlitSurface( source, clip, destination, &offset );
}
bool init()
{
//initialisation de tout les sous-systemes de sdl
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{
return false;
}
//on met en place l'ecran
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
//Si il y a une erreur lors de la mise en place de l'ecran
if( screen == NULL )
{
return false;
}
//Initialisation de SDL_ttf
if( TTF_Init() == -1 )
{
return false;
}
//on met en place la barre caption de la fenetre
SDL_WM_SetCaption( "TTF Test", NULL );
//si tout s'est bien passé
return true;
}
bool load_files()
{
//chargement du fond
background = load_image( "background.png" );
//Ouverture du font
font = TTF_OpenFont( "CaslonBold.ttf", 28 );
//si il y a un probleme au chargement du fond
if( background == NULL )
{
return false;
}
//Si il y a une erreur au chargement du font
if( font == NULL )
{
return false;
}
//Si tout s'est bien passé
return true;
}
void clean_up()
{
//Liberation des surfaces
SDL_FreeSurface( background );
SDL_FreeSurface( message );
//On ferme le font qu'on a utilisé
TTF_CloseFont( font );
//On quitte SDL_ttf
TTF_Quit();
//On quitte SDL
SDL_Quit();
}
int main( int argc, char* args[] )
{
//ce qui va nous permettre de quitter
bool quit = false;
//Initialisation
if( init() == false )
{
return 1;
}
//Chargement des fichiers
if( load_files() == false )
{
return 1;
}
//application du texte
message = TTF_RenderText_Solid( font, "Test pour sdl_ttf", textColor );
//Si il y a une erreur lors de l'application du texte
if( message == NULL )
{
return 1;
}
//Application des surfaces(images) sur l'ecran
apply_surface( 0, 0, background, screen );
apply_surface( 200, 200, message, screen );
//mise à jour de l'ecran
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
//Tant que l'utilisateur n'a pas quitter
while( quit == false )
{
//tant qu'il y a un evenement dans le handler
while( SDL_PollEvent( &event ) )
{
//Si l'utilisateur ferme la fenetre avec le X
if( event.type == SDL_QUIT )
{
//On quitte le programme
quit = true;
}
}
}
//liberation des surface et on quitte sdl
clean_up();
return 0;
} |
Partager