Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //-------------------------------------------------------------------------------------------------
- void Visuals::DrawTextOntoScreenBuffer(const char *textToDisplay, TTF_Font *font, int posX, int posY
- , Uint8 XJustification, Uint8 textRed, Uint8 textGreen, Uint8 textBlue
- , Uint8 outlineRed, Uint8 outlineGreen, Uint8 outlineBlue)
- {
- SDL_Color textColor = { textRed, textGreen, textBlue, 255 };
- SDL_Color outlineColor = { outlineRed, outlineGreen, outlineBlue, 255 };
- SDL_Surface *text = NULL;
- SDL_Surface *textOutline = NULL;
- SDL_Texture *textTexture = NULL;
- SDL_Texture *textOutlineTexture = NULL;
- SDL_Rect destinationRect;
- SDL_Rect destinationOutlineRect;
- int windowWidth;
- int windowHeight;
- int textCacheCheckIndex = NumberOfTextsCached;
- bool sentenceMatches = true;
- bool sentenceIsInCache = false;
- for (textCacheCheckIndex = 0; textCacheCheckIndex < NumberOfTextsCached; textCacheCheckIndex++)
- {
- sentenceMatches = true;
- if ( strlen(TextCachedText[textCacheCheckIndex]) == strlen(textToDisplay) )
- {
- for (Uint16 index = 0; index < strlen(textToDisplay); index++)
- {
- if (TextCachedText[textCacheCheckIndex][index] != textToDisplay[index]) sentenceMatches = false;
- }
- }
- else sentenceMatches = false;
- if (sentenceMatches == true)
- {
- if (TextCachedScreenX[textCacheCheckIndex] == posX && TextCachedScreenY[textCacheCheckIndex] == posY)
- {
- sentenceIsInCache = true;
- break;
- }
- }
- }
- if (sentenceIsInCache == false)
- {
- if (TextTexture[TextCacheCurrentIndex] != NULL) SDL_DestroyTexture(TextTexture[TextCacheCurrentIndex]);
- if (TextOutlineTexture[TextCacheCurrentIndex] != NULL) SDL_DestroyTexture(TextOutlineTexture[TextCacheCurrentIndex]);
- TextTexture[TextCacheCurrentIndex] = NULL;
- TextOutlineTexture[TextCacheCurrentIndex] = NULL;
- TextCachedText[TextCacheCurrentIndex][0] = '\0';
- TextCachedScreenX[TextCacheCurrentIndex] = 320;
- TextCachedScreenY[TextCacheCurrentIndex] = 240;
- TextCachedWidth[TextCacheCurrentIndex] = 0;
- TextCachedHeight[TextCacheCurrentIndex] = 0;
- text = TTF_RenderText_Blended(font, textToDisplay, textColor);
- textOutline = TTF_RenderText_Solid(font, textToDisplay, outlineColor);
- strcpy(TextCachedText[TextCacheCurrentIndex], textToDisplay);
- TextCachedScreenX[TextCacheCurrentIndex] = posX;
- TextCachedScreenY[TextCacheCurrentIndex] = posY;
- TextCachedWidth[TextCacheCurrentIndex] = text->w;
- TextCachedHeight[TextCacheCurrentIndex] = text->h;
- textTexture = SDL_CreateTextureFromSurface(Renderer, text);
- TextTexture[TextCacheCurrentIndex] = SDL_CreateTextureFromSurface(Renderer, text);
- textOutlineTexture = SDL_CreateTextureFromSurface(Renderer, textOutline);
- TextOutlineTexture[TextCacheCurrentIndex] = SDL_CreateTextureFromSurface(Renderer, textOutline);
- if (XJustification == JustifyLeft)
- {
- posX = posX + (text->w / 2);
- }
- else if (XJustification == JustifyCenter)
- {
- posX = (640 / 2);
- }
- else if (XJustification == JustifyRight)
- {
- posX = (640 - posX) - (text->w / 2);
- }
- else if (XJustification == JustifyCenterOnPoint)
- {
- posX = posX;
- }
- if ( TextCacheCurrentIndex < (NumberOfTextsCached-1) ) TextCacheCurrentIndex++;
- else TextCacheCurrentIndex = 0;
- }
- else
- {
- if (XJustification == JustifyLeft)
- {
- posX = posX + (TextCachedWidth[textCacheCheckIndex] / 2);
- }
- else if (XJustification == JustifyCenter)
- {
- posX = (640 / 2);
- }
- else if (XJustification == JustifyRight)
- {
- posX = (640 - posX) - (TextCachedWidth[textCacheCheckIndex] / 2);
- }
- else if (XJustification == JustifyCenterOnPoint)
- {
- posX = posX;
- }
- }
- SDL_GetWindowSize(Window, &windowWidth, &windowHeight);
- float winWidthFixed;
- float winHeightFixed;
- if (ForceAspectRatio == false)
- {
- winWidthFixed = (float)windowWidth / 640;
- winHeightFixed = (float)windowHeight / 480;
- }
- else
- {
- winWidthFixed = 1;
- winHeightFixed = 1;
- }
- if (sentenceIsInCache == false)
- {
- destinationRect.x = (posX * winWidthFixed) - ( (text->w * winWidthFixed) / 2 );
- destinationRect.y = (posY * winHeightFixed) - (winHeightFixed / 2) + 3;
- destinationRect.w = text->w * (winWidthFixed);
- destinationRect.h = text->h * (winHeightFixed);
- }
- else
- {
- destinationRect.x = (posX * winWidthFixed) - ( (TextCachedWidth[textCacheCheckIndex] * winWidthFixed) / 2 );
- destinationRect.y = (posY * winHeightFixed) - (winHeightFixed / 2) + 3;
- destinationRect.w = TextCachedWidth[textCacheCheckIndex] * (winWidthFixed);
- destinationRect.h = TextCachedHeight[textCacheCheckIndex] * (winHeightFixed);
- }
- destinationOutlineRect.x = destinationRect.x;
- destinationOutlineRect.y = destinationRect.y;
- destinationOutlineRect.w = destinationRect.w;
- destinationOutlineRect.h = destinationRect.h;
- for (Sint16 y = -3; y < 4; y+=1)
- {
- for (Sint16 x = -3; x < 4; x+=1)
- {
- destinationOutlineRect.x = destinationRect.x + x;
- destinationOutlineRect.y = destinationRect.y + y;
- if (sentenceIsInCache == false)
- SDL_RenderCopyEx(Renderer, textOutlineTexture, NULL, &destinationOutlineRect, 0, NULL, SDL_FLIP_NONE);
- else
- {
- SDL_RenderCopyEx(Renderer, TextOutlineTexture[textCacheCheckIndex], NULL, &destinationOutlineRect, 0, NULL, SDL_FLIP_NONE);
- }
- }
- }
- if (sentenceIsInCache == false)
- SDL_RenderCopyEx(Renderer, textTexture, NULL, &destinationRect, 0, NULL, SDL_FLIP_NONE);
- else
- SDL_RenderCopyEx(Renderer, TextTexture[textCacheCheckIndex], NULL, &destinationRect, 0, NULL, SDL_FLIP_NONE);
- SDL_DestroyTexture(textOutlineTexture);
- SDL_DestroyTexture(textTexture);
- SDL_FreeSurface(text);
- SDL_FreeSurface(textOutline);
- }
- //-------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement