درس 7 :True Type Font (TTF)

درس 7 :True Type Font (TTF)

SDL_ttf یک کتاب خانه الحاقی است که به وسیله ان می توانیم سطوحی از فونتهای TTF بسازیم

شما می توانید SDL_ttf را از اینجا دانلود کنید here

طبق درس 3 شما می توانید SDL_ttf را نصب کنید

 

//The surfaces

SDL_Surface *background = NULL;

SDL_Surface *message = NULL;

SDL_Surface *screen = NULL;

 

//The event structure

SDL_Event event;

 

//The font that's going to be used

TTF_Font *font;

 

//The color of the font

SDL_Color textColor = { 255, 255, 255 };

 

در اینجا ما متغییر ها را تعریف می کنیم backgroung . screen والبته message که مسئول نگهداری متن ما است

در اینجا دو نوع ساختار جدید می بینید SDL_Color که ساختاری برای نگهداری رنگ است و TTF_Font فونت است که ما می خواهیم استفاده کنیم

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 in error in setting up the screen

    if( screen == NULL )

    {

        return false;   

    }

   

    //Initialize SDL_ttf

    if( TTF_Init() == -1 )

    {

        return false;   

    }

   

    //Set the window caption

    SDL_WM_SetCaption( "TTF Test", NULL );

   

    //If everything initialized fine

    return true;

}

این تابع هم خیلی شبیه تابع قبلی است با فرق این که TTF_Init در آن کار اماده کردن محیط TTF را بر عهده دارد قبل از کار کردن با هر تابع TTF باید این دستور نوشته شود

bool load_files()

{

    //Load the background image

    background = load_image( "background.png" );

   

    //Open the font

    font = TTF_OpenFont( "artro.ttf", 28 );

   

    //If there was a problem in loading the background

    if( background == NULL )

    {

        return false;   

    }

   

    //If there was an error in loading the font

    if( font == NULL )

    {

        return false;

    }

   

    //If everything loaded fine

    return true;   

}

این تابع بارگزاری فایل هاست قبل از استفاده از هر TTF باید بوسیله  TTF_OpenFont اماده شود

اولین پارامتر مسیر و اسم فونت است دومین پارامتر سایز نوشته است در صورت بروز خطا مقدار NULL را بر می گرداند

    //Render the text

    message = TTF_RenderText_Solid( font, "The quick brown fox jumps over the lazy hound", textColor );

   

    //If there was an error in rendering the text

    if( message == NULL )

    {

        return 1;   

    }

   

    //Apply the images to the screen

    apply_surface( 0, 0, background, screen );

    apply_surface( 0, 200, message, screen );

   

    //Update the screen

    if( SDL_Flip( screen ) == -1 )

    {

        return 1;   

    }

سریعترین را برای نمایش فونت استفاده از تابع TTF_RenderText_Solid است که بی کیفیت ترین هم هست 3 پارامتر می گیرد فونت . متن و رنگ راههای دیگر هم هست که در Document TTF موجود است

void clean_up()

{

    //Free the surfaces

    SDL_FreeSurface( background );

    SDL_FreeSurface( message );

   

    //Close the font that was used

    TTF_CloseFont( font );

   

    //Quit SDL_ttf

    TTF_Quit();

   

    //Quit SDL

    SDL_Quit();

}

این تابع هم کار تمیز کردن حافظه را انجام می دهد

 

مباحث تکمیلی :

تابع دیگری به نام

SDL_Surface *TTF_RenderText_Blended(TTF_Font *font, const char *text, SDL_Color fg)

که کیفیت فوق العاده ای دارد

تابع دیگری به نام :

SDL_Surface *TTF_RenderText_Shaded(TTF_Font *font, const char *text, SDL_Color fg, SDL_Color bg)

که 4 پارامتر  می گیرد که یکی هم رنگ پس زمینه است و یک کادر دور ان می کشد

 

تابع دیگری به نام

void TTF_SetFontStyle(TTF_Font *font, int style)

که کار زیر خط دار کردن و توپر کردن و زیر خط دار کردن فونت را تعیین می کند

 

TTF_STYLE_BOLD
TTF_STYLE_ITALIC
TTF_STYLE_UNDERLINE

Example :

TTF_SetFontStyle(font, TTF_STYLE_BOLD|TTF_STYLE_ITALIC);

 

نظرات 0 + ارسال نظر
برای نمایش آواتار خود در این وبلاگ در سایت Gravatar.com ثبت نام کنید. (راهنما)
ایمیل شما بعد از ثبت نمایش داده نخواهد شد