GuiResource.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 <cstring>
  25. #include <inttypes.h>
  26. #include "Assert.h"
  27. #include "Types.h"
  28. #include "Allocator.h"
  29. #include "File.h"
  30. #include "OS.h"
  31. #include "StringUtils.h"
  32. #include "List.h"
  33. #include "Bundle.h"
  34. #include "Device.h"
  35. #include "Renderer.h"
  36. #include "Vector2.h"
  37. #include "Vector3.h"
  38. namespace crown
  39. {
  40. const uint32_t GUI_RESOURCE_VERSION = 1;
  41. //-----------------------------------------------------------------------------
  42. struct GuiHeader
  43. {
  44. uint32_t position[2];
  45. uint32_t size[2];
  46. uint32_t num_rects;
  47. uint32_t num_triangles;
  48. uint32_t num_images;
  49. uint32_t rects_offset;
  50. uint32_t triangles_offset;
  51. uint32_t images_offset;
  52. };
  53. //-----------------------------------------------------------------------------
  54. struct GuiRectData
  55. {
  56. float position[2];
  57. float size[2];
  58. float color[4];
  59. };
  60. //-----------------------------------------------------------------------------
  61. struct GuiTriangleData
  62. {
  63. float points[6];
  64. float color[4];
  65. };
  66. //-----------------------------------------------------------------------------
  67. struct GuiImageData
  68. {
  69. ResourceId material;
  70. float position[2];
  71. float size[2];
  72. };
  73. //-----------------------------------------------------------------------------
  74. struct GuiResource
  75. {
  76. //-----------------------------------------------------------------------------
  77. static void* load(Allocator& allocator, Bundle& bundle, ResourceId id)
  78. {
  79. File* file = bundle.open(id);
  80. const size_t file_size = file->size();
  81. void* res = allocator.allocate(file_size);
  82. file->read(res, file_size);
  83. bundle.close(file);
  84. return res;
  85. }
  86. //-----------------------------------------------------------------------------
  87. static void online(void* /*resource*/)
  88. {
  89. }
  90. //-----------------------------------------------------------------------------
  91. static void unload(Allocator& allocator, void* resource)
  92. {
  93. CE_ASSERT_NOT_NULL(resource);
  94. allocator.deallocate(resource);
  95. }
  96. //-----------------------------------------------------------------------------
  97. static void offline(void* /*resource*/)
  98. {
  99. }
  100. //-----------------------------------------------------------------------------
  101. Vector3 gui_position() const
  102. {
  103. Vector3 pos;
  104. pos.x = ((GuiHeader*)this)->position[0];
  105. pos.y = ((GuiHeader*)this)->position[1];
  106. pos.z = 0.0f;
  107. return pos;
  108. }
  109. //-----------------------------------------------------------------------------
  110. Vector2 gui_size() const
  111. {
  112. Vector2 size;
  113. size.x = ((GuiHeader*)this)->size[0];
  114. size.y = ((GuiHeader*)this)->size[1];
  115. return size;
  116. }
  117. //-----------------------------------------------------------------------------
  118. uint32_t num_rects() const
  119. {
  120. return ((GuiHeader*)this)->num_rects;
  121. }
  122. //-----------------------------------------------------------------------------
  123. GuiRectData get_rect(uint32_t index) const
  124. {
  125. CE_ASSERT(index < num_rects(), "Index out of bounds");
  126. GuiHeader* h = (GuiHeader*) this;
  127. GuiRectData* begin = (GuiRectData*) (((char*) this) + h->rects_offset);
  128. return begin[index];
  129. }
  130. //-----------------------------------------------------------------------------
  131. uint32_t num_triangles() const
  132. {
  133. return ((GuiHeader*)this)->num_triangles;
  134. }
  135. //-----------------------------------------------------------------------------
  136. GuiTriangleData get_triangle(uint32_t index) const
  137. {
  138. CE_ASSERT(index < num_triangles(), "Index out of bounds");
  139. GuiHeader* h = (GuiHeader*) this;
  140. GuiTriangleData* begin = (GuiTriangleData*) (((char*) this) + h->triangles_offset);
  141. return begin[index];
  142. }
  143. //-----------------------------------------------------------------------------
  144. uint32_t num_images() const
  145. {
  146. return ((GuiHeader*)this)->num_images;
  147. }
  148. //-----------------------------------------------------------------------------
  149. GuiImageData get_image(uint32_t index) const
  150. {
  151. CE_ASSERT(index < num_images(), "Index out of bounds");
  152. GuiHeader* h = (GuiHeader*) this;
  153. GuiImageData* begin = (GuiImageData*) (((char*) this) + h->images_offset);
  154. return begin[index];
  155. }
  156. private:
  157. GuiResource();
  158. };
  159. } // namespace crown