106 行
1.9 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright © 2010—2015 Sam Hocevar <sam@hocevar.net>
  5. //
  6. // This library is free software. It comes without any warranty, to
  7. // the extent permitted by applicable law. You can redistribute it
  8. // and/or modify it under the terms of the Do What the Fuck You Want
  9. // to Public License, Version 2, as published by the WTFPL Task Force.
  10. // See http://www.wtfpl.net/ for more details.
  11. //
  12. #include <lol/engine-internal.h>
  13. #include <cstdio>
  14. #include <cstdlib> /* free() */
  15. #include <cstring> /* strdup() */
  16. namespace lol
  17. {
  18. /*
  19. * Text implementation class
  20. */
  21. class TextData
  22. {
  23. friend class Text;
  24. private:
  25. int font, align;
  26. String m_text;
  27. vec3 pos;
  28. };
  29. /*
  30. * Public Text class
  31. */
  32. Text::Text(String const &text, char const *font)
  33. : data(new TextData())
  34. {
  35. data->font = Forge::Register(font);
  36. data->m_text = text;
  37. data->pos = vec3(0, 0, 0);
  38. m_drawgroup = DRAWGROUP_HUD;
  39. }
  40. void Text::SetText(String const &text)
  41. {
  42. data->m_text = text;
  43. }
  44. void Text::SetInt(int val)
  45. {
  46. data->m_text = String::Printf("%i", val);
  47. }
  48. void Text::SetPos(vec3 pos)
  49. {
  50. data->pos = pos;
  51. }
  52. vec3 Text::GetPos()
  53. {
  54. return (vec3)data->pos;
  55. }
  56. void Text::SetAlign(int align)
  57. {
  58. data->align = align;
  59. }
  60. ivec2 Text::GetFontSize()
  61. {
  62. Font *font = Forge::GetFont(data->font);
  63. return font->GetSize();
  64. }
  65. void Text::TickDraw(float seconds, Scene &scene)
  66. {
  67. Entity::TickDraw(seconds, scene);
  68. int length = data->m_text.count();
  69. if (length)
  70. {
  71. Font *font = Forge::GetFont(data->font);
  72. vec3 delta(0.0f);
  73. if (data->align == ALIGN_RIGHT)
  74. delta.x -= length * font->GetSize().x;
  75. else if (data->align == ALIGN_CENTER)
  76. delta.x -= length * font->GetSize().x / 2;
  77. font->Print(data->pos + delta, data->m_text.C());
  78. }
  79. }
  80. Text::~Text()
  81. {
  82. Forge::Deregister(data->font);
  83. delete data;
  84. }
  85. } /* namespace lol */