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.

118 lines
3.1 KiB

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