GuiResource.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. #include <cstring>
  24. #include <inttypes.h>
  25. #include "Allocator.h"
  26. #include "Filesystem.h"
  27. #include "Hash.h"
  28. #include "JSONParser.h"
  29. #include "GuiResource.h"
  30. #include "StringUtils.h"
  31. #include "Log.h"
  32. namespace crown
  33. {
  34. namespace gui_resource
  35. {
  36. //-----------------------------------------------------------------------------
  37. void parse_rect(JSONElement rect, List<float>& positions, List<float>& sizes, List<float>& colors)
  38. {
  39. JSONElement position = rect.key("position");
  40. JSONElement size = rect.key("size");
  41. JSONElement color = rect.key("color");
  42. position.array_value(positions);
  43. size.array_value(sizes);
  44. color.array_value(colors);
  45. }
  46. //-----------------------------------------------------------------------------
  47. void parse_triangle(JSONElement rect, List<float>& points, List<float>& colors)
  48. {
  49. JSONElement point = rect.key("points");
  50. JSONElement color = rect.key("color");
  51. point.array_value(points);
  52. color.array_value(colors);
  53. }
  54. //-----------------------------------------------------------------------------
  55. void compile(Filesystem& fs, const char* resource_path, File* out_file)
  56. {
  57. File* file = fs.open(resource_path, FOM_READ);
  58. char* buf = (char*)default_allocator().allocate(file->size());
  59. file->read(buf, file->size());
  60. JSONParser json(buf);
  61. JSONElement root = json.root();
  62. List<float> m_gui_position(default_allocator());
  63. List<float> m_gui_size(default_allocator());
  64. root.key("position").array_value(m_gui_position);
  65. root.key("size").array_value(m_gui_size);
  66. List<float> m_rect_positions(default_allocator());
  67. List<float> m_rect_sizes(default_allocator());
  68. List<float> m_rect_colors(default_allocator());
  69. // Parse all rects
  70. JSONElement rects = root.key("rects");
  71. uint32_t num_rects = rects.size();
  72. for (uint32_t i = 0; i < num_rects; i++)
  73. {
  74. parse_rect(rects[i], m_rect_positions, m_rect_sizes, m_rect_colors);
  75. }
  76. // Compile all rects
  77. List<float> m_rect_vertices(default_allocator());
  78. List<uint16_t> m_rect_indices(default_allocator());
  79. uint32_t rprx = 0;
  80. uint32_t rclx = 0;
  81. uint32_t ridx = 0;
  82. for (uint32_t i = 0; i < num_rects; i++)
  83. {
  84. // first vertex
  85. m_rect_vertices.push_back(m_rect_positions[rprx]);
  86. m_rect_vertices.push_back(m_rect_positions[rprx+1]);
  87. m_rect_vertices.push_back(m_rect_colors[rclx]);
  88. m_rect_vertices.push_back(m_rect_colors[rclx+1]);
  89. m_rect_vertices.push_back(m_rect_colors[rclx+2]);
  90. m_rect_vertices.push_back(m_rect_colors[rclx+3]);
  91. // second vertex
  92. m_rect_vertices.push_back(m_rect_positions[rprx] + m_rect_sizes[rprx]);
  93. m_rect_vertices.push_back(m_rect_positions[rprx+1]);
  94. m_rect_vertices.push_back(m_rect_colors[rclx]);
  95. m_rect_vertices.push_back(m_rect_colors[rclx+1]);
  96. m_rect_vertices.push_back(m_rect_colors[rclx+2]);
  97. m_rect_vertices.push_back(m_rect_colors[rclx+3]);
  98. // third vertex
  99. m_rect_vertices.push_back(m_rect_positions[rprx] + m_rect_sizes[rprx]);
  100. m_rect_vertices.push_back(m_rect_positions[rprx+1] - m_rect_sizes[rprx+1]);
  101. m_rect_vertices.push_back(m_rect_colors[rclx]);
  102. m_rect_vertices.push_back(m_rect_colors[rclx+1]);
  103. m_rect_vertices.push_back(m_rect_colors[rclx+2]);
  104. m_rect_vertices.push_back(m_rect_colors[rclx+3]);
  105. // fourth vertex
  106. m_rect_vertices.push_back(m_rect_positions[rprx]);
  107. m_rect_vertices.push_back(m_rect_positions[rprx+1] - m_rect_sizes[rprx+1]);
  108. m_rect_vertices.push_back(m_rect_colors[rclx]);
  109. m_rect_vertices.push_back(m_rect_colors[rclx+1]);
  110. m_rect_vertices.push_back(m_rect_colors[rclx+2]);
  111. m_rect_vertices.push_back(m_rect_colors[rclx+3]);
  112. // indices
  113. m_rect_indices.push_back(ridx); m_rect_indices.push_back(ridx+1);
  114. m_rect_indices.push_back(ridx+1); m_rect_indices.push_back(ridx+2);
  115. m_rect_indices.push_back(ridx+2); m_rect_indices.push_back(ridx+3);
  116. m_rect_indices.push_back(ridx+3); m_rect_indices.push_back(ridx);
  117. rprx += 2;
  118. rclx += 4;
  119. ridx += 4;
  120. }
  121. // Parse all triangles
  122. List<float> m_triangle_points(default_allocator());
  123. List<float> m_triangle_colors(default_allocator());
  124. JSONElement triangles = root.key("triangles");
  125. uint32_t num_triangles = triangles.size();
  126. for (uint32_t i = 0; i < num_triangles; i++)
  127. {
  128. parse_triangle(triangles[i], m_triangle_points, m_triangle_colors);
  129. }
  130. // Compile all triangles
  131. List<float> m_triangle_vertices(default_allocator());
  132. List<uint16_t> m_triangle_indices(default_allocator());
  133. uint32_t tpnx = 0;
  134. uint32_t tclx = 0;
  135. uint32_t tidx = 0;
  136. for (uint32_t i = 0; i < num_triangles; i++)
  137. {
  138. // first vertex
  139. m_triangle_vertices.push_back(m_triangle_points[tpnx]);
  140. m_triangle_vertices.push_back(m_triangle_points[tpnx+1]);
  141. m_triangle_vertices.push_back(m_triangle_colors[tclx]);
  142. m_triangle_vertices.push_back(m_triangle_colors[tclx+1]);
  143. m_triangle_vertices.push_back(m_triangle_colors[tclx+2]);
  144. m_triangle_vertices.push_back(m_triangle_colors[tclx+3]);
  145. // second vertex
  146. m_triangle_vertices.push_back(m_triangle_points[tpnx+2]);
  147. m_triangle_vertices.push_back(m_triangle_points[tpnx+3]);
  148. m_triangle_vertices.push_back(m_triangle_colors[tclx]);
  149. m_triangle_vertices.push_back(m_triangle_colors[tclx+1]);
  150. m_triangle_vertices.push_back(m_triangle_colors[tclx+2]);
  151. m_triangle_vertices.push_back(m_triangle_colors[tclx+3]);
  152. // third vertex
  153. m_triangle_vertices.push_back(m_triangle_points[tpnx+4]);
  154. m_triangle_vertices.push_back(m_triangle_points[tpnx+5]);
  155. m_triangle_vertices.push_back(m_triangle_colors[tclx]);
  156. m_triangle_vertices.push_back(m_triangle_colors[tclx+1]);
  157. m_triangle_vertices.push_back(m_triangle_colors[tclx+2]);
  158. m_triangle_vertices.push_back(m_triangle_colors[tclx+3]);
  159. m_triangle_indices.push_back(tidx); m_triangle_indices.push_back(tidx+1);
  160. m_triangle_indices.push_back(tidx+1); m_triangle_indices.push_back(tidx+2);
  161. m_triangle_indices.push_back(tidx+2); m_triangle_indices.push_back(tidx);
  162. tpnx += 6;
  163. tclx += 4;
  164. tidx += 3;
  165. }
  166. fs.close(file);
  167. default_allocator().deallocate(buf);
  168. // Write compiled resource to out_file
  169. GuiHeader h;
  170. h.position[0] = m_gui_position[0];
  171. h.position[1] = m_gui_position[1];
  172. h.size[0] = m_gui_size[0];
  173. h.size[1] = m_gui_size[1];
  174. h.num_rects = num_rects;
  175. h.num_triangles = num_triangles;
  176. h.rect_vertices_off = sizeof(GuiHeader);
  177. h.rect_indices_off = h.rect_vertices_off + sizeof(float) * m_rect_vertices.size();
  178. h.triangle_vertices_off = h.rect_indices_off + sizeof(uint16_t) * m_rect_indices.size();
  179. h.triangle_indices_off = h.triangle_vertices_off + sizeof(float) * m_triangle_vertices.size();
  180. out_file->write((char*) &h, sizeof(GuiHeader));
  181. out_file->write((char*) m_rect_vertices.begin(), sizeof(float) * m_rect_vertices.size());
  182. out_file->write((char*) m_rect_indices.begin(), sizeof(uint16_t) * m_rect_indices.size());
  183. out_file->write((char*) m_triangle_vertices.begin(), sizeof(float) * m_triangle_vertices.size());
  184. out_file->write((char*) m_triangle_indices.begin(), sizeof(uint16_t) * m_triangle_indices.size());
  185. }
  186. } // namespace gui_resource
  187. } // namespace crown