Advertisement
Guest User

GT-R SDL2 Engine - TTF Font Renderer Code

a guest
May 26th, 2020
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.54 KB | None | 0 0
  1. //-------------------------------------------------------------------------------------------------
  2. void Visuals::DrawTextOntoScreenBuffer(const char *textToDisplay, TTF_Font *font, int posX, int posY
  3.                                        , Uint8 XJustification, Uint8 textRed, Uint8 textGreen, Uint8 textBlue
  4.                                        , Uint8 outlineRed, Uint8 outlineGreen, Uint8 outlineBlue)
  5. {
  6. SDL_Color textColor = { textRed, textGreen, textBlue, 255 };
  7. SDL_Color outlineColor = { outlineRed, outlineGreen, outlineBlue, 255 };
  8. SDL_Surface *text = NULL;
  9. SDL_Surface *textOutline = NULL;
  10. SDL_Texture *textTexture = NULL;
  11. SDL_Texture *textOutlineTexture = NULL;
  12. SDL_Rect destinationRect;
  13. SDL_Rect destinationOutlineRect;
  14. int windowWidth;
  15. int windowHeight;
  16. int textCacheCheckIndex = NumberOfTextsCached;
  17. bool sentenceMatches = true;
  18. bool sentenceIsInCache = false;
  19.  
  20.     for (textCacheCheckIndex = 0; textCacheCheckIndex < NumberOfTextsCached; textCacheCheckIndex++)
  21.     {
  22.         sentenceMatches = true;
  23.         if ( strlen(TextCachedText[textCacheCheckIndex]) == strlen(textToDisplay) )
  24.         {
  25.             for (Uint16 index = 0; index < strlen(textToDisplay); index++)
  26.             {
  27.                 if (TextCachedText[textCacheCheckIndex][index] != textToDisplay[index])  sentenceMatches = false;
  28.             }
  29.         }
  30.         else  sentenceMatches = false;
  31.  
  32.         if (sentenceMatches == true)
  33.         {
  34.             if (TextCachedScreenX[textCacheCheckIndex] == posX && TextCachedScreenY[textCacheCheckIndex] == posY)
  35.             {
  36.                 sentenceIsInCache = true;
  37.                 break;
  38.             }
  39.         }
  40.     }
  41.  
  42.     if (sentenceIsInCache == false)
  43.     {
  44.         if (TextTexture[TextCacheCurrentIndex] != NULL)  SDL_DestroyTexture(TextTexture[TextCacheCurrentIndex]);
  45.         if (TextOutlineTexture[TextCacheCurrentIndex] != NULL)  SDL_DestroyTexture(TextOutlineTexture[TextCacheCurrentIndex]);
  46.         TextTexture[TextCacheCurrentIndex] = NULL;
  47.         TextOutlineTexture[TextCacheCurrentIndex] = NULL;
  48.         TextCachedText[TextCacheCurrentIndex][0] = '\0';
  49.         TextCachedScreenX[TextCacheCurrentIndex] = 320;
  50.         TextCachedScreenY[TextCacheCurrentIndex] = 240;
  51.         TextCachedWidth[TextCacheCurrentIndex] = 0;
  52.         TextCachedHeight[TextCacheCurrentIndex] = 0;
  53.  
  54.         text = TTF_RenderText_Blended(font, textToDisplay, textColor);
  55.         textOutline = TTF_RenderText_Solid(font, textToDisplay, outlineColor);
  56.  
  57.         strcpy(TextCachedText[TextCacheCurrentIndex], textToDisplay);
  58.  
  59.         TextCachedScreenX[TextCacheCurrentIndex] = posX;
  60.         TextCachedScreenY[TextCacheCurrentIndex] = posY;
  61.  
  62.         TextCachedWidth[TextCacheCurrentIndex] = text->w;
  63.         TextCachedHeight[TextCacheCurrentIndex] = text->h;
  64.  
  65.         textTexture = SDL_CreateTextureFromSurface(Renderer, text);
  66.         TextTexture[TextCacheCurrentIndex] = SDL_CreateTextureFromSurface(Renderer, text);
  67.  
  68.         textOutlineTexture = SDL_CreateTextureFromSurface(Renderer, textOutline);
  69.         TextOutlineTexture[TextCacheCurrentIndex] = SDL_CreateTextureFromSurface(Renderer, textOutline);
  70.  
  71.         if (XJustification == JustifyLeft)
  72.         {
  73.             posX = posX + (text->w / 2);
  74.         }
  75.         else if (XJustification == JustifyCenter)
  76.         {
  77.             posX = (640 / 2);
  78.         }
  79.         else if (XJustification == JustifyRight)
  80.         {
  81.             posX = (640 - posX) - (text->w / 2);
  82.         }
  83.         else if (XJustification == JustifyCenterOnPoint)
  84.         {
  85.             posX = posX;
  86.         }
  87.  
  88.         if ( TextCacheCurrentIndex < (NumberOfTextsCached-1) )  TextCacheCurrentIndex++;
  89.         else  TextCacheCurrentIndex = 0;
  90.     }
  91.     else
  92.     {
  93.         if (XJustification == JustifyLeft)
  94.         {
  95.             posX = posX + (TextCachedWidth[textCacheCheckIndex] / 2);
  96.         }
  97.         else if (XJustification == JustifyCenter)
  98.         {
  99.             posX = (640 / 2);
  100.         }
  101.         else if (XJustification == JustifyRight)
  102.         {
  103.             posX = (640 - posX) - (TextCachedWidth[textCacheCheckIndex] / 2);
  104.         }
  105.         else if (XJustification == JustifyCenterOnPoint)
  106.         {
  107.             posX = posX;
  108.         }
  109.     }
  110.  
  111.     SDL_GetWindowSize(Window, &windowWidth, &windowHeight);
  112.  
  113.     float winWidthFixed;
  114.     float winHeightFixed;
  115.     if (ForceAspectRatio == false)
  116.     {
  117.         winWidthFixed = (float)windowWidth / 640;
  118.         winHeightFixed = (float)windowHeight / 480;
  119.     }
  120.     else
  121.     {
  122.         winWidthFixed = 1;
  123.         winHeightFixed = 1;
  124.     }
  125.  
  126.     if (sentenceIsInCache == false)
  127.     {
  128.         destinationRect.x = (posX * winWidthFixed) - ( (text->w * winWidthFixed) / 2 );
  129.         destinationRect.y = (posY * winHeightFixed) - (winHeightFixed / 2) + 3;
  130.         destinationRect.w = text->w * (winWidthFixed);
  131.         destinationRect.h = text->h * (winHeightFixed);
  132.     }
  133.     else
  134.     {
  135.         destinationRect.x = (posX * winWidthFixed) - ( (TextCachedWidth[textCacheCheckIndex] * winWidthFixed) / 2 );
  136.         destinationRect.y = (posY * winHeightFixed) - (winHeightFixed / 2) + 3;
  137.         destinationRect.w = TextCachedWidth[textCacheCheckIndex] * (winWidthFixed);
  138.         destinationRect.h = TextCachedHeight[textCacheCheckIndex] * (winHeightFixed);
  139.     }
  140.  
  141.     destinationOutlineRect.x = destinationRect.x;
  142.     destinationOutlineRect.y = destinationRect.y;
  143.     destinationOutlineRect.w = destinationRect.w;
  144.     destinationOutlineRect.h = destinationRect.h;
  145.  
  146.     for (Sint16 y = -3; y < 4; y+=1)
  147.     {
  148.         for (Sint16 x = -3; x < 4; x+=1)
  149.         {
  150.             destinationOutlineRect.x = destinationRect.x + x;
  151.             destinationOutlineRect.y = destinationRect.y + y;
  152.  
  153.             if (sentenceIsInCache == false)
  154.                 SDL_RenderCopyEx(Renderer, textOutlineTexture, NULL, &destinationOutlineRect, 0, NULL, SDL_FLIP_NONE);
  155.             else
  156.             {
  157.                 SDL_RenderCopyEx(Renderer, TextOutlineTexture[textCacheCheckIndex], NULL, &destinationOutlineRect, 0, NULL, SDL_FLIP_NONE);
  158.             }
  159.         }
  160.     }
  161.  
  162.     if (sentenceIsInCache == false)
  163.         SDL_RenderCopyEx(Renderer, textTexture, NULL, &destinationRect, 0, NULL, SDL_FLIP_NONE);
  164.     else
  165.         SDL_RenderCopyEx(Renderer, TextTexture[textCacheCheckIndex], NULL, &destinationRect, 0, NULL, SDL_FLIP_NONE);
  166.  
  167.     SDL_DestroyTexture(textOutlineTexture);
  168.     SDL_DestroyTexture(textTexture);
  169.     SDL_FreeSurface(text);
  170.     SDL_FreeSurface(textOutline);
  171. }
  172.  
  173. //-------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement