Guest User

Untitled

a guest
Jan 15th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. // This function creates the required texture.
  2. bool Init()
  3. {
  4. // ...
  5.  
  6. g_pFont = TTF_OpenFont("../arial.ttf", 12);
  7. if(g_pFont == NULL)
  8. return false;
  9.  
  10. // Write text to surface
  11. g_pText = SDL_DisplayFormatAlpha(TTF_RenderUTF8_Blended(g_pFont, "My first Text!", g_textColor)); //< Doesn't work
  12.  
  13. // Note that Solid and Shaded Does work properly if I uncomment them.
  14. //g_pText = SDL_DisplayFormatAlpha(TTF_RenderUTF8_Solid(g_pFont, "My first Text!", g_textColor));
  15. //g_pText = SDL_DisplayFormatAlpha(TTF_RenderUTF8_Shaded(g_pFont, "My first Text!", g_textColor, g_bgColor));
  16.  
  17. if(g_pText == NULL)
  18. return false;
  19.  
  20. // Prepare the texture for the font
  21. GLenum textFormat;
  22. if(g_pText->format->BytesPerPixel == 4)
  23. {
  24. // alpha
  25. if(g_pText->format->Rmask == 0x000000ff)
  26. textFormat = GL_RGBA;
  27. else
  28. textFormat = GL_BGRA_EXT;
  29. }
  30.  
  31. // Create the font's texture
  32. glGenTextures(1, &g_FontTextureId);
  33. glBindTexture(GL_TEXTURE_2D, g_FontTextureId);
  34. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  35. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
  36. glTexImage2D(GL_TEXTURE_2D, 0, g_pText->format->BytesPerPixel, g_pText->w, g_pText->h, 0, textFormat, GL_UNSIGNED_BYTE, g_pText->pixels);
  37.  
  38. // ...
  39. }
  40.  
  41. // this function is called each frame
  42. void DrawText()
  43. {
  44. SDL_Rect sourceRect;
  45. sourceRect.x = 0;
  46. sourceRect.y = 0;
  47. sourceRect.h = 10;
  48. sourceRect.w = 173;
  49.  
  50. // DestRect is null so the rect is drawn at 0,0
  51. SDL_BlitSurface(g_pText, &sourceRect, g_pSurfaceDisplay, NULL);
  52.  
  53. glBindTexture(GL_TEXTURE_2D, g_FontTextureId);
  54. glEnable(GL_TEXTURE_2D);
  55. glEnable(GL_BLEND);
  56. glBegin( GL_QUADS );
  57.  
  58. glTexCoord2f(0.0f, 0.0f);
  59. glVertex2f(0.0f, 0.0f);
  60.  
  61. glTexCoord2f(0.0f, 1.0f);
  62. glVertex2f(0.0f, 10.0f);
  63.  
  64. glTexCoord2f(1.0f, 1.0f);
  65. glVertex2f(173.0f, 10.0f);
  66.  
  67. glTexCoord2f(1.0f, 0.0f);
  68. glVertex2f(173.0f, 0.0f);
  69.  
  70. glEnd();
  71. glDisable(GL_BLEND);
  72. glDisable(GL_TEXTURE_2D);
  73. }
  74.  
  75. glEnable( GL_BLEND );
  76. glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
  77.  
  78. SDL_BlitSurface(g_pText, &sourceRect, g_pSurfaceDisplay, NULL);
  79.  
  80. g_pText = SDL_DisplayFormatAlpha( TTF_RenderUTF8_Blended(...) );
  81.  
  82. g_pText = TTF_RenderUTF8_Blended(g_pFont, "My first Text!", g_textColor);
Add Comment
Please, Sign In to add comment