#include "coreLabel.h" sdlLabel::sdlLabel(int x, int y, int w, int h, sdlCoreWidget *wPtr, int fsize, CSTR fontName) :sdlCoreWidget(x,y,w,h,wPtr){ m_sColorStyle = DEFAULT_COLOR_STYLE; m_pFontTexture = NULL; m_pFont = NULL; m_sRect.x = x; m_sRect.y = y; m_sRect.w = w; m_sRect.h = h; setDecoraion(false); m_bTextAlign = ALIGN_CENTER; memset( m_pText, 0, SDL_DEFAULT_TEXT_SIZE ); if (fontName != NULL){ m_pFont = TTF_OpenFont(fontName, fsize); } else { m_pFont = TTF_OpenFont("C:\\FruchtSystems\\cortex_test\\cortex_test\\Fonts\\Ubuntu-L.ttf", fsize); }; m_bRedraw = true; }; sdlLabel::~sdlLabel(){ if (m_pFont != NULL){ TTF_CloseFont(m_pFont); m_pFont = NULL; }; if (m_pFontTexture != NULL){ SDL_DestroyTexture(m_pFontTexture); m_pFontTexture = NULL; }; }; void sdlLabel::draw(SDL_Renderer *pRenderer){ if (m_bRedraw){ if (m_pFont == NULL) return; SDL_Surface *pSurface; pSurface = TTF_RenderText_Solid(m_pFont, m_pText, m_sColor); if (pSurface != NULL){ m_nLabelWidth = pSurface->w; m_nLabelHeight = pSurface->h; m_pFontTexture = SDL_CreateTextureFromSurface(pRenderer, pSurface); if (m_pFontTexture != NULL) m_bRedraw = false; }; }; if( m_pFontTexture != NULL ){ SDL_Rect tRect; switch (m_bTextAlign){ case ALIGN_LEFT: tRect.x = 0; break; case ALIGN_CENTER: tRect.x = (m_sRect.w - m_nLabelWidth) / 2; break; case ALIGN_RIGHT: tRect.x = m_sRect.w - m_nLabelWidth; break; }; tRect.y = m_sRect.y + (m_sRect.h - m_nLabelHeight) / 2; //tRect.y = (m_sRect.h - m_pSurface->h) / 2; tRect.w = m_nLabelWidth; tRect.h = m_nLabelHeight; //SDL_RenderCopyEx(pRenderer, m_pTexture, clip, &m_sRect, angle, center, flip); SDL_RenderCopyEx(pRenderer, m_pFontTexture, NULL, &tRect, 0, 0, SDL_FLIP_NONE); }; }; void sdlLabel::setText(CSTR pStr){ if (pStr != NULL){ sprintf(m_pText, "%s", pStr); } else { memset(m_pText, 0, SDL_DEFAULT_TEXT_SIZE ); }; m_bRedraw = true; }; void sdlLabel::setFont(TTF_Font *tPtr){ if (m_pFont != NULL){ TTF_CloseFont(m_pFont); m_pFont = NULL; }; m_pFont = tPtr; m_bRedraw = true; }; void sdlLabel::setColor(SDL_Color *tC){ if (tC == NULL){ m_sColor = m_sColorStyle.font; } else { memcpy(&m_sColor, tC, sizeof(SDL_Color)); }; m_bRedraw = true; }; void sdlLabel::setColor(Uint8 red, Uint8 green, Uint8 blue){ //Modulate texture rgb if (m_pFontTexture != NULL){ SDL_SetTextureColorMod(m_pFontTexture, red, green, blue); }; }; void sdlLabel::setBlendMode(SDL_BlendMode blending){ //Set blending function if (m_pFontTexture != NULL){ SDL_SetTextureBlendMode(m_pFontTexture, blending); }; }; void sdlLabel::setAlpha(Uint8 alpha){ //Modulate texture alpha if (m_pFontTexture != NULL){ SDL_SetTextureAlphaMod(m_pFontTexture, alpha); }; };