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.
 
 
 

109 line
2.0 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2012 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://sam.zoy.org/projects/COPYING.WTFPL 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, length;
  28. char *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. data->text = text ? strdup(text) : NULL;
  39. data->length = text ? strlen(text) : 0;
  40. data->pos = vec3(0, 0, 0);
  41. m_drawgroup = DRAWGROUP_HUD;
  42. }
  43. void Text::SetText(char const *text)
  44. {
  45. if (data->text)
  46. free(data->text);
  47. data->text = text ? strdup(text) : NULL;
  48. data->length = text ? strlen(text) : 0;
  49. }
  50. void Text::SetInt(int val)
  51. {
  52. if (data->text)
  53. free(data->text);
  54. char text[128];
  55. sprintf(text, "%i", val);
  56. data->text = strdup(text);
  57. data->length = strlen(text);
  58. }
  59. void Text::SetPos(vec3 pos)
  60. {
  61. data->pos = pos;
  62. }
  63. void Text::SetAlign(int align)
  64. {
  65. data->align = align;
  66. }
  67. void Text::TickDraw(float seconds)
  68. {
  69. Entity::TickDraw(seconds);
  70. if (data->text)
  71. {
  72. Font *font = Forge::GetFont(data->font);
  73. vec3 delta(0.0f);
  74. if (data->align == ALIGN_RIGHT)
  75. delta.x -= data->length * font->GetSize().x;
  76. else if (data->align == ALIGN_CENTER)
  77. delta.x -= data->length * font->GetSize().x / 2;
  78. font->Print(data->pos + delta, data->text);
  79. }
  80. }
  81. Text::~Text()
  82. {
  83. if (data->text)
  84. free(data->text);
  85. Forge::Deregister(data->font);
  86. delete data;
  87. }
  88. } /* namespace lol */