25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

text.cpp 2.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // Lol Engine
  3. //
  4. // Copyright © 2010—2019 Sam Hocevar <sam@hocevar.net>
  5. //
  6. // Lol Engine 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. Font *m_font;
  26. TextAlign m_align;
  27. std::string m_text;
  28. vec3 m_pos;
  29. vec2 m_scale;
  30. float m_spacing;
  31. };
  32. /*
  33. * Public Text class
  34. */
  35. Text::Text(std::string const &text, char const *font)
  36. : data(new TextData())
  37. {
  38. data->m_font = Font::create(font);
  39. data->m_align = TextAlign::Left;
  40. data->m_text = text;
  41. data->m_pos = vec3(0, 0, 0);
  42. data->m_scale = vec2(1.f);
  43. data->m_spacing = 0.f;
  44. m_drawgroup = tickable::group::draw::entity;
  45. }
  46. void Text::SetText(std::string const &text)
  47. {
  48. data->m_text = text;
  49. }
  50. void Text::SetInt(int val)
  51. {
  52. data->m_text = format("%i", val);
  53. }
  54. void Text::SetPos(vec3 pos)
  55. {
  56. data->m_pos = pos;
  57. }
  58. void Text::SetScale(vec2 scale)
  59. {
  60. data->m_scale = scale;
  61. }
  62. void Text::SetSpacing(float spacing)
  63. {
  64. data->m_spacing = spacing;
  65. }
  66. void Text::SetAlign(TextAlign align)
  67. {
  68. data->m_align = align;
  69. }
  70. vec3 Text::GetPos()
  71. {
  72. return (vec3)data->m_pos;
  73. }
  74. ivec2 Text::GetFontSize()
  75. {
  76. return data->m_font->GetSize();
  77. }
  78. void Text::tick_draw(float seconds, Scene &scene)
  79. {
  80. entity::tick_draw(seconds, scene);
  81. if (auto length = data->m_text.length())
  82. {
  83. vec3 delta(0.0f);
  84. float text_width = ((length - 0.5f) + (length - 1) * data->m_spacing)
  85. * data->m_font->GetSize().x;
  86. if (data->m_align == TextAlign::Right)
  87. delta.x -= text_width * data->m_scale.x;
  88. else if (data->m_align == TextAlign::Center)
  89. delta.x -= 0.5f * text_width * data->m_scale.x;
  90. data->m_font->Print(scene, data->m_pos + delta, data->m_text,
  91. data->m_scale, data->m_spacing);
  92. }
  93. }
  94. Text::~Text()
  95. {
  96. Font::destroy(data->m_font);
  97. delete data;
  98. }
  99. } /* namespace lol */