Draw Text

SDL¿¡¼­ TTF ÆùÆ®¸¦ ÀÌ¿ëÇÏ¿© ÇѱÛÀ» Ãâ·ÂÇغ¸ÀÚ.
SDL¿¡¼­ ÆùÆ®¸¦ Ãâ·ÂÇϱâ À§Çؼ­´Â SDL_ttf°¡ ÇÊ¿äÇÏ´Ù.




´Ù¿î·Îµå:

´ÙÀ½ÀÇ ¸µÅ©¿¡¼­ ´Ù¿îÀ» ¹Þ´Â´Ù.
https://www.libsdl.org/projects/SDL_ttf/
VC¸¦ »ç¿ëÇϹǷΠSDL2_ttf-devel-2.0.15-VC.zip ÆÄÀÏÀ» ´Ù¿î·Îµå ¹Þ´Â´Ù.

´Ù¸¥ SDL°ú °°Àº Æú´õ À§Ä¡¿¡ ¾ÐÃàÀº Ǭ´Ù.
SDL2_ttf-2.0.15 Æú´õ°¡ ÆùÆ® Æú´õÀÌ´Ù.



Çì´õÆÄÀÏ°ú ¶óÀ̺귯¸® ¼³Á¤

Çì´õÆÄÀÏ
#include "../SDL2_ttf-2.0.15/include/SDL_ttf.h"

¶óÀ̺귯¸®
#pragma comment(lib, "../SDL2_ttf-2.0.15/lib/x86/SDL2_ttf.lib")

3°³ÀÇ DLLÀ» ½ÇÇà Æú´õ¿¡ º¹»çÇÑ´Ù.
libfreetype-6.dll
SDL2_ttf.dll
zlib1.dll

ÃʱâÈ­,  Á¾·á


SDL_Init ÇÔ¼ö ½ÇÇàÈÄ¿¡ TTF_Init ÇÔ¼ö·Î ¶óÀ̺귯¸®¸¦ ÃʱâÈ­ ÇÑ´Ù.
//SDL_Init
TTF_Init();

SDL_Quit ÇÔ¼ö È£ÃâÀü¿¡ TTF_Quit ÇÔ¼ö·Î ¶óÀ̺귯¸®¸¦ Á¾·áÇÑ´Ù.
TTF_Quit();
//SDL_Quit

·»´õ·¯¸¦ »ý¼ºÈÄ¿¡ ÆùÆ®¸¦ ¸Þ¸ð¸®·Î ·ÎµùÇÑ´Ù.
ÆùÆ®´Â ½ÇÇàÆÄÀÏ°ú °°Àº À§Ä¡¿¡ µÐ´Ù.
ºñÁÖ¾ó ½ºÆ©µð¿À¿¡¼­ Á÷Á¢ ½ÇÇà ÇÒ¶§´Â ÇÁ·ÎÁ§Æ® ÆÄÀÏÀÌ ÀÖ´Â °÷¿¡ ÆùÆ®°¡ ÀÖ¾î¾ß ÇÑ´Ù.
//SDL_CreateRenderer
TTF_Font* font = TTF_OpenFont("godoMaum.ttf", 60);

ÆùÆ® »ç¿ëÀÌ ³¡³ª¸é SDL_DestroyRenderer ÇÔ¼öÀü¿¡ TTF_CloseFont¸¦ ½ÇÇàÇÏ¿© ÆùÆ®¸¦ Á¤¸®ÇÑ´Ù.
TTF_CloseFont(font);
//SDL_DestroyRenderer

¹®ÀÚ¿­ ÅؽºÃÄ »ý¼º

TTF_RenderText_Solid ÇÔ¼ö·Î SDL_Surface ±¸ÇÏ¿© ¹®ÀÚ¿­À» ·»´õ¸µ ÇÑ´Ù.
SDL_Surface* textSurface = TTF_RenderText_Solid( font, szText, foreground );

SDL_CreateTextureFromSurface ÇÔ¼ö·Î SDL_Surface¸¦ SDL_Texture·Î ¸¸µç´Ù.
gTexture = SDL_CreateTextureFromSurface( renderer, textSurface );

SDL_Texture¸¦ »ý¼ºÇÏ¸é  ¼­Çǽº´Â »èÁ¦ÇÑ´Ù.
SDL_FreeSurface( textSurface );

Text_Free ÇÔ¼ö´Â ÅؽºÃĸ¦ Á¦°Å ÇØÁÖ´Â ÇÔ¼ö·Î µÚ¿¡ ¼³¸íÇÑ´Ù.
¹®ÀÚ¿­ÀÌ ¹Ù²î¸é Text_Create¸¦ ´Ù½Ã ½ÇÇà ÇÑ´Ù.
bool Text_Create(char* szText, SDL_Renderer* renderer, TTF_Font* font)
{
    Text_Free();
     
    SDL_Color foreground = { 255, 0, 0 };
    SDL_Surface* textSurface = TTF_RenderText_Solid( font, szText, foreground );
    if( textSurface == NULL )
    {
        printf( "Unable to render text surface! SDL_ttf Error: %s\n", TTF_GetError() );
    }
    else
    {
        gTexture = SDL_CreateTextureFromSurface( renderer, textSurface );
        if( gTexture == NULL )
        {
            printf( "Unable to create texture from rendered text! SDL Error: %s\n", SDL_GetError() );
        }
        else
        {
            gTextWidth = textSurface->w;
            gTextHeight = textSurface->h;
        }
        SDL_FreeSurface( textSurface );
    }
   
    return gTexture != NULL;
}

·»´õ¸µ

·»´õ¸µ ·çÇÁ¿¡¼­ SDL_RenderCopy ÇÔ¼ö·Î ÅؽºÃĸ¦ ±×¸°´Ù.
SDL_QueryTexture ÇÔ¼ö·Î ÅؽºÃÄ Æø°ú ³ôÀ̸¦ ±¸ÇÒ¼ö ÀÖ´Ù.
void Text_Render( SDL_Renderer* renderer )
{
    SDL_Rect dest;
    dest.x = 0;
    dest.y = 0;
    //dest.w = gTextWidth;
    //dest.h = gTextHeight;
    SDL_QueryTexture(gTexture, NULL, NULL, &dest.w, &dest.h);

    SDL_RenderCopy( renderer, gTexture, NULL, &dest);
}

ÅؽºÃÄ Á¦°Å

SDL_DestroyTexture ÇÔ¼ö·Î ÅؽºÃĸ¦ Á¦°ÅÇÑ´Ù.

void Text_Free()
{
    if( gTexture != NULL )
    {
        SDL_DestroyTexture( gTexture );
        gTexture = NULL;
        gTextWidth = 0;
        gTextHeight = 0;
    }
}

¶Ç ´Ù¸¥ ¹æ¹ý

È¿À²Àº ¶³¾îÁöÁö¸¸ ¸Å ÇÁ·¹ÀÓ ÅؽºÃĸ¦ »ý¼º --> ÅؽºÃÄ ·»´õ¸µ --> ÅؽºÃĸ¦ ¼Ò¸êÇÏ´Â ¹æ¹ýÀÌ ÀÖ´Ù.
void DrawText(wchar_t* text, SDL_Renderer* renderer, TTF_Font* font, int x, int y, bool blend)
{
    SDL_Color foreground = { 255, 0, 0 };
    SDL_Surface* textSurface = NULL;
    if(blend)
        textSurface = TTF_RenderUNICODE_Blended( font, (Uint16*)text, foreground);
    else
        textSurface = TTF_RenderUNICODE_Solid( font, (Uint16*)text, foreground);
   
   
    if( textSurface == NULL )
    {
        printf( "Unable to render text surface! SDL_ttf Error: %s\n", TTF_GetError() );
    }
    else
    {
        SDL_Texture* texture = SDL_CreateTextureFromSurface( renderer, textSurface );
        if( texture == NULL )
        {
            printf( "Unable to create texture! SDL Error: %s\n", SDL_GetError() );
        }
        else
        {
            SDL_Rect dest;
            dest.x = x;
            dest.y = y;
            dest.w = textSurface->w;
            dest.h = textSurface->h;
            SDL_RenderCopy(renderer, texture, NULL, &dest);
        }


        SDL_DestroyTexture(texture);
        SDL_FreeSurface( textSurface );
    }

ÇÑ±Û Ãâ·Â ¹× ºí·»µù

¹®ÀÚ¿­À» ¼­Çǽº¿¡ ±×·ÁÁÖ´Â ÇÔ¼ö´Â ¿©·¯°¡Áö°¡ ÀÖÁö¸¸ 4°¡Áö¸¸ ¼³¸í ÇÑ´Ù.

TTF_RenderText_Solid
TTF_RenderText_Blended

TTF_RenderUNICODE_Solid
TTF_RenderUNICODE_Blended

Solid ´ë½Å Blended¸¦ »ç¿ëÇÏ¸é ·»´õ¸µ Ä÷¸®Æ¼°¡ ÁÁ¾ÆÁø´Ù. ºí·»µùÀÌ µÇ±â ¶§¹®¿¡ °è´Ü Çö»óÀÌ ÁÙ¾îµç´Ù.
ÇѱÛÀ» »ç¿ëÇϱâ À§Çؼ­´Â UNICODE°¡ ºÙ¾î ÀÖ´Â ÇÔ¼ö¸¦ »ç¿ëÇÑ´Ù.

À©µµ¿ìÁî¿¡¼­´Â wchar_t ŸÀÔÀ¸·Î ¹®ÀÚ¿­À» ³Ñ°ÜÁÖ¸é ÇѱÛÀ» Ãâ·Â ÇÒ¼ö ÀÖ´Ù.

wchar_t* text = L"¾È³çÇϼ¼¿ä. ÇѱÛ";
textSurface = TTF_RenderUNICODE_Blended( font, (Uint16*)text, foreground);

ÇÁ·ÎÁ§Æ® ´Ù¿î·Îµå : sdl_font_project.zip

ÂüÁ¶)
ÀúÀÛ±Ç °ÆÁ¤¾ø´Â ¹«·á ÇѱÛÆùÆ®
https://brunch.co.kr/@jade/203

Æ©Å丮¾ó
http://gigi.nullneuron.net/gigilabs/displaying-text-in-sdl2-with-sdl_ttf/
https://lazyfoo.net/tutorials/SDL/16_true_type_fonts/index.php

½Ç½Ã°£À¸·Î ¹®ÀÚ¿­ ·»´õ¸µ
https://thenumbat.github.io/cpp-course/sdl2/07/07.html

ÇѱÛÃâ·Â
https://m.blog.naver.com/samgib/50130337930
https://m.blog.naver.com/pjc0247/80149820083

Ãß°¡ ¼³¸í) È­¸é À§Ä¡ º¸°£:

È­¸éÀÇ ÁÂÇ¥¸¦ ´Ù¸¥ ÁÂÇ¥·Î ¹Ù²Ù±â À§ÇØ º¸°£ ÇÔ¼ö¸¦ ¸¸µé¾ú´Ù.

float lerp(float in, float a1, float b1, float a2, float b2)
{
    float v = (in - a1)/(b1 - a1);
    float ret = a2 + v*(b2 - a2);
    return ret;
}

È­¸é  ¾Æ·¡¸¦ 0, À§¸¦ 1000À¸·Î »ç¿ëÇÑ´Ù.
y = lerp(0,  0, 1000, 768.f, 0.f);