Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

font.cpp 2.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // Lol Engine
  3. //
  4. // Copyright © 2010—2018 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. /*
  19. * Font implementation class
  20. */
  21. class FontData
  22. {
  23. friend class Font;
  24. private:
  25. std::string m_name;
  26. TileSet *tileset;
  27. ivec2 size;
  28. };
  29. /*
  30. * Public Font class
  31. */
  32. Font::Font(std::string const &path)
  33. : data(new FontData())
  34. {
  35. data->m_name = "<font> " + path;
  36. data->tileset = Tiler::Register(path, ivec2::zero, ivec2(16));
  37. data->size = data->tileset->GetTileSize(0);
  38. m_drawgroup = DRAWGROUP_TEXTURE;
  39. }
  40. Font::~Font()
  41. {
  42. Tiler::Deregister(data->tileset);
  43. delete data;
  44. }
  45. void Font::tick_draw(float seconds, Scene &scene)
  46. {
  47. Entity::tick_draw(seconds, scene);
  48. if (data->tileset->GetTexture())
  49. {
  50. data->tileset->GetTexture()->SetMinFiltering(TextureMinFilter::LINEAR_TEXEL_NO_MIPMAP);
  51. data->tileset->GetTexture()->SetMagFiltering(TextureMagFilter::LINEAR_TEXEL);
  52. }
  53. }
  54. std::string Font::GetName() const
  55. {
  56. return data->m_name;
  57. }
  58. void Font::Print(Scene &scene, vec3 pos, std::string const &str, vec2 scale, float spacing)
  59. {
  60. float origin_x = pos.x;
  61. for (int i = 0; i < (int)str.length(); ++i)
  62. {
  63. uint32_t ch = str[i];
  64. switch (ch)
  65. {
  66. case '\r': /* carriage return */
  67. pos.x = origin_x;
  68. break;
  69. case '\b': /* backspace */
  70. pos.x -= data->size.x * scale.x;
  71. break;
  72. case '\n': /* new line */
  73. pos.x = origin_x;
  74. pos.y -= data->size.y * scale.y;
  75. break;
  76. default:
  77. if (ch != ' ')
  78. scene.AddTile(data->tileset, ch & 255, pos, scale, 0.0f);
  79. pos.x += data->size.x * scale.x;
  80. break;
  81. }
  82. pos.x += data->size.x * scale.x * spacing;
  83. }
  84. }
  85. ivec2 Font::GetSize() const
  86. {
  87. return data->size;
  88. }
  89. } /* namespace lol */