You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
1.7 KiB

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