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
118 lines
3.1 KiB
#include "coreLabel.h"
|
|
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){
|
|
m_sColorStyle = DEFAULT_COLOR_STYLE;
|
|
m_nMargin = 5;
|
|
m_pFontTexture = NULL;
|
|
m_pFont = NULL;
|
|
m_bTextAlign = ALIGN_CENTER;
|
|
memset( m_pText, 0, SDL_DEFAULT_TEXT_SIZE );
|
|
if( text!= NULL ) sprintf(m_pText, "%s", text);
|
|
if (fontName != NULL){
|
|
m_pFont = TTF_OpenFont(fontName, fsize);
|
|
} else {
|
|
m_pFont = TTF_OpenFont("arial.ttf", fsize);
|
|
};
|
|
m_bUpdateReq = 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::drawBefore(SDL_Renderer *pRenderer){
|
|
if (m_bUpdateReq){
|
|
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;
|
|
if (m_pFontTexture != NULL) SDL_DestroyTexture(m_pFontTexture);
|
|
m_pFontTexture = SDL_CreateTextureFromSurface(pRenderer, pSurface);
|
|
if (m_pFontTexture != NULL) m_bUpdateReq = false;
|
|
SDL_FreeSurface(pSurface);
|
|
};
|
|
};
|
|
if( m_pFontTexture != NULL ){
|
|
SDL_Rect tRect;
|
|
switch (m_bTextAlign){
|
|
case ALIGN_LEFT: tRect.x = m_nMargin; break;
|
|
case ALIGN_CENTER: tRect.x = (m_sGeometry.w - m_nLabelWidth) / 2; break;
|
|
case ALIGN_RIGHT: tRect.x = m_sGeometry.w - m_nLabelWidth - m_nMargin; break;
|
|
};
|
|
tRect.y = (( m_sGeometry.h - m_nLabelHeight ) >> 1) - 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, NULL, 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_bUpdateReq = true;
|
|
};
|
|
|
|
void sdlLabel::setFont(TTF_Font *tPtr){
|
|
if (m_pFont != NULL){
|
|
TTF_CloseFont(m_pFont);
|
|
m_pFont = NULL;
|
|
};
|
|
m_pFont = tPtr;
|
|
m_bUpdateReq = true;
|
|
};
|
|
|
|
void sdlLabel::setColor(SDL_Color *tC){
|
|
if (tC == NULL){
|
|
m_sColor = m_sColorStyle.font;
|
|
} else {
|
|
memcpy(&m_sColor, tC, sizeof(SDL_Color));
|
|
};
|
|
m_bUpdateReq = true;
|
|
};
|
|
|
|
void sdlLabel::setColor(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha){
|
|
if (m_sColor.a != alpha) {
|
|
m_sColor.a = alpha;
|
|
m_bUpdateReq = true;
|
|
};
|
|
if (m_sColor.r != red) {
|
|
m_sColor.r = red;
|
|
m_bUpdateReq = true;
|
|
};
|
|
if (m_sColor.g != green) {
|
|
m_sColor.g = green;
|
|
m_bUpdateReq = true;
|
|
};
|
|
if (m_sColor.b != blue) {
|
|
m_sColor.b = blue;
|
|
m_bUpdateReq = true;
|
|
};
|
|
};
|
|
|
|
void sdlLabel::setBlendMode(SDL_BlendMode blending){
|
|
//Set blending function
|
|
if (m_pFontTexture != NULL){
|
|
SDL_SetTextureBlendMode(m_pFontTexture, blending);
|
|
};
|
|
};
|
|
|
|
void sdlLabel::setAlpha(uint8_t alpha){
|
|
m_sColor.a = alpha;
|
|
m_bUpdateReq = true;
|
|
//Modulate texture alpha
|
|
if (m_pFontTexture != NULL){
|
|
SDL_SetTextureAlphaMod(m_pFontTexture, alpha);
|
|
};
|
|
};
|