No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

108 líneas
2.2 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
  5. // 2013 Jean-Yves Lamoureux <jylam@lnxscene.org>
  6. // This program is free software; you can redistribute it and/or
  7. // modify it under the terms of the Do What The Fuck You Want To
  8. // Public License, Version 2, as published by Sam Hocevar. See
  9. // http://www.wtfpl.net/ for more details.
  10. //
  11. #include <lol/engine-internal.h>
  12. #include <cstring>
  13. #include <cstdio>
  14. namespace lol
  15. {
  16. /*
  17. * Font implementation class
  18. */
  19. class FontData
  20. {
  21. friend class Font;
  22. private:
  23. String m_name;
  24. TileSet *tileset;
  25. ivec2 size;
  26. };
  27. /*
  28. * Public Font class
  29. */
  30. Font::Font(char const *path)
  31. : data(new FontData())
  32. {
  33. data->m_name = String("<font> ") + path;
  34. data->tileset = Tiler::Register(path, ivec2::zero, ivec2(16));
  35. data->size = data->tileset->GetTileSize(0);
  36. m_drawgroup = DRAWGROUP_TEXTURE;
  37. }
  38. Font::~Font()
  39. {
  40. Tiler::Deregister(data->tileset);
  41. delete data;
  42. }
  43. void Font::TickDraw(float seconds, Scene &scene)
  44. {
  45. Entity::TickDraw(seconds, scene);
  46. if (data->tileset->GetTexture())
  47. {
  48. data->tileset->GetTexture()->SetMinFiltering(TextureMinFilter::LINEAR_TEXEL_NO_MIPMAP);
  49. data->tileset->GetTexture()->SetMagFiltering(TextureMagFilter::LINEAR_TEXEL);
  50. }
  51. }
  52. char const *Font::GetName()
  53. {
  54. return data->m_name.C();
  55. }
  56. void Font::Print(Scene &scene, vec3 pos, char const *str, vec2 scale, float spacing)
  57. {
  58. float origin_x = pos.x;
  59. while (*str)
  60. {
  61. uint32_t ch = (uint8_t)*str++;
  62. switch (ch)
  63. {
  64. case '\r': /* carriage return */
  65. pos.x = origin_x;
  66. break;
  67. case '\b': /* backspace */
  68. pos.x -= data->size.x * scale.x;
  69. break;
  70. case '\n': /* new line */
  71. pos.x = origin_x;
  72. pos.y -= data->size.y * scale.y;
  73. break;
  74. default:
  75. if (ch != ' ')
  76. scene.AddTile(data->tileset, ch & 255, pos, 0, scale, 0.0f);
  77. pos.x += data->size.x * scale.x;
  78. break;
  79. }
  80. pos.x += data->size.x * scale.x * spacing;
  81. }
  82. }
  83. ivec2 Font::GetSize() const
  84. {
  85. return data->size;
  86. }
  87. } /* namespace lol */