Advertisement
Benjamin_Loison

all void displayText()

Aug 4th, 2018
376
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.85 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. }
  67.  
  68. void displayText(unsigned char nb, double centerX, double centerY, double widthDiv2, double heightDiv2, string font, SDL_Color color)
  69. {
  70.     displayText(convertNbToStr(nb), centerX, centerY, widthDiv2, heightDiv2, font, color);
  71. }
  72.  
  73. void displayText(GLuint textTexture, double centerX, double centerY, double widthDiv2, double heightDiv2, SDL_Color color)
  74. {
  75.     glBindTexture(GL_TEXTURE_2D, textTexture);
  76.  
  77.     if(widthDiv2 == -1)
  78.     {
  79.         GLint height, width;
  80.         glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height);
  81.         glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width);
  82.         widthDiv2 = width / 2;
  83.         heightDiv2 = height / 2;
  84.     }
  85.  
  86.     double winW2 = getWindowWidthDiv2();
  87.     double winH2 = getWindowHeightDiv2();
  88.     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,
  89.                         (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};
  90.     float texCoord[] = {0, 0,   1, 0,   1, 1,
  91.                         0, 0,   0, 1,   1, 1};
  92.     textShader.use();
  93.      
  94.     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vertices);
  95.     glEnableVertexAttribArray(0);
  96.  
  97.     glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, texCoord);
  98.     glEnableVertexAttribArray(1);
  99.  
  100.     //Set color
  101.     textShader.setVec4("color", glm::vec4(color.r / 255.0f, color.g / 255.0f, color.b / 255.0f, color.a / 255.0f));
  102.      
  103.     glDrawArrays(GL_TRIANGLES, 0, 6);
  104.  
  105.     glBindTexture(GL_TEXTURE_2D, 0);
  106.     glDisableVertexAttribArray(1);
  107.     glDisableVertexAttribArray(0);
  108.     glUseProgram(0);
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement