Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

125 lignes
2.6 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright © 2010—2019 Sam Hocevar <sam@hocevar.net>
  5. // 2013 Jean-Yves Lamoureux <jylam@lnxscene.org>
  6. //
  7. // Lol Engine is free software. It comes without any warranty, to
  8. // the extent permitted by applicable law. You can redistribute it
  9. // and/or modify it under the terms of the Do What the Fuck You Want
  10. // to Public License, Version 2, as published by the WTFPL Task Force.
  11. // See http://www.wtfpl.net/ for more details.
  12. //
  13. #include <lol/engine-internal.h>
  14. #include <cstring>
  15. #include <cstdio>
  16. namespace lol
  17. {
  18. /* The font cache */
  19. static entity_dict<Font> font_cache;
  20. /*
  21. * Font implementation class
  22. */
  23. class FontData
  24. {
  25. friend class Font;
  26. private:
  27. std::string m_name;
  28. TileSet *tileset;
  29. ivec2 size;
  30. };
  31. /*
  32. * Public Font class
  33. */
  34. Font *Font::create(std::string const &path)
  35. {
  36. auto ret = font_cache.get(path);
  37. return ret ? ret : font_cache.set(path, new Font(path));
  38. }
  39. void Font::destroy(Font *f)
  40. {
  41. // FIXME: decrement!
  42. font_cache.erase(f);
  43. }
  44. Font::Font(std::string const &path)
  45. : data(new FontData())
  46. {
  47. data->m_name = "<font> " + path;
  48. data->tileset = TileSet::create(path, ivec2::zero, ivec2(16));
  49. data->size = data->tileset->GetTileSize(0);
  50. m_drawgroup = tickable::group::draw::texture;
  51. }
  52. Font::~Font()
  53. {
  54. TileSet::destroy(data->tileset);
  55. delete data;
  56. }
  57. void Font::tick_draw(float seconds, Scene &scene)
  58. {
  59. entity::tick_draw(seconds, scene);
  60. if (data->tileset->GetTexture())
  61. {
  62. data->tileset->GetTexture()->SetMinFiltering(TextureMinFilter::LINEAR_TEXEL_NO_MIPMAP);
  63. data->tileset->GetTexture()->SetMagFiltering(TextureMagFilter::LINEAR_TEXEL);
  64. }
  65. }
  66. std::string Font::GetName() const
  67. {
  68. return data->m_name;
  69. }
  70. void Font::Print(Scene &scene, vec3 pos, std::string const &str, vec2 scale, float spacing)
  71. {
  72. float origin_x = pos.x;
  73. for (int i = 0; i < (int)str.length(); ++i)
  74. {
  75. uint32_t ch = str[i];
  76. switch (ch)
  77. {
  78. case '\r': /* carriage return */
  79. pos.x = origin_x;
  80. break;
  81. case '\b': /* backspace */
  82. pos.x -= data->size.x * scale.x;
  83. break;
  84. case '\n': /* new line */
  85. pos.x = origin_x;
  86. pos.y -= data->size.y * scale.y;
  87. break;
  88. default:
  89. if (ch != ' ')
  90. scene.AddTile(data->tileset, ch & 255, pos, scale, 0.0f);
  91. pos.x += data->size.x * scale.x;
  92. break;
  93. }
  94. pos.x += data->size.x * scale.x * spacing;
  95. }
  96. }
  97. ivec2 Font::GetSize() const
  98. {
  99. return data->size;
  100. }
  101. } /* namespace lol */