TextRenderer.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use,
  7. copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following
  10. conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include "TextRenderer.h"
  23. #include <GL/glew.h> // FIXME
  24. #include "Renderer.h"
  25. #include "Device.h"
  26. namespace crown
  27. {
  28. //-----------------------------------------------------------------------------
  29. TextRenderer::TextRenderer()
  30. {
  31. }
  32. //-----------------------------------------------------------------------------
  33. TextRenderer::~TextRenderer()
  34. {
  35. }
  36. //-----------------------------------------------------------------------------
  37. void TextRenderer::BeginDraw()
  38. {
  39. }
  40. //-----------------------------------------------------------------------------
  41. void TextRenderer::EndDraw()
  42. {
  43. }
  44. //-----------------------------------------------------------------------------
  45. void TextRenderer::Draw(const Str& string, int x, int y, Font* font)
  46. {
  47. Renderer* renderer = GetDevice()->GetRenderer();
  48. Font* mFont = font;
  49. Texture* tex = font->GetTexture();
  50. renderer->_SetTexturing(0, true);
  51. renderer->_SetTexture(0, tex);
  52. renderer->_SetTextureMode(0, tex->GetMode(), tex->GetBlendColor());
  53. renderer->_SetTextureFilter(0, tex->GetFilter());
  54. Color4 mColor = Color4::BLACK;
  55. uint mCharSpacing = 0;
  56. uint mLineSpacing = 0;
  57. glColor4fv(mColor.to_float_ptr());
  58. glPushMatrix();
  59. glTranslatef((float)x, (float)y, 0.0f);
  60. glEnable(GL_BLEND);
  61. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  62. for (uint i = 0; i < string.GetLength(); i++)
  63. {
  64. Glyph glyph;
  65. float left, right, bottom, top, advance, dummy;
  66. uint ttSizeX, ttSizeY;
  67. glyph = mFont->GetGlyph(string[i]);
  68. glyph.GetMetrics(left, right, bottom, top, dummy, dummy, advance, dummy);
  69. ttSizeX = (uint)((right - left) * tex->GetWidth());
  70. ttSizeY = (uint)((top - bottom) * tex->GetHeight());
  71. glBegin(GL_QUADS);
  72. glTexCoord2f(left, top);
  73. glVertex2i(0, 0);
  74. glTexCoord2f(left, bottom);
  75. glVertex2i(0, ttSizeY);
  76. glTexCoord2f(right, bottom);
  77. glVertex2i(ttSizeX, ttSizeY);
  78. glTexCoord2f(right, top);
  79. glVertex2i(ttSizeX, 0);
  80. glEnd();
  81. glTranslatef(advance + (float)mCharSpacing, 0, 0);
  82. }
  83. glPopMatrix();
  84. glDisable(GL_TEXTURE_2D);
  85. //glDisable(GL_BLEND);
  86. }
  87. //-----------------------------------------------------------------------------
  88. void TextRenderer::GetStrDimensions(const Str& string, uint start, uint end, int& width, int& height)
  89. {
  90. // if (mFont != NULL)
  91. // {
  92. // return;
  93. // }
  94. // height = 0;
  95. // width = 0;
  96. // //TODO: Should take baseline into account
  97. // if (end == (uint)-1)
  98. // end = string.GetLength();
  99. // for (uint i = start; i < end; i++)
  100. // {
  101. // Glyph glyph;
  102. // float gheight, gwidth, dummy, advance, baseline;
  103. // glyph = mFont->GetGlyph(string[i]);
  104. // glyph.GetMetrics(dummy, dummy, dummy, dummy, gwidth, gheight, advance, baseline);
  105. // height = Math::Max(height, (int)gheight) + (int)Math::Abs(gheight - baseline);
  106. // if (i < string.GetLength() - 1)
  107. // width += (int)advance;
  108. // else
  109. // {
  110. // width += (int)gwidth;
  111. // }
  112. // }
  113. }
  114. //-----------------------------------------------------------------------------
  115. int TextRenderer::GetStrIndexFromDimensions(const Str& string, uint start, const Point2& position, Point2& charPosition)
  116. {
  117. // if (mFont == NULL)
  118. // {
  119. // return -1;
  120. // }
  121. // int width = 0;
  122. // for (uint i = start; i < string.GetLength(); i++)
  123. // {
  124. // Glyph glyph;
  125. // float gheight, gwidth, dummy, advance;
  126. // glyph = mFont->GetGlyph(string[i]);
  127. // glyph.GetMetrics(dummy, dummy, dummy, dummy, gwidth, gheight, advance, dummy);
  128. // if (i >= string.GetLength())
  129. // advance = gwidth;
  130. // if (width + advance / 2.0f >= position.x)
  131. // {
  132. // charPosition.x = width;
  133. // charPosition.y = 0;
  134. // return i;
  135. // }
  136. // width += (int)advance;
  137. // }
  138. // charPosition.x = width;
  139. // charPosition.y = 0;
  140. // return string.GetLength();
  141. }
  142. //-----------------------------------------------------------------------------
  143. int TextRenderer::GetMaxTextHeight()
  144. {
  145. // return mFont->_GetMaxTextHeight();
  146. }
  147. } // namespace crown