Advertisement
Benjamin_Loison

void displayText(string text, double centerX, double centerY

Aug 6th, 2018
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.82 KB | None | 0 0
  1. void displayText(string text, double centerX, double centerY, double widthDiv2, double heightDiv2, string font, SDL_Color color)
  2. {
  3.     SDL_Surface *surface;
  4.     if(texts.find(text) == texts.end())
  5.     {
  6.         //initializeText(text, text, font, color);
  7.         //surface = TTF_RenderUTF8_Blended(fonts[font], text.c_str(), color);
  8.         surface = TTF_RenderText_Blended(fonts[font], text.c_str(), color);
  9.         //texts[text] = surface;
  10.     }
  11.     else
  12.     {
  13.  
  14.         surface = texts[text];
  15.     }
  16.     surface = flipSurface(surface);
  17.     GLuint textTexture;
  18.     glGenTextures(1, &textTexture);
  19.     glBindTexture(GL_TEXTURE_2D, textTexture);
  20.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  21.     GLenum codagePixel;
  22.     if(surface->format->Rmask == 0x000000ff)
  23.         codagePixel = GL_RGBA;
  24.     else
  25.     {
  26.         #ifndef GL_BGRA
  27.             #define GL_BGRA 0x80E1
  28.         #endif
  29.         codagePixel = GL_BGRA;
  30.     }
  31.     if(widthDiv2 == -1) // Using by chat system
  32.     {
  33.         double textWidthDiv2 = surface->w / 4, textHeight = surface->h, textHeightDiv2 = textHeight / 2;
  34.         centerX += textWidthDiv2;
  35.         centerY += textHeightDiv2 + heightDiv2 * textHeight;
  36.         widthDiv2 = textWidthDiv2;
  37.         heightDiv2 = textHeightDiv2;
  38.     }
  39.     glTexImage2D(GL_TEXTURE_2D, 0, 4, surface->w, surface->h, 0, codagePixel, GL_UNSIGNED_BYTE, surface->pixels);
  40.     double winW2 = getWindowWidthDiv2();
  41.     double winH2 = getWindowHeightDiv2();
  42.     float vertices[] = {(float)((centerX - widthDiv2) / winW2 - 1), (float)((centerY - heightDiv2) / winH2 - 1), 0,   (float)((centerX + widthDiv2) / winW2 - 1), (float)((centerY - heightDiv2) / winH2 - 1), 0,   (float)((centerX + widthDiv2) / winW2 - 1), (float)((centerY + heightDiv2) / winH2 - 1), 0,
  43.                         (float)((centerX - widthDiv2) / winW2 - 1), (float)((centerY - heightDiv2) / winH2 - 1), 0,   (float)((centerX - widthDiv2) / winW2 - 1), (float)((centerY + heightDiv2) / winH2 - 1), 0,   (float)((centerX + widthDiv2) / winW2 - 1), (float)((centerY + heightDiv2) / winH2 - 1), 0};
  44.     float texCoord[] = {0, 0,   1, 0,   1, 1,
  45.                         0, 0,   0, 1,   1, 1};
  46.     textShader.use();
  47.      
  48.     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vertices);
  49.     glEnableVertexAttribArray(0);
  50.  
  51.     glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, texCoord);
  52.     glEnableVertexAttribArray(1);
  53.  
  54.     //Set color
  55.     textShader.setVec4("color", glm::vec4(color.r / 255.0f, color.g / 255.0f, color.b / 255.0f, color.a / 255.0f));
  56.      
  57.     glDrawArrays(GL_TRIANGLES, 0, 6);
  58.  
  59.     glBindTexture(GL_TEXTURE_2D, 0);
  60.     glDisableVertexAttribArray(1);
  61.     glDisableVertexAttribArray(0);
  62.     glUseProgram(0);
  63.  
  64.     SDL_FreeSurface(surface);
  65.     glDeleteTextures(1, &textTexture);
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement