You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
2.8 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "coreLabel.h"
  2. sdlLabel::sdlLabel(int x, int y, int w, int h, sdlCoreWidget *wPtr, int fsize, CSTR fontName) :sdlCoreWidget(x,y,w,h,wPtr){
  3. m_sColorStyle = DEFAULT_COLOR_STYLE;
  4. m_pFontTexture = NULL;
  5. m_pFont = NULL;
  6. m_sRect.x = x;
  7. m_sRect.y = y;
  8. m_sRect.w = w;
  9. m_sRect.h = h;
  10. setDecoraion(false);
  11. m_bTextAlign = ALIGN_CENTER;
  12. memset( m_pText, 0, SDL_DEFAULT_TEXT_SIZE );
  13. if (fontName != NULL){
  14. m_pFont = TTF_OpenFont(fontName, fsize);
  15. } else {
  16. m_pFont = TTF_OpenFont("C:\\FruchtSystems\\cortex_test\\cortex_test\\Fonts\\Ubuntu-L.ttf", fsize);
  17. };
  18. m_bRedraw = true;
  19. };
  20. sdlLabel::~sdlLabel(){
  21. if (m_pFont != NULL){
  22. TTF_CloseFont(m_pFont);
  23. m_pFont = NULL;
  24. };
  25. if (m_pFontTexture != NULL){
  26. SDL_DestroyTexture(m_pFontTexture);
  27. m_pFontTexture = NULL;
  28. };
  29. };
  30. void sdlLabel::draw(SDL_Renderer *pRenderer){
  31. if (m_bRedraw){
  32. if (m_pFont == NULL) return;
  33. SDL_Surface *pSurface;
  34. pSurface = TTF_RenderText_Solid(m_pFont, m_pText, m_sColor);
  35. if (pSurface != NULL){
  36. m_nLabelWidth = pSurface->w;
  37. m_nLabelHeight = pSurface->h;
  38. m_pFontTexture = SDL_CreateTextureFromSurface(pRenderer, pSurface);
  39. if (m_pFontTexture != NULL) m_bRedraw = false;
  40. };
  41. };
  42. if( m_pFontTexture != NULL ){
  43. SDL_Rect tRect;
  44. switch (m_bTextAlign){
  45. case ALIGN_LEFT: tRect.x = 0; break;
  46. case ALIGN_CENTER: tRect.x = (m_sRect.w - m_nLabelWidth) / 2; break;
  47. case ALIGN_RIGHT: tRect.x = m_sRect.w - m_nLabelWidth; break;
  48. };
  49. tRect.y = m_sRect.y + (m_sRect.h - m_nLabelHeight) / 2;
  50. //tRect.y = (m_sRect.h - m_pSurface->h) / 2;
  51. tRect.w = m_nLabelWidth;
  52. tRect.h = m_nLabelHeight;
  53. //SDL_RenderCopyEx(pRenderer, m_pTexture, clip, &m_sRect, angle, center, flip);
  54. SDL_RenderCopyEx(pRenderer, m_pFontTexture, NULL, &tRect, 0, 0, SDL_FLIP_NONE);
  55. };
  56. };
  57. void sdlLabel::setText(CSTR pStr){
  58. if (pStr != NULL){
  59. sprintf(m_pText, "%s", pStr);
  60. } else {
  61. memset(m_pText, 0, SDL_DEFAULT_TEXT_SIZE );
  62. };
  63. m_bRedraw = true;
  64. };
  65. void sdlLabel::setFont(TTF_Font *tPtr){
  66. if (m_pFont != NULL){
  67. TTF_CloseFont(m_pFont);
  68. m_pFont = NULL;
  69. };
  70. m_pFont = tPtr;
  71. m_bRedraw = true;
  72. };
  73. void sdlLabel::setColor(SDL_Color *tC){
  74. if (tC == NULL){
  75. m_sColor = m_sColorStyle.font;
  76. } else {
  77. memcpy(&m_sColor, tC, sizeof(SDL_Color));
  78. };
  79. m_bRedraw = true;
  80. };
  81. void sdlLabel::setColor(Uint8 red, Uint8 green, Uint8 blue){
  82. //Modulate texture rgb
  83. if (m_pFontTexture != NULL){
  84. SDL_SetTextureColorMod(m_pFontTexture, red, green, blue);
  85. };
  86. };
  87. void sdlLabel::setBlendMode(SDL_BlendMode blending){
  88. //Set blending function
  89. if (m_pFontTexture != NULL){
  90. SDL_SetTextureBlendMode(m_pFontTexture, blending);
  91. };
  92. };
  93. void sdlLabel::setAlpha(Uint8 alpha){
  94. //Modulate texture alpha
  95. if (m_pFontTexture != NULL){
  96. SDL_SetTextureAlphaMod(m_pFontTexture, alpha);
  97. };
  98. };