Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "TextSprite.h"
- using namespace std;
- 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)
- {
- csdl_setup = passed_SDL_Setup;
- renderer = passed_renderer;
- policeSize = passed_PoliceSize;
- filePath = passed_FilePath;
- sentence = passed_Sentence;
- Message = NULL;
- renderText = false;
- X_pos = x;
- Y_pos = y;
- r = passed_r;
- g = passed_g;
- b = passed_b;
- a = passed_a;
- TTF_Init();
- if(TTF_Init() == -1)
- {
- cout<<"Coudn't initialise TTF_Init : "<<TTF_GetError()<<endl;
- exit(EXIT_FAILURE);
- }
- textColor.r=r;
- textColor.g=g;
- textColor.b=b;
- textColor.a=a;
- cout<<r<<g<<b<<a<<endl;
- font = TTF_OpenFont(filePath.c_str(), policeSize);
- if(!font)
- {
- cout<<"TTF_OpenFont : "<<TTF_GetError()<<endl;
- }
- text = TTF_RenderText_Solid(font, sentence.c_str(), textColor);
- position.x=X_pos;
- position.y=Y_pos;
- position.w=text->w;
- position.h=text->h;
- Message = SDL_CreateTextureFromSurface(renderer, text);
- if (Message == NULL)
- {
- cout<<"Couldn't create "<<filePath.c_str()<<endl;
- }
- }
- CTextSprite::~CTextSprite(void)
- {
- SDL_DestroyRenderer(renderer);
- SDL_DestroyTexture(Message);
- TTF_CloseFont(font);
- Message = NULL;
- font = NULL;
- renderer = NULL;
- TTF_Quit();
- }
- void CTextSprite::DrawText()
- {
- SDL_RenderCopy(renderer, Message, NULL, &position);
- }
- bool CTextSprite::loadFromRenderedText(std::string textureText)
- {
- if(Message!=NULL)
- {
- SDL_DestroyTexture(Message);
- Message = NULL;
- TTF_CloseFont(font);
- font = NULL;
- }
- font = TTF_OpenFont(filePath.c_str(), policeSize);//test2.c_str()
- if(!font)
- {
- //cout<<"Can't create font : "<<TTF_GetError()<<endl;
- return 0;
- }
- if(textureText.size()!=0 && font)
- {
- textSurface = TTF_RenderText_Solid(font, textureText.c_str(), textColor);
- if(textSurface != NULL)
- {
- Message = SDL_CreateTextureFromSurface(renderer, textSurface);
- if(Message==NULL)
- {
- cout<<"Unable to create texture from rendered text! SDL Error: "<<SDL_GetError()<<endl;
- }
- else
- {
- position.x=X_pos;
- position.y=Y_pos;
- position.w=textSurface->w;
- position.h=textSurface->h;
- }
- SDL_FreeSurface(textSurface);
- }
- else
- {
- cout<<"Unable to render text surface! SDL_ttf Error: "<<TTF_GetError()<<endl;
- }
- }
- return Message != NULL;
- }
- bool CTextSprite::SetFontSize(int fontSize)
- {
- policeSize = fontSize;
- if(Message!=NULL)
- {
- SDL_DestroyTexture(Message);
- Message = NULL;
- TTF_CloseFont(font);
- }
- font = TTF_OpenFont(filePath.c_str(), policeSize);
- textSurface = TTF_RenderText_Solid(font, sentence.c_str(), textColor);
- if(textSurface != NULL)
- {
- Message = SDL_CreateTextureFromSurface(renderer, textSurface);
- if(Message==NULL)
- {
- cout<<"Unable to create texture from rendered text size! SDL Error: "<<SDL_GetError()<<endl;
- }
- else
- {
- position.x=X_pos;
- position.y=Y_pos;
- position.w=textSurface->w;
- position.h=textSurface->h;
- }
- SDL_FreeSurface(textSurface);
- }
- else
- {
- cout<<"Unable to render text surface! SDL_ttf Error: "<<TTF_GetError()<<endl;
- }
- return Message != NULL;
- }
- bool CTextSprite::SetFontColor(Uint8 passed_r, Uint8 passed_g, Uint8 passed_b, Uint8 passed_a)
- {
- textColor.r=r;
- textColor.g=g;
- textColor.b=b;
- textColor.a=a;
- if(Message!=NULL)
- {
- SDL_DestroyTexture(Message);
- Message = NULL;
- TTF_CloseFont(font);
- }
- font = TTF_OpenFont(filePath.c_str(), policeSize);
- textSurface = TTF_RenderText_Solid(font, sentence.c_str(), textColor);
- if(textSurface != NULL)
- {
- Message = SDL_CreateTextureFromSurface(renderer, textSurface);
- if(Message==NULL)
- {
- cout<<"Unable to create texture from rendered text color! SDL Error: "<<SDL_GetError()<<endl;
- }
- else
- {
- position.x=X_pos;
- position.y=Y_pos;
- position.w=textSurface->w;
- position.h=textSurface->h;
- }
- SDL_FreeSurface(textSurface);
- }
- else
- {
- cout<<"Unable to render text surface! SDL_ttf Error: "<<TTF_GetError()<<endl;
- }
- return Message != NULL;
- }
- int CTextSprite::GetFontSize()
- {
- return policeSize;
- }
- void CTextSprite::SetX(int X)
- {
- X_pos = X;
- position.x = int(X_pos);
- }
- void CTextSprite::SetY(int Y)
- {
- Y_pos = Y;
- position.y = int(Y_pos);
- }
- void CTextSprite::SetPosition(int X, int Y)
- {
- X_pos = X;
- position.x = int(X_pos);
- Y_pos = Y;
- position.y = int(Y_pos);
- }
- int CTextSprite::GetX()
- {
- return X_pos;
- }
- int CTextSprite::GetY()
- {
- return Y_pos;
- }
- void CTextSprite::SetWidth(int W)
- {
- position.w = W;
- }
- void CTextSprite::SetHeight(int H)
- {
- position.h = H;
- }
- int CTextSprite::GetWidth()
- {
- return position.w;
- }
- int CTextSprite::GetHeight()
- {
- return position.h;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement