font_resource.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include "types.h"
  25. #include "resource.h"
  26. #include "bundle.h"
  27. #include "allocator.h"
  28. #include "file.h"
  29. #include "assert.h"
  30. #include "log.h"
  31. namespace crown
  32. {
  33. //-----------------------------------------------------------------------------
  34. struct FontHeader
  35. {
  36. uint32_t num_glyphs;
  37. uint32_t texture_size; // Font texture size -- pow of 2
  38. uint32_t font_size;
  39. };
  40. //-----------------------------------------------------------------------------
  41. struct FontGlyphData
  42. {
  43. uint32_t id;
  44. uint32_t x;
  45. uint32_t y;
  46. uint32_t width;
  47. uint32_t height;
  48. float x_offset;
  49. float y_offset;
  50. float x_advance;
  51. };
  52. //-----------------------------------------------------------------------------
  53. class FontResource
  54. {
  55. public:
  56. //-----------------------------------------------------------------------------
  57. static void* load(Allocator& allocator, Bundle& bundle, ResourceId id)
  58. {
  59. File* file = bundle.open(id);
  60. const size_t file_size = file->size();
  61. void* res = allocator.allocate(file_size);
  62. file->read(res, file_size);
  63. bundle.close(file);
  64. return res;
  65. }
  66. //-----------------------------------------------------------------------------
  67. static void online(StringId64 /*id*/, ResourceManager& /*rm*/)
  68. {
  69. }
  70. //-----------------------------------------------------------------------------
  71. static void offline(StringId64 /*id*/, ResourceManager& /*rm*/)
  72. {
  73. }
  74. //-----------------------------------------------------------------------------
  75. static void unload(Allocator& allocator, void* resource)
  76. {
  77. allocator.deallocate(resource);
  78. }
  79. //-----------------------------------------------------------------------------
  80. uint32_t num_glyphs() const
  81. {
  82. return ((FontHeader*)this)->num_glyphs;
  83. }
  84. //-----------------------------------------------------------------------------
  85. uint32_t texture_size() const
  86. {
  87. return ((FontHeader*)this)->texture_size;
  88. }
  89. //-----------------------------------------------------------------------------
  90. uint32_t font_size() const
  91. {
  92. return ((FontHeader*)this)->font_size;
  93. }
  94. //-----------------------------------------------------------------------------
  95. FontGlyphData get_glyph(const uint32_t index) const
  96. {
  97. CE_ASSERT(index < num_glyphs(), "Index out of bounds");
  98. FontGlyphData* begin = (FontGlyphData*) (((char*) this) + sizeof(FontHeader));
  99. for (uint32_t i = 0; i < num_glyphs(); i++)
  100. {
  101. if (begin[i].id == index)
  102. {
  103. return begin[i];
  104. }
  105. }
  106. CE_FATAL("Glyph not found");
  107. return FontGlyphData();
  108. }
  109. private:
  110. // Disable construction
  111. FontResource();
  112. };
  113. } // namespace crown