Monday, 14 September 2015

Rendering text in SDL2

http://stackoverflow.com/questions/22886500/rendering-text-sdl2

Example 1:

//this opens a font style and sets a size
TTF_Font* Sans = TTF_OpenFont("Sans.ttf", 24); 

//this is the color in rgb format, maxing out all would give 
//you the color white, and it will be your text's color
SDL_Color White = {255, 255, 255};  

//as TTF_RenderText_Solid could only be used on SDL_Surface
//then you have to create the surface first
SDL_Surface* surfaceMessage = TTF_RenderText_Solid(Sans, "put your text here", White); 

//now you can convert it into a texture
SDL_Texture* Message = SDL_CreateTextureFromSurface(renderer, surfaceMessage); 

SDL_Rect Message_rect; //create a rect
Message_rect.x = 0;  //controls the rect's x coordinate
Message_rect.y = 0; // controls the rect's y coordinte
Message_rect.w = 100; // controls the width of the rect
Message_rect.h = 100; // controls the height of the rect

//Mind you that (0,0) is on the top left of the window/screen, 
//think a rect as the text's box, that way it would be very simple to understance

//Now since it's a texture, you have to put RenderCopy in your 
//game loop area, the area where the whole code executes

//you put the renderer's name first, the Message, the crop size
SDL_RenderCopy(renderer, Message, NULL, &Message_rect); 
//(you can ignore this if you don't want to dabble with cropping),
//and the rect which is the size and coordinate of your texture
Example 2:
std::string score_text = "score: " + std::to_string(score);        
SDL_Color textColor = { 255, 255, 255, 0 };
SDL_Surface* textSurface = TTF_RenderText_Solid(font, score_text.c_str(), textColor);
SDL_Texture* text = SDL_CreateTextureFromSurface(renderer, textSurface);
int text_width = textSurface->w;
int text_height = textSurface->h;
SDL_FreeSurface(textSurface);
SDL_Rect renderQuad = { 20, win_height - 30, text_width, text_height };
SDL_RenderCopy(renderer, text, NULL, &renderQuad);
SDL_DestroyTexture(text);

No comments:

Post a Comment

Please, no foul language, trolling, keep it clean. Thanx.