vitimiti

Function to get a font surface pixels.

Nov 6th, 2015
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.05 KB | None | 0 0
  1. MatVGL::FontData MatVGL::VFonts::createString(MatVGL_Font font, String *text,
  2.                                               Int16 r, Int16 g, Int16 b) {
  3.   // Put the given colors into SDL_Color, making sure they are between 0 and
  4.   // 255.
  5.   if (r < 0)
  6.     r = 0;
  7.   if (g < 0)
  8.     g = 0;
  9.   if (b < 0)
  10.     b = 0;
  11.   if (r > 255)
  12.     r = 255;
  13.   if (g > 255)
  14.     g = 255;
  15.   if (b > 255)
  16.     b = 255;
  17.  
  18.   SDL_Color fontColor{(unsigned char)r, (unsigned char)g, (unsigned char)b,
  19.                       255};
  20.  
  21.   // Create the surface with the given data.
  22.   SDL_Surface *fontSurface =
  23.       TTF_RenderText_Solid(p_fontsMap.find(font)->second, text, fontColor);
  24.  
  25.   // Put all the data into the structure.
  26.   FontData fontData = {(UInt32)fontSurface->w, (UInt32)fontSurface->h,
  27.                        fontSurface->pixels};
  28.  
  29.   // We have to free the surface.
  30.   SDL_FreeSurface(fontSurface);
  31.  
  32.   // Return the surface data.
  33.   return fontData;
  34. }
  35.  
  36. ////////// Fonts.h ↓
  37. typedef struct FontData {
  38.   UInt32 w;
  39.   UInt32 h;
  40.   void *data;
  41. } FontData;
Advertisement
Add Comment
Please, Sign In to add comment