Advertisement
Combreal

TextSprite.cpp

Apr 18th, 2019
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.83 KB | None | 0 0
  1. #include "TextSprite.h"
  2. using namespace std;
  3.  
  4. CTextSprite::CTextSprite(CSDL_Setup* passed_SDL_Setup, SDL_Renderer* passed_renderer, std::string passed_FilePath, std::string passed_Sentence, int passed_PoliceSize, Uint8 passed_r, Uint8 passed_g, Uint8 passed_b, Uint8 passed_a, int x, int y, int w, int h)
  5. {
  6.     csdl_setup = passed_SDL_Setup;
  7.     renderer = passed_renderer;
  8.     policeSize = passed_PoliceSize;
  9.     filePath = passed_FilePath;
  10.     sentence = passed_Sentence;
  11.     Message = NULL;
  12.     renderText = false;
  13.     X_pos = x;
  14.     Y_pos = y;
  15.     r = passed_r;
  16.     g = passed_g;
  17.     b = passed_b;
  18.     a = passed_a;
  19.     TTF_Init();
  20.     if(TTF_Init() == -1)
  21.     {
  22.         cout<<"Coudn't initialise TTF_Init : "<<TTF_GetError()<<endl;
  23.         exit(EXIT_FAILURE);
  24.     }
  25.     textColor.r=r;
  26.     textColor.g=g;
  27.     textColor.b=b;
  28.     textColor.a=a;
  29.     cout<<r<<g<<b<<a<<endl;
  30.     font = TTF_OpenFont(filePath.c_str(), policeSize);
  31.     if(!font)
  32.     {
  33.         cout<<"TTF_OpenFont : "<<TTF_GetError()<<endl;
  34.     }
  35.     text = TTF_RenderText_Solid(font, sentence.c_str(), textColor);
  36.     position.x=X_pos;
  37.     position.y=Y_pos;
  38.     position.w=text->w;
  39.     position.h=text->h;
  40.     Message = SDL_CreateTextureFromSurface(renderer, text);
  41.     if (Message == NULL)
  42.     {
  43.         cout<<"Couldn't create "<<filePath.c_str()<<endl;
  44.     }
  45. }
  46.  
  47.  
  48. CTextSprite::~CTextSprite(void)
  49. {
  50.     SDL_DestroyRenderer(renderer);
  51.     SDL_DestroyTexture(Message);
  52.     TTF_CloseFont(font);
  53.     Message = NULL;
  54.     font = NULL;
  55.     renderer = NULL;
  56.     TTF_Quit();
  57. }
  58.  
  59. void CTextSprite::DrawText()
  60. {
  61.     SDL_RenderCopy(renderer, Message, NULL, &position);
  62. }
  63.  
  64. bool CTextSprite::loadFromRenderedText(std::string textureText)
  65. {
  66.     if(Message!=NULL)
  67.     {
  68.         SDL_DestroyTexture(Message);
  69.         Message = NULL;
  70.         TTF_CloseFont(font);
  71.         font = NULL;
  72.     }
  73.     font = TTF_OpenFont(filePath.c_str(), policeSize);//test2.c_str()
  74.     if(!font)
  75.     {  
  76.         //cout<<"Can't create font : "<<TTF_GetError()<<endl;
  77.         return 0;
  78.     }
  79.     if(textureText.size()!=0 && font)
  80.     {
  81.         textSurface = TTF_RenderText_Solid(font, textureText.c_str(), textColor);
  82.         if(textSurface != NULL)
  83.         {
  84.             Message = SDL_CreateTextureFromSurface(renderer, textSurface);
  85.             if(Message==NULL)
  86.             {
  87.                 cout<<"Unable to create texture from rendered text! SDL Error: "<<SDL_GetError()<<endl;
  88.             }
  89.             else
  90.             {
  91.                 position.x=X_pos;
  92.                 position.y=Y_pos;
  93.                 position.w=textSurface->w;
  94.                 position.h=textSurface->h;
  95.             }
  96.             SDL_FreeSurface(textSurface);
  97.         }
  98.         else
  99.         {
  100.             cout<<"Unable to render text surface! SDL_ttf Error: "<<TTF_GetError()<<endl;
  101.         }
  102.     }
  103.     return Message != NULL;
  104. }
  105.  
  106. bool CTextSprite::SetFontSize(int fontSize)
  107. {
  108.     policeSize = fontSize;
  109.     if(Message!=NULL)
  110.     {
  111.         SDL_DestroyTexture(Message);
  112.         Message = NULL;
  113.         TTF_CloseFont(font);
  114.     }
  115.     font = TTF_OpenFont(filePath.c_str(), policeSize);
  116.     textSurface = TTF_RenderText_Solid(font, sentence.c_str(), textColor);
  117.     if(textSurface != NULL)
  118.     {
  119.         Message = SDL_CreateTextureFromSurface(renderer, textSurface);
  120.         if(Message==NULL)
  121.         {
  122.             cout<<"Unable to create texture from rendered text size! SDL Error: "<<SDL_GetError()<<endl;
  123.         }
  124.         else
  125.         {
  126.             position.x=X_pos;
  127.             position.y=Y_pos;
  128.             position.w=textSurface->w;
  129.             position.h=textSurface->h;
  130.         }
  131.         SDL_FreeSurface(textSurface);
  132.     }
  133.     else
  134.     {
  135.         cout<<"Unable to render text surface! SDL_ttf Error: "<<TTF_GetError()<<endl;
  136.     }
  137.     return Message != NULL;
  138. }
  139.  
  140. bool CTextSprite::SetFontColor(Uint8 passed_r, Uint8 passed_g, Uint8 passed_b, Uint8 passed_a)
  141. {
  142.     textColor.r=r;
  143.     textColor.g=g;
  144.     textColor.b=b;
  145.     textColor.a=a;
  146.     if(Message!=NULL)
  147.     {
  148.         SDL_DestroyTexture(Message);
  149.         Message = NULL;
  150.         TTF_CloseFont(font);
  151.     }
  152.     font = TTF_OpenFont(filePath.c_str(), policeSize);
  153.     textSurface = TTF_RenderText_Solid(font, sentence.c_str(), textColor);
  154.     if(textSurface != NULL)
  155.     {
  156.         Message = SDL_CreateTextureFromSurface(renderer, textSurface);
  157.         if(Message==NULL)
  158.         {
  159.             cout<<"Unable to create texture from rendered text color! SDL Error: "<<SDL_GetError()<<endl;
  160.         }
  161.         else
  162.         {
  163.             position.x=X_pos;
  164.             position.y=Y_pos;
  165.             position.w=textSurface->w;
  166.             position.h=textSurface->h;
  167.         }
  168.         SDL_FreeSurface(textSurface);
  169.     }
  170.     else
  171.     {
  172.         cout<<"Unable to render text surface! SDL_ttf Error: "<<TTF_GetError()<<endl;
  173.     }
  174.     return Message != NULL;
  175. }
  176.  
  177. int CTextSprite::GetFontSize()
  178. {
  179.     return policeSize;
  180. }
  181.  
  182. void CTextSprite::SetX(int X)
  183. {
  184.     X_pos = X;
  185.     position.x = int(X_pos);
  186. }
  187.  
  188. void CTextSprite::SetY(int Y)
  189. {
  190.     Y_pos = Y;
  191.     position.y = int(Y_pos);
  192. }
  193.  
  194.  
  195. void CTextSprite::SetPosition(int X, int Y)
  196. {
  197.     X_pos = X;
  198.     position.x = int(X_pos);
  199.     Y_pos = Y;
  200.     position.y = int(Y_pos);
  201. }
  202.  
  203. int CTextSprite::GetX()
  204. {
  205.     return X_pos;
  206. }
  207.  
  208. int CTextSprite::GetY()
  209. {
  210.     return Y_pos;
  211. }
  212.  
  213. void CTextSprite::SetWidth(int W)
  214. {
  215.     position.w = W;
  216. }
  217.  
  218. void CTextSprite::SetHeight(int H)
  219. {
  220.     position.h = H;
  221. }
  222.  
  223. int CTextSprite::GetWidth()
  224. {
  225.     return position.w;
  226. }
  227.  
  228. int CTextSprite::GetHeight()
  229. {
  230.     return position.h;
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement