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.
 
 
 

106 lines
1.8 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(char const *text, char const *font)
  35. : data(new TextData())
  36. {
  37. data->font = Forge::Register(font);
  38. if (text)
  39. data->m_text = text;
  40. data->pos = vec3(0, 0, 0);
  41. m_drawgroup = DRAWGROUP_HUD;
  42. }
  43. void Text::SetText(char const *text)
  44. {
  45. data->m_text = text;
  46. }
  47. void Text::SetInt(int val)
  48. {
  49. data->m_text = String::Printf("%i", val);
  50. }
  51. void Text::SetPos(vec3 pos)
  52. {
  53. data->pos = pos;
  54. }
  55. vec3 Text::GetPos()
  56. {
  57. return (vec3)data->pos;
  58. }
  59. void Text::SetAlign(int align)
  60. {
  61. data->align = align;
  62. }
  63. void Text::TickDraw(float seconds)
  64. {
  65. Entity::TickDraw(seconds);
  66. int length = data->m_text.Count();
  67. if (length)
  68. {
  69. Font *font = Forge::GetFont(data->font);
  70. vec3 delta(0.0f);
  71. if (data->align == ALIGN_RIGHT)
  72. delta.x -= length * font->GetSize().x;
  73. else if (data->align == ALIGN_CENTER)
  74. delta.x -= length * font->GetSize().x / 2;
  75. font->Print(data->pos + delta, data->m_text.C());
  76. }
  77. }
  78. Text::~Text()
  79. {
  80. Forge::Deregister(data->font);
  81. delete data;
  82. }
  83. } /* namespace lol */